-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathFunctions.cpp
555 lines (488 loc) · 23.1 KB
/
Functions.cpp
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
/*******************************************************
* Copyright (c) 2017, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <af/autograd/Variable.hpp>
#include <af/autograd/Functions.hpp>
namespace af {
namespace autograd {
Variable operator +(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() + rhs.array();
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(grad_output);
inputs[1].addGrad(grad_output);
};
return Variable(result, {lhs, rhs}, grad_func);
}
Variable operator -(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() - rhs.array();
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(grad_output);
inputs[1].addGrad(negate(grad_output));
};
return Variable(result, {lhs, rhs}, grad_func);
}
Variable operator *(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() * rhs.array();
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(grad_output * inputs[1]);
inputs[1].addGrad(grad_output * inputs[0]);
};
return Variable(result, {lhs, rhs}, grad_func);
}
Variable operator /(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() / rhs.array();
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
auto inputs_1_rec = reciprocal(inputs[1]);
auto grad_input_0 = grad_output * inputs_1_rec;
inputs[0].addGrad(grad_input_0);
inputs[1].addGrad(grad_input_0 * negate(inputs[0]) * inputs_1_rec);
};
return Variable(result, {lhs, rhs}, grad_func);
}
Variable operator >(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() > rhs.array();
return Variable(result, false);
}
Variable operator <(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() < rhs.array();
return Variable(result, false);
}
Variable operator >=(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() >= rhs.array();
return Variable(result, false);
}
Variable operator <=(const Variable &lhs, const Variable &rhs)
{
auto result = lhs.array() <= rhs.array();
return Variable(result, false);
}
#define INSTANTIATE_OPERATOR(OP) \
Variable operator OP(const double &lhs_val, const Variable &rhs) \
{ \
auto lhs = Variable( \
af::constant(lhs_val, \
rhs.array().dims(), \
rhs.array().type()), \
false); \
return lhs OP rhs; \
} \
Variable operator OP(const Variable &lhs, const double &rhs_val) \
{ \
auto rhs = Variable( \
af::constant(rhs_val, \
lhs.array().dims(), lhs.array().type()), \
false); \
return lhs OP rhs; \
} \
INSTANTIATE_OPERATOR(+)
INSTANTIATE_OPERATOR(-)
INSTANTIATE_OPERATOR(*)
INSTANTIATE_OPERATOR(/)
INSTANTIATE_OPERATOR(>)
INSTANTIATE_OPERATOR(<)
INSTANTIATE_OPERATOR(>=)
INSTANTIATE_OPERATOR(<=)
#undef INSTANTIATE_OPERATOR
Variable operator !(const Variable &input)
{
auto result = !input.array();
return Variable(result, false);
}
Variable max(const Variable &lhs, const Variable &rhs)
{
auto mask = lhs > rhs;
auto result = max(lhs.array(), rhs.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad( inputs[2] * grad_output);
inputs[1].addGrad(!inputs[2] * grad_output);
};
return Variable(result, {lhs, rhs, mask}, grad_func);
}
Variable min(const Variable &lhs, const Variable &rhs)
{
auto mask = lhs < rhs;
auto result = min(lhs.array(), rhs.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad( inputs[2] * grad_output);
inputs[1].addGrad(!inputs[2] * grad_output);
};
return Variable(result, {lhs, rhs, mask}, grad_func);
}
#define INSTANTIATE_FUNCTION(FN) \
Variable FN(const double &lhs_val, const Variable &rhs) \
{ \
auto lhs = Variable( \
af::constant(lhs_val, \
rhs.array().dims(), \
rhs.array().type()), \
false); \
return FN(lhs,rhs); \
} \
Variable FN(const Variable &lhs, const double &rhs_val) \
{ \
auto rhs = Variable( \
af::constant(rhs_val, \
lhs.array().dims(), lhs.array().type()), \
false); \
return FN(lhs, rhs); \
}
INSTANTIATE_FUNCTION(max);
INSTANTIATE_FUNCTION(min);
#undef INSTANTIATE_FUNCTION
Variable negate(const Variable &input)
{
auto result = 0.0 - input.array();
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(negate(grad_output));
};
return Variable(result, {input}, grad_func);
}
Variable reciprocal(const Variable &input)
{
auto result = 1.0 / input.array();
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
auto res = reciprocal(inputs[0]);
inputs[0].addGrad(negate(grad_output) * res * res);
};
return Variable(result, {input}, grad_func);
}
Variable exp(const Variable &input)
{
auto result = exp(input.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(grad_output * exp(inputs[0]));
};
return Variable(result, {input}, grad_func);
}
Variable log(const Variable &input)
{
auto result = log(input.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(grad_output / inputs[0]);
};
return Variable(result, {input}, grad_func);
}
Variable sin(const Variable &input)
{
auto result = sin(input.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(grad_output * cos(inputs[0]));
};
return Variable(result, {input}, grad_func);
}
Variable cos(const Variable &input)
{
auto result = cos(input.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(grad_output * negate(sin(inputs[0])));
};
return Variable(result, {input}, grad_func);
}
Variable tanh(const Variable &input)
{
auto result = tanh(input.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
auto tmp = tanh(inputs[0]);
inputs[0].addGrad(grad_output * (1.0 - tmp * tmp));
};
return Variable(result, {input}, grad_func);
}
Variable sigmoid(const Variable &input)
{
auto result = sigmoid(input.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
auto tmp = sigmoid(inputs[0]);
inputs[0].addGrad(grad_output * tmp * (1 - tmp));
};
return Variable(result, {input}, grad_func);
}
Variable transpose(const Variable &input)
{
auto result = transpose(input.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(transpose(grad_output));
};
return Variable(result, {input}, grad_func);
}
Variable tileAs(const Variable &input, const Variable &reference)
{
dim4 dims(1,1,1,1);
dim4 rdims = reference.dims();
dim4 idims = input.dims();
for (int i = 0; i < 4; i++) {
dims[i] = rdims[i] / idims[i];
}
auto result = tile(input.array(), dims);
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(sumAs(grad_output, inputs[0]));
};
return Variable(result, {input}, grad_func);
}
Variable sumAs(const Variable &input, const Variable &reference)
{
dim4 rdims = reference.dims();
dim4 idims = input.dims();
auto result = input.array();
for (int i = 0; i < 4; i++) {
if (idims[i] != rdims[i]) result = sum(result, i);
}
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(tileAs(grad_output, inputs[0]));
};
return Variable(result, {input}, grad_func);
}
Variable tile(const Variable &input, const std::vector<int> &repeats)
{
dim4 dims;
for (size_t i = 0; i < repeats.size(); i++) {
dims[i] = repeats[i];
}
auto result = tile(input.array(), dims);
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(sumAs(grad_output, inputs[0]));
};
return Variable(result, {input}, grad_func);
}
Variable sum(const Variable &input, const std::vector<int> &axes)
{
auto result = input.array();
for (size_t i = 0; i < axes.size(); i++) {
result = sum(result, axes[i]);
}
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(tileAs(grad_output, inputs[0]));
};
return Variable(result, {input}, grad_func);
}
Variable mean(const Variable &input, const std::vector<int> &axes)
{
auto result = input.array();
for (size_t i = 0; i < axes.size(); i++) {
result = mean(result, axes[i]);
}
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
dim4 odims = grad_output.dims();
dim4 idims = inputs[0].dims();
dim_t count = 1;
for (int i = 0; i < 4; i++) {
count *= idims[i] / odims[i];
}
inputs[0].addGrad(count * tileAs(grad_output, inputs[0]));
};
return Variable(result, {input}, grad_func);
}
Variable matmul(const Variable &lhs, const Variable &rhs)
{
// lhs:Input[0] -- [M, N]
// rhs:Input[1] -- [N, K]
//matmul(lhs, rhs)
// -- matmul([M, N], [N, K]) -- [M, K]
// result:grad_output -- [M, K]
auto result = matmul(lhs.array(), rhs.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
// matmulNT(grad_output, inputs[1])
// -- matmulNT([M, K], [N, K])
// -- matmul([M, K], [K, N]) -- [M, K]
inputs[0].addGrad(matmulNT(grad_output, inputs[1]));
// matmulTN(inputs[0], grad_output)
// -- matmulTN([M, N], [M, K])
// -- matmul([N, M], [M, K]) -- [N, K]
inputs[1].addGrad(matmulTN(inputs[0], grad_output));
};
return Variable(result, {lhs, rhs}, grad_func);
}
Variable matmulTN(const Variable &lhs, const Variable &rhs)
{
// lhs:Input[0] -- [N, M]
// rhs:Input[1] -- [N, K]
// matmulTN(lhs, rhs)
// -- matmulTN([N, M], [N, K])
// -- matmul([M, N], [N, K]) -- [M, K]
// result:grad_output -- [M, K]
auto result = matmulTN(lhs.array(), rhs.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
// matmulNT(inputs[1], grad_output)
// -- matmulNT([N, K], [M, K])
// -- matmul([N, K], [K, M]) -- [N, M]
inputs[0].addGrad(matmulNT(inputs[1], grad_output));
// matmul(inputs[0], grad_output)
// -- matmulNT([N, M], [M, K]) -- [N, K]
inputs[1].addGrad(matmul(inputs[0], grad_output));
};
return Variable(result, {lhs, rhs}, grad_func);
}
Variable matmulNT(const Variable &lhs, const Variable &rhs)
{
// lhs:Input[0] -- [M, N]
// rhs:Input[1] -- [K, N]
// matmulNT(lhs, rhs)
// -- matmulNT([M, N], [K, N])
// -- matmul([M, N], [N, K]) -- [M, K]
// result:grad_output -- [M, K]
auto result = matmulNT(lhs.array(), rhs.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
// matmul(grad_output, inputs[1])
// -- matmul([M, K], [K, N]) -- [M, N]
inputs[0].addGrad(matmul(grad_output, inputs[1]));
// matmulTN(grad_output, inputs[0])
// -- matmulTN([M, K], [M, N])
// -- matmul([K, M], [M, N]) -- [K, N]
inputs[1].addGrad(matmulTN(grad_output, inputs[0]));
};
return Variable(result, {lhs, rhs}, grad_func);
}
Variable abs(const Variable &input)
{
auto result = af::abs(input.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
// af::sign returns signbit
// Convert it into -1, 1
auto sign = Variable(1 - 2 * af::sign(inputs[0].array()), false);
inputs[0].addGrad(sign * grad_output);
};
return Variable(result, {input}, grad_func);
}
Variable flat(const Variable &input)
{
auto result = af::flat(input.array());
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(moddims(grad_output, inputs[0].dims()));
};
return Variable(result, {input}, grad_func);
}
Variable moddims(const Variable &input, const dim4 &dims)
{
auto result = af::moddims(input.array(), dims);
auto grad_func = [](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(moddims(grad_output, inputs[0].dims()));
};
return Variable(result, {input}, grad_func);
}
Variable reorder(const Variable &input, int d0, int d1, int d2, int d3)
{
array res = reorder(input.array(), d0, d1, d2, d3);
int tmp[] = {d0, d1, d2, d3};
int tmp2[4];
for(int i = 0; i < 4; i++){
tmp2[tmp[i]] = i;
}
auto grad_func = [tmp2](std::vector<Variable> &inputs, const Variable &grad_output){
inputs[0].addGrad(reorder(grad_output, tmp2[0], tmp2[1], tmp2[2], tmp2[3]));
};
return Variable(res, {input}, grad_func);
}
Variable unwrap(const Variable &input, int wx, int wy, int sx, int sy, int px, int py)
{
array res = unwrap(input.array(), wx, wy, sx, sy, px, py);
auto grad_func = [wx, wy, sx, sy, px, py](std::vector<Variable> &inputs, const Variable &grad_output) {
dim4 d = inputs[0].dims();
inputs[0].addGrad(wrap(grad_output, d[0], d[1], wx, wy, sx, sy, px, py));
};
return Variable(res, {input}, grad_func);
}
Variable wrap(const Variable &input, int ox, int oy, int wx, int wy, int sx, int sy, int px, int py)
{
array res = wrap(input.array(), ox, oy, wx, wy, sx, sy, px, py);
auto grad_func = [wx, wy, sx, sy, px, py](std::vector<Variable> &inputs, const Variable &grad_output) {
inputs[0].addGrad(unwrap(grad_output, wx, wy, sx, sy, px, py));
};
return Variable(res, {input}, grad_func);
}
Variable convolve2(const Variable &input, const Variable &weights, int wx, int wy, int sx, int sy, int px, int py)
{
dim4 idims = input.array().dims(); // (x_i, y_i, c_i, n )
dim4 wdims = weights.array().dims(); // (wx, wy, c_i, c_o)
int x_i = idims[0]; //size of x dim of input
int y_i = idims[1]; //size of y dim of input
int c_i = idims[2]; //number of input channels
int n = idims[3]; //batch size (1 for now)
int x_o = (x_i + 2 * px - wx) / sx + 1; //size of x dim of output
int y_o = (y_i + 2 * py - wy) / sy + 1; //size of x dim of output
int c_o = wdims[3]; //number of output channels
array windows = unwrap(input.array(), wx, wy, sx, sy, px, py);
array lhs = moddims(
reorder(windows, 1, 0, 2, 3),
dim4(x_o * y_o, wx * wy * c_i, n, 1));
array rhs = moddims(weights.array(), dim4(wx * wy * c_i, c_o, 1, 1));
//TODO: This loop can be replaced with a batched matmult as soon as
//that is added to arrayfire
std::vector<array> out;
for(int i = 0; i < n; i++){
array res = matmul(lhs(span, span, i), rhs);
out.push_back(moddims(res , dim4(x_o, y_o, c_o, 1)));
}
//LOL @ C++ API - need this loop to have arbitrary batch size
array result = out[0];
for(int i = 1; i < n; i+=3){
int rem = n - i;
if(rem >= 3){
result = join(3, result, out[i], out[i+1], out[i+2]);
}else if(rem == 2){
result = join(3, result, out[i], out[i+1]);
break;
}else if(rem == 1){
result = join(3, result, out[i]);
break;
}else{
break;
}
}
auto grad_func = [wx, wy, sx, sy, px, py, c_i, n](std::vector<Variable> &inputs, const Variable &grad_output) {
dim4 odims = grad_output.array().dims();
dim4 wdims = inputs[1].array().dims();
dim4 idims = inputs[0].array().dims();
auto grad_out_reshape = moddims(grad_output, dim4(odims[0]*odims[1], odims[2], odims[3], 1));
auto weights_reshape = moddims(inputs[1], dim4(wdims[0]*wdims[1]*wdims[2], wdims[3], 1, 1));
//TODO: This really needs batched matmul...
//TODO: This doesn't work for n > 1
//TODO: Can these lines be shortened? - This seems like a large grad function - perhaps this
// could all be implemented in Conv2D::forward(). I had to implement the helper functions anyways
/*
std::vector<array> out;
for(int i = 0; i < n; i++){
auto a = matmulNT(grad_out_reshape(span, span, i), weights_reshape); //Problem is here - can't call () on Variable
auto adims = a.array().dims();
auto b = moddims(a, dim4(adims[0], wx*wy, c_i, adims[3]));
auto c = reorder(b, 1, 0, 2, 3);
out.push_pack(wrap(c, idims[0], idims[1], wx, wy, sx, sy, px, py));
}
array result = out[0];
for(int i = 1; i < n; i+=3){
int rem = n - i;
if(rem >= 3){
result = join(3, result, out[i], out[i+1], out[i+2]);
}else if(rem == 2){
result = join(3, result, out[i], out[i+1]);
break;
}else if(rem == 1){
result = join(3, result, out[i]);
break;
}else{
break;
}
}
*/
auto a = matmulNT(grad_out_reshape, weights_reshape);
auto adims = a.array().dims();
auto b = moddims(a, dim4(adims[0], wx*wy, c_i, adims[3]));
auto c = reorder(b, 1, 0, 2, 3);
inputs[0].addGrad(wrap(c, idims[0], idims[1], wx, wy, sx, sy, px, py));
auto d = matmulTN(inputs[2],grad_out_reshape);
inputs[1].addGrad(moddims(d, dim4(wx, wy, c_i, d.dims()[1])));
};
return Variable(result, {input, weights, Variable(lhs, false)}, grad_func);
}
}
}