test_matmul.py 6.74 KB
Newer Older
1
2
3
4
5
6
import sys

import backend as F
import pytest
import torch

7
from dgl.sparse import bspmm, diag, from_coo, mm, val_like
8
9
10

from .utils import (
    clone_detach_and_grad,
11
    dense_mask,
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
    rand_coo,
    rand_csc,
    rand_csr,
    sparse_matrix_to_dense,
    sparse_matrix_to_torch_sparse,
)

# TODO(#4818): Skipping tests on win.
if not sys.platform.startswith("linux"):
    pytest.skip("skipping tests on win", allow_module_level=True)


@pytest.mark.parametrize("create_func", [rand_coo, rand_csr, rand_csc])
@pytest.mark.parametrize("shape", [(2, 7), (5, 2)])
@pytest.mark.parametrize("nnz", [1, 10])
@pytest.mark.parametrize("out_dim", [None, 10])
def test_spmm(create_func, shape, nnz, out_dim):
    dev = F.ctx()
    A = create_func(shape, nnz, dev)
    if out_dim is not None:
        X = torch.randn(shape[1], out_dim, requires_grad=True, device=dev)
    else:
        X = torch.randn(shape[1], requires_grad=True, device=dev)

    sparse_result = A @ X
    grad = torch.randn_like(sparse_result)
    sparse_result.backward(grad)

40
    adj = sparse_matrix_to_dense(A)
41
    XX = clone_detach_and_grad(X)
42
    dense_result = torch.matmul(adj, XX)
43
    if out_dim is None:
44
45
46
        dense_result = dense_result.view(-1)
    dense_result.backward(grad)
    assert torch.allclose(sparse_result, dense_result, atol=1e-05)
47
    assert torch.allclose(X.grad, XX.grad, atol=1e-05)
48
    assert torch.allclose(
49
        dense_mask(adj.grad, A),
50
        sparse_matrix_to_dense(val_like(A, A.val.grad)),
51
        atol=1e-05,
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
@pytest.mark.parametrize("create_func", [rand_coo, rand_csr, rand_csc])
@pytest.mark.parametrize("shape", [(2, 7), (5, 2)])
@pytest.mark.parametrize("nnz", [1, 10])
def test_bspmm(create_func, shape, nnz):
    dev = F.ctx()
    A = create_func(shape, nnz, dev, 2)
    X = torch.randn(shape[1], 10, 2, requires_grad=True, device=dev)

    sparse_result = bspmm(A, X)
    grad = torch.randn_like(sparse_result)
    sparse_result.backward(grad)

    XX = clone_detach_and_grad(X)
    torch_A = A.dense().clone().detach().requires_grad_()
    torch_result = torch_A.permute(2, 0, 1) @ XX.permute(2, 0, 1)

    torch_result.backward(grad.permute(2, 0, 1))
    assert torch.allclose(
        sparse_result.permute(2, 0, 1), torch_result, atol=1e-05
    )
    assert torch.allclose(X.grad, XX.grad, atol=1e-05)
    assert torch.allclose(
        dense_mask(torch_A.grad, A),
        sparse_matrix_to_dense(val_like(A, A.val.grad)),
        atol=1e-05,
    )


83
84
85
86
87
88
@pytest.mark.parametrize("create_func1", [rand_coo, rand_csr, rand_csc])
@pytest.mark.parametrize("create_func2", [rand_coo, rand_csr, rand_csc])
@pytest.mark.parametrize("shape_n_m", [(5, 5), (5, 6)])
@pytest.mark.parametrize("shape_k", [3, 4])
@pytest.mark.parametrize("nnz1", [1, 10])
@pytest.mark.parametrize("nnz2", [1, 10])
89
def test_spspmm(create_func1, create_func2, shape_n_m, shape_k, nnz1, nnz2):
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    dev = F.ctx()
    shape1 = shape_n_m
    shape2 = (shape_n_m[1], shape_k)
    A1 = create_func1(shape1, nnz1, dev)
    A2 = create_func2(shape2, nnz2, dev)
    A3 = A1 @ A2
    grad = torch.randn_like(A3.val)
    A3.val.backward(grad)

    torch_A1 = sparse_matrix_to_torch_sparse(A1)
    torch_A2 = sparse_matrix_to_torch_sparse(A2)
    torch_A3 = torch.sparse.mm(torch_A1, torch_A2)
    torch_A3_grad = sparse_matrix_to_torch_sparse(A3, grad)
    torch_A3.backward(torch_A3_grad)

    with torch.no_grad():
        assert torch.allclose(A3.dense(), torch_A3.to_dense(), atol=1e-05)
        assert torch.allclose(
            val_like(A1, A1.val.grad).dense(),
            torch_A1.grad.to_dense(),
            atol=1e-05,
        )
        assert torch.allclose(
            val_like(A2, A2.val.grad).dense(),
            torch_A2.grad.to_dense(),
            atol=1e-05,
        )
117
118
119
120
121
122
123
124
125


def test_spspmm_duplicate():
    dev = F.ctx()

    row = torch.tensor([1, 0, 0, 0, 1]).to(dev)
    col = torch.tensor([1, 1, 1, 2, 2]).to(dev)
    val = torch.randn(len(row)).to(dev)
    shape = (4, 4)
126
    A1 = from_coo(row, col, val, shape)
127
128
129
130
131

    row = torch.tensor([1, 0, 0, 1]).to(dev)
    col = torch.tensor([1, 1, 2, 2]).to(dev)
    val = torch.randn(len(row)).to(dev)
    shape = (4, 4)
132
    A2 = from_coo(row, col, val, shape)
133
134
135
136
137
138
139
140
141
142
143
144
145
146

    try:
        A1 @ A2
    except:
        pass
    else:
        assert False, "Should raise error."

    try:
        A2 @ A1
    except:
        pass
    else:
        assert False, "Should raise error."
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214


@pytest.mark.parametrize("create_func", [rand_coo, rand_csr, rand_csc])
@pytest.mark.parametrize("sparse_shape", [(5, 5), (5, 6)])
@pytest.mark.parametrize("nnz", [1, 10])
def test_sparse_diag_mm(create_func, sparse_shape, nnz):
    dev = F.ctx()
    diag_shape = sparse_shape[1], sparse_shape[1]
    A = create_func(sparse_shape, nnz, dev)
    diag_val = torch.randn(sparse_shape[1], device=dev, requires_grad=True)
    D = diag(diag_val, diag_shape)
    # (TODO) Need to use dgl.sparse.matmul after rename mm to matmul
    B = mm(A, D)
    grad = torch.randn_like(B.val)
    B.val.backward(grad)

    torch_A = sparse_matrix_to_torch_sparse(A)
    torch_D = sparse_matrix_to_torch_sparse(D.as_sparse())
    torch_B = torch.sparse.mm(torch_A, torch_D)
    torch_B_grad = sparse_matrix_to_torch_sparse(B, grad)
    torch_B.backward(torch_B_grad)

    with torch.no_grad():
        assert torch.allclose(B.dense(), torch_B.to_dense(), atol=1e-05)
        assert torch.allclose(
            val_like(A, A.val.grad).dense(),
            torch_A.grad.to_dense(),
            atol=1e-05,
        )
        assert torch.allclose(
            diag(D.val.grad, D.shape).dense(),
            torch_D.grad.to_dense(),
            atol=1e-05,
        )


@pytest.mark.parametrize("create_func", [rand_coo, rand_csr, rand_csc])
@pytest.mark.parametrize("sparse_shape", [(5, 5), (5, 6)])
@pytest.mark.parametrize("nnz", [1, 10])
def test_diag_sparse_mm(create_func, sparse_shape, nnz):
    dev = F.ctx()
    diag_shape = sparse_shape[0], sparse_shape[0]
    A = create_func(sparse_shape, nnz, dev)
    diag_val = torch.randn(sparse_shape[0], device=dev, requires_grad=True)
    D = diag(diag_val, diag_shape)
    # (TODO) Need to use dgl.sparse.matmul after rename mm to matmul
    B = mm(D, A)
    grad = torch.randn_like(B.val)
    B.val.backward(grad)

    torch_A = sparse_matrix_to_torch_sparse(A)
    torch_D = sparse_matrix_to_torch_sparse(D.as_sparse())
    torch_B = torch.sparse.mm(torch_D, torch_A)
    torch_B_grad = sparse_matrix_to_torch_sparse(B, grad)
    torch_B.backward(torch_B_grad)

    with torch.no_grad():
        assert torch.allclose(B.dense(), torch_B.to_dense(), atol=1e-05)
        assert torch.allclose(
            val_like(A, A.val.grad).dense(),
            torch_A.grad.to_dense(),
            atol=1e-05,
        )
        assert torch.allclose(
            diag(D.val.grad, D.shape).dense(),
            torch_D.grad.to_dense(),
            atol=1e-05,
        )