-
Notifications
You must be signed in to change notification settings - Fork 74k
/
sequence_ops_gpu.cu.cc
69 lines (52 loc) · 2.23 KB
/
sequence_ops_gpu.cu.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#define EIGEN_USE_GPU
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/kernels/sequence_ops.h"
#include "tensorflow/core/util/gpu_kernel_helper.h"
namespace tensorflow {
using GPUDevice = Eigen::GpuDevice;
namespace {
template <typename T>
__global__ void RangeKernel(int64_t size, T start, T delta,
T* __restrict__ output) {
for (int64_t i : GpuGridRangeX(size)) {
output[i] = start + static_cast<T>(i) * delta;
}
}
} // namespace
namespace functor {
template <typename T>
struct RangeFunctor<GPUDevice, T> {
void operator()(OpKernelContext* context, int64_t size, T start, T delta,
typename TTypes<T>::Flat output) const {
const GPUDevice& device = context->eigen_gpu_device();
GpuLaunchConfig config = GetGpuLaunchConfig(
size, device, &RangeKernel<T>,
/*dynamic_shared_memory_size=*/0, /*block_size_limit=*/0);
OP_REQUIRES_OK(context,
GpuLaunchKernel(RangeKernel<T>, config.block_count,
config.thread_per_block, 0, device.stream(),
size, start, delta, output.data()));
}
};
} // namespace functor
#define DEFINE_FUNCTOR(T) template struct functor::RangeFunctor<GPUDevice, T>;
TF_CALL_float(DEFINE_FUNCTOR);
TF_CALL_double(DEFINE_FUNCTOR);
TF_CALL_int32(DEFINE_FUNCTOR);
TF_CALL_int64(DEFINE_FUNCTOR);
#undef DEFINE_FUNCTOR
} // namespace tensorflow
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM