test_gather.py 1.14 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from itertools import product

import pytest
import torch
from torch_scatter import gather_coo, gather_csr

from .utils import tensor

dtypes = [torch.float]
devices = [torch.device('cuda')]


@pytest.mark.skipif(not torch.cuda.is_available(), reason='CUDA not available')
@pytest.mark.parametrize('dtype,device', product(dtypes, devices))
def test_forward(dtype, device):
    src = tensor([[1, 2], [3, 4], [5, 6], [7, 8]], dtype, device)
rusty1s's avatar
rusty1s committed
17
18
    src = tensor([1, 2, 3, 4], dtype, device)
    src.requires_grad_()
rusty1s's avatar
rusty1s committed
19
20
21
22
    indptr = tensor([0, 2, 5, 5, 6], torch.long, device)
    index = tensor([0, 0, 1, 1, 1, 3], torch.long, device)

    out = src.index_select(0, index)
rusty1s's avatar
rusty1s committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    grad_out = torch.randn_like(out)
    out.backward(grad_out)
    print('EXPECTED')
    print(out)
    print(src.grad)

    src.grad = None
    out = gather_csr(src, indptr)
    out.backward(grad_out)
    print('CSR')
    print(out)
    print(src.grad)
    # print('CSR', out)

    # out = gather_coo(src, index)
    # print('COO', out)

    # print('Expected', out)
    src.grad = None
    out = gather_coo(src, index)
    out.backward(grad_out)
    print('COO')
    print(out)
    print(src.grad)