test_forward.py 3.55 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
6
from itertools import product

import pytest
import torch
import torch_scatter

rusty1s's avatar
rusty1s committed
7
8
from .utils import dtypes, devices, tensor

rusty1s's avatar
g++4.9  
rusty1s committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
tests = [{
    'name': 'add',
    'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
    'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
    'dim': -1,
    'fill_value': 0,
    'expected': [[0, 0, 4, 3, 3, 0], [2, 4, 4, 0, 0, 0]],
}, {
    'name': 'add',
    'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
    'index': [0, 1, 1, 0],
    'dim': 0,
    'fill_value': 0,
    'expected': [[6, 5], [6, 8]],
}, {
    'name': 'sub',
    'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
    'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
    'dim': -1,
    'fill_value': 9,
    'expected': [[9, 9, 5, 6, 6, 9], [7, 5, 5, 9, 9, 9]],
}, {
    'name': 'sub',
    'src': [[5, 2], [2, 2], [4, 2], [1, 3]],
    'index': [0, 1, 1, 0],
    'dim': 0,
    'fill_value': 9,
    'expected': [[3, 4], [3, 5]],
}, {
    'name': 'mul',
    'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
    'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
    'dim': -1,
    'fill_value': 1,
    'expected': [[1, 1, 4, 3, 2, 0], [0, 4, 3, 1, 1, 1]],
}, {
    'name': 'mul',
    'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
    'index': [0, 1, 1, 0],
    'dim': 0,
    'fill_value': 1,
    'expected': [[5, 6], [8, 15]],
}, {
    'name': 'div',
    'src': [[2, 1, 1, 4, 2], [1, 2, 1, 2, 4]],
    'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
    'dim': -1,
    'fill_value': 1,
    'expected': [[1, 1, 0.25, 0.5, 0.5, 1], [0.5, 0.25, 0.5, 1, 1, 1]],
}, {
    'name': 'div',
    'src': [[4, 2], [2, 1], [4, 2], [1, 2]],
    'index': [0, 1, 1, 0],
    'dim': 0,
    'fill_value': 1,
    'expected': [[0.25, 0.25], [0.125, 0.5]],
}, {
    'name': 'mean',
    'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
    'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
    'dim': -1,
    'fill_value': 0,
    'expected': [[0, 0, 4, 3, 1.5, 0], [1, 4, 2, 0, 0, 0]],
}, {
    'name': 'mean',
    'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
    'index': [0, 1, 1, 0],
    'dim': 0,
    'fill_value': 0,
    'expected': [[3, 2.5], [3, 4]],
}, {
    'name': 'max',
    'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
    'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
    'dim': -1,
    'fill_value': 0,
    'expected': [[0, 0, 4, 3, 2, 0], [2, 4, 3, 0, 0, 0]],
    'expected_arg': [[-1, -1, 3, 4, 0, 1], [1, 4, 3, -1, -1, -1]],
}, {
    'name': 'max',
    'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
    'index': [0, 1, 1, 0],
    'dim': 0,
    'fill_value': 0,
    'expected': [[5, 3], [4, 5]],
    'expected_arg': [[0, 3], [2, 1]],
}, {
    'name': 'min',
    'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
    'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
    'dim': -1,
    'fill_value': 9,
    'expected': [[9, 9, 4, 3, 1, 0], [0, 4, 1, 9, 9, 9]],
    'expected_arg': [[-1, -1, 3, 4, 2, 1], [0, 4, 2, -1, -1, -1]],
}, {
    'name': 'min',
    'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
    'index': [0, 1, 1, 0],
    'dim': 0,
    'fill_value': 9,
    'expected': [[1, 2], [2, 3]],
    'expected_arg': [[3, 0], [1, 2]],
}]
rusty1s's avatar
rusty1s committed
112
113
114
115
116
117


@pytest.mark.parametrize('test,dtype,device', product(tests, dtypes, devices))
def test_forward(test, dtype, device):
    src = tensor(test['src'], dtype, device)
    index = tensor(test['index'], torch.long, device)
rusty1s's avatar
rusty1s committed
118
    expected = tensor(test['expected'], dtype, device)
rusty1s's avatar
rusty1s committed
119
120

    op = getattr(torch_scatter, 'scatter_{}'.format(test['name']))
rusty1s's avatar
rusty1s committed
121
    out = op(src, index, test['dim'], fill_value=test['fill_value'])
rusty1s's avatar
rusty1s committed
122

rusty1s's avatar
rusty1s committed
123
124
125
126
127
    if isinstance(out, tuple):
        assert out[0].tolist() == expected.tolist()
        assert out[1].tolist() == test['expected_arg']
    else:
        assert out.tolist() == expected.tolist()