|
1 | | -from os import path as osp |
2 | 1 | from itertools import product |
3 | 2 |
|
4 | 3 | import pytest |
5 | | -import json |
6 | 4 | import torch |
7 | 5 | import torch_scatter |
8 | 6 |
|
9 | | -from .utils import tensors, Tensor |
10 | | - |
11 | | -f = open(osp.join(osp.dirname(__file__), 'forward.json'), 'r') |
12 | | -data = json.load(f) |
13 | | -f.close() |
14 | | - |
15 | | - |
16 | | -@pytest.mark.parametrize('tensor,i', product(tensors, range(len(data)))) |
17 | | -def test_forward_cpu(tensor, i): |
18 | | - name = data[i]['name'] |
19 | | - index = torch.LongTensor(data[i]['index']) |
20 | | - input = Tensor(tensor, data[i]['input']) |
21 | | - dim = data[i]['dim'] |
22 | | - fill_value = data[i]['fill_value'] |
23 | | - expected = torch.FloatTensor(data[i]['expected']).type_as(input) |
24 | | - output = expected.new(expected.size()).fill_(fill_value) |
25 | | - |
26 | | - func = getattr(torch_scatter, 'scatter_{}_'.format(name)) |
27 | | - result = func(output, index, input, dim) |
28 | | - assert output.tolist() == expected.tolist() |
29 | | - if 'expected_arg' in data[i]: |
30 | | - expected_arg = torch.LongTensor(data[i]['expected_arg']) |
31 | | - assert result[1].tolist() == expected_arg.tolist() |
32 | | - |
33 | | - func = getattr(torch_scatter, 'scatter_{}'.format(name)) |
34 | | - result = func(index, input, dim, fill_value=fill_value) |
35 | | - if 'expected_arg' not in data[i]: |
36 | | - assert result.tolist() == expected.tolist() |
37 | | - else: |
38 | | - expected_arg = torch.LongTensor(data[i]['expected_arg']) |
39 | | - assert result[0].tolist() == expected.tolist() |
40 | | - assert result[1].tolist() == expected_arg.tolist() |
41 | | - |
42 | | - |
43 | | -@pytest.mark.skipif(not torch.cuda.is_available(), reason='no CUDA') |
44 | | -@pytest.mark.parametrize('tensor,i', product(tensors, range(len(data)))) |
45 | | -def test_forward_gpu(tensor, i): # pragma: no cover |
46 | | - name = data[i]['name'] |
47 | | - index = torch.cuda.LongTensor(data[i]['index']) |
48 | | - input = Tensor(tensor, data[i]['input']).cuda() |
49 | | - dim = data[i]['dim'] |
50 | | - fill_value = data[i]['fill_value'] |
51 | | - expected = torch.FloatTensor(data[i]['expected']).type_as(input) |
52 | | - output = expected.new(expected.size()).fill_(fill_value).cuda() |
53 | | - |
54 | | - func = getattr(torch_scatter, 'scatter_{}_'.format(name)) |
55 | | - result = func(output, index, input, dim) |
56 | | - assert output.cpu().tolist() == expected.tolist() |
57 | | - if 'expected_arg' in data[i]: |
58 | | - expected_arg = torch.LongTensor(data[i]['expected_arg']) |
59 | | - assert result[1].cpu().tolist() == expected_arg.tolist() |
60 | | - func = getattr(torch_scatter, 'scatter_{}'.format(name)) |
61 | | - result = func(index, input, dim, fill_value=fill_value) |
62 | | - if 'expected_arg' not in data[i]: |
63 | | - assert result.cpu().tolist() == expected.tolist() |
64 | | - else: |
65 | | - expected_arg = torch.LongTensor(data[i]['expected_arg']) |
66 | | - assert result[0].cpu().tolist() == expected.tolist() |
67 | | - assert result[1].cpu().tolist() == expected_arg.tolist() |
| 7 | +from .utils import dtypes, devices, tensor |
| 8 | + |
| 9 | +tests = [{ |
| 10 | + 'name': 'add', |
| 11 | + 'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]], |
| 12 | + 'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]], |
| 13 | + 'fill_value': 0, |
| 14 | + 'expected': [[0, 0, 4, 3, 3, 0], [2, 4, 4, 0, 0, 0]] |
| 15 | +}] |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize('test,dtype,device', product(tests, dtypes, devices)) |
| 19 | +def test_forward(test, dtype, device): |
| 20 | + src = tensor(test['src'], dtype, device) |
| 21 | + index = tensor(test['index'], torch.long, device) |
| 22 | + |
| 23 | + op = getattr(torch_scatter, 'scatter_{}'.format(test['name'])) |
| 24 | + output = op(src, index, fill_value=test['fill_value']) |
| 25 | + |
| 26 | + assert output.tolist() == test['expected'] |
| 27 | + # name = data[i]['name'] |
| 28 | + # index = torch.LongTensor(data[i]['index']) |
| 29 | + # input = Tensor(tensor, data[i]['input']) |
| 30 | + # dim = data[i]['dim'] |
| 31 | + # fill_value = data[i]['fill_value'] |
| 32 | + # expected = torch.FloatTensor(data[i]['expected']).type_as(input) |
| 33 | + # output = expected.new(expected.size()).fill_(fill_value) |
| 34 | + |
| 35 | + # func = getattr(torch_scatter, 'scatter_{}_'.format(name)) |
| 36 | + # result = func(output, index, input, dim) |
| 37 | + # assert output.tolist() == expected.tolist() |
| 38 | + # if 'expected_arg' in data[i]: |
| 39 | + # expected_arg = torch.LongTensor(data[i]['expected_arg']) |
| 40 | + # assert result[1].tolist() == expected_arg.tolist() |
| 41 | + |
| 42 | + # func = getattr(torch_scatter, 'scatter_{}'.format(name)) |
| 43 | + # result = func(index, input, dim, fill_value=fill_value) |
| 44 | + # if 'expected_arg' not in data[i]: |
| 45 | + # assert result.tolist() == expected.tolist() |
| 46 | + # else: |
| 47 | + # expected_arg = torch.LongTensor(data[i]['expected_arg']) |
| 48 | + # assert result[0].tolist() == expected.tolist() |
| 49 | + # assert result[1].tolist() == expected_arg.tolist() |
0 commit comments