|
25 | 25 | DEFAULT_STREAM_IDX, |
26 | 26 | STREAM_NAME_TEMPLATE, |
27 | 27 | ) |
28 | | -from torch._inductor.stream_utils import get_stream_name |
| 28 | +from torch._inductor.stream_utils import get_raw_stream_name, get_stream_name |
29 | 29 | from torch._inductor.test_case import TestCase as InductorTestCase |
30 | 30 | from torch._inductor.utils import IndentedBuffer |
31 | 31 | from torch.testing import FileCheck |
@@ -123,6 +123,10 @@ def test_get_stream_name_side_streams(self): |
123 | 123 | self.assertEqual(get_stream_name(2), "stream2") |
124 | 124 | self.assertEqual(get_stream_name(10), "stream10") |
125 | 125 |
|
| 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 | + |
126 | 130 | def test_get_stream_name_cached(self): |
127 | 131 | """Test that get_stream_name results are cached.""" |
128 | 132 | # Call twice and verify we get the same object (cached) |
@@ -226,6 +230,47 @@ def fn(x, y): |
226 | 230 | self.assertIn("torch.cuda.stream", code) |
227 | 231 | self.assertIn("synchronize_stream", code) |
228 | 232 |
|
| 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 | + |
229 | 274 | def test_compile_preserves_stream_semantics(self): |
230 | 275 | """Test that compiled code preserves stream execution semantics.""" |
231 | 276 | from torch._inductor.utils import run_and_get_code |
@@ -1188,8 +1233,8 @@ def forward(self, L_x_: "f32[1024]"): |
1188 | 1233 | triton_kernel.run(arg0_1, buf0, 1024, stream=raw_stream) |
1189 | 1234 | with torch.cuda.stream(default_stream): |
1190 | 1235 | 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) |
1193 | 1238 | torch.ops.streams.synchronize_stream.default(1) |
1194 | 1239 | return (buf3, )""", |
1195 | 1240 | ) |
|
0 commit comments