Debian Codegraph @

test/test_scatter.py

raw
1 from itertools import product
2
3 import pytest
4 import torch
5 import torch_scatter
6 from torch.autograd import gradcheck
7 from torch_scatter.testing import devices, dtypes, reductions, tensor
8
9 reductions = reductions + ['mul']
10
11 tests = [
12 {
13 'src': [1, 3, 2, 4, 5, 6],
14 'index': [0, 1, 0, 1, 1, 3],
15 'dim': -1,
16 'sum': [3, 12, 0, 6],
17 'add': [3, 12, 0, 6],
18 'mul': [2, 60, 1, 6],
19 'mean': [1.5, 4, 0, 6],
20 'min': [1, 3, 0, 6],
21 'arg_min': [0, 1, 6, 5],
22 'max': [2, 5, 0, 6],
23 'arg_max': [2, 4, 6, 5],
24 },
25 {
26 'src': [[1, 2], [5, 6], [3, 4], [7, 8], [9, 10], [11, 12]],
27 'index': [0, 1, 0, 1, 1, 3],
28 'dim': 0,
29 'sum': [[4, 6], [21, 24], [0, 0], [11, 12]],
30 'add': [[4, 6], [21, 24], [0, 0], [11, 12]],
31 'mul': [[1 * 3, 2 * 4], [5 * 7 * 9, 6 * 8 * 10], [1, 1], [11, 12]],
32 'mean': [[2, 3], [7, 8], [0, 0], [11, 12]],
33 'min': [[1, 2], [5, 6], [0, 0], [11, 12]],
34 'arg_min': [[0, 0], [1, 1], [6, 6], [5, 5]],
35 'max': [[3, 4], [9, 10], [0, 0], [11, 12]],
36 'arg_max': [[2, 2], [4, 4], [6, 6], [5, 5]],
37 },
38 {
39 'src': [[1, 5, 3, 7, 9, 11], [2, 4, 8, 6, 10, 12]],
40 'index': [[0, 1, 0, 1, 1, 3], [0, 0, 1, 0, 1, 2]],
41 'dim': 1,
42 'sum': [[4, 21, 0, 11], [12, 18, 12, 0]],
43 'add': [[4, 21, 0, 11], [12, 18, 12, 0]],
44 'mul': [[1 * 3, 5 * 7 * 9, 1, 11], [2 * 4 * 6, 8 * 10, 12, 1]],
45 'mean': [[2, 7, 0, 11], [4, 9, 12, 0]],
46 'min': [[1, 5, 0, 11], [2, 8, 12, 0]],
47 'arg_min': [[0, 1, 6, 5], [0, 2, 5, 6]],
48 'max': [[3, 9, 0, 11], [6, 10, 12, 0]],
49 'arg_max': [[2, 4, 6, 5], [3, 4, 5, 6]],
50 },
51 {
52 'src': [[[1, 2], [5, 6], [3, 4]], [[10, 11], [7, 9], [12, 13]]],
53 'index': [[0, 1, 0], [2, 0, 2]],
54 'dim': 1,
55 'sum': [[[4, 6], [5, 6], [0, 0]], [[7, 9], [0, 0], [22, 24]]],
56 'add': [[[4, 6], [5, 6], [0, 0]], [[7, 9], [0, 0], [22, 24]]],
57 'mul': [[[3, 8], [5, 6], [1, 1]], [[7, 9], [1, 1], [120, 11 * 13]]],
58 'mean': [[[2, 3], [5, 6], [0, 0]], [[7, 9], [0, 0], [11, 12]]],
59 'min': [[[1, 2], [5, 6], [0, 0]], [[7, 9], [0, 0], [10, 11]]],
60 'arg_min': [[[0, 0], [1, 1], [3, 3]], [[1, 1], [3, 3], [0, 0]]],
61 'max': [[[3, 4], [5, 6], [0, 0]], [[7, 9], [0, 0], [12, 13]]],
62 'arg_max': [[[2, 2], [1, 1], [3, 3]], [[1, 1], [3, 3], [2, 2]]],
63 },
64 {
65 'src': [[1, 3], [2, 4]],
66 'index': [[0, 0], [0, 0]],
67 'dim': 1,
68 'sum': [[4], [6]],
69 'add': [[4], [6]],
70 'mul': [[3], [8]],
71 'mean': [[2], [3]],
72 'min': [[1], [2]],
73 'arg_min': [[0], [0]],
74 'max': [[3], [4]],
75 'arg_max': [[1], [1]],
76 },
77 {
78 'src': [[[1, 1], [3, 3]], [[2, 2], [4, 4]]],
79 'index': [[0, 0], [0, 0]],
80 'dim': 1,
81 'sum': [[[4, 4]], [[6, 6]]],
82 'add': [[[4, 4]], [[6, 6]]],
83 'mul': [[[3, 3]], [[8, 8]]],
84 'mean': [[[2, 2]], [[3, 3]]],
85 'min': [[[1, 1]], [[2, 2]]],
86 'arg_min': [[[0, 0]], [[0, 0]]],
87 'max': [[[3, 3]], [[4, 4]]],
88 'arg_max': [[[1, 1]], [[1, 1]]],
89 },
90 ]
91
92
93 @pytest.mark.parametrize('test,reduce,dtype,device',
94 product(tests, reductions, dtypes, devices))
95 def test_forward(test, reduce, dtype, device):
96 src = tensor(test['src'], dtype, device)
97 index = tensor(test['index'], torch.long, device)
98 dim = test['dim']
99 expected = tensor(test[reduce], dtype, device)
100
101 fn = getattr(torch_scatter, 'scatter_' + reduce)
102 jit = torch.jit.script(fn)
103 out1 = fn(src, index, dim)
104 out2 = jit(src, index, dim)
105 if isinstance(out1, tuple):
106 out1, arg_out1 = out1
107 out2, arg_out2 = out2
108 arg_expected = tensor(test['arg_' + reduce], torch.long, device)
109 assert torch.all(arg_out1 == arg_expected)
110 assert arg_out1.tolist() == arg_out1.tolist()
111 assert torch.all(out1 == expected)
112 assert out1.tolist() == out2.tolist()
113
114
115 @pytest.mark.parametrize('test,reduce,device',
116 product(tests, reductions, devices))
117 def test_backward(test, reduce, device):
118 src = tensor(test['src'], torch.double, device)
119 src.requires_grad_()
120 index = tensor(test['index'], torch.long, device)
121 dim = test['dim']
122
123 assert gradcheck(torch_scatter.scatter,
124 (src, index, dim, None, None, reduce))
125
126
127 @pytest.mark.parametrize('test,reduce,dtype,device',
128 product(tests, reductions, dtypes, devices))
129 def test_out(test, reduce, dtype, device):
130 src = tensor(test['src'], dtype, device)
131 index = tensor(test['index'], torch.long, device)
132 dim = test['dim']
133 expected = tensor(test[reduce], dtype, device)
134
135 out = torch.full_like(expected, -2)
136
137 getattr(torch_scatter, 'scatter_' + reduce)(src, index, dim, out)
138
139 if reduce == 'sum' or reduce == 'add':
140 expected = expected - 2
141 elif reduce == 'mul':
142 expected = out # We can not really test this here.
143 elif reduce == 'mean':
144 expected = out # We can not really test this here.
145 elif reduce == 'min':
146 expected = expected.fill_(-2)
147 elif reduce == 'max':
148 expected[expected == 0] = -2
149 else:
150 raise ValueError
151
152 assert torch.all(out == expected)
153
154
155 @pytest.mark.parametrize('test,reduce,dtype,device',
156 product(tests, reductions, dtypes, devices))
157 def test_non_contiguous(test, reduce, dtype, device):
158 src = tensor(test['src'], dtype, device)
159 index = tensor(test['index'], torch.long, device)
160 dim = test['dim']
161 expected = tensor(test[reduce], dtype, device)
162
163 if src.dim() > 1:
164 src = src.transpose(0, 1).contiguous().transpose(0, 1)
165 if index.dim() > 1:
166 index = index.transpose(0, 1).contiguous().transpose(0, 1)
167
168 out = getattr(torch_scatter, 'scatter_' + reduce)(src, index, dim)
169 if isinstance(out, tuple):
170 out, arg_out = out
171 arg_expected = tensor(test['arg_' + reduce], torch.long, device)
172 assert torch.all(arg_out == arg_expected)
173 assert torch.all(out == expected)