Skip to content

Commit e4c37cc

Browse files
authored
Avoid raw stream name collisions in Inductor (#182178)
Use raw_streamN for generated raw CUDA stream handles so they cannot clobber user stream object bindings such as stream1. Add a CUDA:1 regression test covering the aux-stream, Triton-launch, aux-stream ordering that previously crashed. Authored with Claude.
1 parent 822d047 commit e4c37cc

5 files changed

Lines changed: 62 additions & 9 deletions

File tree

‎test/inductor/test_user_streams.py‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
DEFAULT_STREAM_IDX,
2626
STREAM_NAME_TEMPLATE,
2727
)
28-
from torch._inductor.stream_utils import get_stream_name
28+
from torch._inductor.stream_utils import get_raw_stream_name, get_stream_name
2929
from torch._inductor.test_case import TestCase as InductorTestCase
3030
from torch._inductor.utils import IndentedBuffer
3131
from torch.testing import FileCheck
@@ -123,6 +123,10 @@ def test_get_stream_name_side_streams(self):
123123
self.assertEqual(get_stream_name(2), "stream2")
124124
self.assertEqual(get_stream_name(10), "stream10")
125125

126+
def test_get_raw_stream_name(self):
127+
self.assertEqual(get_raw_stream_name(0), "raw_stream0")
128+
self.assertEqual(get_raw_stream_name(1), "raw_stream1")
129+
126130
def test_get_stream_name_cached(self):
127131
"""Test that get_stream_name results are cached."""
128132
# Call twice and verify we get the same object (cached)
@@ -226,6 +230,47 @@ def fn(x, y):
226230
self.assertIn("torch.cuda.stream", code)
227231
self.assertIn("synchronize_stream", code)
228232

233+
@unittest.skipIf(
234+
not TEST_CUDA or torch.cuda.device_count() < 2,
235+
"requires at least two CUDA devices",
236+
)
237+
def test_raw_stream_name_does_not_clobber_user_stream_on_cuda_1(self):
238+
from torch._inductor.utils import run_and_get_code
239+
240+
device = torch.device("cuda:1")
241+
aux = torch.cuda.Stream(device=device)
242+
ev0 = torch.cuda.Event()
243+
ev1 = torch.cuda.Event()
244+
245+
def fn(x, w):
246+
ev0.record()
247+
with torch.cuda.stream(aux):
248+
ev0.wait()
249+
a = torch.mm(x, w)
250+
ev1.record()
251+
252+
ev1.wait()
253+
c = (a.sin() * a.cos() + 0.1).relu()
254+
255+
with torch.cuda.stream(aux):
256+
d = torch.mm(c, w.t())
257+
258+
return d
259+
260+
x = torch.randn(64, 128, device=device)
261+
w = torch.randn(128, 64, device=device)
262+
263+
expected = fn(x, w)
264+
torch.cuda.synchronize()
265+
266+
actual, (code,) = run_and_get_code(torch.compile(fn, fullgraph=True), x, w)
267+
torch.cuda.synchronize()
268+
269+
self.assertEqual(actual, expected)
270+
self.assertIn("stream1 = get_external_object_by_index", code)
271+
self.assertIn("raw_stream1 = get_raw_stream(1)", code)
272+
self.assertNotRegex(code, r"(?m)^\s*stream1 = get_raw_stream\(1\)")
273+
229274
def test_compile_preserves_stream_semantics(self):
230275
"""Test that compiled code preserves stream execution semantics."""
231276
from torch._inductor.utils import run_and_get_code
@@ -1188,8 +1233,8 @@ def forward(self, L_x_: "f32[1024]"):
11881233
triton_kernel.run(arg0_1, buf0, 1024, stream=raw_stream)
11891234
with torch.cuda.stream(default_stream):
11901235
buf3 = empty_strided_cuda((1024, ), (1, ), torch.float32)
1191-
stream0 = get_raw_stream(0)
1192-
triton_kernel.run(arg0_1, buf0, buf3, 1024, stream=stream0)
1236+
raw_stream0 = get_raw_stream(0)
1237+
triton_kernel.run(arg0_1, buf0, buf3, 1024, stream=raw_stream0)
11931238
torch.ops.streams.synchronize_stream.default(1)
11941239
return (buf3, )""",
11951240
)

‎torch/_inductor/codegen/triton.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
SchedulerNode,
6767
)
6868
from ..shape_propagation import get_broadcasted_shape
69+
from ..stream_utils import get_raw_stream_name
6970
from ..utils import (
7071
cache_on_self,
7172
DelayReplaceLine,
@@ -5454,7 +5455,7 @@ def codegen_kernel_benchmark(self, num_gb: float | None) -> IndentedBuffer:
54545455
result.writeline(
54555456
V.graph.device_ops.set_device(index)
54565457
) # no-op to ensure context
5457-
stream_name = f"stream{index}"
5458+
stream_name = get_raw_stream_name(index)
54585459
result.writeline(f"{stream_name} = get_raw_stream({index})")
54595460
result.writeline(
54605461
f"{str(Placeholder.KERNEL_NAME)}.run(*args, stream={stream_name})"

‎torch/_inductor/codegen/triton_combo_kernel.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
SequentialFlattenComboKernelGrid,
2121
)
2222
from ..scheduler import BaseSchedulerNode
23+
from ..stream_utils import get_raw_stream_name
2324
from ..utils import Placeholder, triton_version_uses_attrs_dict
2425
from ..virtualized import V
2526
from .common import (
@@ -1056,7 +1057,7 @@ def codegen_kernel_benchmark(self, num_gb: float) -> IndentedBuffer:
10561057
result.writeline(
10571058
V.graph.device_ops.set_device(index)
10581059
) # no-op to ensure context
1059-
stream_name = f"stream{index}"
1060+
stream_name = get_raw_stream_name(index)
10601061
result.writeline(f"{stream_name} = get_raw_stream({index})")
10611062
result.writeline(
10621063
f"{str(Placeholder.KERNEL_NAME)}.run(*args, stream={stream_name})"

‎torch/_inductor/codegen/wrapper.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from ..runtime import triton_heuristics
4949
from ..runtime.hints import DeviceProperties
5050
from ..stream_constants import DEFAULT_STREAM, DEFAULT_STREAM_IDX, STREAM_NAME_TEMPLATE
51-
from ..stream_utils import get_stream_name
51+
from ..stream_utils import get_raw_stream_name, get_stream_name
5252
from ..utils import (
5353
cache_on_self,
5454
DelayReplaceLine,
@@ -1716,7 +1716,7 @@ def codegen_deferred_alignment_copies(self, input_names: Iterable[str]) -> None:
17161716
# is important for nested subgraph codegening.
17171717
def write_get_raw_stream(self, device_idx: int, graph_name: str) -> str:
17181718
self.write_get_raw_stream_header()
1719-
name = f"stream{device_idx}"
1719+
name = get_raw_stream_name(device_idx)
17201720
if config.triton.autotune_at_compile_time:
17211721
self.kernel_autotune_calls.writeline(
17221722
f"{name} = get_raw_stream({device_idx})"
@@ -1786,7 +1786,7 @@ def codegen_device_guard_enter(
17861786
# Need get_raw_stream for subgraph
17871787
self.write_get_raw_stream_header()
17881788
self.kernel_autotune_calls.writeline(
1789-
f"stream{device_idx} = get_raw_stream({device_idx})"
1789+
f"{get_raw_stream_name(device_idx)} = get_raw_stream({device_idx})"
17901790
)
17911791
self.last_seen_device_guard_index = device_idx
17921792
self._num_streams: int = num_streams
@@ -4308,7 +4308,7 @@ def _write_get_raw_stream(
43084308
name = f"{current_stream_name}_raw"
43094309
self.writeline(f"{name} = {current_stream_name}.cuda_stream")
43104310
else:
4311-
name = f"stream{device_idx}"
4311+
name = get_raw_stream_name(device_idx)
43124312
self.writeline(f"{name} = get_raw_stream({device_idx})")
43134313
return name
43144314

‎torch/_inductor/stream_utils.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"DEFAULT_STREAM",
1616
"DEFAULT_STREAM_IDX",
1717
"STREAM_NAME_TEMPLATE",
18+
"get_raw_stream_name",
1819
"get_stream_name",
1920
]
2021

@@ -31,3 +32,8 @@ def get_stream_name(stream_idx: int) -> str:
3132
return DEFAULT_STREAM
3233
else:
3334
return STREAM_NAME_TEMPLATE.format(stream_idx=stream_idx)
35+
36+
37+
def get_raw_stream_name(device_idx: int) -> str:
38+
"""Generate variable name for a raw stream handle on the given device."""
39+
return f"raw_stream{device_idx}"

0 commit comments

Comments
 (0)