weighting.py 3.15 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
6
7
import torch
from torch.autograd import Function

from .utils.ffi import weighting_forward as weighting_fw
from .utils.ffi import weighting_backward_src as weighting_bw_src
from .utils.ffi import weighting_backward_weight as weighting_bw_weight
from .utils.ffi import weighting_backward_basis as weighting_bw_basis
rusty1s's avatar
rusty1s committed
8
from .utils.ffi import weighting_backward as weighting_bw
rusty1s's avatar
rusty1s committed
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


def weighting_forward(src, weight, basis, weight_index):
    output = src.new(src.size(0), weight.size(2))
    weighting_fw(output, src, weight, basis, weight_index)
    return output


def weighting_backward_src(grad_output, weight, basis, weight_index):
    grad_src = grad_output.new(grad_output.size(0), weight.size(1))
    weighting_bw_src(grad_src, grad_output, weight, basis, weight_index)
    return grad_src


def weighting_backward_weight(grad_output, src, basis, weight_index, K):
    grad_weight = src.new(K, src.size(1), grad_output.size(1))
    weighting_bw_weight(grad_weight, grad_output, src, basis, weight_index)
    return grad_weight


def weighting_backward_basis(grad_output, src, weight, weight_index):
    grad_basis = src.new(weight_index.size())
    weighting_bw_basis(grad_basis, grad_output, src, weight, weight_index)
    return grad_basis


rusty1s's avatar
rusty1s committed
35
36
37
38
39
40
41
42
43
44
def weighting_backward(grad_output, src, weight, basis, weight_index):
    grad_src = src.new(src.size())
    # grad_weight = weight.new(weight.size())
    grad_weight = weight.new(weight.size(0), weight.size(2), weight.size(1))
    grad_basis = basis.new(basis.size())
    weighting_bw(grad_src, grad_weight, grad_basis, grad_output, src, weight,
                 basis, weight_index)
    return grad_src, grad_weight.transpose(1, 2), grad_basis


rusty1s's avatar
rusty1s committed
45
46
47
48
49
50
51
52
53
54
55
56
57
class SplineWeighting(Function):
    def __init__(self, weight_index):
        super(SplineWeighting, self).__init__()
        self.weight_index = weight_index

    def forward(self, src, weight, basis):
        self.save_for_backward(src, weight, basis)
        return weighting_forward(src, weight, basis, self.weight_index)

    def backward(self, grad_output):
        grad_src = grad_weight = grad_basis = None
        src, weight, basis = self.saved_tensors

rusty1s's avatar
rusty1s committed
58
59
60
61
62
63
64
        needs_src, needs_weight, needs_basis = self.needs_input_grad

        if needs_src and needs_weight and needs_basis:
            return weighting_backward(grad_output, src, weight, basis,
                                      self.weight_index)

        if needs_src:
rusty1s's avatar
rusty1s committed
65
66
            grad_src = weighting_backward_src(grad_output, weight, basis,
                                              self.weight_index)
rusty1s's avatar
rusty1s committed
67
        if needs_weight:
rusty1s's avatar
rusty1s committed
68
            K = weight.size(0)
rusty1s's avatar
rusty1s committed
69
            grad_weight = weighting_backward_weight(grad_output, src, basis,
rusty1s's avatar
rusty1s committed
70
                                                    self.weight_index, K)
rusty1s's avatar
rusty1s committed
71
        if needs_basis:
rusty1s's avatar
rusty1s committed
72
73
74
75
76
77
78
79
80
81
82
            grad_basis = weighting_backward_basis(grad_output, src, weight,
                                                  self.weight_index)

        return grad_src, grad_weight, grad_basis


def spline_weighting(src, weight, basis, weight_index):
    if torch.is_tensor(src):
        return weighting_forward(src, weight, basis, weight_index)
    else:
        return SplineWeighting(weight_index)(src, weight, basis)