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

import pytest
import torch
from torch.autograd import gradcheck
from torch_spline_conv import spline_basis, spline_weighting
limm's avatar
limm committed
7
from torch_spline_conv.testing import devices, dtypes, tensor
limm's avatar
limm committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

tests = [{
    'x': [[1, 2], [3, 4]],
    '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]],
    'expected': [
        [0.5 * ((1 * (1 + 5)) + (2 * (2 + 6)))],
        [0.5 * ((3 * (5 + 7)) + (4 * (6 + 8)))],
    ]
}]


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

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

    out = spline_weighting(x, weight, basis, weight_index)
limm's avatar
limm committed
33
    assert torch.allclose(out, expected)
limm's avatar
limm committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


@pytest.mark.parametrize('device', devices)
def test_spline_weighting_backward(device):
    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)
    degree = 1

    basis, weight_index = spline_basis(pseudo, kernel_size, is_open_spline,
                                       degree)
    basis.requires_grad_()

    x = torch.rand((4, 2), dtype=torch.double, device=device)
    x.requires_grad_()
    weight = torch.rand((25, 2, 4), dtype=torch.double, device=device)
    weight.requires_grad_()

    data = (x, weight, basis, weight_index)
    assert gradcheck(spline_weighting, data, eps=1e-6, atol=1e-4) is True