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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
#include <cub/cub.cuh>
#include "caffe2/core/context_gpu.h"
#include "caffe2/operators/pack_segments.h"
namespace caffe2 {
namespace {
template <typename T, typename Data_T>
__global__ void PackSegmentsKernel(
const Data_T* data_ptr,
const T* lengths_ptr,
const T* lengths_cum_sum,
const T max_length,
const int64_t num_seq,
const int64_t cell_size,
Data_T padding,
Data_T* out_ptr) {
CUDA_1D_KERNEL_LOOP(i, num_seq * max_length * cell_size) {
int seq = (i / cell_size) / max_length;
int cell = (i / cell_size) % max_length;
int offset = i % cell_size;
if (cell >= lengths_ptr[seq]) {
out_ptr[i] = padding;
} else {
int32_t idx = (lengths_cum_sum[seq] + cell) * cell_size + offset;
out_ptr[i] = data_ptr[idx];
}
}
}
template <typename T, typename Data_T>
__global__ void UnpackSegmentsKernel(
const Data_T* data_ptr,
const T* lengths_ptr,
const T* lengths_cum_sum,
const T max_length,
const int64_t num_seq,
const int64_t cell_size,
Data_T* out_ptr) {
CUDA_1D_KERNEL_LOOP(i, num_seq * max_length * cell_size) {
int seq = (i / cell_size) / max_length;
int cell = (i / cell_size) % max_length;
int offset = i % cell_size;
if (cell < lengths_ptr[seq]) {
int idx = (lengths_cum_sum[seq] + cell) * cell_size + offset;
out_ptr[idx] = data_ptr[i];
}
}
}
template <typename T>
int64_t int_array_sum(
const T* dev_array,
int64_t num_items,
Tensor<CUDAContext>& dev_buffer,
Tensor<CUDAContext>& dev_sum,
Tensor<CPUContext>& host_sum,
CUDAContext& context) {
// Retrieve buffer size
size_t temp_storage_bytes = 0;
cub::DeviceReduce::Sum(
nullptr,
temp_storage_bytes,
dev_array,
dev_sum.mutable_data<int64_t>(),
num_items,
context.cuda_stream());
// Allocate temporary storage
auto buffer_size = (temp_storage_bytes + sizeof(T)) / sizeof(T);
dev_buffer.Resize(buffer_size);
void* dev_temp_storage = static_cast<void*>(dev_buffer.mutable_data<T>());
// Find sumimum
cub::DeviceReduce::Sum(
dev_temp_storage,
temp_storage_bytes,
dev_array,
dev_sum.mutable_data<int64_t>(),
num_items,
context.cuda_stream());
// Copy to host
host_sum.CopyFrom<CUDAContext>(dev_sum);
context.FinishDeviceComputation();
return *host_sum.data<int64_t>();
}
template <typename T>
T array_max(
const T* dev_array,
int64_t num_items,
Tensor<CUDAContext>& dev_max_buffer,
Tensor<CUDAContext>& dev_max,
Tensor<CPUContext>& host_max,
CUDAContext& context) {
// Retrieve buffer size
size_t temp_storage_bytes = 0;
cub::DeviceReduce::Max(
nullptr,
temp_storage_bytes,
dev_array,
dev_max.mutable_data<T>(),
num_items,
context.cuda_stream());
// Allocate temporary storage
auto buffer_size = (temp_storage_bytes + sizeof(T)) / sizeof(T);
dev_max_buffer.Resize(buffer_size);
void* dev_temp_storage = static_cast<void*>(dev_max_buffer.mutable_data<T>());
// Find maximum
cub::DeviceReduce::Max(
dev_temp_storage,
temp_storage_bytes,
dev_array,
dev_max.mutable_data<T>(),
num_items,
context.cuda_stream());
// Copy to host
host_max.CopyFrom<CUDAContext>(dev_max);
context.FinishDeviceComputation();
return *host_max.data<T>();
}
template <typename T>
void array_prefix_sum_exclusive(
const T* dev_array,
const int32_t num_items,
Tensor<CUDAContext>& prefix_buffer,
Tensor<CUDAContext>& prefix_sum,
CUDAContext& context) {
// Retrieve buffer size
size_t temp_storage_bytes = 0;
prefix_sum.Resize(num_items);
cub::DeviceScan::ExclusiveSum(
nullptr,
temp_storage_bytes,
dev_array,
prefix_sum.mutable_data<T>(),
num_items,
context.cuda_stream());
// Allocate temporary storage
auto buffer_size = (temp_storage_bytes + sizeof(T)) / sizeof(T);
prefix_buffer.Resize(buffer_size);
void* dev_temp_storage = static_cast<void*>(prefix_buffer.mutable_data<T>());
// Exclusive sum
cub::DeviceScan::ExclusiveSum(
dev_temp_storage,
temp_storage_bytes,
dev_array,
prefix_sum.mutable_data<T>(),
num_items,
context.cuda_stream());
}
} // namespace
template <>
template <typename T>
bool PackSegmentsOp<CUDAContext>::DoRunWithType() {
return DispatchHelper<TensorTypes2<char, int32_t, int64_t, float>, T>::call(
this, Input(DATA));
}
template <>
template <typename T, typename Data_T>
bool PackSegmentsOp<CUDAContext>::DoRunWithType2() {
const auto& data = Input(DATA);
const auto& lengths = Input(LENGTHS);
int64_t num_seq = lengths.dim(0);
const Data_T* data_ptr = data.data<Data_T>();
const T* lengths_ptr = lengths.data<T>();
auto* out = Output(0);
if (return_presence_mask_) {
CAFFE_THROW("CUDA version of PackSegments does not support presence mask.");
}
CAFFE_ENFORCE(data.ndim() >= 1, "DATA should be at least 1-D");
CAFFE_ENFORCE(lengths.ndim() == 1, "LENGTH should be 1-D");
// Find the length of the longest sequence.
dev_max_length_.Resize(1);
host_max_length_.Resize(1);
const T max_length = array_max<T>(
lengths_ptr,
num_seq,
dev_buffer_,
dev_max_length_,
host_max_length_,
context_);
// Compute prefix sum over the lengths
array_prefix_sum_exclusive<T>(
lengths_ptr, num_seq, dev_buffer_, dev_lengths_prefix_sum_, context_);
// create output tensor
auto shape = data.dims(); // Shape of out is batch_size x max_len x ...
shape[0] = max_length;
shape.insert(shape.begin(), lengths.size());
out->Resize(shape);
Data_T* out_ptr = static_cast<Data_T*>(out->raw_mutable_data(data.meta()));
// Return empty out (with the proper shape) if first dim is 0.
if (!data.dim(0)) {
return true;
}
// Do padding
Data_T padding = out->IsType<float>() ? padding_ : 0;
int64_t cell_size = data.size() / data.dim(0);
PackSegmentsKernel<<<
CAFFE_GET_BLOCKS(num_seq * max_length * cell_size),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(
data_ptr,
lengths_ptr,
dev_lengths_prefix_sum_.data<T>(),
max_length,
num_seq,
cell_size,
padding,
out_ptr);
return true;
}
template <>
template <typename T>
bool UnpackSegmentsOp<CUDAContext>::DoRunWithType() {
return DispatchHelper<TensorTypes2<char, int32_t, int64_t, float>, T>::call(
this, Input(DATA));
}
template <>
template <typename T, typename Data_T>
bool UnpackSegmentsOp<CUDAContext>::DoRunWithType2() {
const auto& data = Input(DATA);
const auto& lengths = Input(LENGTHS);
int64_t num_seq = lengths.dim(0);
const Data_T* data_ptr = data.data<Data_T>();
const T* lengths_ptr = lengths.data<T>();
auto* out = Output(0);
CAFFE_ENFORCE(data.ndim() >= 1, "DATA should be at least 1-D");
CAFFE_ENFORCE(lengths.ndim() == 1, "LENGTH should be 1-D");
// Compute prefix sum over the lengths
array_prefix_sum_exclusive<T>(
lengths_ptr, num_seq, dev_buffer_, dev_lengths_prefix_sum_, context_);
// compute max of the lengths
dev_max_length_.Resize(1);
host_max_length_.Resize(1);
const T max_length = array_max<T>(
lengths_ptr,
num_seq,
dev_buffer_,
dev_max_length_,
host_max_length_,
context_);
// compute num of cells: sum of the lengths
dev_num_cell_.Resize(1);
host_num_cell_.Resize(1);
const int64_t num_cell = int_array_sum<T>(
lengths_ptr,
num_seq,
dev_buffer_,
dev_num_cell_,
host_num_cell_,
context_);
// create output tensor
auto shape = data.dims();
CAFFE_ENFORCE(
shape[0] == lengths.dim(0), "LENGTH should match DATA in dimension 0");
shape.erase(shape.begin());
shape[0] = num_cell;
out->Resize(shape);
Data_T* out_ptr = static_cast<Data_T*>(out->raw_mutable_data(data.meta()));
// Return empty out (with the proper shape) if any of the dimensions is 0.
if (!(data.dim(0) * data.dim(1))) {
return true;
}
// Unpack
int64_t cell_size = data.size() / (data.dim(0) * data.dim(1));
UnpackSegmentsKernel<<<
CAFFE_GET_BLOCKS(num_seq * max_length * cell_size),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(
data_ptr,
lengths_ptr,
dev_lengths_prefix_sum_.data<T>(),
max_length,
num_seq,
cell_size,
out_ptr);
return true;
}
REGISTER_CUDA_OPERATOR(UnpackSegments, UnpackSegmentsOp<CUDAContext>);
REGISTER_CUDA_OPERATOR(PackSegments, PackSegmentsOp<CUDAContext>);
} // namespace caffe2
|