spline_linear_gpu_test.py 1.38 KB
Newer Older
rusty1s's avatar
rename  
rusty1s committed
1
2
3
import unittest

import torch
rusty1s's avatar
rusty1s committed
4
from numpy.testing import assert_equal, assert_almost_equal
rusty1s's avatar
rename  
rusty1s committed
5
6
7
8
9
10
11
12

if torch.cuda.is_available():
    from .spline_linear_gpu import spline_linear_gpu


class SplineLinearGPUTest(unittest.TestCase):
    @unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
    def test_open_spline(self):
rusty1s's avatar
rusty1s committed
13
14
15
        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])
rusty1s's avatar
rename  
rusty1s committed
16

rusty1s's avatar
rusty1s committed
17
        a1, i1 = spline_linear_gpu(input, kernel_size, is_open_spline, 5)
rusty1s's avatar
rename  
rusty1s committed
18

rusty1s's avatar
rusty1s committed
19
20
        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
21

rusty1s's avatar
rusty1s committed
22
23
        assert_almost_equal(a1.cpu().numpy(), a2, 2)
        assert_equal(i1.cpu().numpy(), i2)
rusty1s's avatar
rename  
rusty1s committed
24
25
26

    @unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
    def test_closed_spline(self):
rusty1s's avatar
rusty1s committed
27
28
29
        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])
rusty1s's avatar
rename  
rusty1s committed
30

rusty1s's avatar
rusty1s committed
31
        a1, i1 = spline_linear_gpu(input, kernel_size, is_open_spline, 4)
rusty1s's avatar
rename  
rusty1s committed
32

rusty1s's avatar
rusty1s committed
33
34
        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
35

rusty1s's avatar
rusty1s committed
36
37
        assert_almost_equal(a1.cpu().numpy(), a2, 2)
        assert_equal(i1.cpu().numpy(), i2)