1 /* Copyright 2016 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 #if GOOGLE_CUDA 17 18 #define EIGEN_USE_GPU 19 20 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 21 22 #include "tensorflow/core/framework/register_types.h" 23 #include "tensorflow/core/kernels/ops_util.h" 24 #include "tensorflow/core/kernels/tile_functor.h" 25 #include "tensorflow/core/util/cuda_kernel_helper.h" 26 27 namespace tensorflow { 28 namespace internal { 29 30 template <typename T> 31 __global__ void TileKernel(int nthreads, const T* src, const int32* buf, 32 const int32 ndims, T* dst) { 33 const int32* in_strides = buf; 34 const int32* out_strides = buf + ndims; 35 const int32* in_dim_sizes = buf + ndims * 2; 36 CUDA_1D_KERNEL_LOOP(o_idx, nthreads) { 37 int32 i_idx = 0; 38 int32 t = o_idx; 39 for (int i = 0; i < ndims; ++i) { 40 i_idx += t / out_strides[i] % in_dim_sizes[i] * in_strides[i]; 41 t %= out_strides[i]; 42 } 43 dst[o_idx] = ldg(src + i_idx); 44 } 45 } 46 47 template <typename Device, typename T> 48 void TileSimple(const Device& d, Tensor* out, const Tensor& in) { 49 // Ensures we can use 32-bit index. 50 const int64 in_nelem = in.NumElements(); 51 CHECK_LT(in_nelem, kint32max) << "Tensor too large to transpose on GPU"; 52 const int64 out_nelem = out->NumElements(); 53 CHECK_LT(out_nelem, kint32max) << "Tensor too large to transpose on GPU"; 54 // Pack strides and input dimension sizes into one buffer. 55 const int32 ndims = in.dims(); 56 gtl::InlinedVector<int32, 24> host_buf(ndims * 3); 57 gtl::InlinedVector<int32, 8> in_strides = ComputeStride<int32>(in.shape()); 58 gtl::InlinedVector<int32, 8> out_strides = ComputeStride<int32>(out->shape()); 59 for (int i = 0; i < ndims; ++i) { 60 host_buf[i] = in_strides[i]; 61 host_buf[ndims + i] = out_strides[i]; 62 host_buf[ndims * 2 + i] = in.dim_size(i); 63 } 64 // Copies the input strides, output strides and input dimension sizes to the 65 // device. 66 auto num_bytes = sizeof(int64) * host_buf.size(); 67 auto dev_buf = d.allocate(num_bytes); 68 // NOTE: host_buf is not allocated by CudaHostAllocator, and 69 // therefore we are doing a sync copy effectively. 70 d.memcpyHostToDevice(dev_buf, host_buf.data(), num_bytes); 71 // Launch kernel to q[...] = p[...]. 72 const T* p = in.flat<T>().data(); 73 T* q = out->flat<T>().data(); 74 CudaLaunchConfig cfg = GetCudaLaunchConfig(out_nelem, d); 75 TileKernel<<<cfg.block_count, cfg.thread_per_block, 0, d.stream()>>>( 76 cfg.virtual_thread_count, p, reinterpret_cast<const int32*>(dev_buf), 77 ndims, q); 78 // Safe to deallocate immediately after the kernel launch. 79 d.deallocate(dev_buf); 80 } 81 82 } // end namespace internal 83 84 namespace functor { 85 86 typedef Eigen::GpuDevice GPUDevice; 87 88 // Register functors used for Tile functor. 89 #define DEFINE_TYPE(T) \ 90 template struct Tile<GPUDevice, T, int32>; \ 91 template struct Tile<GPUDevice, T, int64>; 92 93 TF_CALL_bool(DEFINE_TYPE); 94 TF_CALL_int16(DEFINE_TYPE); 95 TF_CALL_int32(DEFINE_TYPE); 96 TF_CALL_int64(DEFINE_TYPE); 97 TF_CALL_float(DEFINE_TYPE); 98 TF_CALL_double(DEFINE_TYPE); 99 TF_CALL_half(DEFINE_TYPE); 100 TF_CALL_complex64(DEFINE_TYPE); 101 TF_CALL_complex128(DEFINE_TYPE); 102 103 #undef DEFINE_TYPE 104 105 } // end namespace functor 106 } // namespace tensorflow 107 #endif // GOOGLE_CUDA 108