Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Aug 20, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushpull_backward.cpp
More file actions
Latest commit
429 lines (391 loc) · 19.3 KB
/
Copy pathpushpull_backward.cpp
File metadata and controls
429 lines (391 loc) · 19.3 KB
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
// Adjoints of the pushpull ops (see pushpull.h for the calling contract).
//
// A separate translation unit from `pushpull.cpp` purely for build cost:
// these instantiate the same ndim x order x bound x dtype matrix a second
// time, and `pushpull` is already the most expensive module here (and by a
// wide margin on the CUDA side, where ptxas memory -- not just wall time --
// is the binding constraint; cf. the reg_field / reg_field_rls split).
#include"pushpull.h"
#include<cstdint>
#include"pushpull_dispatch.h"
FF_NAMESPACE_BEGIN(FF)
FF_NAMESPACE_BEGIN(FF_DEVICE)
/***********************************************************************
* LEAF WRAPPERS *
***********************************************************************/
namespace {
// ---------------------------------------------------------------------
// Backward leaves.
//
// `abs` is a compile-time flag on the impl templates. For the pull/push/
// count adjoints it is pinned to `false` (see pushpull.h: a signed spline
// derivative is what an adjoint means); only `grad_backward` branches on
// it, mirroring `_grad`.
// ---------------------------------------------------------------------
template <int ndim, spline::type I, bound::type B,
typenamescalar_t, typenameoffset_t>
inlinevoid_pull_backward(
const bound::BoundVec & bvec, const spline::SplineVec & svec,
int64_t nbatch, int64_t n1, int extrapolate,
void * out, void * gout,
constvoid * inp, constvoid * ginp, constvoid * grid,
constint64_t * size_grid, constint64_t * size_splinc,
constint64_t * stride_out, constint64_t * stride_gout,
constint64_t * stride_inp, constint64_t * stride_ginp,
constint64_t * stride_grid)
{
constoffset_t * _sg = copy_if_needed<offset_t *>(size_grid, n1);
constoffset_t * _ss = copy_if_needed<offset_t *>(size_splinc, n1);
constoffset_t * _so = copy_if_needed<offset_t *>(stride_out, n1);
constoffset_t * _sgo = copy_if_needed<offset_t *>(stride_gout, n1);
constoffset_t * _si = copy_if_needed<offset_t *>(stride_inp, n1);
constoffset_t * _sgi = copy_if_needed<offset_t *>(stride_ginp, n1);
constoffset_t * _sgr = copy_if_needed<offset_t *>(stride_grid, n1);
scalar_t * _out = static_cast< scalar_t *>(out);
scalar_t * _gout = static_cast< scalar_t *>(gout);
constscalar_t * _inp = static_cast<constscalar_t *>(inp);
constscalar_t * _ginp = static_cast<constscalar_t *>(ginp);
constscalar_t * _grid = static_cast<constscalar_t *>(grid);
pushpull::pull_backward<ndim, false, reduce_t, scalar_t, offset_t, I, B>(
static_cast<offset_t>(nbatch), extrapolate, _out, _gout,
_inp, _ginp, _grid,
_sg, _ss, _so, _sgo, _si, _sgi, _sgr, bvec, svec);
free_if_needed<int64_t *>(_sg); free_if_needed<int64_t *>(_ss);
free_if_needed<int64_t *>(_so); free_if_needed<int64_t *>(_sgo);
free_if_needed<int64_t *>(_si); free_if_needed<int64_t *>(_sgi);
free_if_needed<int64_t *>(_sgr);
}
template <int ndim, spline::type I, bound::type B,
typenamescalar_t, typenameoffset_t>
inlinevoid_push_backward(
const bound::BoundVec & bvec, const spline::SplineVec & svec,
int64_t nbatch, int64_t n1, int extrapolate,
void * out, void * gout,
constvoid * inp, constvoid * ginp, constvoid * grid,
constint64_t * size_grid, constint64_t * size_splinc,
constint64_t * stride_out, constint64_t * stride_gout,
constint64_t * stride_inp, constint64_t * stride_ginp,
constint64_t * stride_grid)
{
constoffset_t * _sg = copy_if_needed<offset_t *>(size_grid, n1);
constoffset_t * _ss = copy_if_needed<offset_t *>(size_splinc, n1);
constoffset_t * _so = copy_if_needed<offset_t *>(stride_out, n1);
constoffset_t * _sgo = copy_if_needed<offset_t *>(stride_gout, n1);
constoffset_t * _si = copy_if_needed<offset_t *>(stride_inp, n1);
constoffset_t * _sgi = copy_if_needed<offset_t *>(stride_ginp, n1);
constoffset_t * _sgr = copy_if_needed<offset_t *>(stride_grid, n1);
scalar_t * _out = static_cast< scalar_t *>(out);
scalar_t * _gout = static_cast< scalar_t *>(gout);
constscalar_t * _inp = static_cast<constscalar_t *>(inp);
constscalar_t * _ginp = static_cast<constscalar_t *>(ginp);
constscalar_t * _grid = static_cast<constscalar_t *>(grid);
pushpull::push_backward<ndim, false, reduce_t, scalar_t, offset_t, I, B>(
static_cast<offset_t>(nbatch), extrapolate, _out, _gout,
_inp, _ginp, _grid,
_sg, _ss, _so, _sgo, _si, _sgi, _sgr, bvec, svec);
free_if_needed<int64_t *>(_sg); free_if_needed<int64_t *>(_ss);
free_if_needed<int64_t *>(_so); free_if_needed<int64_t *>(_sgo);
free_if_needed<int64_t *>(_si); free_if_needed<int64_t *>(_sgi);
free_if_needed<int64_t *>(_sgr);
}
template <int ndim, spline::type I, bound::type B,
typenamescalar_t, typenameoffset_t>
inlinevoid_count_backward(
const bound::BoundVec & bvec, const spline::SplineVec & svec,
int64_t nbatch, int64_t n1, int extrapolate,
void * gout, constvoid * ginp, constvoid * grid,
constint64_t * size_grid, constint64_t * size_splinc,
constint64_t * stride_gout, constint64_t * stride_ginp,
constint64_t * stride_grid)
{
constoffset_t * _sg = copy_if_needed<offset_t *>(size_grid, n1);
constoffset_t * _ss = copy_if_needed<offset_t *>(size_splinc, n1);
constoffset_t * _sgo = copy_if_needed<offset_t *>(stride_gout, n1);
constoffset_t * _sgi = copy_if_needed<offset_t *>(stride_ginp, n1);
constoffset_t * _sgr = copy_if_needed<offset_t *>(stride_grid, n1);
scalar_t * _gout = static_cast< scalar_t *>(gout);
constscalar_t * _ginp = static_cast<constscalar_t *>(ginp);
constscalar_t * _grid = static_cast<constscalar_t *>(grid);
pushpull::count_backward<ndim, false, reduce_t, scalar_t, offset_t, I, B>(
static_cast<offset_t>(nbatch), extrapolate, _gout, _ginp, _grid,
_sg, _ss, _sgo, _sgi, _sgr, bvec, svec);
free_if_needed<int64_t *>(_sg); free_if_needed<int64_t *>(_ss);
free_if_needed<int64_t *>(_sgo); free_if_needed<int64_t *>(_sgi);
free_if_needed<int64_t *>(_sgr);
}
// grad_backward: `ginp` carries the extra trailing (D) axis of `grad`'s
// output, so `stride_ginp` has length n1 + 1 (cf. `_grad`'s stride_out).
template <int ndim, spline::type I, bound::type B,
typenamescalar_t, typenameoffset_t>
inlinevoid_grad_backward(
const bound::BoundVec & bvec, const spline::SplineVec & svec,
int64_t nbatch, int64_t n1, int extrapolate, bool abs,
void * out, void * gout,
constvoid * inp, constvoid * ginp, constvoid * grid,
constint64_t * size_grid, constint64_t * size_splinc,
constint64_t * stride_out, constint64_t * stride_gout,
constint64_t * stride_inp, constint64_t * stride_ginp,
constint64_t * stride_grid)
{
constoffset_t * _sg = copy_if_needed<offset_t *>(size_grid, n1);
constoffset_t * _ss = copy_if_needed<offset_t *>(size_splinc, n1);
constoffset_t * _so = copy_if_needed<offset_t *>(stride_out, n1);
constoffset_t * _sgo = copy_if_needed<offset_t *>(stride_gout, n1);
constoffset_t * _si = copy_if_needed<offset_t *>(stride_inp, n1);
constoffset_t * _sgi = copy_if_needed<offset_t *>(stride_ginp, n1 + 1);
constoffset_t * _sgr = copy_if_needed<offset_t *>(stride_grid, n1);
scalar_t * _out = static_cast< scalar_t *>(out);
scalar_t * _gout = static_cast< scalar_t *>(gout);
constscalar_t * _inp = static_cast<constscalar_t *>(inp);
constscalar_t * _ginp = static_cast<constscalar_t *>(ginp);
constscalar_t * _grid = static_cast<constscalar_t *>(grid);
if (abs)
pushpull::grad_backward<ndim, true, reduce_t, scalar_t, offset_t, I, B>(
static_cast<offset_t>(nbatch), extrapolate, _out, _gout,
_inp, _ginp, _grid,
_sg, _ss, _so, _sgo, _si, _sgi, _sgr, bvec, svec);
else
pushpull::grad_backward<ndim, false, reduce_t, scalar_t, offset_t, I, B>(
static_cast<offset_t>(nbatch), extrapolate, _out, _gout,
_inp, _ginp, _grid,
_sg, _ss, _so, _sgo, _si, _sgi, _sgr, bvec, svec);
free_if_needed<int64_t *>(_sg); free_if_needed<int64_t *>(_ss);
free_if_needed<int64_t *>(_so); free_if_needed<int64_t *>(_sgo);
free_if_needed<int64_t *>(_si); free_if_needed<int64_t *>(_sgi);
free_if_needed<int64_t *>(_sgr);
}
} // anonymous namespace
/***********************************************************************
* PULL BACKWARD *
***********************************************************************/
voidpull_backward(
DLTensor & out_,
DLTensor & gout_,
const DLTensor & inp_,
const DLTensor & ginp_,
const DLTensor & grid_,
int8_t spline,
int8_t bound,
int8_t extrapolate,
intptr_t/* stream <unused> */
)
{
ContiguousStrides _out(out_), _gout(gout_), _inp(inp_), _ginp(ginp_), _grid(grid_);
DLTensor & out = _out.t;
DLTensor & gout = _gout.t;
const DLTensor & inp = _inp.t;
const DLTensor & ginp = _ginp.t;
const DLTensor & grid = _grid.t;
constint ndim = static_cast<int>(grid.shape[grid.ndim - 1]);
constint32_t nbatch = grid.ndim - ndim - 1;
constint64_t n1 = grid.ndim;
CHECK_NO_LANES (out)
CHECK_SAME_DTYPE(out, gout)
CHECK_SAME_DTYPE(out, inp)
CHECK_SAME_DTYPE(out, ginp)
CHECK_SAME_DTYPE(out, grid)
CHECK_SAME(out.ndim, grid.ndim, "out and grid must have the same rank")
CHECK_SAME(gout.ndim, grid.ndim, "gout and grid must have the same rank")
CHECK_SAME(inp.ndim, grid.ndim, "inp and grid must have the same rank")
CHECK_SAME(ginp.ndim, grid.ndim, "ginp and grid must have the same rank")
if (nbatch < 0)
throwstd::invalid_argument("grid rank is too small for the coordinate dim");
// `out` mirrors `inp` (the field), `gout` mirrors `grid`.
for (int32_t d = 0; d < out.ndim; ++d)
CHECK_SAME(out.shape[d], inp.shape[d], "out and inp must have the same shape")
for (int32_t d = 0; d < gout.ndim; ++d)
CHECK_SAME(gout.shape[d], grid.shape[d], "gout and grid must have the same shape")
CHECK_SAME(ginp.shape[ginp.ndim-1], inp.shape[inp.ndim-1], "channel counts differ")
CHECK_SAME_BATCH(inp, grid, nbatch)
CHECK_SAME_BATCH(ginp, grid, nbatch)
constbool use_32bits = CANUSE32BITS(out) && CANUSE32BITS(gout)
&& CANUSE32BITS(inp) && CANUSE32BITS(ginp)
&& CANUSE32BITS(grid);
constauto code = static_cast<DLDataTypeCode>(out.dtype.code);
constauto bits = out.dtype.bits;
constspline_t spl = static_cast<spline_t>(spline);
constbound_t bnd = static_cast<bound_t >(bound);
const bound::BoundVec bvec(bnd);
const spline::SplineVec svec(spl);
constint ex = static_cast<int>(extrapolate);
DISPATCH_PP(_pull_backward,
bvec, svec,
static_cast<int64_t>(nbatch), n1, ex,
VOIDPTR(out), VOIDPTR(gout),
CVOIDPTR(inp), CVOIDPTR(ginp), CVOIDPTR(grid),
grid.shape, inp.shape,
out.strides, gout.strides, inp.strides, ginp.strides, grid.strides)
}
/***********************************************************************
* PUSH BACKWARD *
***********************************************************************/
voidpush_backward(
DLTensor & out_,
DLTensor & gout_,
const DLTensor & inp_,
const DLTensor & ginp_,
const DLTensor & grid_,
int8_t spline,
int8_t bound,
int8_t extrapolate,
intptr_t/* stream <unused> */
)
{
ContiguousStrides _out(out_), _gout(gout_), _inp(inp_), _ginp(ginp_), _grid(grid_);
DLTensor & out = _out.t;
DLTensor & gout = _gout.t;
const DLTensor & inp = _inp.t;
const DLTensor & ginp = _ginp.t;
const DLTensor & grid = _grid.t;
constint ndim = static_cast<int>(grid.shape[grid.ndim - 1]);
constint32_t nbatch = grid.ndim - ndim - 1;
constint64_t n1 = grid.ndim;
CHECK_NO_LANES (out)
CHECK_SAME_DTYPE(out, gout)
CHECK_SAME_DTYPE(out, inp)
CHECK_SAME_DTYPE(out, ginp)
CHECK_SAME_DTYPE(out, grid)
CHECK_SAME(out.ndim, grid.ndim, "out and grid must have the same rank")
CHECK_SAME(gout.ndim, grid.ndim, "gout and grid must have the same rank")
CHECK_SAME(inp.ndim, grid.ndim, "inp and grid must have the same rank")
CHECK_SAME(ginp.ndim, grid.ndim, "ginp and grid must have the same rank")
if (nbatch < 0)
throwstd::invalid_argument("grid rank is too small for the coordinate dim");
// Here both `out` and `inp` are grid-shaped; `ginp` is the field.
for (int32_t d = 0; d < out.ndim; ++d)
CHECK_SAME(out.shape[d], inp.shape[d], "out and inp must have the same shape")
for (int32_t d = 0; d < gout.ndim; ++d)
CHECK_SAME(gout.shape[d], grid.shape[d], "gout and grid must have the same shape")
CHECK_SAME(ginp.shape[ginp.ndim-1], inp.shape[inp.ndim-1], "channel counts differ")
CHECK_SAME_BATCH(inp, grid, nbatch)
CHECK_SAME_BATCH(ginp, grid, nbatch)
constbool use_32bits = CANUSE32BITS(out) && CANUSE32BITS(gout)
&& CANUSE32BITS(inp) && CANUSE32BITS(ginp)
&& CANUSE32BITS(grid);
constauto code = static_cast<DLDataTypeCode>(out.dtype.code);
constauto bits = out.dtype.bits;
constspline_t spl = static_cast<spline_t>(spline);
constbound_t bnd = static_cast<bound_t >(bound);
const bound::BoundVec bvec(bnd);
const spline::SplineVec svec(spl);
constint ex = static_cast<int>(extrapolate);
// size_splinc is the *pushed volume*, i.e. ginp's shape.
DISPATCH_PP(_push_backward,
bvec, svec,
static_cast<int64_t>(nbatch), n1, ex,
VOIDPTR(out), VOIDPTR(gout),
CVOIDPTR(inp), CVOIDPTR(ginp), CVOIDPTR(grid),
grid.shape, ginp.shape,
out.strides, gout.strides, inp.strides, ginp.strides, grid.strides)
}
/***********************************************************************
* COUNT BACKWARD *
***********************************************************************/
voidcount_backward(
DLTensor & gout_,
const DLTensor & ginp_,
const DLTensor & grid_,
int8_t spline,
int8_t bound,
int8_t extrapolate,
intptr_t/* stream <unused> */
)
{
ContiguousStrides _gout(gout_), _ginp(ginp_), _grid(grid_);
DLTensor & gout = _gout.t;
const DLTensor & ginp = _ginp.t;
const DLTensor & grid = _grid.t;
constint ndim = static_cast<int>(grid.shape[grid.ndim - 1]);
constint32_t nbatch = grid.ndim - ndim - 1;
constint64_t n1 = grid.ndim;
CHECK_NO_LANES (gout)
CHECK_SAME_DTYPE(gout, ginp)
CHECK_SAME_DTYPE(gout, grid)
CHECK_SAME(gout.ndim, grid.ndim, "gout and grid must have the same rank")
CHECK_SAME(ginp.ndim, grid.ndim, "ginp and grid must have the same rank")
if (nbatch < 0)
throwstd::invalid_argument("grid rank is too small for the coordinate dim");
for (int32_t d = 0; d < gout.ndim; ++d)
CHECK_SAME(gout.shape[d], grid.shape[d], "gout and grid must have the same shape")
CHECK_SAME(ginp.shape[ginp.ndim-1], 1, "count gradient must have a single channel")
CHECK_SAME_BATCH(ginp, grid, nbatch)
constbool use_32bits = CANUSE32BITS(gout) && CANUSE32BITS(ginp)
&& CANUSE32BITS(grid);
constauto code = static_cast<DLDataTypeCode>(gout.dtype.code);
constauto bits = gout.dtype.bits;
constspline_t spl = static_cast<spline_t>(spline);
constbound_t bnd = static_cast<bound_t >(bound);
const bound::BoundVec bvec(bnd);
const spline::SplineVec svec(spl);
constint ex = static_cast<int>(extrapolate);
DISPATCH_PP(_count_backward,
bvec, svec,
static_cast<int64_t>(nbatch), n1, ex,
VOIDPTR(gout), CVOIDPTR(ginp), CVOIDPTR(grid),
grid.shape, ginp.shape,
gout.strides, ginp.strides, grid.strides)
}
/***********************************************************************
* GRAD BACKWARD *
***********************************************************************/
voidgrad_backward(
DLTensor & out_,
DLTensor & gout_,
const DLTensor & inp_,
const DLTensor & ginp_,
const DLTensor & grid_,
int8_t spline,
int8_t bound,
int8_t extrapolate,
bool abs,
intptr_t/* stream <unused> */
)
{
ContiguousStrides _out(out_), _gout(gout_), _inp(inp_), _ginp(ginp_), _grid(grid_);
DLTensor & out = _out.t;
DLTensor & gout = _gout.t;
const DLTensor & inp = _inp.t;
const DLTensor & ginp = _ginp.t;
const DLTensor & grid = _grid.t;
constint ndim = static_cast<int>(grid.shape[grid.ndim - 1]);
constint32_t nbatch = grid.ndim - ndim - 1;
constint64_t n1 = grid.ndim;
CHECK_NO_LANES (out)
CHECK_SAME_DTYPE(out, gout)
CHECK_SAME_DTYPE(out, inp)
CHECK_SAME_DTYPE(out, ginp)
CHECK_SAME_DTYPE(out, grid)
CHECK_SAME(out.ndim, grid.ndim, "out and grid must have the same rank")
CHECK_SAME(gout.ndim, grid.ndim, "gout and grid must have the same rank")
CHECK_SAME(inp.ndim, grid.ndim, "inp and grid must have the same rank")
CHECK_SAME(ginp.ndim, grid.ndim + 1, "ginp must have an extra trailing axis")
if (nbatch < 0)
throwstd::invalid_argument("grid rank is too small for the coordinate dim");
for (int32_t d = 0; d < out.ndim; ++d)
CHECK_SAME(out.shape[d], inp.shape[d], "out and inp must have the same shape")
for (int32_t d = 0; d < gout.ndim; ++d)
CHECK_SAME(gout.shape[d], grid.shape[d], "gout and grid must have the same shape")
CHECK_SAME(ginp.shape[ginp.ndim-1], ndim, "ginp trailing axis must equal ndim")
CHECK_SAME(ginp.shape[ginp.ndim-2], inp.shape[inp.ndim-1], "channel counts differ")
CHECK_SAME_BATCH(inp, grid, nbatch)
CHECK_SAME_BATCH(ginp, grid, nbatch)
constbool use_32bits = CANUSE32BITS(out) && CANUSE32BITS(gout)
&& CANUSE32BITS(inp) && CANUSE32BITS(ginp)
&& CANUSE32BITS(grid);
constauto code = static_cast<DLDataTypeCode>(out.dtype.code);
constauto bits = out.dtype.bits;
constspline_t spl = static_cast<spline_t>(spline);
constbound_t bnd = static_cast<bound_t >(bound);
const bound::BoundVec bvec(bnd);
const spline::SplineVec svec(spl);
constint ex = static_cast<int>(extrapolate);
DISPATCH_PP(_grad_backward,
bvec, svec,
static_cast<int64_t>(nbatch), n1, ex, abs,
VOIDPTR(out), VOIDPTR(gout),
CVOIDPTR(inp), CVOIDPTR(ginp), CVOIDPTR(grid),
grid.shape, inp.shape,
out.strides, gout.strides, inp.strides, ginp.strides, grid.strides)
}
FF_NAMESPACE_END(FF_DEVICE)
FF_NAMESPACE_END(FF)