test_add.py 1.56 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
import time
rusty1s's avatar
rusty1s committed
2
from itertools import product
rusty1s's avatar
rusty1s committed
3
4
from scipy.io import loadmat
import numpy as np
rusty1s's avatar
rusty1s committed
5
6
7
8

import pytest
import torch
from torch_sparse.tensor import SparseTensor
rusty1s's avatar
rusty1s committed
9
from torch_sparse.add import sparse_add
rusty1s's avatar
rusty1s committed
10
11
12

from .utils import dtypes, devices, tensor

rusty1s's avatar
rusty1s committed
13
14
15
devices = ['cpu']
dtypes = [torch.float]

rusty1s's avatar
rusty1s committed
16
17
18

@pytest.mark.parametrize('dtype,device', product(dtypes, devices))
def test_sparse_add(dtype, device):
rusty1s's avatar
rusty1s committed
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
    name = ('DIMACS10', 'citationCiteseer')[1]
    mat_scipy = loadmat(f'benchmark/{name}.mat')['Problem'][0][0][2].tocsr()
    mat = SparseTensor.from_scipy(mat_scipy)

    mat1 = mat[:, 0:100000]
    mat2 = mat[:, 100000:200000]
    print(mat1.shape)
    print(mat2.shape)

    # 0.0159 to beat
    t = time.perf_counter()
    mat = sparse_add(mat1, mat2)
    print(time.perf_counter() - t)
    print(mat.nnz())

    mat1 = mat_scipy[:, 0:100000]
    mat2 = mat_scipy[:, 100000:200000]
    t = time.perf_counter()
    mat = mat1 + mat2
    print(time.perf_counter() - t)
    print(mat.nnz)

    # mat1 + mat2

    # mat1 = mat1.tocoo()
    # mat2 = mat2.tocoo()

    # row1, col1 = mat1.row, mat1.col
    # row2, col2 = mat2.row, mat2.col

    # idx1 = row1 * 100000 + col1
    # idx2 = row2 * 100000 + col2

    # t = time.perf_counter()
    # np.union1d(idx1, idx2)
    # print(time.perf_counter() - t)

    # index = tensor([[0, 0, 1], [0, 1, 2]], torch.long, device)
    # mat1 = SparseTensor(index)
    # print()
    # print(mat1.to_dense())
rusty1s's avatar
rusty1s committed
60

rusty1s's avatar
rusty1s committed
61
62
63
    # index = tensor([[0, 0, 1, 2], [0, 1, 1, 0]], torch.long, device)
    # mat2 = SparseTensor(index)
    # print(mat2.to_dense())
rusty1s's avatar
rusty1s committed
64

rusty1s's avatar
rusty1s committed
65
    # add(mat1, mat2)