-
Notifications
You must be signed in to change notification settings - Fork 8
/
SoftMaxTree.cu
485 lines (404 loc) · 15.3 KB
/
SoftMaxTree.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
#include "utils.h"
#define SOFTMAXTREE_THREADS 32
#define SOFTMAXTREE_MAXCHILDREN 10000
__global__ void cunnx_SoftMaxTree_updateOutput_kernel(
float *output, float *logsoftOutput, float *input, float *weight,
float *bias, float *target, float *childParent, float *parentChildren,
int nInput, int rootId, int maxFamilyPath)
{
__shared__ float buffer[SOFTMAXTREE_THREADS+1];
__shared__ float linearOutput[SOFTMAXTREE_MAXCHILDREN];
int tx = threadIdx.x;
int i_step = blockDim.x;
int k = blockIdx.x;
float *input_k = input + k*nInput;
float *nodeOutput, *nodeWeight, *nodeBias;
float narrowsum = 0;
int childId = target[k] - 1;
int parentId, parentIdx, childIdx, nChildren;
float *node;
int n = 0;
// loop through nodes
while(1)
{
/* get next Node in Tree */
node = childParent + childId*2;
parentId = (int)node[0] - 1;
childIdx = (int)node[1] - 1;
node = parentChildren + parentId*2;
parentIdx = (int)node[0] - 1;
nChildren = (int)node[1];
CudaAssert(childIdx < nChildren)
/* Linear */
nodeWeight = weight + parentIdx*nInput;
nodeBias = bias + parentIdx;
// addmv (dot products)
for (int j=0; j<nChildren; j++)
{
// zero buffer
buffer[tx] = 0;
// multiply
for (int i=tx; i<nInput; i+=i_step)
{
buffer[tx] += input_k[i]*nodeWeight[j*nInput + i];
}
// add (reduce)
for (unsigned int stride = blockDim.x >> 1; stride > 0; stride >>= 1)
{
__syncthreads();
if (tx < stride)
buffer[tx] += buffer[tx+stride];
}
if (tx == 0)
{
linearOutput[j] = buffer[0] + nodeBias[j];
}
}
__syncthreads();
/* LogSoftMax */
nodeOutput = logsoftOutput + maxFamilyPath*k + n;
// max?
buffer[tx] = -FLT_MAX;
for (int i=tx; i<nChildren; i+=i_step)
{
float z = linearOutput[i];
if(buffer[tx] < z)
buffer[tx] = z;
}
for (unsigned int stride = blockDim.x >> 1; stride > 0; stride >>= 1)
{
__syncthreads();
if ((tx < stride) && (buffer[tx] < buffer[tx+stride]))
buffer[tx] = buffer[tx+stride];
}
if (tx == 0)
{
float max_k = -FLT_MAX;
if(max_k < buffer[0])
max_k = buffer[0];
buffer[SOFTMAXTREE_THREADS] = max_k;
}
__syncthreads();
// logadd?
float max_k = buffer[SOFTMAXTREE_THREADS];
buffer[tx] = 0;
for (int i=tx; i<nChildren; i+=i_step)
{
buffer[tx] += expf(linearOutput[i]-max_k);
}
// reduce
for (unsigned int stride = blockDim.x >> 1; stride > 0; stride >>= 1)
{
__syncthreads();
if (tx < stride)
buffer[tx] += buffer[tx+stride];
}
if (tx == 0)
{
float m = max_k + logf(buffer[0]);
buffer[SOFTMAXTREE_THREADS] = m;
}
__syncthreads();
// logsoftmax
float logsum_k = buffer[SOFTMAXTREE_THREADS];
for (int i=tx; i<nChildren; i+=i_step)
{
nodeOutput[i] = linearOutput[i] - logsum_k;
}
__syncthreads();
/* Narrow + CAddTable (without log, would have been CMulTable) */
if (tx == 0)
narrowsum += nodeOutput[childIdx];
n += nChildren;
CudaAssert((n <= maxFamilyPath))
/* Break when root is reached */
if (parentId == rootId)
{
break;
}
childId = parentId;
}
if (tx == 0)
{
output[k] = narrowsum;
}
}
static int cunnx_SoftMaxTree_updateOutput(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *target = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
int inputSize = luaT_getfieldcheckint(L, 1, "inputSize");
int rootId = luaT_getfieldcheckint(L, 1, "rootId") - 1;
int maxFamilyPath = (int)luaT_getfieldcheckint(L, 1, "maxFamilyPath");
int maxFamily = (int)luaT_getfieldcheckint(L, 1, "maxFamily");
THCudaTensor *childParent = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "childParentCuda", "torch.CudaTensor");
THCudaTensor *parentChildren = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "parentChildrenCuda", "torch.CudaTensor");
THCudaTensor *logsoftOutput = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "_multiBuffer", "torch.CudaTensor");
THCudaTensor *weight = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "weight", "torch.CudaTensor");
THCudaTensor *bias = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "bias", "torch.CudaTensor");
THCudaTensor *output = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "output", "torch.CudaTensor");
luaL_argcheck(L, input->nDimension == 2, 2, "2D(batch mode) tensor expected");
luaL_argcheck(L, input->size[1] == inputSize, 2, "invalid input size");
luaL_argcheck(L, maxFamily <= SOFTMAXTREE_MAXCHILDREN, 2, "Hierarchy has node(s) with too many children");
input = THCudaTensor_newContiguous(state, input);
THCudaTensor_resize1d(state, output, input->size[0]);
/* call cudakernel */
dim3 blocks(input->size[0]); // each block is an example
dim3 threads(SOFTMAXTREE_THREADS);
cunnx_SoftMaxTree_updateOutput_kernel<<<blocks,threads>>>(
THCudaTensor_data(state, output), THCudaTensor_data(state, logsoftOutput),
THCudaTensor_data(state, input), THCudaTensor_data(state, weight),
THCudaTensor_data(state, bias), THCudaTensor_data(state, target),
THCudaTensor_data(state, childParent), THCudaTensor_data(state, parentChildren),
input->size[1], rootId, maxFamilyPath
);
cudaError errcode = cudaGetLastError();
if(errcode != cudaSuccess)
THError(cudaGetErrorString(errcode));
THCudaTensor_free(state, input);
return 1;
}
__global__ void cunnx_SoftMaxTree_updateGradInput_kernel(
float *gradInput, float *logsoftOutput, float *gradOutput, float* weight,
float *target, float *childParent, float *parentChildren,
int nInput, int rootId, int maxFamilyPath)
{
__shared__ float buffer[SOFTMAXTREE_THREADS];
int tx = threadIdx.x;
int i_step = blockDim.x;
int k = blockIdx.x;
float *gradInput_k = gradInput + k*nInput;
float *nodeGrad, *nodeWeight;
float grad = gradOutput[k];
int childId = target[k] - 1;
int parentId, parentIdx, childIdx, nChildren;
float *node;
int n = 0;
// zero gradInputs (for accumulation)
for (int i=tx; i<nInput; i+=i_step)
gradInput_k[i] = 0;
// loop through nodes
while(1)
{
/* get next Node in Tree */
node = childParent + childId*2;
parentId = (int)node[0] - 1;
childIdx = (int)node[1] - 1;
node = parentChildren + parentId*2;
parentIdx = (int)node[0] - 1;
nChildren = (int)node[1];
/* CAddTable + Narrow + LogSoftMax */
// AKA linearGradOutput (we reuse the _multiBuffer Tensor)
nodeGrad = logsoftOutput + maxFamilyPath*k + n;
for(int i=tx; i<nChildren; i+=i_step)
{
nodeGrad[i] = -expf(nodeGrad[i])*grad;
}
__syncthreads();
if (tx == 0)
{
nodeGrad[childIdx] += grad;
}
__syncthreads();
/* Linear */
nodeWeight = weight + parentIdx*nInput;
// addmv (dot products)
for (int i=tx; i<nInput; i+=i_step)
{
// zero buffer
buffer[tx] = 0;
for (int j=0; j<nChildren; j++)
{
// multiply
buffer[tx] += nodeGrad[j]*nodeWeight[j*nInput + i];
}
// accumulate into global memory
gradInput_k[i] += buffer[tx];
}
n += nChildren;
CudaAssert((n <= maxFamilyPath))
/* Break when root is reached */
if (parentId == rootId)
{
break;
}
childId = parentId;
}
}
static int cunnx_SoftMaxTree_updateGradInput(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *gradOutput = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
THCudaTensor *target = (THCudaTensor*)luaT_checkudata(L, 4, "torch.CudaTensor");
int inputSize = luaT_getfieldcheckint(L, 1, "inputSize");
int rootId = luaT_getfieldcheckint(L, 1, "rootId") - 1;
int maxFamilyPath = (int)luaT_getfieldcheckint(L, 1, "maxFamilyPath");
THCudaTensor *childParent = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "childParentCuda", "torch.CudaTensor");
THCudaTensor *parentChildren = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "parentChildrenCuda", "torch.CudaTensor");
THCudaTensor *logsoftOutput = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "_multiBuffer", "torch.CudaTensor");
THCudaTensor *weight = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "weight", "torch.CudaTensor");
THCudaTensor *bias = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "bias", "torch.CudaTensor");
THCudaTensor *gradInput = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "_gradInput", "torch.CudaTensor");
luaL_argcheck(L, input->nDimension == 2, 2, "2D(batch mode) tensor expected");
luaL_argcheck(L, input->size[1] == inputSize, 2, "invalid input size");
luaL_argcheck(L, gradOutput->nDimension == 1, 2, "1D tensor expected");
THCudaTensor_resizeAs(state, gradInput, input);
/* call cudakernel */
dim3 blocks(input->size[0]); // each block is an example
dim3 threads(SOFTMAXTREE_THREADS);
cunnx_SoftMaxTree_updateGradInput_kernel<<<blocks,threads>>>(
THCudaTensor_data(state, gradInput), THCudaTensor_data(state, logsoftOutput),
THCudaTensor_data(state, gradOutput), THCudaTensor_data(state, weight),
THCudaTensor_data(state, target), THCudaTensor_data(state, childParent),
THCudaTensor_data(state, parentChildren),
input->size[1], rootId, maxFamilyPath
);
cudaError errcode = cudaGetLastError();
if(errcode != cudaSuccess)
THError(cudaGetErrorString(errcode));
return 1;
}
__global__ void cunnx_SoftMaxTree_accGradParameters_kernel(
float *gradWeight, float *gradBias, float *input,
float *linearGradOutput, int *nodeUpdateCuda, float *target,
float *childParent, float *parentChildren,
int nInput, int rootId, int maxFamilyPath, int maxDept, float scale)
{
__shared__ float buffer[SOFTMAXTREE_THREADS];
int tx = threadIdx.x;
int i_step = blockDim.x;
int k = blockIdx.x;
float *input_k = input + k*nInput;
float *nodeGradOutput, *nodeGradWeight, *nodeGradBias;
// reuse _multiBuffer for keeping track of which node gets gradients
int *nodeUpdate = nodeUpdateCuda + maxDept*k;
int childId = target[k] - 1;
int parentId, parentIdx, nChildren;
float *node;
int n = 0;
int m = 0;
// loop through nodes
while(1)
{
/* get next Node in Tree */
node = childParent + childId*2;
parentId = (int)node[0] - 1;
node = parentChildren + parentId*2;
parentIdx = (int)node[0] - 1;
nChildren = (int)node[1];
nodeGradOutput = linearGradOutput + maxFamilyPath*k + n;
nodeGradWeight = gradWeight + parentIdx*nInput;
nodeGradBias = gradBias + parentIdx;
// addr weights (scalar-products)
for (int i=tx; i<nInput; i+=i_step)
{
// copy input to buffer
buffer[tx] = input_k[i]; // replace shared with register?
for (int j=0; j<nChildren; j++)
{
// multiply accumulate weights
float dw = scale*nodeGradOutput[j]*buffer[tx];
atomicAdd(&nodeGradWeight[j*nInput + i], dw);
}
}
// cadd bias
for (int j=tx; j<nChildren; j+=i_step)
{
// multiply accumulate biases
float db = scale*nodeGradOutput[j];
atomicAdd(&nodeGradBias[j], db);
}
// keep track of which node gets gradients
nodeUpdate[m] = (int)parentId;
n += nChildren;
CudaAssert((n <= maxFamilyPath))
m += 1;
CudaAssert((m <= maxDept))
/* Break when root is reached */
if (parentId == rootId)
{
if (m < maxDept)
nodeUpdate[m] = -1; // zero means end of buffer
break;
}
childId = parentId;
}
}
static int cunnx_SoftMaxTree_accGradParameters(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *target = (THCudaTensor*)luaT_checkudata(L, 4, "torch.CudaTensor");
float scale = luaL_optnumber(L, 5, 1);
int inputSize = luaT_getfieldcheckint(L, 1, "inputSize");
int rootId = luaT_getfieldcheckint(L, 1, "rootId") - 1;
int maxFamilyPath = (int)luaT_getfieldcheckint(L, 1, "maxFamilyPath");
int maxDept = (int)luaT_getfieldcheckint(L, 1, "maxDept");
THCudaTensor *childParent = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "childParentCuda", "torch.CudaTensor");
THCudaTensor *parentChildren = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "parentChildrenCuda", "torch.CudaTensor");
THCudaTensor *linearGradOutput = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "_multiBuffer", "torch.CudaTensor");
THCudaIntTensor *nodeUpdateCuda = (THCudaIntTensor*)luaT_getfieldcheckudata(L, 1, "_nodeUpdateCuda", "torch.CudaIntTensor");
THIntTensor *nodeUpdateHost = (THIntTensor*)luaT_getfieldcheckudata(L, 1, "_nodeUpdateHost", "torch.IntTensor");
THCudaTensor *gradWeight = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "gradWeight", "torch.CudaTensor");
THCudaTensor *gradBias = (THCudaTensor*)luaT_getfieldcheckudata(L, 1, "gradBias", "torch.CudaTensor");
int i, j;
THIntTensor *nodeUpdate;
lua_getfield(L, 1, "updates");
luaL_argcheck(L, input->nDimension == 2, 2, "2D(batch mode) tensor expected");
luaL_argcheck(L, input->size[1] == inputSize, 2, "invalid input size");
input = THCudaTensor_newContiguous(state, input);
/* call cudakernel */
dim3 blocks(input->size[0]); // each block is an example
dim3 threads(SOFTMAXTREE_THREADS);
cunnx_SoftMaxTree_accGradParameters_kernel<<<blocks,threads>>>(
THCudaTensor_data(state, gradWeight), THCudaTensor_data(state, gradBias),
THCudaTensor_data(state, input), THCudaTensor_data(state, linearGradOutput),
THCudaIntTensor_data(state, nodeUpdateCuda), THCudaTensor_data(state, target),
THCudaTensor_data(state, childParent), THCudaTensor_data(state, parentChildren),
input->size[1], rootId, maxFamilyPath, maxDept, scale
);
cudaError errcode = cudaGetLastError();
if(errcode != cudaSuccess)
THError(cudaGetErrorString(errcode));
// copy updated nodeIds from device to host
THIntTensor_copyCuda(state, nodeUpdateHost, nodeUpdateCuda);
nodeUpdate = THIntTensor_new();
// fill updates table
for (i=0; i<nodeUpdateHost->size[0]; i++)
{
THIntTensor_select(nodeUpdate, nodeUpdateHost, 0, i);
for (j=0; j<nodeUpdateHost->size[1]; j++)
{
int nodeId = THIntTensor_get1d(nodeUpdate, j);
double count;
if (nodeId == -1)
{
break;
}
/* updates will contain nodeId (key) sum of scales (value)*/
lua_pushinteger(L, (int)(nodeId+1));
lua_gettable(L, -2);
count = lua_tonumber(L, -1) + scale;
lua_pop(L, 1);
lua_pushinteger(L, (int)(nodeId+1)); /* key */
lua_pushnumber(L, count); /* value */
lua_settable(L, -3);
}
}
THIntTensor_free(nodeUpdate);
return 0;
}
static const struct luaL_Reg cunnx_SoftMaxTree__ [] = {
{"SoftMaxTree_updateOutput", cunnx_SoftMaxTree_updateOutput},
{"SoftMaxTree_updateGradInput", cunnx_SoftMaxTree_updateGradInput},
{"SoftMaxTree_accGradParameters", cunnx_SoftMaxTree_accGradParameters},
{NULL, NULL}
};
static void cunnx_SoftMaxTree_init(lua_State *L)
{
luaT_pushmetatable(L, "torch.CudaTensor");
luaT_registeratname(L, cunnx_SoftMaxTree__, "nn");
lua_pop(L,1);
}