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