|
| 1 | +import importlib.util |
| 2 | +from itertools import product |
| 3 | + |
| 4 | +import pytest |
| 5 | +import torch |
| 6 | +import torch_cluster as tc |
| 7 | + |
| 8 | +knn = tc.knn |
| 9 | +knn_graph = tc.knn_graph |
| 10 | + |
| 11 | + |
| 12 | +pytestmark = pytest.mark.skipif( |
| 13 | + not (torch.cuda.is_available() and importlib.util.find_spec('triton') is not None), |
| 14 | + reason='CUDA and Triton are required for Triton benchmark tests.', |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def to_set(edge_index): |
| 19 | + return set([(i, j) for i, j in edge_index.t().tolist()]) |
| 20 | + |
| 21 | + |
| 22 | +def _make_batch(num_nodes: int, num_groups: int, |
| 23 | + device: torch.device) -> torch.Tensor: |
| 24 | + groups = max(1, min(num_groups, num_nodes)) |
| 25 | + counts = torch.full((groups, ), num_nodes // groups, device=device, |
| 26 | + dtype=torch.long) |
| 27 | + remainder = num_nodes % groups |
| 28 | + if remainder: |
| 29 | + counts[:remainder] += 1 |
| 30 | + return torch.repeat_interleave(torch.arange(groups, device=device), |
| 31 | + counts) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize('num_x,num_y,num_groups', |
| 35 | + ((*p[0], p[1]) for p in product([(256, 128), (1024, 512), (4096, 2048), (255, 127), |
| 36 | + (256, 5), (1024, 5), (4096, 5), (255, 5)], |
| 37 | + [1, 2, 4, 8, 16, 32]) if p[1] <= min(p[0]))) |
| 38 | +@pytest.mark.benchmark(group="knn") |
| 39 | +def test_triton_knn_benchmark_cuda(benchmark, num_x, num_y, num_groups): |
| 40 | + torch.manual_seed(99) |
| 41 | + x = torch.randn(num_x, 16, device='cuda') |
| 42 | + y = torch.randn(num_y, 16, device='cuda') |
| 43 | + groups = min(num_groups, x.size(0), y.size(0)) |
| 44 | + batch_x = _make_batch(num_x, groups, x.device) |
| 45 | + batch_y = _make_batch(num_y, groups, y.device) |
| 46 | + |
| 47 | + def cuda_fn(): |
| 48 | + knn(x, y, k=16, batch_x=batch_x, batch_y=batch_y, cosine=False, use_triton=False) |
| 49 | + |
| 50 | + for _ in range(5): |
| 51 | + cuda_fn() |
| 52 | + torch.cuda.synchronize() |
| 53 | + |
| 54 | + benchmark(cuda_fn) |
| 55 | + print(f"[knn][cuda] num_x={num_x} num_y={num_y} groups={groups}") |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize('num_x,num_y,num_groups', |
| 59 | + ((*p[0], p[1]) for p in product([(256, 128), (1024, 512), (4096, 2048), (255, 127), |
| 60 | + (256, 5), (1024, 5), (4096, 5), (255, 5)], |
| 61 | + [1, 2, 4, 8, 16, 32]) if p[1] <= min(p[0]))) |
| 62 | +@pytest.mark.benchmark(group="knn") |
| 63 | +def test_triton_knn_benchmark_triton_cosine(benchmark, num_x, num_y, num_groups): |
| 64 | + torch.manual_seed(99) |
| 65 | + x = torch.randn(num_x, 16, device='cuda') |
| 66 | + y = torch.randn(num_y, 16, device='cuda') |
| 67 | + groups = min(num_groups, x.size(0), y.size(0)) |
| 68 | + batch_x = _make_batch(num_x, groups, x.device) |
| 69 | + batch_y = _make_batch(num_y, groups, y.device) |
| 70 | + |
| 71 | + def cuda_fn(): |
| 72 | + return knn(x, y, k=16, batch_x=batch_x, batch_y=batch_y, cosine=True, use_triton=False) |
| 73 | + |
| 74 | + def triton_fn(): |
| 75 | + return knn(x, y, k=16, batch_x=batch_x, batch_y=batch_y, cosine=True, use_triton=True) |
| 76 | + |
| 77 | + for i in range(5): |
| 78 | + if i == 0: |
| 79 | + out_cuda = cuda_fn() |
| 80 | + out_triton = triton_fn() |
| 81 | + assert to_set(out_cuda) == to_set(out_triton) |
| 82 | + else: |
| 83 | + triton_fn() |
| 84 | + torch.cuda.synchronize() |
| 85 | + |
| 86 | + benchmark(triton_fn) |
| 87 | + print(f"[knn][triton] num_x={num_x} num_y={num_y} groups={groups}") |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.parametrize('num_x,num_y,num_groups', |
| 91 | + ((*p[0], p[1]) for p in product([(256, 128), (1024, 512), (4096, 2048), (255, 127), |
| 92 | + (256, 5), (1024, 5), (4096, 5), (255, 5)], |
| 93 | + [1, 2, 4, 8, 16, 32]) if p[1] <= min(p[0]))) |
| 94 | +@pytest.mark.benchmark(group="knn") |
| 95 | +def test_triton_knn_benchmark_triton(benchmark, num_x, num_y, num_groups): |
| 96 | + torch.manual_seed(99) |
| 97 | + x = torch.randn(num_x, 16, device='cuda') |
| 98 | + y = torch.randn(num_y, 16, device='cuda') |
| 99 | + groups = min(num_groups, x.size(0), y.size(0)) |
| 100 | + batch_x = _make_batch(num_x, groups, x.device) |
| 101 | + batch_y = _make_batch(num_y, groups, y.device) |
| 102 | + |
| 103 | + def cuda_fn(): |
| 104 | + return knn(x, y, k=16, batch_x=batch_x, batch_y=batch_y, cosine=False, use_triton=False) |
| 105 | + |
| 106 | + def triton_fn(): |
| 107 | + return knn(x, y, k=16, batch_x=batch_x, batch_y=batch_y, cosine=False, use_triton=True) |
| 108 | + |
| 109 | + for i in range(5): |
| 110 | + if i == 0: |
| 111 | + out_cuda = cuda_fn() |
| 112 | + out_triton = triton_fn() |
| 113 | + assert to_set(out_cuda) == to_set(out_triton) |
| 114 | + else: |
| 115 | + triton_fn() |
| 116 | + torch.cuda.synchronize() |
| 117 | + |
| 118 | + benchmark(triton_fn) |
| 119 | + print(f"[knn][triton] num_x={num_x} num_y={num_y} groups={groups}") |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.parametrize('num_x', [256, 1024, 4096, 255]) |
| 123 | +@pytest.mark.parametrize('num_groups', [1, 2, 4, 6, 8, 16, 24, 32]) |
| 124 | +@pytest.mark.benchmark(group="knn_graph") |
| 125 | +def test_triton_knn_graph_benchmark_cuda(benchmark, num_x, num_groups): |
| 126 | + torch.manual_seed(199) |
| 127 | + x = torch.randn(num_x, 8, device='cuda') |
| 128 | + groups = min(num_groups, x.size(0)) |
| 129 | + batch = _make_batch(num_x, groups, x.device) |
| 130 | + k = min(16, max(1, num_x - 1)) |
| 131 | + |
| 132 | + def cuda_fn(): |
| 133 | + knn_graph(x, k=k, batch=batch, loop=False, use_triton=False) |
| 134 | + |
| 135 | + for _ in range(5): |
| 136 | + cuda_fn() |
| 137 | + torch.cuda.synchronize() |
| 138 | + |
| 139 | + benchmark(cuda_fn) |
| 140 | + print(f"[knn_graph][cuda] num_x={num_x} groups={groups} k={k}") |
| 141 | + |
| 142 | + |
| 143 | +@pytest.mark.parametrize('num_x', [256, 1024, 4096, 255]) |
| 144 | +@pytest.mark.parametrize('num_groups', [1, 2, 4, 6, 8, 16, 24, 32]) |
| 145 | +@pytest.mark.benchmark(group="knn_graph") |
| 146 | +def test_triton_knn_graph_benchmark_triton(benchmark, num_x, num_groups): |
| 147 | + torch.manual_seed(199) |
| 148 | + x = torch.randn(num_x, 8, device='cuda') |
| 149 | + groups = min(num_groups, x.size(0)) |
| 150 | + batch = _make_batch(num_x, groups, x.device) |
| 151 | + k = min(16, max(1, num_x - 1)) |
| 152 | + |
| 153 | + def cuda_fn(): |
| 154 | + return knn_graph(x, k=k, batch=batch, loop=False, use_triton=False) |
| 155 | + |
| 156 | + def triton_fn(): |
| 157 | + return knn_graph(x, k=k, batch=batch, loop=False, use_triton=True) |
| 158 | + |
| 159 | + for i in range(5): |
| 160 | + if i == 0: |
| 161 | + out_cuda = cuda_fn() |
| 162 | + out_triton = triton_fn() |
| 163 | + assert to_set(out_cuda) == to_set(out_triton) |
| 164 | + else: |
| 165 | + triton_fn() |
| 166 | + torch.cuda.synchronize() |
| 167 | + |
| 168 | + benchmark(triton_fn) |
| 169 | + print(f"[knn_graph][triton] num_x={num_x} groups={groups} k={k}") |
0 commit comments