-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhingetree_conv_gpu.cu
600 lines (442 loc) · 29.5 KB
/
hingetree_conv_gpu.cu
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*-
* Nathan Lay
* AI Resource at National Cancer Institute
* National Institutes of Health
* May 2021
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <tuple>
#include <utility>
#include <functional>
#include "torch/extension.h"
#include "HingeTreeCommon.cuh"
#include "ImageToMatrix.h"
typedef c10::IntArrayRef IntArrayRef;
// From: https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html
// And from: https://stackoverflow.com/questions/39274472/error-function-atomicadddouble-double-has-already-been-defined
#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600
//#if __CUDA_ARCH__ < 600
#else
static inline __device__ double atomicAdd(double* address, double val)
{
unsigned long long int* address_as_ull =
(unsigned long long int*)address;
unsigned long long int old = *address_as_ull, assumed;
do {
assumed = old;
old = atomicCAS(address_as_ull, assumed,
__double_as_longlong(val +
__longlong_as_double(assumed)));
// Note: uses integer comparison to avoid hang in case of NaN (since NaN != NaN)
} while (assumed != old);
return __longlong_as_double(old);
}
#endif
namespace {
template<typename TreeTraitsTypeGPU, typename RealType>
__global__ void ForwardKernel(const RealType *d_matrix, const RealType *d_inThresholds, const int64_t *d_inOrdinals, const RealType *d_inWeights, RealType *d_outData,
int64_t i64TreeDepth, int64_t i64ThresholdStride, int64_t i64WeightsStride, int64_t i64InnerWeightsNum, int64_t i64OutChannels, int64_t i64Rows, int64_t i64Cols) {
typedef typename TreeTraitsTypeGPU::KeyType KeyType;
const int64_t j = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
const int64_t k = (int64_t)blockIdx.y * blockDim.y + threadIdx.y;
if (j < i64OutChannels && k < i64Rows) {
const RealType * const d_thresholds = d_inThresholds + j*i64ThresholdStride;
const int64_t * const d_ordinals = d_inOrdinals + j*i64ThresholdStride;
const RealType * const d_row = d_matrix + k*i64Cols;
// leaf key, margin, ordinal index
const auto keyMarginTuple = TreeTraitsTypeGPU::ComputeKeyAndSignedMargin(d_row, d_thresholds, d_ordinals, i64TreeDepth, 1);
const KeyType key = keyMarginTuple.leafKey;
const RealType signedMargin = keyMarginTuple.signedMargin;
const RealType margin = std::abs(signedMargin);
const RealType * const d_leafWeights = d_inWeights + (j*i64WeightsStride + key)*i64InnerWeightsNum;
RealType * const d_out = d_outData + (j*i64Rows + k)*i64InnerWeightsNum;
for (int64_t l = 0; l < i64InnerWeightsNum; ++l)
d_out[l] += d_leafWeights[l] * margin;
}
}
template<typename TreeTraitsTypeGPU, typename RealType>
__global__ void BackwardThresholdsKernel(const RealType *d_matrix, const RealType *d_inThresholds, const int64_t *d_inOrdinals, const RealType *d_inWeights,
const RealType *d_outDataGradient, RealType *d_inThresholdsGradient, int64_t i64TreeDepth, int64_t i64ThresholdStride, int64_t i64WeightsStride, int64_t i64InnerWeightsNum, int64_t i64OutChannels,
int64_t i64Rows, int64_t i64Cols) {
typedef typename TreeTraitsTypeGPU::KeyType KeyType;
const int64_t j = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
const int64_t k = (int64_t)blockIdx.y * blockDim.y + threadIdx.y;
if (j < i64OutChannels && k < i64Rows) {
const RealType * const d_thresholds = d_inThresholds + j*i64ThresholdStride;
const int64_t * const d_ordinals = d_inOrdinals + j*i64ThresholdStride;
RealType * const d_thresholdsGradient = d_inThresholdsGradient + j*i64ThresholdStride;
const RealType * const d_row = d_matrix + k*i64Cols;
// leaf key, margin, ordinal index
const auto keyMarginTuple = TreeTraitsTypeGPU::ComputeKeyAndSignedMargin(d_row, d_thresholds, d_ordinals, i64TreeDepth, 1);
const KeyType key = keyMarginTuple.leafKey;
const RealType signedMargin = keyMarginTuple.signedMargin;
const KeyType thresholdIndex = keyMarginTuple.thresholdIndex;
const RealType sign = RealType((RealType(0) < signedMargin) - (signedMargin < RealType(0)));
const RealType * const d_leafWeights = d_inWeights + (j*i64WeightsStride + key)*i64InnerWeightsNum;
const RealType * const d_outGradient = d_outDataGradient + (j*i64Rows + k)*i64InnerWeightsNum;
RealType tmpSum = RealType(0);
for (int64_t l = 0; l < i64InnerWeightsNum; ++l)
tmpSum += d_leafWeights[l] * d_outGradient[l];
tmpSum *= -sign;
atomicAdd(d_thresholdsGradient + thresholdIndex, tmpSum); // Do this just once
//d_thresholdsGradient[thresholdIndex] += -sign * d_leafWeights[l] * d_outGradient[l];
}
}
template<typename TreeTraitsTypeGPU, typename RealType>
__global__ void BackwardWeightsKernel(const RealType *d_matrix, const RealType *d_inThresholds, const int64_t *d_inOrdinals, /*const RealType *d_inWeights,*/
const RealType *d_outDataGradient, RealType *d_inWeightsGradient, int64_t i64TreeDepth, int64_t i64ThresholdStride, int64_t i64WeightsStride, int64_t i64InnerWeightsNum, int64_t i64OutChannels,
int64_t i64Rows, int64_t i64Cols) {
typedef typename TreeTraitsTypeGPU::KeyType KeyType;
const int64_t j = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
const int64_t k = (int64_t)blockIdx.y * blockDim.y + threadIdx.y;
if (j < i64OutChannels && k < i64Rows) {
const RealType * const d_thresholds = d_inThresholds + j*i64ThresholdStride;
const int64_t * const d_ordinals = d_inOrdinals + j*i64ThresholdStride;
const RealType * const d_row = d_matrix + k*i64Cols;
// leaf key, margin, ordinal index
const auto keyMarginTuple = TreeTraitsTypeGPU::ComputeKeyAndSignedMargin(d_row, d_thresholds, d_ordinals, i64TreeDepth, 1);
const KeyType key = keyMarginTuple.leafKey;
const RealType signedMargin = keyMarginTuple.signedMargin;
const RealType margin = std::abs(signedMargin);
const RealType * const d_outGradient = d_outDataGradient + (j*i64Rows + k)*i64InnerWeightsNum;
RealType * const d_leafWeightsGradient = d_inWeightsGradient + (j*i64WeightsStride + key)*i64InnerWeightsNum;
for (int64_t l = 0; l < i64InnerWeightsNum; ++l) {
atomicAdd(d_leafWeightsGradient + l, margin * d_outGradient[l]); // Really bad!
//d_leafWeightsGradient[l] += margin * d_outGradient[l];
}
}
}
template<typename TreeTraitsTypeGPU, typename RealType>
__global__ void BackwardDataKernel(const RealType *d_matrix, const int64_t *d_indexMatrix, const RealType *d_inThresholds, const int64_t *d_inOrdinals, const RealType *d_inWeights,
const RealType *d_outDataGradient, RealType *d_inDataGradient, int64_t i64TreeDepth, int64_t i64ThresholdStride, int64_t i64WeightsStride, int64_t i64InnerWeightsNum, int64_t i64OutChannels,
int64_t i64Rows, int64_t i64Cols) {
typedef typename TreeTraitsTypeGPU::KeyType KeyType;
const int64_t j = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
const int64_t k = (int64_t)blockIdx.y * blockDim.y + threadIdx.y;
if (j < i64OutChannels && k < i64Rows) {
const RealType * const d_thresholds = d_inThresholds + j*i64ThresholdStride;
const int64_t * const d_ordinals = d_inOrdinals + j*i64ThresholdStride;
const RealType * const d_row = d_matrix + k*i64Cols;
const int64_t * const d_i64IndexRow = d_indexMatrix + k*i64Cols;
// leaf key, margin, ordinal index
const auto keyMarginTuple = TreeTraitsTypeGPU::ComputeKeyAndSignedMargin(d_row, d_thresholds, d_ordinals, i64TreeDepth, 1);
const KeyType key = keyMarginTuple.leafKey;
const RealType signedMargin = keyMarginTuple.signedMargin;
const KeyType thresholdIndex = keyMarginTuple.thresholdIndex;
const int64_t i64FeatureIndex = d_ordinals[thresholdIndex];
const int64_t i64ImageIndex = d_i64IndexRow[i64FeatureIndex];
if (i64ImageIndex >= 0) {
const RealType * const d_leafWeights = d_inWeights + (j*i64WeightsStride + key)*i64InnerWeightsNum;
const RealType * const d_outGradient = d_outDataGradient + (j*i64Rows + k)*i64InnerWeightsNum;
const RealType sign = RealType((RealType(0) < signedMargin) - (signedMargin < RealType(0)));
RealType tmpSum = RealType(0);
for (int64_t l = 0; l < i64InnerWeightsNum; ++l)
tmpSum += d_leafWeights[l] * d_outGradient[l];
tmpSum *= sign;
atomicAdd(d_inDataGradient + i64ImageIndex, tmpSum); // Do this just once
}
}
}
} // end anonymous namespace
template<typename RealType, unsigned int Dimension, typename TreeTraitsType>
torch::Tensor hingetree_conv_gpu_forward(torch::Tensor inData, torch::Tensor inThresholds, torch::Tensor inOrdinals, torch::Tensor inWeights,
IntArrayRef kernelSize, IntArrayRef stride, IntArrayRef padding, IntArrayRef dilation) {
typedef bleak::HingeTreeCommonGPU<TreeTraitsType> TreeTraitsTypeGPU;
typedef bleak::ImageToMatrix<RealType, Dimension> ImageToMatrixType;
if (kernelSize.size() != Dimension || stride.size() != Dimension || padding.size() != Dimension || dilation.size() != Dimension)
return torch::Tensor();
if (inData.dim() != Dimension+2 || inThresholds.dim() != 3 || inOrdinals.dim() != 3 || inWeights.dim() < 3)
return torch::Tensor();
if (inThresholds.sizes() != inOrdinals.sizes() || inWeights.sizes()[0] != inThresholds.sizes()[0])
return torch::Tensor();
const int64_t i64Groups = inData.sizes()[1] / inWeights.sizes()[1];
if (i64Groups*inWeights.sizes()[1] != inData.sizes()[1])
return torch::Tensor();
if ((inWeights.sizes()[0] % i64Groups) != 0) // Must also divide output channels
return torch::Tensor();
// C x H x W x ...
int64_t a_i64ImageSize[Dimension+1] = { 0 };
std::copy_n(inData.sizes().slice(1).data(), Dimension+1, a_i64ImageSize);
a_i64ImageSize[0] = 1; // Process 1 channel at a time
ImageToMatrixType clImageToMatrix;
clImageToMatrix.SetKernelSize(kernelSize.data());
clImageToMatrix.SetStride(stride.data());
clImageToMatrix.SetPadding(padding.data());
clImageToMatrix.SetDilate(dilation.data());
if (!clImageToMatrix.Good(a_i64ImageSize))
return torch::Tensor();
const int64_t i64KernelCount = clImageToMatrix.ComputeKernelCount();
if (inOrdinals.min().to(torch::kCPU).item<int64_t>() < 0 || inOrdinals.max().to(torch::kCPU).item<int64_t>() >= i64KernelCount)
return torch::Tensor();
const int64_t i64NumLeavesPerTree = inWeights.sizes()[2];
const int64_t i64TreeDepth = TreeTraitsType::ComputeDepth(i64NumLeavesPerTree);
if (i64TreeDepth > TreeTraitsType::GetMaxDepth() || inThresholds.sizes()[2] != TreeTraitsType::GetThresholdCount(i64TreeDepth))
return torch::Tensor();
const int64_t i64BatchSize = inData.sizes()[0];
const int64_t i64InChannels = inData.sizes()[1];
const int64_t i64OutChannels = inWeights.sizes()[0];
const int64_t i64NumDecisionsPerTree = inThresholds.sizes()[2];
std::vector<IntArrayRef::value_type> vSizes;
vSizes.resize(2);
vSizes[0] = inData.sizes()[0]; // batch size
vSizes[1] = inWeights.sizes()[0]; // Number of output channels
{
const auto tmpSizes = clImageToMatrix.ComputeOutputSize(a_i64ImageSize);
vSizes.insert(vSizes.end(), tmpSizes.begin(), tmpSizes.end());
}
{
auto inWeightsSlice = inWeights.sizes().slice(3);
vSizes.insert(vSizes.end(), inWeightsSlice.begin(), inWeightsSlice.end());
}
int64_t i64InnerWeightsNum = 1;
{
auto inWeightsSlice = inWeights.sizes().slice(3);
i64InnerWeightsNum = std::accumulate(inWeightsSlice.begin(), inWeightsSlice.end(), (int64_t)1, std::multiplies<IntArrayRef::value_type>());
}
int64_t i64InChannelSize = 1;
{
auto inDataSlice = inData.sizes().slice(2);
i64InChannelSize = std::accumulate(inDataSlice.begin(), inDataSlice.end(), (int64_t)1, std::multiplies<IntArrayRef::value_type>());
}
const int64_t i64OutDataImageSize = clImageToMatrix.ComputeOutputCount(a_i64ImageSize);
// Index matrix dimensions
int64_t i64Rows = 0;
int64_t i64Cols = 0;
clImageToMatrix.ComputeMatrixDimensions(i64Rows, i64Cols, a_i64ImageSize);
torch::Tensor indexMatrix;
torch::Tensor featureMatrix;
{
auto clOptions = torch::TensorOptions().dtype(torch::kInt64).device(inData.device());
indexMatrix = torch::empty({ i64Rows, i64Cols }, clOptions);
}
int64_t * const d_indexMatrix = indexMatrix.data_ptr<int64_t>();
{
auto clOptions = torch::TensorOptions().dtype(inData.dtype()).device(inData.device());
featureMatrix = torch::empty({ i64Rows, i64Cols }, clOptions);
}
RealType * const d_featureMatrix = featureMatrix.data_ptr<RealType>();
clImageToMatrix.ExtractIndexMatrixGPU(d_indexMatrix, a_i64ImageSize);
torch::Tensor outData;
{
auto clOptions = torch::TensorOptions().dtype(inData.dtype()).device(inData.device());
outData = torch::zeros(IntArrayRef(vSizes.data(), vSizes.size()),clOptions);
}
RealType * const d_outData = outData.data_ptr<RealType>();
const RealType * const d_inData = inData.data_ptr<RealType>();
const RealType * const d_inThresholds = inThresholds.data_ptr<RealType>();
const int64_t * const d_inOrdinals = inOrdinals.data_ptr<int64_t>();
const RealType * const d_inWeights = inWeights.data_ptr<RealType>();
// Trees vs Patch Rows (m_iRows)
const dim3 threadsPerBlock(16, 16);
const dim3 numBlocks((i64OutChannels + threadsPerBlock.x-1) / threadsPerBlock.x, (i64Rows + threadsPerBlock.y-1) / threadsPerBlock.y);
const int i64WeightsStride = i64InChannels * i64NumLeavesPerTree;
const int i64ThresholdStride = i64InChannels * i64NumDecisionsPerTree;
for (int64_t i = 0; i < i64BatchSize; ++i) {
for (int64_t g = 0; g < i64Groups; ++g) {
for (int64_t c = 0; c < i64InChannels; ++c) {
clImageToMatrix.ExtractMatrixGPU(d_featureMatrix, d_inData + ((i*i64Groups + g)*i64InChannels + c)*i64InChannelSize, d_indexMatrix, a_i64ImageSize);
ForwardKernel<TreeTraitsTypeGPU><<<numBlocks, threadsPerBlock>>>(d_featureMatrix, d_inThresholds + ((g*i64OutChannels + 0)*i64InChannels + c)*i64NumDecisionsPerTree,
d_inOrdinals + ((g*i64OutChannels + 0)*i64InChannels + c)*i64NumDecisionsPerTree,
d_inWeights + (((g*i64OutChannels + 0)*i64InChannels + c)*i64NumLeavesPerTree + 0)*i64InnerWeightsNum,
d_outData + (i*i64Groups + g)*i64OutChannels*i64Rows*i64InnerWeightsNum,
i64TreeDepth, i64ThresholdStride, i64WeightsStride, i64InnerWeightsNum, i64OutChannels, i64Rows, i64Cols);
}
}
}
return outData;
}
template<typename RealType, unsigned int Dimension, typename TreeTraitsType>
std::vector<torch::Tensor> hingetree_conv_gpu_backward(torch::Tensor inData, bool bInDataGrad, torch::Tensor inThresholds, bool bInThresholdsGrad, torch::Tensor inOrdinals, bool bInOrdinalsGrad, torch::Tensor inWeights, bool bInWeightsGrad, torch::Tensor outDataGrad, IntArrayRef kernelSize, IntArrayRef stride, IntArrayRef padding, IntArrayRef dilation) {
typedef bleak::HingeTreeCommonGPU<TreeTraitsType> TreeTraitsTypeGPU;
typedef bleak::ImageToMatrix<RealType, Dimension> ImageToMatrixType;
if (bInOrdinalsGrad) // NEVER differentiable
return std::vector<torch::Tensor>();
if (kernelSize.size() != Dimension || stride.size() != Dimension || padding.size() != Dimension || dilation.size() != Dimension)
return std::vector<torch::Tensor>();
if (inData.dim() != Dimension+2 || inThresholds.dim() != 3 || inOrdinals.dim() != 3 || inWeights.dim() < 3)
return std::vector<torch::Tensor>();
if (inThresholds.sizes() != inOrdinals.sizes() || inWeights.sizes()[0] != inThresholds.sizes()[0])
return std::vector<torch::Tensor>();
const int64_t i64Groups = inData.sizes()[1] / inWeights.sizes()[1];
if (i64Groups*inWeights.sizes()[1] != inData.sizes()[1])
return std::vector<torch::Tensor>();
if ((inWeights.sizes()[0] % i64Groups) != 0) // Must also divide output channels
return std::vector<torch::Tensor>();
// C x H x W x ...
int64_t a_i64ImageSize[Dimension+1] = { 0 };
std::copy_n(inData.sizes().slice(1).data(), Dimension+1, a_i64ImageSize);
a_i64ImageSize[0] = 1; // Process 1 channel at a time
ImageToMatrixType clImageToMatrix;
clImageToMatrix.SetKernelSize(kernelSize.data());
clImageToMatrix.SetStride(stride.data());
clImageToMatrix.SetPadding(padding.data());
clImageToMatrix.SetDilate(dilation.data());
if (!clImageToMatrix.Good(a_i64ImageSize))
return std::vector<torch::Tensor>();
const int64_t i64KernelCount = clImageToMatrix.ComputeKernelCount();
if (inOrdinals.min().to(torch::kCPU).item<int64_t>() < 0 || inOrdinals.max().to(torch::kCPU).item<int64_t>() >= i64KernelCount)
return std::vector<torch::Tensor>();
const int64_t i64NumLeavesPerTree = inWeights.sizes()[2];
const int64_t i64TreeDepth = TreeTraitsType::ComputeDepth(i64NumLeavesPerTree);
if (i64TreeDepth > TreeTraitsType::GetMaxDepth() || inThresholds.sizes()[2] != TreeTraitsType::GetThresholdCount(i64TreeDepth))
return std::vector<torch::Tensor>();
const int64_t i64BatchSize = inData.sizes()[0];
const int64_t i64InChannels = inData.sizes()[1];
const int64_t i64OutChannels = inWeights.sizes()[0];
const int64_t i64NumDecisionsPerTree = inThresholds.sizes()[2];
std::vector<IntArrayRef::value_type> vSizes;
vSizes.resize(2);
vSizes[0] = inData.sizes()[0]; // batch size
vSizes[1] = inWeights.sizes()[0]; // Number of output channels
{
const auto tmpSizes = clImageToMatrix.ComputeOutputSize(a_i64ImageSize);
vSizes.insert(vSizes.end(), tmpSizes.begin(), tmpSizes.end());
}
{
auto inWeightsSlice = inWeights.sizes().slice(3);
vSizes.insert(vSizes.end(), inWeightsSlice.begin(), inWeightsSlice.end());
}
if (outDataGrad.sizes() != IntArrayRef(vSizes.data(), vSizes.size()))
return std::vector<torch::Tensor>();
int64_t i64InnerWeightsNum = 1;
{
auto inWeightsSlice = inWeights.sizes().slice(3);
i64InnerWeightsNum = std::accumulate(inWeightsSlice.begin(), inWeightsSlice.end(), (int64_t)1, std::multiplies<IntArrayRef::value_type>());
}
int64_t i64InChannelSize = 1;
{
auto inDataSlice = inData.sizes().slice(2);
i64InChannelSize = std::accumulate(inDataSlice.begin(), inDataSlice.end(), (int64_t)1, std::multiplies<IntArrayRef::value_type>());
}
const int64_t i64OutDataImageSize = clImageToMatrix.ComputeOutputCount(a_i64ImageSize);
// Index matrix dimensions
int64_t i64Rows = 0;
int64_t i64Cols = 0;
clImageToMatrix.ComputeMatrixDimensions(i64Rows, i64Cols, a_i64ImageSize);
torch::Tensor indexMatrix;
torch::Tensor featureMatrix;
{
auto clOptions = torch::TensorOptions().dtype(torch::kInt64).device(inData.device());
indexMatrix = torch::empty({ i64Rows, i64Cols }, clOptions);
}
int64_t * const d_indexMatrix = indexMatrix.data_ptr<int64_t>();
{
auto clOptions = torch::TensorOptions().dtype(inData.dtype()).device(inData.device());
featureMatrix = torch::empty({ i64Rows, i64Cols }, clOptions);
}
RealType * const d_featureMatrix = featureMatrix.data_ptr<RealType>();
clImageToMatrix.ExtractIndexMatrixGPU(d_indexMatrix, a_i64ImageSize);
const RealType * const d_outDataGrad = outDataGrad.data_ptr<RealType>();
const RealType * const d_inData = inData.data_ptr<RealType>();
const RealType * const d_inThresholds = inThresholds.data_ptr<RealType>();
const int64_t * const d_inOrdinals = inOrdinals.data_ptr<int64_t>();
const RealType * const d_inWeights = inWeights.data_ptr<RealType>();
// Trees vs Patch Rows (m_iRows)
const dim3 threadsPerBlock(16, 16);
const dim3 numBlocks((i64OutChannels + threadsPerBlock.x-1) / threadsPerBlock.x, (i64Rows + threadsPerBlock.y-1) / threadsPerBlock.y);
const int i64WeightsStride = i64InChannels * i64NumLeavesPerTree;
const int i64ThresholdStride = i64InChannels * i64NumDecisionsPerTree;
//auto clOptions = torch::TensorOptions().dtype(inData.dtype()).device(inData.device());
std::vector<torch::Tensor> vGrads(4);
if (bInDataGrad) {
torch::Tensor inDataGrad = torch::zeros_like(inData);
RealType * const d_inDataGrad = inDataGrad.data_ptr<RealType>();
for (int64_t i = 0; i < i64BatchSize; ++i) {
for (int64_t g = 0; g < i64Groups; ++g) {
for (int64_t c = 0; c < i64InChannels; ++c) {
clImageToMatrix.ExtractMatrixGPU(d_featureMatrix, d_inData + ((i*i64Groups + g)*i64InChannels + c)*i64InChannelSize, d_indexMatrix, a_i64ImageSize);
BackwardDataKernel<TreeTraitsTypeGPU><<<numBlocks, threadsPerBlock>>>(d_featureMatrix, d_indexMatrix,
d_inThresholds + ((g*i64OutChannels + 0)*i64InChannels + c)*i64NumDecisionsPerTree,
d_inOrdinals + ((g*i64OutChannels + 0)*i64InChannels + c)*i64NumDecisionsPerTree,
d_inWeights + (((g*i64OutChannels + 0)*i64InChannels + c)*i64NumLeavesPerTree + 0)*i64InnerWeightsNum,
d_outDataGrad + (i*i64Groups + g)*i64OutChannels*i64Rows*i64InnerWeightsNum,
d_inDataGrad + ((i*i64Groups + g)*i64InChannels + c)*i64InChannelSize,
i64TreeDepth, i64ThresholdStride, i64WeightsStride, i64InnerWeightsNum, i64OutChannels, i64Rows, i64Cols);
}
}
}
vGrads[0] = inDataGrad;
}
if (bInThresholdsGrad) {
torch::Tensor inThresholdsGrad = torch::zeros_like(inThresholds);
RealType * const d_inThresholdsGrad = inThresholdsGrad.data_ptr<RealType>();
for (int64_t i = 0; i < i64BatchSize; ++i) {
for (int64_t g = 0; g < i64Groups; ++g) {
for (int64_t c = 0; c < i64InChannels; ++c) {
clImageToMatrix.ExtractMatrixGPU(d_featureMatrix, d_inData + ((i*i64Groups + g)*i64InChannels + c)*i64InChannelSize, d_indexMatrix, a_i64ImageSize);
BackwardThresholdsKernel<TreeTraitsTypeGPU><<<numBlocks, threadsPerBlock>>>(d_featureMatrix,
d_inThresholds + ((g*i64OutChannels + 0)*i64InChannels + c)*i64NumDecisionsPerTree,
d_inOrdinals + ((g*i64OutChannels + 0)*i64InChannels + c)*i64NumDecisionsPerTree,
d_inWeights + (((g*i64OutChannels + 0)*i64InChannels + c)*i64NumLeavesPerTree + 0)*i64InnerWeightsNum,
d_outDataGrad + (i*i64Groups + g)*i64OutChannels*i64Rows*i64InnerWeightsNum,
d_inThresholdsGrad + ((g*i64OutChannels + 0)*i64InChannels +c)*i64NumDecisionsPerTree,
i64TreeDepth, i64ThresholdStride, i64WeightsStride, i64InnerWeightsNum, i64OutChannels, i64Rows, i64Cols);
}
}
}
vGrads[1] = inThresholdsGrad;
}
if (bInWeightsGrad) {
torch::Tensor inWeightsGrad = torch::zeros_like(inWeights);
RealType * const d_inWeightsGrad = inWeightsGrad.data_ptr<RealType>();
for (int64_t i = 0; i < i64BatchSize; ++i) {
for (int64_t g = 0; g < i64Groups; ++g) {
for (int64_t c = 0; c < i64InChannels; ++c) {
clImageToMatrix.ExtractMatrixGPU(d_featureMatrix, d_inData + ((i*i64Groups + g)*i64InChannels + c)*i64InChannelSize, d_indexMatrix, a_i64ImageSize);
BackwardWeightsKernel<TreeTraitsTypeGPU><<<numBlocks, threadsPerBlock>>>(d_featureMatrix,
d_inThresholds + ((g*i64OutChannels + 0)*i64InChannels + c)*i64NumDecisionsPerTree,
d_inOrdinals + ((g*i64OutChannels + 0)*i64InChannels + c)*i64NumDecisionsPerTree,
/*d_inWeights + (((g*i64OutChannels + 0)*i64InChannels + c)*i64NumLeavesPerTree + 0)*i64InnerWeightsNum,*/
d_outDataGrad + (i*i64Groups + g)*i64OutChannels*i64Rows*i64InnerWeightsNum,
d_inWeightsGrad + (((g*i64OutChannels + 0)*i64InChannels + c)*i64NumLeavesPerTree + 0)*i64InnerWeightsNum,
i64TreeDepth, i64ThresholdStride, i64WeightsStride, i64InnerWeightsNum, i64OutChannels, i64Rows, i64Cols);
}
}
}
vGrads[3] = inWeightsGrad;
}
return vGrads;
}
// 1D
template torch::Tensor hingetree_conv_gpu_forward<float, 1, bleak::HingeTreeCommon<float>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template torch::Tensor hingetree_conv_gpu_forward<double, 1, bleak::HingeTreeCommon<double>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template torch::Tensor hingetree_conv_gpu_forward<float, 1, bleak::HingeFernCommon<float>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template torch::Tensor hingetree_conv_gpu_forward<double, 1, bleak::HingeFernCommon<double>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<float, 1, bleak::HingeTreeCommon<float>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<double, 1, bleak::HingeTreeCommon<double>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<float, 1, bleak::HingeFernCommon<float>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<double, 1, bleak::HingeFernCommon<double>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
// 2D
template torch::Tensor hingetree_conv_gpu_forward<float, 2, bleak::HingeTreeCommon<float>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template torch::Tensor hingetree_conv_gpu_forward<double, 2, bleak::HingeTreeCommon<double>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template torch::Tensor hingetree_conv_gpu_forward<float, 2, bleak::HingeFernCommon<float>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template torch::Tensor hingetree_conv_gpu_forward<double, 2, bleak::HingeFernCommon<double>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<float, 2, bleak::HingeTreeCommon<float>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<double, 2, bleak::HingeTreeCommon<double>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<float, 2, bleak::HingeFernCommon<float>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<double, 2, bleak::HingeFernCommon<double>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
// 3D
template torch::Tensor hingetree_conv_gpu_forward<float, 3, bleak::HingeTreeCommon<float>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template torch::Tensor hingetree_conv_gpu_forward<double, 3, bleak::HingeTreeCommon<double>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template torch::Tensor hingetree_conv_gpu_forward<float, 3, bleak::HingeFernCommon<float>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template torch::Tensor hingetree_conv_gpu_forward<double, 3, bleak::HingeFernCommon<double>>(torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<float, 3, bleak::HingeTreeCommon<float>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<double, 3, bleak::HingeTreeCommon<double>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<float, 3, bleak::HingeFernCommon<float>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);
template std::vector<torch::Tensor> hingetree_conv_gpu_backward<double, 3, bleak::HingeFernCommon<double>>(torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, bool, torch::Tensor, IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef);