test_grid.py 4.05 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
    position = Tensor(tensor, [2, 6])
rusty1s's avatar
rusty1s committed
11
    size = torch.LongTensor([5])
rusty1s's avatar
rusty1s committed
12
13
14
15
16
    expected = torch.LongTensor([0, 0])
    output, _ = grid_cluster(position, size)
    assert output.tolist() == expected.tolist()

    expected = torch.LongTensor([0, 1])
rusty1s's avatar
rename  
rusty1s committed
17
    output, _ = grid_cluster(position, size, origin=0)
rusty1s's avatar
rusty1s committed
18
19
20
21
22
23
24
25
26
    assert output.tolist() == expected.tolist()

    position = Tensor(tensor, [0, 17, 2, 8, 3])
    expected = torch.LongTensor([0, 2, 0, 1, 0])
    output, _ = grid_cluster(position, size)
    assert output.tolist() == expected.tolist()

    output, _ = grid_cluster(position, size, fake_nodes=True)
    expected = torch.LongTensor([0, 3, 0, 1, 0])
rusty1s's avatar
rusty1s committed
27
28
    assert output.tolist() == expected.tolist()

rusty1s's avatar
rusty1s committed
29
30
31
    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
32
    output, _ = grid_cluster(position, size)
rusty1s's avatar
rusty1s committed
33
    assert output.tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
34

rusty1s's avatar
rusty1s committed
35
36
    position = Tensor(tensor, [[0, 11, 2, 2, 8], [0, 9, 8, 2, 3]]).t()
    output, _ = grid_cluster(position, size)
rusty1s's avatar
rusty1s committed
37
38
    assert output.tolist() == expected.tolist()

rusty1s's avatar
rusty1s committed
39
    output, _ = grid_cluster(position.expand(2, 5, 2), size)
rusty1s's avatar
rusty1s committed
40
41
    assert output.tolist() == expected.expand(2, 5).tolist()

rusty1s's avatar
rusty1s committed
42
43
44
    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])
rusty1s's avatar
rusty1s committed
45
46
47
48
49
50
51
    expected_batch2 = torch.LongTensor([0, 0, 0, 0, 1, 1, 1, 1])
    output, batch2 = grid_cluster(position, size, batch)
    assert output.tolist() == expected.tolist()
    assert batch2.tolist() == expected_batch2.tolist()

    output, C = grid_cluster(position, size, batch, fake_nodes=True)
    expected = torch.LongTensor([0, 5, 1, 0, 2, 6, 11, 7, 6, 8])
rusty1s's avatar
rusty1s committed
52
    assert output.tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
53
    assert C == 6
rusty1s's avatar
rusty1s committed
54
55
56
57
58


@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
59
    position = Tensor(tensor, [2, 6]).cuda()
rusty1s's avatar
rusty1s committed
60
    size = torch.cuda.LongTensor([5])
rusty1s's avatar
rusty1s committed
61
62
63
64
65
    expected = torch.LongTensor([0, 0])
    output, _ = grid_cluster(position, size)
    assert output.cpu().tolist() == expected.tolist()

    expected = torch.LongTensor([0, 1])
rusty1s's avatar
rename  
rusty1s committed
66
    output, _ = grid_cluster(position, size, origin=0)
rusty1s's avatar
rusty1s committed
67
68
69
70
71
72
73
74
75
    assert output.cpu().tolist() == expected.tolist()

    position = Tensor(tensor, [0, 17, 2, 8, 3]).cuda()
    expected = torch.LongTensor([0, 2, 0, 1, 0])
    output, _ = grid_cluster(position, size)
    assert output.cpu().tolist() == expected.tolist()

    output, _ = grid_cluster(position, size, fake_nodes=True)
    expected = torch.LongTensor([0, 3, 0, 1, 0])
rusty1s's avatar
rusty1s committed
76
    assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
77

rusty1s's avatar
rusty1s committed
78
79
80
    position = Tensor(tensor, [[0, 0], [9, 9], [2, 8], [2, 2], [8, 3]])
    position = position.cuda()
    size = torch.cuda.LongTensor([5, 5])
rusty1s's avatar
rusty1s committed
81
82
    expected = torch.LongTensor([0, 3, 1, 0, 2])
    output, _ = grid_cluster(position, size)
rusty1s's avatar
rusty1s committed
83
    assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
84

rusty1s's avatar
rusty1s committed
85
    position = Tensor(tensor, [[0, 11, 2, 2, 8], [0, 9, 8, 2, 3]])
rusty1s's avatar
rusty1s committed
86
    position = position.cuda().t()
rusty1s's avatar
rusty1s committed
87
    output, _ = grid_cluster(position, size)
rusty1s's avatar
rusty1s committed
88
    assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
89

rusty1s's avatar
rusty1s committed
90
    output, _ = grid_cluster(position.expand(2, 5, 2), size)
rusty1s's avatar
rusty1s committed
91
92
    assert output.tolist() == expected.expand(2, 5).tolist()

rusty1s's avatar
rusty1s committed
93
94
95
    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])
rusty1s's avatar
rusty1s committed
96
97
98
99
100
101
102
    expected_batch2 = torch.LongTensor([0, 0, 0, 0, 1, 1, 1, 1])
    output, batch2 = grid_cluster(position, size, batch)
    assert output.cpu().tolist() == expected.tolist()
    assert batch2.cpu().tolist() == expected_batch2.tolist()

    output, C = grid_cluster(position, size, batch, fake_nodes=True)
    expected = torch.LongTensor([0, 5, 1, 0, 2, 6, 11, 7, 6, 8])
rusty1s's avatar
rusty1s committed
103
    assert output.cpu().tolist() == expected.tolist()
rusty1s's avatar
rusty1s committed
104
    assert C == 6