Skip to content

Commit 1c501d6

Browse files
committed
Gate torch.compile in tests for windows
1 parent 4106591 commit 1c501d6

6 files changed

Lines changed: 88 additions & 34 deletions

File tree

test/test_fps.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
import torch
55
from torch import Tensor
66
from torch_cluster import fps
7-
from torch_cluster.testing import devices, grad_dtypes, tensor
7+
from torch_cluster.testing import (
8+
devices,
9+
grad_dtypes,
10+
has_compiler,
11+
tensor,
12+
)
813

914

10-
@torch.compile
11-
def fps2(x: Tensor, ratio: Tensor) -> Tensor:
15+
def fps2_impl(x: Tensor, ratio: Tensor) -> Tensor:
1216
return fps(x, None, ratio, False)
1317

1418

19+
if has_compiler():
20+
fps2 = torch.compile(fps2_impl)
21+
else:
22+
fps2 = fps2_impl
23+
24+
1525
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
1626
def test_fps(dtype, device):
1727
x = tensor([

test/test_grid.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
import torch
55
from torch_cluster import grid_cluster
6-
from torch_cluster.testing import devices, dtypes, tensor
6+
from torch_cluster.testing import devices, dtypes, has_compiler, tensor
77

88
tests = [{
99
'pos': [2, 6],
@@ -39,5 +39,6 @@ def test_grid_cluster(test, dtype, device):
3939
cluster = grid_cluster(pos, size, start, end)
4040
assert cluster.tolist() == test['cluster']
4141

42-
jit = torch.compile(grid_cluster)
43-
assert torch.equal(jit(pos, size, start, end), cluster)
42+
if has_compiler():
43+
jit = torch.compile(grid_cluster)
44+
assert torch.equal(jit(pos, size, start, end), cluster)

test/test_knn.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
import scipy.spatial
66
import torch
77
from torch_cluster import knn, knn_graph
8-
from torch_cluster.testing import devices, grad_dtypes, tensor, triton_wrap
8+
from torch_cluster.testing import (
9+
devices,
10+
grad_dtypes,
11+
has_compiler,
12+
tensor,
13+
triton_wrap,
14+
)
915

1016
HAS_CUDA = torch.cuda.is_available()
1117
HAS_TRITON = importlib.util.find_spec('triton') is not None
@@ -41,9 +47,10 @@ def test_knn(dtype, device, use_triton):
4147
edge_index = knn(x, y, 2, use_triton=use_triton)
4248
assert to_set(edge_index) == {(0, 2), (0, 3), (1, 0), (1, 1)}
4349

44-
jit = torch.compile(knn)
45-
edge_index = jit(x, y, 2, use_triton=use_triton)
46-
assert to_set(edge_index) == {(0, 2), (0, 3), (1, 0), (1, 1)}
50+
if has_compiler():
51+
jit = torch.compile(knn)
52+
edge_index = jit(x, y, 2, use_triton=use_triton)
53+
assert to_set(edge_index) == {(0, 2), (0, 3), (1, 0), (1, 1)}
4754

4855
edge_index = knn(x, y, 2, batch_x, batch_y, use_triton=use_triton)
4956
assert to_set(edge_index) == {(0, 2), (0, 3), (1, 4), (1, 5)}
@@ -113,18 +120,19 @@ def test_knn_graph(dtype, device, use_triton):
113120
(2, 3),
114121
}
115122

116-
jit = torch.compile(knn_graph)
117-
edge_index = jit(x, k=2, flow='source_to_target')
118-
assert to_set(edge_index) == {
119-
(1, 0),
120-
(3, 0),
121-
(0, 1),
122-
(2, 1),
123-
(1, 2),
124-
(3, 2),
125-
(0, 3),
126-
(2, 3),
127-
}
123+
if has_compiler():
124+
jit = torch.compile(knn_graph)
125+
edge_index = jit(x, k=2, flow='source_to_target')
126+
assert to_set(edge_index) == {
127+
(1, 0),
128+
(3, 0),
129+
(0, 1),
130+
(2, 1),
131+
(1, 2),
132+
(3, 2),
133+
(0, 3),
134+
(2, 3),
135+
}
128136

129137

130138
@pytest.mark.parametrize(

test/test_radius.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
import scipy.spatial
55
import torch
66
from torch_cluster import radius, radius_graph
7-
from torch_cluster.testing import devices, floating_dtypes, tensor
7+
from torch_cluster.testing import (
8+
devices,
9+
floating_dtypes,
10+
has_compiler,
11+
tensor,
12+
)
813

914

1015
def to_set(edge_index):
@@ -44,10 +49,19 @@ def test_radius(dtype, device):
4449
assert to_set(edge_index) == set([(0, 0), (0, 1), (0, 2), (0, 3), (1, 1),
4550
(1, 2), (1, 5), (1, 6)])
4651

47-
jit = torch.compile(radius)
48-
edge_index = jit(x, y, 2, max_num_neighbors=4)
49-
assert to_set(edge_index) == set([(0, 0), (0, 1), (0, 2), (0, 3), (1, 1),
50-
(1, 2), (1, 5), (1, 6)])
52+
if has_compiler():
53+
jit = torch.compile(radius)
54+
edge_index = jit(x, y, 2, max_num_neighbors=4)
55+
assert to_set(edge_index) == set([
56+
(0, 0),
57+
(0, 1),
58+
(0, 2),
59+
(0, 3),
60+
(1, 1),
61+
(1, 2),
62+
(1, 5),
63+
(1, 6),
64+
])
5165

5266
edge_index = radius(x, y, 2, batch_x, batch_y, max_num_neighbors=4)
5367
assert to_set(edge_index) == set([(0, 0), (0, 1), (0, 2), (0, 3), (1, 5),
@@ -78,10 +92,19 @@ def test_radius_graph(dtype, device):
7892
assert to_set(edge_index) == set([(1, 0), (3, 0), (0, 1), (2, 1), (1, 2),
7993
(3, 2), (0, 3), (2, 3)])
8094

81-
jit = torch.compile(radius_graph)
82-
edge_index = jit(x, r=2.5, flow='source_to_target')
83-
assert to_set(edge_index) == set([(1, 0), (3, 0), (0, 1), (2, 1), (1, 2),
84-
(3, 2), (0, 3), (2, 3)])
95+
if has_compiler():
96+
jit = torch.compile(radius_graph)
97+
edge_index = jit(x, r=2.5, flow='source_to_target')
98+
assert to_set(edge_index) == set([
99+
(1, 0),
100+
(3, 0),
101+
(0, 1),
102+
(2, 1),
103+
(1, 2),
104+
(3, 2),
105+
(0, 3),
106+
(2, 3),
107+
])
85108

86109
edge_index = radius_graph(x, r=100, flow='source_to_target',
87110
max_num_neighbors=1)

test/test_rw.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
import torch
33
from torch_cluster import random_walk
4-
from torch_cluster.testing import devices, tensor
4+
from torch_cluster.testing import devices, has_compiler, tensor
55

66

77
@pytest.mark.parametrize('device', devices)
@@ -31,8 +31,12 @@ def test_rw_small(device):
3131
out = random_walk(row, col, start, walk_length, num_nodes=3)
3232
assert out.tolist() == [[0, 1, 0, 1, 0], [1, 0, 1, 0, 1], [2, 2, 2, 2, 2]]
3333

34-
jit = torch.compile(random_walk)
35-
assert torch.equal(jit(row, col, start, walk_length, num_nodes=3), out)
34+
if has_compiler():
35+
jit = torch.compile(random_walk)
36+
assert torch.equal(
37+
jit(row, col, start, walk_length, num_nodes=3),
38+
out,
39+
)
3640

3741

3842
@pytest.mark.parametrize('device', devices)

torch_cluster/testing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from typing import Any
2+
import platform
3+
import shutil
24

35
import torch
46

@@ -27,3 +29,9 @@ def triton_wrap(dt_device_seq):
2729
for dt, device in dt_device_seq
2830
for use_triton in ([False, True] if device.type == 'cuda' else [False])
2931
]
32+
33+
34+
def has_compiler() -> bool:
35+
if platform.system().lower() != 'windows':
36+
return True
37+
return shutil.which('cl') is not None

0 commit comments

Comments
 (0)