This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
DeviceTensor-inl.cuh
509 lines (424 loc) · 13.3 KB
/
DeviceTensor-inl.cuh
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <assert.h>
#include <sstream>
#ifndef __CUDA_ARCH__
// host code
#include <stdexcept>
#endif
namespace facebook { namespace cuda {
namespace detail {
template <typename T, int N>
__host__ __device__ void copy(T to[N], T from[N]) {
for (int i = 0; i < N; ++i) {
to[i] = from[i];
}
}
} // namespace detail
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
__host__ __device__
DeviceTensor<T, Dim, IndexT, PtrTraits>::DeviceTensor()
: data_(NULL) {
cuda_static_assert(Dim > 0);
for (int i = 0; i < Dim; ++i) {
size_[i] = 0;
stride_[i] = (IndexT) 1;
}
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
__host__ __device__
DeviceTensor<T, Dim, IndexT, PtrTraits>::
DeviceTensor(DataPtrType data, const IndexT sizes[Dim])
: data_(data) {
cuda_static_assert(Dim > 0);
for (int i = 0; i < Dim; ++i) {
size_[i] = sizes[i];
}
stride_[Dim - 1] = (IndexT) 1;
for (int i = Dim - 2; i >= 0; --i) {
stride_[i] = stride_[i + 1] * sizes[i + 1];
}
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
__host__ __device__
DeviceTensor<T, Dim, IndexT, PtrTraits>::DeviceTensor(DataPtrType data,
const IndexT sizes[Dim],
const IndexT strides[Dim])
: data_(data) {
cuda_static_assert(Dim > 0);
for (int i = 0; i < Dim; ++i) {
size_[i] = sizes[i];
stride_[i] = strides[i];
}
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <int OtherDim>
__host__ __device__ bool
DeviceTensor<T, Dim, IndexT, PtrTraits>::isSameSize(
const DeviceTensor<T, OtherDim, IndexT, PtrTraits>& rhs) const {
if (Dim != OtherDim) {
return false;
}
for (int i = 0; i < Dim; ++i) {
if (size_[i] != rhs.size_[i]) {
return false;
}
}
return true;
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <int OtherDim>
__host__ __device__ bool
DeviceTensor<T, Dim, IndexT, PtrTraits>::isSameSizeAndStride(
const DeviceTensor<T, OtherDim, IndexT, PtrTraits>& rhs) const {
if (!isSameSize(rhs)) {
return false;
}
for (int i = 0; i < Dim; ++i) {
if (stride_[i] != rhs.stride_[i]) {
return false;
}
}
return true;
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
std::string
DeviceTensor<T, Dim, IndexT, PtrTraits>::toString() const {
std::stringstream ss;
ss << "sizes: [";
for (int i = 0; i < Dim; ++i) {
ss << getSize(i);
if (i < Dim - 1) {
ss << ", ";
}
}
ss << "] strides: [";
for (int i = 0; i < Dim; ++i) {
ss << getStride(i);
if (i < Dim - 1) {
ss << ", ";
}
}
ss << "]";
return ss.str();
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <typename U>
__host__ __device__ DeviceTensor<U, Dim, IndexT, PtrTraits>
DeviceTensor<T, Dim, IndexT, PtrTraits>::cast() {
cuda_static_assert(sizeof(U) == sizeof(T));
return DeviceTensor<U, Dim, IndexT, PtrTraits>(
reinterpret_cast<U*>(data_), size_, stride_);
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <typename U>
__host__ __device__ const DeviceTensor<U, Dim, IndexT, PtrTraits>
DeviceTensor<T, Dim, IndexT, PtrTraits>::cast() const {
cuda_static_assert(sizeof(U) == sizeof(T));
return DeviceTensor<U, Dim, IndexT, PtrTraits>(reinterpret_cast<U*>(data_),
size_,
stride_);
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
__host__ __device__ long
DeviceTensor<T, Dim, IndexT, PtrTraits>::numElements() const {
long size = getSize(0);
for (int i = 1; i < Dim; ++i) {
size *= getSize(i);
}
return size;
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
void
DeviceTensor<T, Dim, IndexT, PtrTraits>::
permuteDims(const std::vector<int>& perm) {
// This only works for contiguous tensors since strides are
// recomputed
#ifndef __CUDA_ARCH__
if (perm.size() != Dim) {
throw std::invalid_argument("Permutation list must be of the same size "
"as our dimension");
}
if (getStride(Dim - 1) != (IndexT) 1) {
throw std::invalid_argument("Innermost dimension must have stride 1");
}
for (int i = 0; i < Dim; ++i) {
if (!isContiguousDim(i)) {
throw std::invalid_argument("All dimensions must be contiguous");
}
}
#endif
// Permute
IndexT newSizes[Dim];
IndexT newStrides[Dim];
for (int i = 0; i < Dim; ++i) {
newSizes[i] = getSize(perm[i]);
}
newStrides[Dim - 1] = 1;
for (int i = Dim - 2; i >= 0; --i) {
newStrides[i] = newStrides[i + 1] * newSizes[i + 1];
}
detail::copy<IndexT, Dim>(size_, newSizes);
detail::copy<IndexT, Dim>(stride_, newStrides);
// Output sanity check
for (int i = 0; i < Dim; ++i) {
assert(isContiguousDim(i));
}
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
__host__ __device__ bool
DeviceTensor<T, Dim, IndexT, PtrTraits>::isContiguous() const {
long prevSize = 1;
for (int i = Dim - 1; i >= 0; --i) {
if (getSize(i) != (IndexT) 1) {
if (getStride(i) == prevSize) {
prevSize *= getSize(i);
} else {
return false;
}
}
}
return true;
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
__host__ __device__ bool
DeviceTensor<T, Dim, IndexT, PtrTraits>::isConsistentlySized(int i) const {
if (i == 0 && getStride(i) > 0 && getSize(i) > 0) {
return true;
} else if ((i > 0) && (i < Dim) && (getStride(i) > 0) &&
((getStride(i - 1) / getStride(i)) >= getSize(i))) {
return true;
}
return false;
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
__host__ __device__ bool
DeviceTensor<T, Dim, IndexT, PtrTraits>::isConsistentlySized() const {
for (int i = 0; i < Dim; ++i) {
if (!isConsistentlySized(i)) {
return false;
}
}
return true;
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
__host__ __device__ bool
DeviceTensor<T, Dim, IndexT, PtrTraits>::isContiguousDim(int i) const {
return (i == Dim - 1) || // just in case
((i < Dim - 1) &&
((getStride(i) / getStride(i + 1)) == getSize(i + 1)));
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
__host__ __device__ DeviceTensor<T, Dim, IndexT, PtrTraits>
DeviceTensor<T, Dim, IndexT, PtrTraits>::transpose(int dim1, int dim2) const {
#ifdef __CUDA_ARCH__
// Device code
assert(dim1 >= 0 && dim1 < Dim);
assert(dim1 >= 0 && dim2 < Dim);
#else
// Host code
if (dim1 < 0 || dim1 >= Dim) {
throw std::invalid_argument("dim1 out of bounds");
}
if (dim2 < 0 || dim2 >= Dim) {
throw std::invalid_argument("dim2 out of bounds");
}
#endif
IndexT newSize[Dim];
IndexT newStride[Dim];
for (int i = 0; i < Dim; ++i) {
newSize[i] = size_[i];
newStride[i] = stride_[i];
}
IndexT tmp = newSize[dim1];
newSize[dim1] = newSize[dim2];
newSize[dim2] = tmp;
tmp = newStride[dim1];
newStride[dim1] = newStride[dim2];
newStride[dim2] = tmp;
return DeviceTensor<T, Dim, IndexT, PtrTraits>(data_, newSize, newStride);
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <int NewDim>
__host__ __device__ DeviceTensor<T, NewDim, IndexT, PtrTraits>
DeviceTensor<T, Dim, IndexT, PtrTraits>::upcastOuter() {
// Can only create tensors of greater dimension
cuda_static_assert(NewDim > Dim);
IndexT newSize[NewDim];
IndexT newStride[NewDim];
int shift = NewDim - Dim;
for (int i = 0; i < NewDim; ++i) {
if (i < shift) {
// These are the extended dimensions
newSize[i] = (IndexT) 1;
newStride[i] = size_[0] * stride_[0];
} else {
// Shift the remaining dimensions
newSize[i] = size_[i - shift];
newStride[i] = stride_[i - shift];
}
}
return DeviceTensor<T, NewDim, IndexT, PtrTraits>(data_, newSize, newStride);
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <int NewDim>
__host__ __device__ DeviceTensor<T, NewDim, IndexT, PtrTraits>
DeviceTensor<T, Dim, IndexT, PtrTraits>::upcastInner() {
// Can only create tensors of greater dimension
cuda_static_assert(NewDim > Dim);
IndexT newSize[NewDim];
IndexT newStride[NewDim];
for (int i = 0; i < NewDim; ++i) {
if (i < Dim) {
// Existing dimensions get copied over
newSize[i] = size_[i];
newStride[i] = stride_[i];
} else {
// Extended dimensions
newSize[i] = (IndexT) 1;
newStride[i] = (IndexT) 1;
}
}
return DeviceTensor<T, NewDim, IndexT, PtrTraits>(data_, newSize, newStride);
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <int NewDim>
__host__ __device__ DeviceTensor<T, NewDim, IndexT, PtrTraits>
DeviceTensor<T, Dim, IndexT, PtrTraits>::downcastOuter() {
// Can only create tensors of lesser dimension
cuda_static_assert(NewDim < Dim);
// We can't downcast non-contiguous tensors, since it leaves
// garbage data in the tensor. The tensor needs to be contiguous
// in all of the dimensions we are collapsing (no padding in
// them).
for (int i = 0; i < Dim - NewDim; ++i) {
bool cont = isContiguousDim(i);
#ifdef __CUDA_ARCH__
// Device code
assert(cont);
#else
// Host code
if (!cont) {
throw std::invalid_argument("Can only downcast contiguous tensors");
}
#endif
}
IndexT newSize[NewDim];
IndexT newStride[NewDim];
int ignoredDims = Dim - NewDim;
IndexT collapsedSize = 1;
for (int i = 0; i < Dim; ++i) {
if (i < ignoredDims) {
// Collapse these dimensions
collapsedSize *= getSize(i);
} else {
// Non-collapsed dimensions
if (i == ignoredDims) {
// This is the first non-collapsed dimension
newSize[i - ignoredDims] = collapsedSize * getSize(i);
} else {
// Subsequent non-collapsed dimensions
newSize[i - ignoredDims] = getSize(i);
}
newStride[i - ignoredDims] = getStride(i);
}
}
return DeviceTensor<T, NewDim, IndexT, PtrTraits>(
data_, newSize, newStride);
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <int NewDim>
__host__ __device__ DeviceTensor<T, NewDim, IndexT, PtrTraits>
DeviceTensor<T, Dim, IndexT, PtrTraits>::downcastInner() {
// Can only create tensors of lesser dimension
cuda_static_assert(NewDim < Dim);
// We can't downcast non-contiguous tensors, since it leaves
// garbage data in the tensor. The tensor needs to be contiguous
// in all of the dimensions we are collapsing (no padding in
// them).
for (int i = NewDim; i < Dim; ++i) {
bool cont = isContiguousDim(i);
#ifdef __CUDA_ARCH__
// Device code
assert(cont);
#else
// Host code
if (!cont) {
throw std::invalid_argument("Can only downcast contiguous tensors");
}
#endif
}
IndexT newSize[NewDim];
IndexT newStride[NewDim];
IndexT collapsedSize = 1;
for (int i = Dim - 1; i >= 0; --i) {
if (i >= NewDim) {
// Collapse these dimensions
collapsedSize *= getSize(i);
} else {
// Non-collapsed dimensions
if (i == NewDim - 1) {
// This is the first non-collapsed dimension
newSize[i] = collapsedSize * getSize(i);
newStride[i] = getStride(Dim - 1);
} else {
// Subsequent non-collapsed dimensions
newSize[i] = getSize(i);
newStride[i] = getStride(i);
}
}
}
return DeviceTensor<T, NewDim, IndexT, PtrTraits>(data_, newSize, newStride);
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <int SubDim>
__host__ __device__ DeviceTensor<T, SubDim, IndexT, PtrTraits>
DeviceTensor<T, Dim, IndexT, PtrTraits>::view(DataPtrType at) {
cuda_static_assert(SubDim >= 1 && SubDim < Dim);
IndexT viewSizes[SubDim];
IndexT viewStrides[SubDim];
for (int i = 0; i < SubDim; ++i) {
viewSizes[i] = size_[Dim - SubDim + i];
viewStrides[i] = stride_[Dim - SubDim + i];
}
return DeviceTensor<T, SubDim, IndexT, PtrTraits>(at, viewSizes, viewStrides);
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
template <int SubDim>
__host__ __device__ DeviceTensor<T, SubDim, IndexT, PtrTraits>
DeviceTensor<T, Dim, IndexT, PtrTraits>::view() {
return view<SubDim>(data_);
}
template <typename T, int Dim,
typename IndexT, template <typename U> class PtrTraits>
void
DeviceTensor<T, Dim, IndexT, PtrTraits>::zero(cudaStream_t stream) {
#ifndef __CUDA_ARCH__
if (!isContiguous()) {
throw std::invalid_argument("fillAsync only works on contiguous data");
}
#endif
cudaMemsetAsync(data(), 0, numElements() * sizeof(T), stream);
}
} } // namespace