"tests/test_utils/test_env.py" did not exist on "270e470ee17c0f0f3a28eda3ccb90abae4c385f9"
test_rowptr.py 932 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from itertools import product

import pytest
import torch

from torch_sparse import rowptr_cpu
from .utils import tensor, devices

if torch.cuda.is_available():
    from torch_sparse import rowptr_cuda

tests = [
    {
        'row': [0, 0, 1, 1, 1, 2, 2],
        'size': 5,
        'rowptr': [0, 2, 5, 7, 7, 7],
    },
    {
        'row': [0, 0, 1, 1, 1, 4, 4],
        'size': 5,
        'rowptr': [0, 2, 5, 5, 5, 7],
    },
    {
        'row': [2, 2, 4, 4],
        'size': 7,
        'rowptr': [0, 0, 0, 2, 2, 4, 4, 4],
    },
]


def rowptr(row, size):
rusty1s's avatar
typo  
rusty1s committed
32
    return (rowptr_cuda if row.is_cuda else rowptr_cpu).rowptr(row, size)
33
34
35
36
37
38
39
40
41
42


@pytest.mark.parametrize('test,device', product(tests, devices))
def test_rowptr(test, device):
    row = tensor(test['row'], torch.long, device)
    size = test['size']
    expected = tensor(test['rowptr'], torch.long, device)

    out = rowptr(row, size)
    assert torch.all(out == expected)