Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #define EIGEN_USE_THREADS
     17 
     18 #include "tensorflow/core/kernels/concat_lib_cpu.h"
     19 #include <vector>
     20 #include "tensorflow/core/framework/register_types.h"
     21 #include "tensorflow/core/kernels/concat_lib.h"
     22 
     23 namespace tensorflow {
     24 
     25 namespace {
     26 template <typename T>
     27 struct MemCpyCopier {
     28   inline void Copy(T* dst, const T* src, int input_index, size_t n) {
     29     if (DataTypeCanUseMemcpy(DataTypeToEnum<T>::v())) {
     30       memcpy(dst, src, n * sizeof(T));
     31     } else {
     32       for (size_t k = 0; k < n; ++k) {
     33         *dst++ = *src++;
     34       }
     35     }
     36   }
     37 };
     38 template <>
     39 struct MemCpyCopier<ResourceHandle> {
     40   inline void Copy(ResourceHandle* dst, const ResourceHandle* src,
     41                    int input_index, size_t n) {
     42     for (size_t k = 0; k < n; ++k) {
     43       *dst++ = *src++;
     44     }
     45   }
     46 };
     47 
     48 }  // namespace
     49 
     50 template <typename T>
     51 void ConcatCPU(
     52     DeviceBase* d,
     53     const std::vector<std::unique_ptr<typename TTypes<T, 2>::ConstMatrix>>&
     54         inputs,
     55     typename TTypes<T, 2>::Matrix* output) {
     56   if (std::is_same<T, string>::value) {
     57     // use a large cost here to force strings to be handled by separate threads
     58     ConcatCPUImpl<T>(d, inputs, 100000, MemCpyCopier<T>(), output);
     59   } else {
     60     ConcatCPUImpl<T>(d, inputs, sizeof(T) /* cost_per_unit */,
     61                      MemCpyCopier<T>(), output);
     62   }
     63 }
     64 
     65 #define REGISTER(T)                                                            \
     66   template void ConcatCPU<T>(                                                  \
     67       DeviceBase*,                                                             \
     68       const std::vector<std::unique_ptr<typename TTypes<T, 2>::ConstMatrix>>&, \
     69       typename TTypes<T, 2>::Matrix* output);
     70 TF_CALL_ALL_TYPES(REGISTER)
     71 REGISTER(quint8)
     72 REGISTER(qint8)
     73 REGISTER(quint16)
     74 REGISTER(qint16)
     75 REGISTER(qint32)
     76 
     77 #if defined(IS_MOBILE_PLATFORM) && !defined(SUPPORT_SELECTIVE_REGISTRATION) && \
     78     !defined(__ANDROID_TYPES_FULL__)
     79     // Primarily used for SavedModel support on mobile. Registering it here only
     80     // if __ANDROID_TYPES_FULL__ is not defined (which already registers string)
     81     // to avoid duplicate registration.
     82     REGISTER(string);
     83 #endif  // defined(IS_MOBILE_PLATFORM) &&
     84         // !defined(SUPPORT_SELECTIVE_REGISTRATION) &&
     85         // !defined(__ANDROID_TYPES_FULL__)
     86 
     87 #ifdef TENSORFLOW_USE_SYCL
     88 template <typename T>
     89 void ConcatSYCL(
     90     const Eigen::SyclDevice& d,
     91     const std::vector<std::unique_ptr<typename TTypes<T, 2>::ConstMatrix>>&
     92         inputs,
     93     typename TTypes<T, 2>::Matrix* output) {
     94   ConcatSYCLImpl<T>(d, inputs, sizeof(T) /* cost_per_unit */, MemCpyCopier<T>(),
     95                     output);
     96 }
     97 #define REGISTER_SYCL(T)                                                       \
     98   template void ConcatSYCL<T>(                                                 \
     99       const Eigen::SyclDevice&,                                                \
    100       const std::vector<std::unique_ptr<typename TTypes<T, 2>::ConstMatrix>>&, \
    101       typename TTypes<T, 2>::Matrix* output);
    102 
    103 TF_CALL_GPU_NUMBER_TYPES_NO_HALF(REGISTER_SYCL)
    104 
    105 #undef REGISTER_SYCL
    106 #endif  // TENSORFLOW_USE_SYCL
    107 }  // namespace tensorflow
    108