Debian Codegraph @

test/test_gather.py

raw
1 from itertools import product
2
3 import pytest
4 import torch
5 from torch.autograd import gradcheck
6 from torch_scatter import gather_coo, gather_csr
7 from torch_scatter.testing import devices, dtypes, tensor
8
9 tests = [
10 {
11 'src': [1, 2, 3, 4],
12 'index': [0, 0, 1, 1, 1, 3],
13 'indptr': [0, 2, 5, 5, 6],
14 'expected': [1, 1, 2, 2, 2, 4],
15 },
16 {
17 'src': [[1, 2], [3, 4], [5, 6], [7, 8]],
18 'index': [0, 0, 1, 1, 1, 3],
19 'indptr': [0, 2, 5, 5, 6],
20 'expected': [[1, 2], [1, 2], [3, 4], [3, 4], [3, 4], [7, 8]]
21 },
22 {
23 'src': [[1, 3, 5, 7], [2, 4, 6, 8]],
24 'index': [[0, 0, 1, 1, 1, 3], [0, 0, 0, 1, 1, 2]],
25 'indptr': [[0, 2, 5, 5, 6], [0, 3, 5, 6, 6]],
26 'expected': [[1, 1, 3, 3, 3, 7], [2, 2, 2, 4, 4, 6]],
27 },
28 {
29 'src': [[[1, 2], [3, 4], [5, 6]], [[7, 9], [10, 11], [12, 13]]],
30 'index': [[0, 0, 1], [0, 2, 2]],
31 'indptr': [[0, 2, 3, 3], [0, 1, 1, 3]],
32 'expected': [[[1, 2], [1, 2], [3, 4]], [[7, 9], [12, 13], [12, 13]]],
33 },
34 {
35 'src': [[1], [2]],
36 'index': [[0, 0], [0, 0]],
37 'indptr': [[0, 2], [0, 2]],
38 'expected': [[1, 1], [2, 2]],
39 },
40 {
41 'src': [[[1, 1]], [[2, 2]]],
42 'index': [[0, 0], [0, 0]],
43 'indptr': [[0, 2], [0, 2]],
44 'expected': [[[1, 1], [1, 1]], [[2, 2], [2, 2]]],
45 },
46 ]
47
48
49 @pytest.mark.parametrize('test,dtype,device', product(tests, dtypes, devices))
50 def test_forward(test, dtype, device):
51 src = tensor(test['src'], dtype, device)
52 index = tensor(test['index'], torch.long, device)
53 indptr = tensor(test['indptr'], torch.long, device)
54 expected = tensor(test['expected'], dtype, device)
55
56 out = gather_csr(src, indptr)
57 assert torch.all(out == expected)
58
59 out = gather_coo(src, index)
60 assert torch.all(out == expected)
61
62
63 @pytest.mark.parametrize('test,device', product(tests, devices))
64 def test_backward(test, device):
65 src = tensor(test['src'], torch.double, device)
66 src.requires_grad_()
67 index = tensor(test['index'], torch.long, device)
68 indptr = tensor(test['indptr'], torch.long, device)
69
70 assert gradcheck(gather_csr, (src, indptr, None)) is True
71 assert gradcheck(gather_coo, (src, index, None)) is True
72
73
74 @pytest.mark.parametrize('test,dtype,device', product(tests, dtypes, devices))
75 def test_out(test, dtype, device):
76 src = tensor(test['src'], dtype, device)
77 index = tensor(test['index'], torch.long, device)
78 indptr = tensor(test['indptr'], torch.long, device)
79 expected = tensor(test['expected'], dtype, device)
80
81 size = list(src.size())
82 size[index.dim() - 1] = index.size(-1)
83 out = src.new_full(size, -2)
84
85 gather_csr(src, indptr, out)
86 assert torch.all(out == expected)
87
88 out.fill_(-2)
89
90 gather_coo(src, index, out)
91 assert torch.all(out == expected)
92
93
94 @pytest.mark.parametrize('test,dtype,device', product(tests, dtypes, devices))
95 def test_non_contiguous(test, dtype, device):
96 src = tensor(test['src'], dtype, device)
97 index = tensor(test['index'], torch.long, device)
98 indptr = tensor(test['indptr'], torch.long, device)
99 expected = tensor(test['expected'], dtype, device)
100
101 if src.dim() > 1:
102 src = src.transpose(0, 1).contiguous().transpose(0, 1)
103 if index.dim() > 1:
104 index = index.transpose(0, 1).contiguous().transpose(0, 1)
105 if indptr.dim() > 1:
106 indptr = indptr.transpose(0, 1).contiguous().transpose(0, 1)
107
108 out = gather_csr(src, indptr)
109 assert torch.all(out == expected)
110
111 out = gather_coo(src, index)
112 assert torch.all(out == expected)