-
Notifications
You must be signed in to change notification settings - Fork 74k
/
inplace_ops_functor_gpu.cu.cc
222 lines (202 loc) · 7.45 KB
/
inplace_ops_functor_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Copyright 2016 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 "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/kernels/inplace_ops_functor.h"
#include "tensorflow/core/util/gpu_kernel_helper.h"
namespace tensorflow {
namespace functor {
typedef Eigen::GpuDevice Device;
template <typename T>
__global__ void DoParallelConcatOpKernel(int nthreads, const int64 rows,
const int64 cols, int32 loc,
const T* __restrict__ src,
T* __restrict__ dst) {
GPU_1D_KERNEL_LOOP(idx, nthreads) {
int64 c = idx % cols;
int64 r = (loc % rows + rows) % rows; // Guard index range.
T* p = dst + r * cols + c;
const T* q = src + idx;
*p = ldg(q);
}
}
template <typename T>
Status DoParallelConcatUpdate(const Device& d, const Tensor& value, int32 loc,
Tensor* output) {
const int64 nelem = value.NumElements();
GpuLaunchConfig cfg = GetGpuLaunchConfig(nelem, d);
auto Toutput = output->flat_outer_dims<T>();
const int64 nrows = Toutput.dimension(0);
const int64 ncols = Toutput.dimension(1);
const T* src = value.flat<T>().data();
T* dst = output->flat<T>().data();
TF_CHECK_OK(GpuLaunchKernel(
DoParallelConcatOpKernel<T>, cfg.block_count, cfg.thread_per_block, 0,
d.stream(), cfg.virtual_thread_count, nrows, ncols, loc, src, dst));
return OkStatus();
}
template <>
Status DoParallelConcat(const Device& d, const Tensor& value, int32 loc,
Tensor* output) {
CHECK_EQ(value.dtype(), output->dtype());
switch (value.dtype()) {
#define CASE(type) \
case DataTypeToEnum<type>::value: \
return DoParallelConcatUpdate<type>(d, value, loc, output); \
break;
CASE(float)
CASE(double)
CASE(Eigen::half)
CASE(Eigen::bfloat16)
// Using TF_CALL_GPU_NUMBER_TYPES(CASE) results in the compiler complaining
// that CASE is not defined...hence the above construction
#undef CASE
default:
return errors::InvalidArgument("Unsupported data type: ",
DataTypeString(value.dtype()));
}
return OkStatus();
}
template <typename T, InplaceOpType op>
__global__ void DoInplaceOpKernel(int nthreads, const int64 rows,
const int64 cols, const int64 n,
const T* __restrict__ src,
const int32* __restrict__ rowids,
T* __restrict__ dst) {
GPU_1D_KERNEL_LOOP(idx, nthreads) {
int64 r = idx / cols;
int64 c = idx % cols;
r = (rowids[r] % rows + rows) % rows; // Guard index range.
T* p = dst + r * cols + c;
const T* q = src + idx;
switch (op) {
case I_UPDATE:
*p = ldg(q);
break;
case I_ADD:
*p += ldg(q);
break;
case I_SUB:
*p -= ldg(q);
break;
}
}
}
template <typename T>
void DoInplaceOp(const Device& d, InplaceOpType op, const Tensor& i,
const Tensor& v, Tensor* y) {
const int64 nelem = v.NumElements();
GpuLaunchConfig cfg = GetGpuLaunchConfig(nelem, d);
auto Ty = y->flat_outer_dims<T>();
const int64 nrows = Ty.dimension(0);
const int64 ncols = Ty.dimension(1);
const int64 n = i.NumElements();
const T* src = v.flat<T>().data();
// TODO(sjhwang): Check that first dimension fits in int32 range.
const int32* rowids = i.flat<int32>().data();
T* dst = y->flat<T>().data();
switch (op) {
case I_UPDATE:
TF_CHECK_OK(GpuLaunchKernel(DoInplaceOpKernel<T, I_UPDATE>,
cfg.block_count, cfg.thread_per_block, 0,
d.stream(), cfg.virtual_thread_count, nrows,
ncols, n, src, rowids, dst));
break;
case I_ADD:
TF_CHECK_OK(GpuLaunchKernel(DoInplaceOpKernel<T, I_ADD>, cfg.block_count,
cfg.thread_per_block, 0, d.stream(),
cfg.virtual_thread_count, nrows, ncols, n,
src, rowids, dst));
break;
case I_SUB:
TF_CHECK_OK(GpuLaunchKernel(DoInplaceOpKernel<T, I_SUB>, cfg.block_count,
cfg.thread_per_block, 0, d.stream(),
cfg.virtual_thread_count, nrows, ncols, n,
src, rowids, dst));
break;
}
}
template <bool>
void DoInplaceOp(const Device& d, InplaceOpType op, const Tensor& i,
const Tensor& v, Tensor* y) {
const int64 nelem = v.NumElements();
GpuLaunchConfig cfg = GetGpuLaunchConfig(nelem, d);
auto Ty = y->flat_outer_dims<bool>();
const int64 nrows = Ty.dimension(0);
const int64 ncols = Ty.dimension(1);
const int64 n = i.NumElements();
const bool* src = v.flat<bool>().data();
// TODO(sjhwang): Check that first dimension fits in int32 range.
const int32* rowids = i.flat<int32>().data();
bool* dst = y->flat<bool>().data();
if (op == I_UPDATE) {
TF_CHECK_OK(GpuLaunchKernel(DoInplaceOpKernel<bool, I_UPDATE>,
cfg.block_count, cfg.thread_per_block, 0,
d.stream(), cfg.virtual_thread_count, nrows,
ncols, n, src, rowids, dst));
}
}
template <>
Status DoInplace(const Device& d, InplaceOpType op, const Tensor& i,
const Tensor& v, Tensor* y) {
CHECK_EQ(v.dtype(), y->dtype());
switch (v.dtype()) {
#define CASE(type) \
case DataTypeToEnum<type>::value: \
DoInplaceOp<type>(d, op, i, v, y); \
break;
CASE(bool)
CASE(float)
CASE(double)
CASE(Eigen::half)
CASE(Eigen::bfloat16)
CASE(uint8_t)
CASE(int8_t)
CASE(int64_t)
CASE(uint64_t)
#undef CASE
default:
return errors::InvalidArgument("Unsupported data type from DoInplace: ",
DataTypeString(v.dtype()));
}
return OkStatus();
}
template <>
Status DoCopy(const Device& d, const Tensor& x, Tensor* y) {
CHECK_EQ(x.dtype(), y->dtype());
switch (x.dtype()) {
#define CASE(type) \
case DataTypeToEnum<type>::value: \
y->flat<type>().device(d) = x.flat<type>(); \
break;
CASE(bool)
CASE(float)
CASE(double)
CASE(Eigen::half)
CASE(Eigen::bfloat16)
CASE(complex64)
CASE(complex128)
CASE(uint8_t)
CASE(int8_t)
CASE(int64_t)
CASE(uint64_t)
#undef CASE
default:
return errors::InvalidArgument("Unsupported dtype from DoCopy: ",
DataTypeString(x.dtype()));
}
return OkStatus();
}
} // end namespace functor
} // namespace tensorflow
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM