| 1 | from itertools import product |
| 2 | |
| 3 | import pytest |
| 4 | import torch |
| 5 | from torch_cluster import grid_cluster |
| 6 | from torch_cluster.testing import devices, dtypes, tensor |
| 7 | |
| 8 | tests = [{ |
| 9 | 'pos': [2, 6], |
| 10 | 'size': [5], |
| 11 | 'cluster': [0, 0], |
| 12 | }, { |
| 13 | 'pos': [2, 6], |
| 14 | 'size': [5], |
| 15 | 'start': [0], |
| 16 | 'cluster': [0, 1], |
| 17 | }, { |
| 18 | 'pos': [[0, 0], [11, 9], [2, 8], [2, 2], [8, 3]], |
| 19 | 'size': [5, 5], |
| 20 | 'cluster': [0, 5, 3, 0, 1], |
| 21 | }, { |
| 22 | 'pos': [[0, 0], [11, 9], [2, 8], [2, 2], [8, 3]], |
| 23 | 'size': [5, 5], |
| 24 | 'end': [19, 19], |
| 25 | 'cluster': [0, 6, 4, 0, 1], |
| 26 | }] |
| 27 | |
| 28 | |
| 29 | @pytest.mark.parametrize('test,dtype,device', product(tests, dtypes, devices)) |
| 30 | def test_grid_cluster(test, dtype, device): |
| 31 | if dtype == torch.bfloat16 and device == torch.device('cuda:0'): |
| 32 | return |
| 33 | |
| 34 | pos = tensor(test['pos'], dtype, device) |
| 35 | size = tensor(test['size'], dtype, device) |
| 36 | start = tensor(test.get('start'), dtype, device) |
| 37 | end = tensor(test.get('end'), dtype, device) |
| 38 | |
| 39 | cluster = grid_cluster(pos, size, start, end) |
| 40 | assert cluster.tolist() == test['cluster'] |
| 41 | |
| 42 | jit = torch.jit.script(grid_cluster) |
| 43 | assert torch.equal(jit(pos, size, start, end), cluster) |