Debian Codegraph @

test/test_multi_gpu.py

raw
1 from itertools import product
2
3 import pytest
4 import torch
5 import torch_scatter
6 from torch_scatter.testing import dtypes, reductions, tensor
7
8 tests = [
9 {
10 'src': [1, 2, 3, 4, 5, 6],
11 'index': [0, 0, 1, 1, 1, 3],
12 'indptr': [0, 2, 5, 5, 6],
13 'dim': 0,
14 'sum': [3, 12, 0, 6],
15 'add': [3, 12, 0, 6],
16 'mean': [1.5, 4, 0, 6],
17 'min': [1, 3, 0, 6],
18 'max': [2, 5, 0, 6],
19 },
20 ]
21
22
23 @pytest.mark.skipif(not torch.cuda.is_available(), reason='CUDA not available')
24 @pytest.mark.skipif(torch.cuda.device_count() < 2, reason='No multiple GPUS')
25 @pytest.mark.parametrize('test,reduce,dtype', product(tests, reductions,
26 dtypes))
27 def test_forward(test, reduce, dtype):
28 device = torch.device('cuda:1')
29 src = tensor(test['src'], dtype, device)
30 index = tensor(test['index'], torch.long, device)
31 indptr = tensor(test['indptr'], torch.long, device)
32 dim = test['dim']
33 expected = tensor(test[reduce], dtype, device)
34
35 out = torch_scatter.scatter(src, index, dim, reduce=reduce)
36 assert torch.all(out == expected)
37
38 out = torch_scatter.segment_coo(src, index, reduce=reduce)
39 assert torch.all(out == expected)
40
41 out = torch_scatter.segment_csr(src, indptr, reduce=reduce)
42 assert torch.all(out == expected)