test_spspmm.py 2.54 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
from itertools import product

import pytest
import torch
Mario Geiger's avatar
Mario Geiger committed
5
from torch_sparse import spspmm, SparseTensor, transpose
rusty1s's avatar
rusty1s committed
6

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


rusty1s's avatar
rusty1s committed
10
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
rusty1s's avatar
rusty1s committed
11
12
13
14
15
16
17
18
19
def test_spspmm(dtype, device):
    indexA = torch.tensor([[0, 0, 1, 2, 2], [1, 2, 0, 0, 1]], device=device)
    valueA = tensor([1, 2, 3, 4, 5], dtype, device)
    indexB = torch.tensor([[0, 2], [1, 0]], device=device)
    valueB = tensor([2, 4], dtype, device)

    indexC, valueC = spspmm(indexA, valueA, indexB, valueB, 3, 3, 2)
    assert indexC.tolist() == [[0, 1, 2], [0, 1, 1]]
    assert valueC.tolist() == [8, 6, 8]
Mario Geiger's avatar
Mario Geiger committed
20
21


Mario Geiger's avatar
Mario Geiger committed
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
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
def test_spspmm_2(dtype, device):
    row = torch.tensor(
        [0, 1, 1, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9],
        device=device
    )
    col = torch.tensor(
        [0, 5, 10, 15, 1, 2, 3, 7, 13, 6, 9, 5, 10, 15, 11, 14, 5, 15],
        device=device
    )
    value = torch.tensor(
        [1, 3**-0.5, 3**-0.5, 3**-0.5, 1, 1, 1, -2**-0.5, -2**-0.5,
         -2**-0.5, -2**-0.5, 6**-0.5, -6**0.5 / 3, 6**-0.5, -2**-0.5,
         -2**-0.5, 2**-0.5, -2**-0.5],
        dtype=dtype, device=device
    )
    index = torch.stack([row, col])

    m = value.new_zeros(10, 16)
    m[index[0], index[1]] = value

    index_t, value_t = transpose(index, value, 10, 16)

    index, value = spspmm(index, value, index_t, value_t, 10, 16, 10)

    mask = value.abs() > 1e-4
    index, value = index[:, mask], value[mask]

    assert index.tolist() == [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
    assert value.tolist() == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]


Mario Geiger's avatar
Mario Geiger committed
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
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
def test_sparse_tensor_spspmm(dtype, device):
    x = SparseTensor(
        row=torch.tensor(
            [0, 1, 1, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9],
            device=device
        ),
        col=torch.tensor(
            [0, 5, 10, 15, 1, 2, 3, 7, 13, 6, 9, 5, 10, 15, 11, 14, 5, 15],
            device=device
        ),
        value=torch.tensor(
            [1, 3**-0.5, 3**-0.5, 3**-0.5, 1, 1, 1, -2**-0.5, -2**-0.5,
             -2**-0.5, -2**-0.5, 6**-0.5, -6**0.5 / 3, 6**-0.5, -2**-0.5,
             -2**-0.5, 2**-0.5, -2**-0.5],
            dtype=dtype, device=device
        ),
    )

    i0 = torch.eye(10, dtype=dtype, device=device)

    i1 = x @ x.to_dense().t()
    assert torch.allclose(i0, i1)

    i1 = x @ x.t()
    i1 = i1.to_dense()
    assert torch.allclose(i0, i1)