• 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_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 
31 template <typename T>
32 void TileSimple(const Eigen::ThreadPoolDevice& d, Tensor* out,
33                 const Tensor& in);
34 
35 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
36 template <typename T>
37 void TileSimple(const Eigen::GpuDevice& d, Tensor* out, const Tensor& in);
38 #endif  // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
39 
40 template <typename Device, typename T, typename Tmultiples, int NDIM>
TileUsingEigen(const Device & d,Tensor * out,const Tensor & in,const gtl::ArraySlice<Tmultiples> broadcast_array)41 void TileUsingEigen(const Device& d, Tensor* out, const Tensor& in,
42                     const gtl::ArraySlice<Tmultiples> broadcast_array) {
43   auto x = in.tensor<T, NDIM>();
44   auto y = out->tensor<T, NDIM>();
45 
46   bool use_32bit = y.size() < Eigen::NumTraits<int>::highest();
47 
48   Eigen::array<Tmultiples, NDIM> b;
49   for (int i = 0; i < NDIM; ++i) b[i] = broadcast_array[i];
50   if (use_32bit && Eigen::internal::is_same<Device, Eigen::GpuDevice>::value) {
51     // Use 32bit indexing to speed up the computations
52     To32Bit(y).device(d) = To32Bit(x).broadcast(b);
53   } else {
54     y.device(d) = x.broadcast(b);
55   }
56 }
57 
58 template <typename Device, typename T, typename Tmultiples>
TileUsingEigen(const Device & d,Tensor * out,const Tensor & in,const gtl::ArraySlice<Tmultiples>)59 void TileUsingEigen(const Device& d, Tensor* out, const Tensor& in,
60                     const gtl::ArraySlice<Tmultiples>) {
61   auto x = in.tensor<T, 0>();
62   auto y = out->tensor<T, 0>();
63   // In the scalar case we simply copy the input.
64   y.device(d) = x;
65 }
66 
67 }  // end namespace internal
68 
69 namespace functor {
70 
71 template <typename Device, typename T, typename Tmultiples>
72 struct Tile {
operatorTile73   void operator()(const Device& d, Tensor* out, const Tensor& in,
74                   const gtl::ArraySlice<Tmultiples> broadcast_array) const {
75     switch (in.dims()) {
76       case 0:
77         internal::TileUsingEigen<Device, T, Tmultiples>(d, out, in,
78                                                         broadcast_array);
79         break;
80       case 1:
81         internal::TileUsingEigen<Device, T, Tmultiples, 1>(d, out, in,
82                                                            broadcast_array);
83         break;
84       case 2:
85         internal::TileUsingEigen<Device, T, Tmultiples, 2>(d, out, in,
86                                                            broadcast_array);
87         break;
88       case 3:
89         internal::TileUsingEigen<Device, T, Tmultiples, 3>(d, out, in,
90                                                            broadcast_array);
91         break;
92       case 4:
93         internal::TileUsingEigen<Device, T, Tmultiples, 4>(d, out, in,
94                                                            broadcast_array);
95         break;
96       case 5:
97         internal::TileUsingEigen<Device, T, Tmultiples, 5>(d, out, in,
98                                                            broadcast_array);
99         break;
100       case 6:
101         internal::TileUsingEigen<Device, T, Tmultiples, 6>(d, out, in,
102                                                            broadcast_array);
103         break;
104       case 7:
105         internal::TileUsingEigen<Device, T, Tmultiples, 7>(d, out, in,
106                                                            broadcast_array);
107         break;
108       default:
109         internal::TileSimple<T>(d, out, in);
110         break;
111     }
112   }
113 };
114 
115 }  // end namespace functor
116 }  // end namespace tensorflow
117 
118 #endif  // TENSORFLOW_CORE_KERNELS_TILE_FUNCTOR_H_
119