"megatron/training/initialize.py" did not exist on "716da5d8e61cd4f6d8af8e55747a6e9e22831328"
test_cat.py 1.48 KB
Newer Older
rusty1s's avatar
cat  
rusty1s committed
1
2
3
4
5
import pytest
import torch
from torch_sparse.tensor import SparseTensor
from torch_sparse.cat import cat

rusty1s's avatar
rusty1s committed
6
from .utils import devices, tensor
rusty1s's avatar
cat  
rusty1s committed
7
8


rusty1s's avatar
rusty1s committed
9
10
@pytest.mark.parametrize('device', devices)
def test_cat(device):
rusty1s's avatar
cat  
rusty1s committed
11
12
    index = tensor([[0, 0, 1], [0, 1, 2]], torch.long, device)
    mat1 = SparseTensor(index)
rusty1s's avatar
rusty1s committed
13
    mat1.fill_cache_()
rusty1s's avatar
cat  
rusty1s committed
14
15
16

    index = tensor([[0, 0, 1, 2], [0, 1, 1, 0]], torch.long, device)
    mat2 = SparseTensor(index)
rusty1s's avatar
rusty1s committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    mat2.fill_cache_()

    out = cat([mat1, mat2], dim=0)
    assert out.to_dense().tolist() == [[1, 1, 0], [0, 0, 1], [1, 1, 0],
                                       [0, 1, 0], [1, 0, 0]]
    assert len(out.storage.cached_keys()) == 2
    assert out.storage.has_rowcount()
    assert out.storage.has_rowptr()

    out = cat([mat1, mat2], dim=1)
    assert out.to_dense().tolist() == [[1, 1, 0, 1, 1], [0, 0, 1, 0, 1],
                                       [0, 0, 0, 1, 0]]
    assert len(out.storage.cached_keys()) == 2
    assert out.storage.has_colcount()
    assert out.storage.has_colptr()

    out = cat([mat1, mat2], dim=(0, 1))
    assert out.to_dense().tolist() == [[1, 1, 0, 0, 0], [0, 0, 1, 0, 0],
                                       [0, 0, 0, 1, 1], [0, 0, 0, 0, 1],
                                       [0, 0, 0, 1, 0]]
    assert len(out.storage.cached_keys()) == 6
rusty1s's avatar
cat  
rusty1s committed
38

rusty1s's avatar
rusty1s committed
39
40
41
42
    mat1.set_value_(torch.randn((mat1.nnz(), 4), device=device))
    out = cat([mat1, mat1], dim=-1)
    assert out.storage.value.size() == (mat1.nnz(), 8)
    assert len(out.storage.cached_keys()) == 6