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 || TENSORFLOW_USE_ROCM
17
18 #define EIGEN_USE_GPU
19
20 #include "tensorflow/core/framework/register_types.h"
21 #include "tensorflow/core/framework/tensor_types.h"
22 #include "tensorflow/core/kernels/roll_op.h"
23 #include "tensorflow/core/platform/types.h"
24 #include "tensorflow/core/util/gpu_kernel_helper.h"
25
26 namespace tensorflow {
27
28 typedef Eigen::GpuDevice GPUDevice;
29
30 namespace {
31
32 template <typename T>
RollKernel(const int32 nthreads,const int32 num_dims,const T * __restrict__ input,T * __restrict__ output,const int32 * __restrict__ dim_size,const int32 * __restrict__ threshold,const int64 * __restrict__ dim_range)33 __global__ void RollKernel(const int32 nthreads, const int32 num_dims,
34 const T* __restrict__ input, T* __restrict__ output,
35 const int32* __restrict__ dim_size,
36 const int32* __restrict__ threshold,
37 const int64* __restrict__ dim_range) {
38 CUDA_1D_KERNEL_LOOP(out_idx, nthreads) {
39 int64 offset = 0;
40 for (int i = 0; i < num_dims; i++) {
41 const int64 stride = dim_range[i] / dim_size[i];
42 const int shift = dim_size[i] - threshold[i];
43 const int indx = (out_idx / stride) % dim_size[i];
44 const int shifted_indx = (indx + shift) % dim_size[i];
45 offset += (shifted_indx - indx) * stride;
46 }
47 output[out_idx + offset] = input[out_idx];
48 }
49 }
50 } // namespace
51
52 namespace functor {
53
54 template <typename T>
55 struct Roll<GPUDevice, T> {
operator ()tensorflow::functor::Roll56 void operator()(const OpKernelContext* context, const int64 num_elements,
57 const int num_dims, const gtl::ArraySlice<int32> dim_size,
58 const T* input, T* output,
59 const gtl::ArraySlice<int32> threshold,
60 const gtl::ArraySlice<int64_t> dim_range, const int64 isd) {
61 if (!num_elements) return;
62 const GPUDevice& d = context->eigen_device<GPUDevice>();
63
64 auto dim_bytes = sizeof(int32) * dim_size.size();
65 auto dim_buf = d.allocate(dim_bytes);
66
67 auto thres_bytes = sizeof(int32) * threshold.size();
68 auto thres_buf = d.allocate(thres_bytes);
69
70 auto range_bytes = sizeof(int64) * dim_range.size();
71 auto range_buf = d.allocate(range_bytes);
72
73 d.memcpyHostToDevice(dim_buf, dim_size.data(), dim_bytes);
74 d.memcpyHostToDevice(thres_buf, threshold.data(), thres_bytes);
75 d.memcpyHostToDevice(range_buf, dim_range.data(), range_bytes);
76
77 GpuLaunchConfig cfg = GetGpuLaunchConfig(num_elements, d);
78
79 TF_CHECK_OK(GpuLaunchKernel(RollKernel<T>, cfg.block_count,
80 cfg.thread_per_block, 0, d.stream(),
81 cfg.virtual_thread_count, num_dims, input,
82 output, reinterpret_cast<const int32*>(dim_buf),
83 reinterpret_cast<const int32*>(thres_buf),
84 reinterpret_cast<const int64*>(range_buf)));
85
86 d.deallocate(dim_buf);
87 d.deallocate(thres_buf);
88 d.deallocate(range_buf);
89 }
90 };
91
92 #define DEFINE_GPU_SPECS(T) template struct Roll<GPUDevice, T>;
93
94 TF_CALL_int32(DEFINE_GPU_SPECS);
95 TF_CALL_int64(DEFINE_GPU_SPECS);
96 TF_CALL_uint32(DEFINE_GPU_SPECS);
97 TF_CALL_GPU_NUMBER_TYPES(DEFINE_GPU_SPECS);
98 TF_CALL_COMPLEX_TYPES(DEFINE_GPU_SPECS);
99
100 #undef DEFINE_GPU_SPECS
101 } // namespace functor
102 } // namespace tensorflow
103
104 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
105