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 { |
29 | 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 { |
80 | explicit VecPermute(const Eigen::DSizes<Eigen::DenseIndex, 10>& dst) |
81 | : dst(dst) {} |
82 | Eigen::DSizes<Eigen::DenseIndex, 1> dimensions( |
83 | typename TTypes<T>::ConstFlat input) const { |
84 | Eigen::DSizes<Eigen::DenseIndex, 1> result; |
85 | result[0] = input.dimension(0); |
86 | return result; |
87 | } |
88 | template <typename Output, typename Device> |
89 | void eval(typename TTypes<T>::ConstFlat input, Output& output, |
90 | const Device& d) const { |
91 | for (int i = 0; i < input.size(); ++i) { |
92 | output.template chip<0>(dst[i]).device(d) = input.template chip<0>(i); |
93 | } |
94 | } |
95 | |
96 | private: |
97 | Eigen::DSizes<Eigen::DenseIndex, 10> dst; |
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, |
105 | const Eigen::DSizes<Eigen::DenseIndex, 10>& dst) { |
106 | y.device(d) = x.customOp(VecPermute<T>(dst)); |
107 | } |
108 | }; |
109 | |
110 | } // namespace functor |
111 | } // namespace tensorflow |
112 | |
113 | #endif // TENSORFLOW_CORE_KERNELS_DATA_FORMAT_OPS_H_ |
114 | |