spline_linear_gpu_test.py 1.76 KB
Newer Older
Jan Eric Lenssen's avatar
Jan Eric Lenssen committed
1
'''
rusty1s's avatar
rename  
rusty1s committed
2
3
4
import unittest

import torch
rusty1s's avatar
rusty1s committed
5
from numpy.testing import assert_equal, assert_almost_equal
rusty1s's avatar
rename  
rusty1s committed
6
7

if torch.cuda.is_available():
8
9
    from .compute_spline_basis import compute_spline_basis
    from .compute_spline_basis import get_basis_kernel
rusty1s's avatar
rename  
rusty1s committed
10
11
12
13
14


class SplineLinearGPUTest(unittest.TestCase):
    @unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
    def test_open_spline(self):
rusty1s's avatar
rusty1s committed
15
16
17
        input = torch.cuda.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
        kernel_size = torch.cuda.LongTensor([5])
        is_open_spline = torch.cuda.LongTensor([1])
18
19
20
21
        k_max = 2
        K = 5
        dim = 1
        basis_kernel = get_basis_kernel(k_max, K, dim, 1)
22
23
        a1, i1 = compute_spline_basis(input, kernel_size, is_open_spline, 5,
                                      basis_kernel)
rusty1s's avatar
rename  
rusty1s committed
24

rusty1s's avatar
rusty1s committed
25
26
        a2 = [[0, 1], [0.2, 0.8], [0, 1], [0, 1], [0, 1], [0.8, 0.2], [0, 1]]
        i2 = [[1, 0], [1, 0], [2, 1], [3, 2], [4, 3], [4, 3], [0, 4]]
rusty1s's avatar
rename  
rusty1s committed
27

rusty1s's avatar
rusty1s committed
28
29
        assert_almost_equal(a1.cpu().numpy(), a2, 2)
        assert_equal(i1.cpu().numpy(), i2)
rusty1s's avatar
rename  
rusty1s committed
30
31
32

    @unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
    def test_closed_spline(self):
rusty1s's avatar
rusty1s committed
33
34
35
        input = torch.cuda.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
        kernel_size = torch.cuda.LongTensor([4])
        is_open_spline = torch.cuda.LongTensor([0])
36
37
38
39
        k_max = 2
        K = 4
        dim = 1
        basis_kernel = get_basis_kernel(k_max, K, dim, 1)
40
41
        a1, i1 = compute_spline_basis(input, kernel_size, is_open_spline, 4,
                                      basis_kernel)
rusty1s's avatar
rename  
rusty1s committed
42

rusty1s's avatar
rusty1s committed
43
44
        a2 = [[0, 1], [0.2, 0.8], [0, 1], [0, 1], [0, 1], [0.8, 0.2], [0, 1]]
        i2 = [[1, 0], [1, 0], [2, 1], [3, 2], [0, 3], [0, 3], [1, 0]]
rusty1s's avatar
rename  
rusty1s committed
45

rusty1s's avatar
rusty1s committed
46
47
        assert_almost_equal(a1.cpu().numpy(), a2, 2)
        assert_equal(i1.cpu().numpy(), i2)
Jan Eric Lenssen's avatar
Jan Eric Lenssen committed
48
49

'''