test_grid.py 1.76 KB
Newer Older
rusty1s's avatar
rusty1s committed
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
import pytest
import torch
from torch_cluster import grid_cluster

from .utils import tensors, Tensor


@pytest.mark.parametrize('tensor', tensors)
def test_grid_cluster_cpu(tensor):
    position = Tensor(tensor, [[0, 0], [9, 9], [2, 8], [2, 2], [8, 3]])
    size = torch.LongTensor([5, 5])
    expected = torch.LongTensor([0, 3, 1, 0, 2])

    output = grid_cluster(position, size)
    assert output.tolist() == expected.tolist()

    output = grid_cluster(position.expand(2, 5, 2), size)
    assert output.tolist() == expected.expand(2, 5).tolist()

    expected = torch.LongTensor([0, 1, 3, 2, 4])
    batch = torch.LongTensor([0, 0, 1, 1, 1])
    output = grid_cluster(position, size, batch)
    assert output.tolist() == expected.tolist()

    output = grid_cluster(position.expand(2, 5, 2), size, batch.expand(2, 5))
    assert output.tolist() == expected.expand(2, 5).tolist()


@pytest.mark.skipif(not torch.cuda.is_available(), reason='no CUDA')
@pytest.mark.parametrize('tensor', tensors)
def test_grid_cluster_gpu(tensor):  # pragma: no cover
rusty1s's avatar
rusty1s committed
32
33
34
35
36
    position = Tensor(tensor, [[0, 0], [9, 9], [2, 8], [2, 2], [8, 3]]).cuda()
    size = torch.cuda.LongTensor([5, 5])
    expected = torch.LongTensor([0, 3, 1, 0, 2])

    output = grid_cluster(position, size)
rusty1s's avatar
rusty1s committed
37
    # assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
38
39

    output = grid_cluster(position.expand(2, 5, 2), size)
rusty1s's avatar
rusty1s committed
40
    # assert output.cpu().tolist() == expected.expand(2, 5).tolist()
rusty1s's avatar
rusty1s committed
41
42
43
44

    expected = torch.LongTensor([0, 1, 3, 2, 4])
    batch = torch.cuda.LongTensor([0, 0, 1, 1, 1])
    output = grid_cluster(position, size, batch)
rusty1s's avatar
rusty1s committed
45
    # assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
46
47

    output = grid_cluster(position.expand(2, 5, 2), size, batch.expand(2, 5))
rusty1s's avatar
rusty1s committed
48
    # assert output.cpu().tolist() == expected.expand(2, 5).tolist()