test_eye.py 2.29 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
from itertools import product
rusty1s's avatar
rusty1s committed
2

rusty1s's avatar
rusty1s committed
3
import pytest
rusty1s's avatar
rusty1s committed
4
import torch
rusty1s's avatar
rusty1s committed
5
from torch_sparse.tensor import SparseTensor
rusty1s's avatar
rusty1s committed
6

rusty1s's avatar
rusty1s committed
7
8
9
10
11
from .utils import dtypes, devices


@pytest.mark.parametrize('dtype,device', product(dtypes, devices))
def test_eye(dtype, device):
rusty1s's avatar
rusty1s committed
12
13
    mat = SparseTensor.eye(3, dtype=dtype, device=device)
    assert mat.storage.col().device == device
rusty1s's avatar
rusty1s committed
14
15
16
17
18
    assert mat.storage.sparse_sizes() == (3, 3)
    assert mat.storage.row().tolist() == [0, 1, 2]
    assert mat.storage.rowptr().tolist() == [0, 1, 2, 3]
    assert mat.storage.col().tolist() == [0, 1, 2]
    assert mat.storage.value().tolist() == [1, 1, 1]
rusty1s's avatar
rusty1s committed
19
    assert mat.storage.value().dtype == dtype
rusty1s's avatar
rusty1s committed
20
21
    assert mat.storage.num_cached_keys() == 0

rusty1s's avatar
rusty1s committed
22
23
    mat = SparseTensor.eye(3, has_value=False)
    assert mat.storage.col().device == device
rusty1s's avatar
rusty1s committed
24
25
26
27
28
29
30
    assert mat.storage.sparse_sizes() == (3, 3)
    assert mat.storage.row().tolist() == [0, 1, 2]
    assert mat.storage.rowptr().tolist() == [0, 1, 2, 3]
    assert mat.storage.col().tolist() == [0, 1, 2]
    assert mat.storage.value() is None
    assert mat.storage.num_cached_keys() == 0

rusty1s's avatar
rusty1s committed
31
32
    mat = SparseTensor.eye(3, 4, fill_cache=True)
    assert mat.storage.col().device == device
rusty1s's avatar
rusty1s committed
33
34
35
36
37
38
39
40
41
42
43
    assert mat.storage.sparse_sizes() == (3, 4)
    assert mat.storage.row().tolist() == [0, 1, 2]
    assert mat.storage.rowptr().tolist() == [0, 1, 2, 3]
    assert mat.storage.col().tolist() == [0, 1, 2]
    assert mat.storage.num_cached_keys() == 5
    assert mat.storage.rowcount().tolist() == [1, 1, 1]
    assert mat.storage.colptr().tolist() == [0, 1, 2, 3, 3]
    assert mat.storage.colcount().tolist() == [1, 1, 1, 0]
    assert mat.storage.csr2csc().tolist() == [0, 1, 2]
    assert mat.storage.csc2csr().tolist() == [0, 1, 2]

rusty1s's avatar
rusty1s committed
44
45
    mat = SparseTensor.eye(4, 3, fill_cache=True)
    assert mat.storage.col().device == device
rusty1s's avatar
rusty1s committed
46
47
48
49
50
51
52
53
54
55
    assert mat.storage.sparse_sizes() == (4, 3)
    assert mat.storage.row().tolist() == [0, 1, 2]
    assert mat.storage.rowptr().tolist() == [0, 1, 2, 3, 3]
    assert mat.storage.col().tolist() == [0, 1, 2]
    assert mat.storage.num_cached_keys() == 5
    assert mat.storage.rowcount().tolist() == [1, 1, 1, 0]
    assert mat.storage.colptr().tolist() == [0, 1, 2, 3]
    assert mat.storage.colcount().tolist() == [1, 1, 1]
    assert mat.storage.csr2csc().tolist() == [0, 1, 2]
    assert mat.storage.csc2csr().tolist() == [0, 1, 2]