test_grid.py 2.71 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
6
7
8
9
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):
rusty1s's avatar
rusty1s committed
10
11
12
13
14
15
    position = Tensor(tensor, [0, 9, 2, 8, 3])
    size = torch.LongTensor([5])
    expected = torch.LongTensor([0, 1, 0, 1, 0])
    output = grid_cluster(position, size)
    assert output.tolist() == expected.tolist()

rusty1s's avatar
rusty1s committed
16
17
18
    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])
rusty1s's avatar
rusty1s committed
19
20
    output = grid_cluster(position, size)
    assert output.tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
21

rusty1s's avatar
rusty1s committed
22
    position = Tensor(tensor, [[0, 9, 2, 2, 8], [0, 9, 8, 2, 3]]).t()
rusty1s's avatar
rusty1s committed
23
24
25
26
27
28
    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()

rusty1s's avatar
rusty1s committed
29
30
31
32
33
    position = position.repeat(2, 1)
    batch = torch.LongTensor([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    expected = torch.LongTensor([0, 3, 1, 0, 2, 4, 7, 5, 4, 6])
    expected_batch = torch.LongTensor([0, 0, 0, 0, 1, 1, 1, 1])
    output, reduced_batch = grid_cluster(position, size, batch)
rusty1s's avatar
rusty1s committed
34
    assert output.tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
35
    assert reduced_batch.tolist() == expected_batch.tolist()
rusty1s's avatar
rusty1s committed
36
37
38
39
40


@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
41
42
43
44
45
    position = Tensor(tensor, [0, 9, 2, 8, 3]).cuda()
    size = torch.cuda.LongTensor([5])
    expected = torch.cuda.LongTensor([0, 1, 0, 1, 0])
    output = grid_cluster(position, size)
    assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
46

rusty1s's avatar
rusty1s committed
47
48
49
50
51
52
    position = Tensor(tensor, [[0, 0], [9, 9], [2, 8], [2, 2], [8, 3]])
    position = position.cuda()
    size = torch.cuda.LongTensor([5, 5])
    expected = torch.cuda.LongTensor([0, 3, 1, 0, 2])
    output = grid_cluster(position, size)
    assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
53

rusty1s's avatar
rusty1s committed
54
55
56
57
    position = Tensor(tensor, [[0, 9, 2, 2, 8], [0, 9, 8, 2, 3]])
    position = position.cuda().t()
    output = grid_cluster(position, size)
    assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
58

rusty1s's avatar
rusty1s committed
59
60
61
    output = grid_cluster(position.expand(2, 5, 2), size)
    assert output.tolist() == expected.expand(2, 5).tolist()

rusty1s's avatar
rusty1s committed
62
63
64
65
66
    position = position.repeat(2, 1)
    batch = torch.cuda.LongTensor([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
    expected = torch.LongTensor([0, 3, 1, 0, 2, 4, 7, 5, 4, 6])
    expected_batch = torch.LongTensor([0, 0, 0, 0, 1, 1, 1, 1])
    output, reduced_batch = grid_cluster(position, size, batch)
rusty1s's avatar
rusty1s committed
67
    assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
68
    assert reduced_batch.cpu().tolist() == expected_batch.tolist()