| 16 | |
| 17 | |
| 18 | class TestIndexing(unittest.TestCase): |
| 19 | def test_sub2ind(self, b=10, n=20, max_size=20): |
| 20 | size = torch.randint(max_size-1, size=[b, 3]) + 1 # (b, 3) |
| 21 | total_cells = torch.prod(size, dim=-1) # (b,) |
| 22 | subidx = torch.rand(b, n, 3).floor().long() |
| 23 | |
| 24 | # python |
| 25 | stime = timer() |
| 26 | ind_gt = naive.sub2ind(subidx, size) |
| 27 | time_python = timer() - stime |
| 28 | |
| 29 | # cpp |
| 30 | stime = timer() |
| 31 | ind_cpp = pr_cpp.sub2ind(subidx, size) |
| 32 | time_cpp = timer() - stime |
| 33 | |
| 34 | print('sub2ind') |
| 35 | print(f'python: {time_python * 1000.:.3f} ms') |
| 36 | print(f'cpp: {time_cpp * 1000.:.3f} ms ({time_python/time_cpp:.2f} speed up)') |
| 37 | |
| 38 | assert torch.allclose(ind_gt, ind_cpp) |
| 39 | |
| 40 | |
| 41 | def test_ind2sub(self, b=10, n=20, max_size=20): |
| 42 | size = torch.randint(max_size-1, size=[b, 3]) + 1 # (b, 3) |
| 43 | total_cells = torch.prod(size, dim=-1) # (b,) |
| 44 | ind = (torch.rand(b, n) * total_cells.unsqueeze(-1)).floor().long() |
| 45 | |
| 46 | # python |
| 47 | stime = timer() |
| 48 | out_gt = naive.ind2sub(ind, size) |
| 49 | time_python = timer() - stime |
| 50 | |
| 51 | # cpp |
| 52 | stime = timer() |
| 53 | out_cpp = pr_cpp.ind2sub(ind, size) |
| 54 | time_cpp = timer() - stime |
| 55 | |
| 56 | print('ind2sub') |
| 57 | print(f'python: {time_python * 1000.:.3f} ms') |
| 58 | print(f'cpp: {time_cpp * 1000.:.3f} ms ({time_python/time_cpp:.2f} speed up)') |
| 59 | |
| 60 | assert torch.allclose(out_gt, out_cpp) |
| 61 | |
| 62 | |
| 63 | class TestGatherPoints(unittest.TestCase): |
nothing calls this directly
no outgoing calls
no test coverage detected