ffi.py 1.71 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
6
7
from .._ext import ffi

implemented_degrees = {1: 'linear', 2: 'quadratic', 3: 'cubic'}


def get_func(name, is_cuda, tensor=None):
    prefix = 'THCC' if is_cuda else 'TH'
rusty1s's avatar
rusty1s committed
8
    prefix += 'Tensor' if tensor is None else tensor.type().split('.')[-1]
rusty1s's avatar
rusty1s committed
9
10
11
12
13
14
15
16
17
18
    return getattr(ffi, '{}_{}'.format(prefix, name))


def get_degree_str(degree):
    degree = implemented_degrees.get(degree)
    assert degree is not None, (
        'No implementation found for specified B-spline degree')
    return degree


rusty1s's avatar
rusty1s committed
19
def fw_basis(degree, basis, weight_index, pseudo, kernel_size, is_open_spline):
rusty1s's avatar
rusty1s committed
20
21
22
    name = '{}BasisForward'.format(get_degree_str(degree))
    func = get_func(name, basis.is_cuda, basis)
    func(basis, weight_index, pseudo, kernel_size, is_open_spline)
rusty1s's avatar
rusty1s committed
23
24


rusty1s's avatar
rusty1s committed
25
def bw_basis(degree, self, grad_basis, pseudo, kernel_size, is_open_spline):
rusty1s's avatar
rusty1s committed
26
27
28
    name = '{}BasisBackward'.format(get_degree_str(degree))
    func = get_func(name, self.is_cuda, self)
    func(self, grad_basis, pseudo, kernel_size, is_open_spline)
rusty1s's avatar
rusty1s committed
29
30


rusty1s's avatar
rusty1s committed
31
def fw_weighting(self, src, weight, basis, weight_index):
rusty1s's avatar
rusty1s committed
32
33
34
35
    func = get_func('weightingForward', self.is_cuda, self)
    func(self, src, weight, basis, weight_index)


rusty1s's avatar
rusty1s committed
36
def bw_weighting_src(self, grad_output, weight, basis, weight_index):
rusty1s's avatar
rusty1s committed
37
38
39
40
    func = get_func('weightingBackwardSrc', self.is_cuda, self)
    func(self, grad_output, weight, basis, weight_index)


rusty1s's avatar
rusty1s committed
41
def bw_weighting_weight(self, grad_output, src, basis, weight_index):
rusty1s's avatar
rusty1s committed
42
43
44
45
    func = get_func('weightingBackwardWeight', self.is_cuda, self)
    func(self, grad_output, src, basis, weight_index)


rusty1s's avatar
rusty1s committed
46
def bw_weighting_basis(self, grad_output, src, weight, weight_index):
rusty1s's avatar
rusty1s committed
47
48
    func = get_func('weightingBackwardBasis', self.is_cuda, self)
    func(self, grad_output, src, weight, weight_index)