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_CORE_KERNELS_DATA_FORMAT_OPS_H_ 17 #define TENSORFLOW_CORE_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 { operatorDataFormatDimMap29 void operator()(const Device& d, typename TTypes<T>::ConstFlat x, 30 typename TTypes<T>::Flat y, const TTypes<int>::Vec dst) { 31 auto zero = x.constant(0); 32 auto one = x.constant(1); 33 auto two = x.constant(2); 34 35 auto f_zero = x.constant(dst(0)); 36 auto f_one = x.constant(dst(1)); 37 auto f_two = x.constant(dst(2)); 38 auto f_three = x.constant(dst(3)); 39 40 auto four = x.constant(4); 41 auto x_mod = (x + four) % 4; 42 43 auto is_zero = (x_mod == zero); 44 auto is_one = (x_mod == one); 45 auto is_two = (x_mod == two); 46 47 y.device(d) = is_zero.select( 48 f_zero, is_one.select(f_one, is_two.select(f_two, f_three))); 49 } 50 }; 51 52 template <typename T> 53 struct VecPermute { VecPermuteVecPermute54 VecPermute(const Eigen::DSizes<Eigen::DenseIndex, 8>& dst) : dst_(dst) {} dimensionsVecPermute55 Eigen::DSizes<Eigen::DenseIndex, 1> dimensions( 56 typename TTypes<T>::ConstFlat input) const { 57 Eigen::DSizes<Eigen::DenseIndex, 1> result; 58 result[0] = input.dimension(0); 59 return result; 60 } 61 template <typename Output, typename Device> evalVecPermute62 void eval(typename TTypes<T>::ConstFlat input, Output& output, 63 const Device& d) const { 64 for (int i = 0; i < input.size(); ++i) { 65 output.template chip<0>(dst_[i]).device(d) = input.template chip<0>(i); 66 } 67 } 68 69 private: 70 Eigen::DSizes<Eigen::DenseIndex, 8> dst_; 71 }; 72 73 // Functor used by DataFormatVecPermuteOp to do the computations. 74 template <typename Device, typename T> 75 struct DataFormatVecPermute { operatorDataFormatVecPermute76 void operator()(const Device& d, typename TTypes<T>::ConstFlat x, 77 typename TTypes<T>::Flat y, 78 const Eigen::DSizes<Eigen::DenseIndex, 8>& dst) { 79 y.device(d) = x.customOp(VecPermute<T>(dst)); 80 } 81 }; 82 83 } // namespace functor 84 } // namespace tensorflow 85 86 #endif // TENSORFLOW_CORE_KERNELS_DATA_FORMAT_OPS_H_ 87