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_TILE_FUNCTOR_H_
17 #define TENSORFLOW_CORE_KERNELS_TILE_FUNCTOR_H_
18
19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
20
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/framework/tensor_types.h"
23 #include "tensorflow/core/platform/types.h"
24
25 namespace tensorflow {
26
27 namespace internal {
28
29 // Device-specific naive implementation for tile.
30 template <typename Device, typename T>
31 void TileSimple(const Device& d, Tensor* out, const Tensor& in);
32
33 template <typename Device, typename T, typename Tmultiples, int NDIM>
TileUsingEigen(const Device & d,Tensor * out,const Tensor & in,const gtl::ArraySlice<Tmultiples> & broadcast_array)34 void TileUsingEigen(const Device& d, Tensor* out, const Tensor& in,
35 const gtl::ArraySlice<Tmultiples>& broadcast_array) {
36 auto x = in.tensor<T, NDIM>();
37 auto y = out->tensor<T, NDIM>();
38
39 bool use_32bit = y.size() < Eigen::NumTraits<int>::highest();
40
41 Eigen::array<Tmultiples, NDIM> b;
42 for (int i = 0; i < NDIM; ++i) b[i] = broadcast_array[i];
43 if (use_32bit && Eigen::internal::is_same<Device, Eigen::GpuDevice>::value) {
44 // Use 32bit indexing to speed up the computations
45 To32Bit(y).device(d) = To32Bit(x).broadcast(b);
46 } else {
47 y.device(d) = x.broadcast(b);
48 }
49 }
50
51 template <typename Device, typename T, typename Tmultiples>
TileUsingEigen(const Device & d,Tensor * out,const Tensor & in,const gtl::ArraySlice<Tmultiples> &)52 void TileUsingEigen(const Device& d, Tensor* out, const Tensor& in,
53 const gtl::ArraySlice<Tmultiples>&) {
54 auto x = in.tensor<T, 0>();
55 auto y = out->tensor<T, 0>();
56 // In the scalar case we simply copy the input.
57 y.device(d) = x;
58 }
59
60 } // end namespace internal
61
62 namespace functor {
63
64 template <typename Device, typename T, typename Tmultiples>
65 struct Tile {
operatorTile66 void operator()(const Device& d, Tensor* out, const Tensor& in,
67 const gtl::ArraySlice<Tmultiples> broadcast_array) const {
68 switch (in.dims()) {
69 case 0:
70 internal::TileUsingEigen<Device, T, Tmultiples>(d, out, in,
71 broadcast_array);
72 break;
73 case 1:
74 internal::TileUsingEigen<Device, T, Tmultiples, 1>(d, out, in,
75 broadcast_array);
76 break;
77 case 2:
78 internal::TileUsingEigen<Device, T, Tmultiples, 2>(d, out, in,
79 broadcast_array);
80 break;
81 case 3:
82 internal::TileUsingEigen<Device, T, Tmultiples, 3>(d, out, in,
83 broadcast_array);
84 break;
85 case 4:
86 internal::TileUsingEigen<Device, T, Tmultiples, 4>(d, out, in,
87 broadcast_array);
88 break;
89 case 5:
90 internal::TileUsingEigen<Device, T, Tmultiples, 5>(d, out, in,
91 broadcast_array);
92 break;
93 case 6:
94 internal::TileUsingEigen<Device, T, Tmultiples, 6>(d, out, in,
95 broadcast_array);
96 break;
97 case 7:
98 internal::TileUsingEigen<Device, T, Tmultiples, 7>(d, out, in,
99 broadcast_array);
100 break;
101 default:
102 internal::TileSimple<Device, T>(d, out, in);
103 break;
104 }
105 }
106 };
107
108 } // end namespace functor
109 } // end namespace tensorflow
110
111 #endif // TENSORFLOW_CORE_KERNELS_TILE_FUNCTOR_H_
112