• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (dst.size() == 4) {
32       auto zero = x.constant(0);
33       auto one = x.constant(1);
34       auto two = x.constant(2);
35 
36       auto f_zero = x.constant(dst(0));
37       auto f_one = x.constant(dst(1));
38       auto f_two = x.constant(dst(2));
39       auto f_three = x.constant(dst(3));
40 
41       auto four = x.constant(4);
42       auto x_mod = (x + four) % 4;
43 
44       auto is_zero = (x_mod == zero);
45       auto is_one = (x_mod == one);
46       auto is_two = (x_mod == two);
47 
48       y.device(d) = is_zero.select(
49           f_zero, is_one.select(f_one, is_two.select(f_two, f_three)));
50     } else {
51       auto zero = x.constant(0);
52       auto one = x.constant(1);
53       auto two = x.constant(2);
54       auto three = x.constant(3);
55 
56       auto f_zero = x.constant(dst(0));
57       auto f_one = x.constant(dst(1));
58       auto f_two = x.constant(dst(2));
59       auto f_three = x.constant(dst(3));
60       auto f_four = x.constant(dst(4));
61 
62       auto five = x.constant(5);
63       auto x_mod = (x + five) % 5;
64 
65       auto is_zero = (x_mod == zero);
66       auto is_one = (x_mod == one);
67       auto is_two = (x_mod == two);
68       auto is_three = (x_mod == three);
69 
70       y.device(d) = is_zero.select(
71           f_zero,
72           is_one.select(
73               f_one, is_two.select(f_two, is_three.select(f_three, f_four))));
74     }
75   }
76 };
77 
78 template <typename T>
79 struct VecPermute {
VecPermuteVecPermute80   VecPermute(const Eigen::DSizes<Eigen::DenseIndex, 8>& dst) : dst_(dst) {}
dimensionsVecPermute81   Eigen::DSizes<Eigen::DenseIndex, 1> dimensions(
82       typename TTypes<T>::ConstFlat input) const {
83     Eigen::DSizes<Eigen::DenseIndex, 1> result;
84     result[0] = input.dimension(0);
85     return result;
86   }
87   template <typename Output, typename Device>
evalVecPermute88   void eval(typename TTypes<T>::ConstFlat input, Output& output,
89             const Device& d) const {
90     for (int i = 0; i < input.size(); ++i) {
91       output.template chip<0>(dst_[i]).device(d) = input.template chip<0>(i);
92     }
93   }
94 
95  private:
96   Eigen::DSizes<Eigen::DenseIndex, 8> dst_;
97 };
98 
99 // Functor used by DataFormatVecPermuteOp to do the computations.
100 template <typename Device, typename T>
101 struct DataFormatVecPermute {
operatorDataFormatVecPermute102   void operator()(const Device& d, typename TTypes<T>::ConstFlat x,
103                   typename TTypes<T>::Flat y,
104                   const Eigen::DSizes<Eigen::DenseIndex, 8>& dst) {
105     y.device(d) = x.customOp(VecPermute<T>(dst));
106   }
107 };
108 
109 }  // namespace functor
110 }  // namespace tensorflow
111 
112 #endif  // TENSORFLOW_CORE_KERNELS_DATA_FORMAT_OPS_H_
113