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 #ifndef TENSORFLOW_KERNELS_CONCAT_LIB_H_
     17 #define TENSORFLOW_KERNELS_CONCAT_LIB_H_
     18 
     19 #include <vector>
     20 
     21 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     22 #include "tensorflow/core/framework/device_base.h"
     23 
     24 namespace tensorflow {
     25 
     26 // Functors to concatenate tensors. These always take a rank-2 tensor (i.e a
     27 // matrix) and concatenate it along the axis 1 ("putting them next to each
     28 // other" as opposed to "putting them on top of one another").
     29 //
     30 // Any concatenation of n-dimensional tensors across any axis can be reduced to
     31 // a concatenation of two-dimensional tensors across the axis 1 by first
     32 // partitioning the axes of the original tensors into those less than the axis
     33 // to be concatenated across and the rest. Then reshape the tensors into a
     34 // two-dimensional tensor by collapsing these two sets of axes and concatenate
     35 // the resulting matrices across the axis 1, finally reshaping the result to
     36 // have the proper shape.
     37 //
     38 // So, for example, when stacking N tensors, reshape each to have shape
     39 // {1, Numelements} and reshape the result matrix to have shape
     40 // {1, N * NumElements} before passing it to this functor.
     41 
     42 // Assumes all inputs are nonempty
     43 template <typename T>
     44 void ConcatCPU(
     45     DeviceBase* d,
     46     const std::vector<std::unique_ptr<typename TTypes<T, 2>::ConstMatrix>>&
     47         inputs,
     48     typename TTypes<T, 2>::Matrix* output);
     49 #if GOOGLE_CUDA
     50 template <typename T>
     51 void ConcatGPU(
     52     OpKernelContext* c,
     53     const std::vector<std::unique_ptr<typename TTypes<T, 2>::ConstMatrix>>&
     54         inputs_flat,
     55     Tensor* output, typename TTypes<T, 2>::Tensor* output_flat);
     56 
     57 #endif  // GOOGLE_CUDA
     58 
     59 #ifdef TENSORFLOW_USE_SYCL
     60 template <typename T>
     61 void ConcatSYCL(
     62     const Eigen::SyclDevice& d,
     63     const std::vector<std::unique_ptr<typename TTypes<T, 2>::ConstMatrix>>&
     64         inputs,
     65     typename TTypes<T, 2>::Matrix* output);
     66 #endif  // TENSORFLOW_USE_SYCL
     67 }  // namespace tensorflow
     68 
     69 #endif  // TENSORFLOW_KERNELS_CONCAT_LIB_H_
     70