Home | History | Annotate | Download | only in framework
      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_FRAMEWORK_TENSOR_TYPES_H_
     17 #define TENSORFLOW_FRAMEWORK_TENSOR_TYPES_H_
     18 
     19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     20 
     21 namespace tensorflow {
     22 
     23 // Helper to define Tensor types given that the scalar is of type T.
     24 template <typename T, int NDIMS = 1, typename IndexType = Eigen::DenseIndex>
     25 struct TTypes {
     26   // Rank-<NDIMS> tensor of scalar type T.
     27   typedef Eigen::TensorMap<Eigen::Tensor<T, NDIMS, Eigen::RowMajor, IndexType>,
     28                            Eigen::Aligned>
     29       Tensor;
     30   typedef Eigen::TensorMap<
     31       Eigen::Tensor<const T, NDIMS, Eigen::RowMajor, IndexType>, Eigen::Aligned>
     32       ConstTensor;
     33 
     34   // Unaligned Rank-<NDIMS> tensor of scalar type T.
     35   typedef Eigen::TensorMap<Eigen::Tensor<T, NDIMS, Eigen::RowMajor, IndexType> >
     36       UnalignedTensor;
     37   typedef Eigen::TensorMap<
     38       Eigen::Tensor<const T, NDIMS, Eigen::RowMajor, IndexType> >
     39       UnalignedConstTensor;
     40 
     41   typedef Eigen::TensorMap<Eigen::Tensor<T, NDIMS, Eigen::RowMajor, int>,
     42                            Eigen::Aligned>
     43       Tensor32Bit;
     44 
     45   // Scalar tensor (implemented as a rank-0 tensor) of scalar type T.
     46   typedef Eigen::TensorMap<
     47       Eigen::TensorFixedSize<T, Eigen::Sizes<>, Eigen::RowMajor, IndexType>,
     48       Eigen::Aligned>
     49       Scalar;
     50   typedef Eigen::TensorMap<Eigen::TensorFixedSize<const T, Eigen::Sizes<>,
     51                                                   Eigen::RowMajor, IndexType>,
     52                            Eigen::Aligned>
     53       ConstScalar;
     54 
     55   // Unaligned Scalar tensor of scalar type T.
     56   typedef Eigen::TensorMap<
     57       Eigen::TensorFixedSize<T, Eigen::Sizes<>, Eigen::RowMajor, IndexType> >
     58       UnalignedScalar;
     59   typedef Eigen::TensorMap<Eigen::TensorFixedSize<const T, Eigen::Sizes<>,
     60                                                   Eigen::RowMajor, IndexType> >
     61       UnalignedConstScalar;
     62 
     63   // Rank-1 tensor (vector) of scalar type T.
     64   typedef Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType>,
     65                            Eigen::Aligned>
     66       Flat;
     67   typedef Eigen::TensorMap<
     68       Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType>, Eigen::Aligned>
     69       ConstFlat;
     70   typedef Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType>,
     71                            Eigen::Aligned>
     72       Vec;
     73   typedef Eigen::TensorMap<
     74       Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType>, Eigen::Aligned>
     75       ConstVec;
     76 
     77   // Unaligned Rank-1 tensor (vector) of scalar type T.
     78   typedef Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType> >
     79       UnalignedFlat;
     80   typedef Eigen::TensorMap<
     81       Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType> >
     82       UnalignedConstFlat;
     83   typedef Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, IndexType> >
     84       UnalignedVec;
     85   typedef Eigen::TensorMap<
     86       Eigen::Tensor<const T, 1, Eigen::RowMajor, IndexType> >
     87       UnalignedConstVec;
     88 
     89   // Rank-2 tensor (matrix) of scalar type T.
     90   typedef Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, IndexType>,
     91                            Eigen::Aligned>
     92       Matrix;
     93   typedef Eigen::TensorMap<
     94       Eigen::Tensor<const T, 2, Eigen::RowMajor, IndexType>, Eigen::Aligned>
     95       ConstMatrix;
     96 
     97   // Unaligned Rank-2 tensor (matrix) of scalar type T.
     98   typedef Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, IndexType> >
     99       UnalignedMatrix;
    100   typedef Eigen::TensorMap<
    101       Eigen::Tensor<const T, 2, Eigen::RowMajor, IndexType> >
    102       UnalignedConstMatrix;
    103 };
    104 
    105 typedef typename TTypes<float, 1>::Tensor32Bit::Index Index32;
    106 
    107 template <typename DSizes>
    108 Eigen::DSizes<Index32, DSizes::count> To32BitDims(const DSizes& in) {
    109   Eigen::DSizes<Index32, DSizes::count> out;
    110   for (int i = 0; i < DSizes::count; ++i) {
    111     out[i] = in[i];
    112   }
    113   return out;
    114 }
    115 
    116 template <typename TensorType>
    117 typename TTypes<typename TensorType::Scalar,
    118                 TensorType::NumIndices>::Tensor32Bit
    119 To32Bit(TensorType in) {
    120   typedef typename TTypes<typename TensorType::Scalar,
    121                           TensorType::NumIndices>::Tensor32Bit RetType;
    122   return RetType(in.data(), To32BitDims(in.dimensions()));
    123 }
    124 
    125 }  // namespace tensorflow
    126 #endif  // TENSORFLOW_FRAMEWORK_TENSOR_TYPES_H_
    127