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_DATA_FORMAT_OPS_H_
     17 #define TENSORFLOW_KERNELS_DATA_FORMAT_OPS_H_
     18 // Functor definition for data format dim mapping ops, must be compilable
     19 // by nvcc.
     20 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     21 #include "tensorflow/core/framework/tensor_types.h"
     22 
     23 namespace tensorflow {
     24 namespace functor {
     25 
     26 // Functor used by DataFormatDimMapOP to do the computations.
     27 template <typename Device, typename T>
     28 struct DataFormatDimMap {
     29   void operator()(const Device& d, typename TTypes<T>::ConstFlat x,
     30                   typename TTypes<T>::Flat y) {
     31     auto zero = x.constant(0);
     32     auto one = x.constant(1);
     33     auto three = x.constant(3);
     34     auto four = x.constant(4);
     35     auto x_mod = (x + four) % 4;
     36     auto is_zero = (x_mod == zero);
     37     auto is_three = (x_mod == three);
     38     y.device(d) = is_zero.select(zero, is_three.select(one, x_mod + one));
     39   }
     40 };
     41 
     42 template <typename T>
     43 struct VecPermuteNHWCToNCHW {
     44   Eigen::DSizes<Eigen::DenseIndex, 1> dimensions(
     45       typename TTypes<T>::ConstFlat input) const {
     46     Eigen::DSizes<Eigen::DenseIndex, 1> result;
     47     result[0] = input.dimension(0);
     48     return result;
     49   }
     50   template <typename Output, typename Device>
     51   void eval(typename TTypes<T>::ConstFlat input, Output& output,
     52             const Device& d) const {
     53     if (input.size() == 8) {
     54       output.template chip<0>(0).device(d) = input.template chip<0>(0);
     55       output.template chip<0>(1).device(d) = input.template chip<0>(1);
     56       output.template chip<0>(2).device(d) = input.template chip<0>(6);
     57       output.template chip<0>(3).device(d) = input.template chip<0>(7);
     58       output.template chip<0>(4).device(d) = input.template chip<0>(2);
     59       output.template chip<0>(5).device(d) = input.template chip<0>(3);
     60       output.template chip<0>(6).device(d) = input.template chip<0>(4);
     61       output.template chip<0>(7).device(d) = input.template chip<0>(5);
     62     } else {
     63       output.template chip<0>(0).device(d) = input.template chip<0>(0);
     64       output.template chip<0>(1).device(d) = input.template chip<0>(3);
     65       output.template chip<0>(2).device(d) = input.template chip<0>(1);
     66       output.template chip<0>(3).device(d) = input.template chip<0>(2);
     67     }
     68   }
     69 };
     70 
     71 template <typename T>
     72 struct VecPermuteNCHWToNHWC {
     73   Eigen::DSizes<Eigen::DenseIndex, 1> dimensions(
     74       typename TTypes<T>::ConstFlat input) const {
     75     Eigen::DSizes<Eigen::DenseIndex, 1> result;
     76     result[0] = input.dimension(0);
     77     return result;
     78   }
     79   template <typename Output, typename Device>
     80   void eval(typename TTypes<T>::ConstFlat input, Output& output,
     81             const Device& d) const {
     82     if (input.size() == 8) {
     83       output.template chip<0>(0).device(d) = input.template chip<0>(0);
     84       output.template chip<0>(1).device(d) = input.template chip<0>(1);
     85       output.template chip<0>(2).device(d) = input.template chip<0>(4);
     86       output.template chip<0>(3).device(d) = input.template chip<0>(5);
     87       output.template chip<0>(4).device(d) = input.template chip<0>(6);
     88       output.template chip<0>(5).device(d) = input.template chip<0>(7);
     89       output.template chip<0>(6).device(d) = input.template chip<0>(2);
     90       output.template chip<0>(7).device(d) = input.template chip<0>(3);
     91     } else {
     92       output.template chip<0>(0).device(d) = input.template chip<0>(0);
     93       output.template chip<0>(1).device(d) = input.template chip<0>(2);
     94       output.template chip<0>(2).device(d) = input.template chip<0>(3);
     95       output.template chip<0>(3).device(d) = input.template chip<0>(1);
     96     }
     97   }
     98 };
     99 
    100 // Functor used by DataFormatVecPermuteOp to do the computations.
    101 template <typename Device, typename T>
    102 struct DataFormatVecPermute {
    103   void operator()(const Device& d, typename TTypes<T>::ConstFlat x,
    104                   typename TTypes<T>::Flat y, bool nhwc_to_nchw) {
    105     if (nhwc_to_nchw) {
    106       y.device(d) = x.customOp(VecPermuteNHWCToNCHW<T>());
    107     } else {
    108       y.device(d) = x.customOp(VecPermuteNCHWToNHWC<T>());
    109     }
    110   }
    111 };
    112 
    113 }  // namespace functor
    114 }  // namespace tensorflow
    115 
    116 #endif  // TENSORFLOW_KERNELS_DATA_FORMAT_OPS_H_
    117