forked from tile-ai/tilelang
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_compile_once.py
More file actions
170 lines (126 loc) · 5.02 KB
/
Copy pathtest_compile_once.py
File metadata and controls
170 lines (126 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""Tests for distributed compile_once coordination."""
from __future__ import annotations
import os
import importlib
import time
import pytest
import torch
import torch.distributed as dist
import tilelang
import tilelang.language as T
import tilelang.testing
from testing.python.distributed._utils import distributed_test
os.environ.setdefault("NCCL_DEBUG", "WARN")
def _identity_kernel():
@T.prim_func
def main(src: T.Tensor((16,), T.float32), dst: T.Tensor((16,), T.float32)):
with T.Kernel(1, threads=32):
tx = T.get_thread_binding()
if tx < 16:
dst[tx] = src[tx]
return main
@tilelang.jit(compile_once=True)
def _jit_identity_kernel():
return _identity_kernel()
@distributed_test(nprocs=4)
def test_compile_once_root_runs_before_non_root(local_rank: int, num_ranks: int):
from tilelang.distributed.host import init_dist
_, _, group = init_dist(local_rank, num_ranks)
tilelang_jit = importlib.import_module("tilelang.jit")
compile_time = None
def fake_cached(**_kwargs):
nonlocal compile_time
compile_time = time.monotonic()
return f"kernel:{local_rank}"
monkeypatch = pytest.MonkeyPatch()
try:
monkeypatch.setattr(tilelang_jit, "cached", fake_cached)
result = tilelang.compile(_identity_kernel(), compile_once=True, compile_group=group)
assert result == f"kernel:{local_rank}"
compile_times = [None for _ in range(num_ranks)]
dist.all_gather_object(compile_times, compile_time, group=group)
assert compile_times[0] is not None
assert compile_times[1] is not None
assert compile_times[0] <= compile_times[1]
finally:
monkeypatch.undo()
dist.destroy_process_group()
@tilelang.testing.requires_cuda_compute_version_ge(9, 0)
@distributed_test(nprocs=4)
def test_compile_once_compile_api(local_rank: int, num_ranks: int):
from tilelang.distributed.host import init_dist
_, _, group = init_dist(local_rank, num_ranks)
kernel = tilelang.compile(_identity_kernel(), compile_once=True, compile_group=group)
src = torch.arange(16, dtype=torch.float32, device=f"cuda:{local_rank}") + local_rank
dst = torch.empty_like(src)
kernel(src, dst)
torch.cuda.synchronize()
assert torch.equal(src, dst)
dist.destroy_process_group()
@tilelang.testing.requires_cuda_compute_version_ge(9, 0)
@distributed_test(nprocs=4)
def test_compile_once_jit_api(local_rank: int, num_ranks: int):
from tilelang.distributed.host import init_dist
_, _, group = init_dist(local_rank, num_ranks)
_jit_identity_kernel.compile_group = group
try:
kernel = _jit_identity_kernel()
src = torch.arange(16, dtype=torch.float32, device=f"cuda:{local_rank}") + local_rank
dst = torch.empty_like(src)
kernel(src, dst)
torch.cuda.synchronize()
assert torch.equal(src, dst)
finally:
_jit_identity_kernel.compile_group = None
dist.destroy_process_group()
@distributed_test(nprocs=4)
def test_compile_once_nonzero_root(local_rank: int, num_ranks: int):
from tilelang.distributed.host import init_dist
_, _, group = init_dist(local_rank, num_ranks)
tilelang_jit = importlib.import_module("tilelang.jit")
compile_time = None
def fake_cached(**_kwargs):
nonlocal compile_time
compile_time = time.monotonic()
return f"kernel:{local_rank}"
monkeypatch = pytest.MonkeyPatch()
try:
monkeypatch.setattr(tilelang_jit, "cached", fake_cached)
result = tilelang.compile(
_identity_kernel(),
compile_once=True,
compile_group=group,
compile_root=1,
)
assert result == f"kernel:{local_rank}"
compile_times = [None for _ in range(num_ranks)]
dist.all_gather_object(compile_times, compile_time, group=group)
assert compile_times[0] is not None
assert compile_times[1] is not None
assert compile_times[1] <= compile_times[0]
finally:
monkeypatch.undo()
dist.destroy_process_group()
@distributed_test(nprocs=4)
def test_compile_once_root_failure_reaches_all_ranks(local_rank: int, num_ranks: int):
from tilelang.distributed.host import init_dist
_, _, group = init_dist(local_rank, num_ranks)
tilelang_jit = importlib.import_module("tilelang.jit")
compile_calls = 0
def fake_cached(**_kwargs):
nonlocal compile_calls
compile_calls += 1
if local_rank == 0:
raise ValueError("root compile failed")
return f"kernel:{local_rank}"
monkeypatch = pytest.MonkeyPatch()
try:
monkeypatch.setattr(tilelang_jit, "cached", fake_cached)
with pytest.raises(RuntimeError, match="root compile failed"):
tilelang.compile(_identity_kernel(), compile_once=True, compile_group=group)
assert compile_calls == (1 if local_rank == 0 else 0)
finally:
monkeypatch.undo()
dist.destroy_process_group()
if __name__ == "__main__":
tilelang.testing.main()