test_weighting.py 1.83 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
from itertools import product

import pytest
import torch
rusty1s's avatar
rusty1s committed
5
from torch.autograd import gradcheck
rusty1s's avatar
rusty1s committed
6
from torch_spline_conv import spline_basis, spline_weighting
rusty1s's avatar
update  
rusty1s committed
7
from torch_spline_conv.testing import devices, dtypes, tensor
rusty1s's avatar
rusty1s committed
8
9

tests = [{
rusty1s's avatar
rusty1s committed
10
    'x': [[1, 2], [3, 4]],
rusty1s's avatar
rusty1s committed
11
12
13
    'weight': [[[1], [2]], [[3], [4]], [[5], [6]], [[7], [8]]],
    'basis': [[0.5, 0, 0.5, 0], [0, 0, 0.5, 0.5]],
    'weight_index': [[0, 1, 2, 3], [0, 1, 2, 3]],
rusty1s's avatar
rename  
rusty1s committed
14
    'expected': [
rusty1s's avatar
rusty1s committed
15
16
17
18
19
20
        [0.5 * ((1 * (1 + 5)) + (2 * (2 + 6)))],
        [0.5 * ((3 * (5 + 7)) + (4 * (6 + 8)))],
    ]
}]


rusty1s's avatar
rusty1s committed
21
22
@pytest.mark.parametrize('test,dtype,device', product(tests, dtypes, devices))
def test_spline_weighting_forward(test, dtype, device):
rusty1s's avatar
update  
rusty1s committed
23
24
25
    if dtype == torch.bfloat16 and device == torch.device('cuda:0'):
        return

rusty1s's avatar
rusty1s committed
26
    x = tensor(test['x'], dtype, device)
rusty1s's avatar
rusty1s committed
27
28
29
    weight = tensor(test['weight'], dtype, device)
    basis = tensor(test['basis'], dtype, device)
    weight_index = tensor(test['weight_index'], torch.long, device)
rusty1s's avatar
update  
rusty1s committed
30
    expected = tensor(test['expected'], dtype, device)
rusty1s's avatar
rusty1s committed
31

rusty1s's avatar
rusty1s committed
32
    out = spline_weighting(x, weight, basis, weight_index)
rusty1s's avatar
update  
rusty1s committed
33
    assert torch.allclose(out, expected)
rusty1s's avatar
rusty1s committed
34
35


rusty1s's avatar
rusty1s committed
36
@pytest.mark.parametrize('device', devices)
rusty1s's avatar
rusty1s committed
37
def test_spline_weighting_backward(device):
rusty1s's avatar
rusty1s committed
38
39
40
    pseudo = torch.rand((4, 2), dtype=torch.double, device=device)
    kernel_size = tensor([5, 5], torch.long, device)
    is_open_spline = tensor([1, 1], torch.uint8, device)
rusty1s's avatar
rusty1s committed
41
    degree = 1
rusty1s's avatar
rusty1s committed
42

rusty1s's avatar
rusty1s committed
43
44
    basis, weight_index = spline_basis(pseudo, kernel_size, is_open_spline,
                                       degree)
rusty1s's avatar
rusty1s committed
45
    basis.requires_grad_()
rusty1s's avatar
rusty1s committed
46

rusty1s's avatar
rusty1s committed
47
48
    x = torch.rand((4, 2), dtype=torch.double, device=device)
    x.requires_grad_()
rusty1s's avatar
rusty1s committed
49
50
    weight = torch.rand((25, 2, 4), dtype=torch.double, device=device)
    weight.requires_grad_()
rusty1s's avatar
rusty1s committed
51

rusty1s's avatar
rusty1s committed
52
    data = (x, weight, basis, weight_index)
rusty1s's avatar
rusty1s committed
53
    assert gradcheck(spline_weighting, data, eps=1e-6, atol=1e-4) is True