Skip to content

Commit 1c93ab2

Browse files
soumithclaude
andcommitted
Add tests for torch.compile stream synchronization fix
Added comprehensive tests to verify the fix for GitHub issue pytorch/pytorch#157363: 1. test_compile_with_linear_layer: - Tests custom CUDA kernels with nn.Linear + torch.compile - Verifies correct behavior with various input sizes (1000, 5000, 10000) - Uses reduce-overhead mode to reproduce the original issue conditions 2. test_compile_custom_only: - Tests custom operations without linear layers - Ensures custom operations work correctly with torch.compile These tests ensure that custom CUDA kernels properly synchronize with PyTorch's CUDA stream when used with torch.compile, preventing race conditions that previously caused incorrect outputs. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 69d3b2c commit 1c93ab2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test/test_extension.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from torch import Tensor
77
from typing import Tuple
88
import torch.nn.functional as F
9+
import torch.nn as nn
910

1011

1112
def reference_muladd(a, b, c):
@@ -119,5 +120,54 @@ def test_opcheck_cuda(self):
119120
self._opcheck("cuda")
120121

121122

123+
class TestTorchCompileStreamSync(TestCase):
124+
"""Test for GitHub issue pytorch/pytorch#157363 - stream synchronization with torch.compile"""
125+
126+
@unittest.skipIf(not torch.cuda.is_available(), "requires cuda")
127+
def test_compile_with_linear_layer(self):
128+
"""Test custom CUDA kernels with nn.Linear + torch.compile (the original failing case)"""
129+
130+
class Model(nn.Module):
131+
def __init__(self, size):
132+
super().__init__()
133+
self.linear = nn.Linear(size, size, device="cuda", dtype=torch.float32)
134+
135+
def forward(self, x):
136+
return extension_cpp.ops.mymuladd(self.linear(x), self.linear(x), 0.0)
137+
138+
# Test sizes that previously failed
139+
for size in [1000, 5000, 10000]:
140+
with self.subTest(size=size):
141+
torch.manual_seed(42)
142+
model = Model(size)
143+
x = torch.randn((1, size), device="cuda", dtype=torch.float32)
144+
145+
with torch.no_grad():
146+
expected = model(x)
147+
compiled_model = torch.compile(model, mode="reduce-overhead", fullgraph=True)
148+
actual = compiled_model(x)
149+
150+
torch.testing.assert_close(actual, expected, rtol=1e-5, atol=1e-5)
151+
152+
@unittest.skipIf(not torch.cuda.is_available(), "requires cuda")
153+
def test_compile_custom_only(self):
154+
"""Test custom operations alone with torch.compile"""
155+
156+
def model(x):
157+
return extension_cpp.ops.mymuladd(x, x, 1.0)
158+
159+
for size in [1000, 5000, 10000]:
160+
with self.subTest(size=size):
161+
torch.manual_seed(42)
162+
x = torch.randn((size,), device="cuda", dtype=torch.float32)
163+
164+
with torch.no_grad():
165+
expected = model(x)
166+
compiled_model = torch.compile(model, mode="reduce-overhead", fullgraph=True)
167+
actual = compiled_model(x)
168+
169+
torch.testing.assert_close(actual, expected, rtol=1e-5, atol=1e-5)
170+
171+
122172
if __name__ == "__main__":
123173
unittest.main()

0 commit comments

Comments
 (0)