File size: 12,052 Bytes
2571f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Non-differentiable forward/backward components.
These components are put together in `interpol.autograd` to generate
differentiable functions.

Note
----
.. I removed @torch.jit.script from these entry-points because compiling
   all possible combinations of bound+interpolation made the first call
   extremely slow.
.. I am not using the dot/multi_dot helpers even though they should be
   more efficient that "multiply and sum" because I haven't had the time
   to test them. It would be worth doing it.
"""
import torch
from typing import List, Optional, Tuple
from .jit_utils import list_all, dot, dot_multi, pad_list_int
from .bounds import Bound
from .splines import Spline
from . import iso0, iso1, nd
Tensor = torch.Tensor


@torch.jit.script
def make_bound(bound: List[int]) -> List[Bound]:
    return [Bound(b) for b in bound]


@torch.jit.script
def make_spline(spline: List[int]) -> List[Spline]:
    return [Spline(s) for s in spline]


# @torch.jit.script
def grid_pull(inp, grid, bound: List[int], interpolation: List[int],
              extrapolate: int):
    """
    inp: (B, C, *spatial_in) tensor
    grid: (B, *spatial_out, D) tensor
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, C, *spatial_out) tensor
    """
    dim = grid.shape[-1]
    bound = pad_list_int(bound, dim)
    interpolation = pad_list_int(interpolation, dim)
    bound_fn = make_bound(bound)
    is_iso1 = list_all([order == 1 for order in interpolation])
    if is_iso1:
        if dim == 3:
            return iso1.pull3d(inp, grid, bound_fn, extrapolate)
        elif dim == 2:
            return iso1.pull2d(inp, grid, bound_fn, extrapolate)
        elif dim == 1:
            return iso1.pull1d(inp, grid, bound_fn, extrapolate)
    is_iso0 = list_all([order == 0 for order in interpolation])
    if is_iso0:
        if dim == 3:
            return iso0.pull3d(inp, grid, bound_fn, extrapolate)
        elif dim == 2:
            return iso0.pull2d(inp, grid, bound_fn, extrapolate)
        elif dim == 1:
            return iso0.pull1d(inp, grid, bound_fn, extrapolate)
    spline_fn = make_spline(interpolation)
    return nd.pull(inp, grid, bound_fn, spline_fn, extrapolate)


# @torch.jit.script
def grid_push(inp, grid, shape: Optional[List[int]], bound: List[int],
              interpolation: List[int], extrapolate: int):
    """
    inp: (B, C, *spatial_in) tensor
    grid: (B, *spatial_in, D) tensor
    shape: List{D}[int] tensor, optional, default=spatial_in
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, C, *shape) tensor
    """
    dim = grid.shape[-1]
    bound = pad_list_int(bound, dim)
    interpolation = pad_list_int(interpolation, dim)
    bound_fn = make_bound(bound)
    is_iso1 = list_all([order == 1 for order in interpolation])
    if is_iso1:
        if dim == 3:
            return iso1.push3d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 2:
            return iso1.push2d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 1:
            return iso1.push1d(inp, grid, shape, bound_fn, extrapolate)
    is_iso0 = list_all([order == 0 for order in interpolation])
    if is_iso0:
        if dim == 3:
            return iso0.push3d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 2:
            return iso0.push2d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 1:
            return iso0.push1d(inp, grid, shape, bound_fn, extrapolate)
    spline_fn = make_spline(interpolation)
    return nd.push(inp, grid, shape, bound_fn, spline_fn, extrapolate)


# @torch.jit.script
def grid_count(grid, shape: Optional[List[int]], bound: List[int],
              interpolation: List[int], extrapolate: int):
    """
    grid: (B, *spatial_in, D) tensor
    shape: List{D}[int] tensor, optional, default=spatial_in
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, 1, *shape) tensor
    """
    dim = grid.shape[-1]
    bound = pad_list_int(bound, dim)
    interpolation = pad_list_int(interpolation, dim)
    bound_fn = make_bound(bound)
    gshape = list(grid.shape[-dim-1:-1])
    if shape is None:
        shape = gshape
    inp = torch.ones([], dtype=grid.dtype, device=grid.device)
    inp = inp.expand([len(grid), 1] + gshape)
    is_iso1 = list_all([order == 1 for order in interpolation])
    if is_iso1:
        if dim == 3:
            return iso1.push3d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 2:
            return iso1.push2d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 1:
            return iso1.push1d(inp, grid, shape, bound_fn, extrapolate)
    is_iso0 = list_all([order == 0 for order in interpolation])
    if is_iso0:
        if dim == 3:
            return iso0.push3d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 2:
            return iso0.push2d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 1:
            return iso0.push1d(inp, grid, shape, bound_fn, extrapolate)
    spline_fn = make_spline(interpolation)
    return nd.push(inp, grid, shape, bound_fn, spline_fn, extrapolate)


# @torch.jit.script
def grid_grad(inp, grid, bound: List[int], interpolation: List[int],
              extrapolate: int):
    """
    inp: (B, C, *spatial_in) tensor
    grid: (B, *spatial_out, D) tensor
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, C, *spatial_out, D) tensor
    """
    dim = grid.shape[-1]
    bound = pad_list_int(bound, dim)
    interpolation = pad_list_int(interpolation, dim)
    bound_fn = make_bound(bound)
    is_iso1 = list_all([order == 1 for order in interpolation])
    if is_iso1:
        if dim == 3:
            return iso1.grad3d(inp, grid, bound_fn, extrapolate)
        elif dim == 2:
            return iso1.grad2d(inp, grid, bound_fn, extrapolate)
        elif dim == 1:
            return iso1.grad1d(inp, grid, bound_fn, extrapolate)
    is_iso0 = list_all([order == 0 for order in interpolation])
    if is_iso0:
        return iso0.grad(inp, grid, bound_fn, extrapolate)
    spline_fn = make_spline(interpolation)
    return nd.grad(inp, grid, bound_fn, spline_fn, extrapolate)


# @torch.jit.script
def grid_pushgrad(inp, grid, shape: List[int], bound: List[int],
                  interpolation: List[int], extrapolate: int):
    """ /!\ Used only in backward pass of grid_grad
    inp: (B, C, *spatial_in, D) tensor
    grid: (B, *spatial_in, D) tensor
    shape: List{D}[int], optional
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, C, *shape) tensor
    """
    dim = grid.shape[-1]
    bound = pad_list_int(bound, dim)
    interpolation = pad_list_int(interpolation, dim)
    bound_fn = make_bound(bound)
    is_iso1 = list_all([order == 1 for order in interpolation])
    if is_iso1:
        if dim == 3:
            return iso1.pushgrad3d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 2:
            return iso1.pushgrad2d(inp, grid, shape, bound_fn, extrapolate)
        elif dim == 1:
            return iso1.pushgrad1d(inp, grid, shape, bound_fn, extrapolate)
    is_iso0 = list_all([order == 0 for order in interpolation])
    if is_iso0:
        return iso0.pushgrad(inp, grid, shape, bound_fn, extrapolate)
    spline_fn = make_spline(interpolation)
    return nd.pushgrad(inp, grid, shape, bound_fn, spline_fn, extrapolate)


# @torch.jit.script
def grid_hess(inp, grid, bound: List[int], interpolation: List[int],
              extrapolate: int):
    """ /!\ Used only in backward pass of grid_grad
    inp: (B, C, *spatial_in) tensor
    grid: (B, *spatial_out, D) tensor
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, C, *spatial_out, D, D) tensor
    """
    dim = grid.shape[-1]
    bound = pad_list_int(bound, dim)
    interpolation = pad_list_int(interpolation, dim)
    bound_fn = make_bound(bound)
    is_iso1 = list_all([order == 1 for order in interpolation])
    if is_iso1:
        if dim == 3:
            return iso1.hess3d(inp, grid, bound_fn, extrapolate)
        if dim == 2:
            return iso1.hess2d(inp, grid, bound_fn, extrapolate)
        if dim == 1:
            return iso1.hess1d(inp, grid, bound_fn, extrapolate)
    is_iso0 = list_all([order == 0 for order in interpolation])
    if is_iso0:
        return iso0.hess(inp, grid, bound_fn, extrapolate)
    spline_fn = make_spline(interpolation)
    return nd.hess(inp, grid, bound_fn, spline_fn, extrapolate)


# @torch.jit.script
def grid_pull_backward(grad, inp, grid, bound: List[int],
                       interpolation: List[int], extrapolate: int) \
        -> Tuple[Optional[Tensor], Optional[Tensor], ]:
    """
    grad: (B, C, *spatial_out) tensor
    inp: (B, C, *spatial_in) tensor
    grid: (B, *spatial_out, D) tensor
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, C, *spatial_in) tensor, (B, *spatial_out, D)
    """
    dim = grid.shape[-1]
    grad_inp: Optional[Tensor] = None
    grad_grid: Optional[Tensor] = None
    if inp.requires_grad:
        grad_inp = grid_push(grad, grid, inp.shape[-dim:], bound, interpolation, extrapolate)
    if grid.requires_grad:
        grad_grid = grid_grad(inp, grid, bound, interpolation, extrapolate)
        # grad_grid = dot(grad_grid, grad.unsqueeze(-1), dim=1)
        grad_grid = (grad_grid * grad.unsqueeze(-1)).sum(dim=1)
    return grad_inp, grad_grid


# @torch.jit.script
def grid_push_backward(grad, inp, grid, bound: List[int],
                       interpolation: List[int], extrapolate: int) \
        -> Tuple[Optional[Tensor], Optional[Tensor], ]:
    """
    grad: (B, C, *spatial_out) tensor
    inp: (B, C, *spatial_in) tensor
    grid: (B, *spatial_in, D) tensor
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, C, *spatial_in) tensor, (B, *spatial_in, D)
    """
    grad_inp: Optional[Tensor] = None
    grad_grid: Optional[Tensor] = None
    if inp.requires_grad:
        grad_inp = grid_pull(grad, grid, bound, interpolation, extrapolate)
    if grid.requires_grad:
        grad_grid = grid_grad(grad, grid, bound, interpolation, extrapolate)
        # grad_grid = dot(grad_grid, inp.unsqueeze(-1), dim=1)
        grad_grid = (grad_grid * inp.unsqueeze(-1)).sum(dim=1)
    return grad_inp, grad_grid


# @torch.jit.script
def grid_count_backward(grad, grid, bound: List[int],
                       interpolation: List[int], extrapolate: int) \
        -> Optional[Tensor]:
    """
    grad: (B, C, *spatial_out) tensor
    grid: (B, *spatial_in, D) tensor
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, C, *spatial_in) tensor, (B, *spatial_in, D)
    """
    if grid.requires_grad:
        return grid_grad(grad, grid, bound, interpolation, extrapolate).sum(1)
    return None


# @torch.jit.script
def grid_grad_backward(grad, inp, grid, bound: List[int],
                       interpolation: List[int], extrapolate: int) \
        -> Tuple[Optional[Tensor], Optional[Tensor]]:
    """
    grad: (B, C, *spatial_out, D) tensor
    inp: (B, C, *spatial_in) tensor
    grid: (B, *spatial_out, D) tensor
    bound: List{D}[int] tensor
    interpolation: List{D}[int]
    extrapolate: int
    returns: (B, C, *spatial_in, D) tensor, (B, *spatial_out, D)
    """
    dim = grid.shape[-1]
    shape = inp.shape[-dim:]
    grad_inp: Optional[Tensor] = None
    grad_grid: Optional[Tensor] = None
    if inp.requires_grad:
        grad_inp = grid_pushgrad(grad, grid, shape, bound, interpolation, extrapolate)
    if grid.requires_grad:
        grad_grid = grid_hess(inp, grid, bound, interpolation, extrapolate)
        # grad_grid = dot_multi(grad_grid, grad.unsqueeze(-1), dim=[1, -2])
        grad_grid = (grad_grid * grad.unsqueeze(-1)).sum(dim=[1, -2])
    return grad_inp, grad_grid