diff --git a/tests/diffusion/layers/test_norm.py b/tests/diffusion/layers/test_norm.py index 55bfbab00..3af50fa87 100644 --- a/tests/diffusion/layers/test_norm.py +++ b/tests/diffusion/layers/test_norm.py @@ -4,6 +4,7 @@ import pytest import torch +from pytest_mock import MockerFixture pytestmark = [pytest.mark.core_model, pytest.mark.diffusion, pytest.mark.cpu] @@ -298,6 +299,55 @@ def test_rmsnorm_numerical_correctness(): torch.testing.assert_close(out, expected, atol=1e-5, rtol=1e-5) +# ── RMSNorm compile-path regression tests ── + + +def test_rmsnorm_forward_cuda_does_not_call_fused_during_compile(mocker: MockerFixture) -> None: + """Regression: _forward_fused must not be called during torch.compile tracing. + + Under HSDP, RMSNorm.weight is a DTensor. Accessing .data on a DTensor inside + _forward_fused during tracing produces an orphan all-gather node outside the + compile boundary, causing inductor's compute_ancestors to raise KeyError. + The is_compiling() guard in forward_cuda/forward_hip prevents this by routing + to forward_native during tracing. + + If someone removes the guard, this test will catch the regression by asserting + that _forward_fused was not called while is_compiling() returns True. + """ + from vllm_omni.diffusion.layers.norm import RMSNorm + + norm = RMSNorm(hidden_size=64) + x = torch.randn(2, 4, 64) + + mock_fused = mocker.patch.object(norm, "_forward_fused", wraps=norm._forward_fused) + mocker.patch("torch.compiler.is_compiling", return_value=True) + + out = norm.forward_cuda(x) + + mock_fused.assert_not_called() + assert out.shape == x.shape + + +def test_rmsnorm_forward_hip_does_not_call_fused_during_compile(mocker: MockerFixture) -> None: + """Regression: same guard must be present in forward_hip. + + forward_hip is the entry point on ROCm (AMD GPU). It must behave identically + to forward_cuda with respect to the is_compiling() guard. + """ + from vllm_omni.diffusion.layers.norm import RMSNorm + + norm = RMSNorm(hidden_size=64) + x = torch.randn(2, 4, 64) + + mock_fused = mocker.patch.object(norm, "_forward_fused", wraps=norm._forward_fused) + mocker.patch("torch.compiler.is_compiling", return_value=True) + + out = norm.forward_hip(x) + + mock_fused.assert_not_called() + assert out.shape == x.shape + + def test_rmsnorm_matches_reference_implementation(): """Verify RMSNorm matches a reference implementation.""" from vllm_omni.diffusion.layers.norm import RMSNorm diff --git a/vllm_omni/diffusion/layers/norm.py b/vllm_omni/diffusion/layers/norm.py index 2f320c2a6..51c627975 100644 --- a/vllm_omni/diffusion/layers/norm.py +++ b/vllm_omni/diffusion/layers/norm.py @@ -84,6 +84,13 @@ class RMSNorm(CustomOp): self, x: torch.Tensor, ) -> torch.Tensor: + # During torch.compile tracing, fused_rms_norm writes to `out` in-place + # (returns None) and accesses self.weight.data, which is a DTensor under + # HSDP. Both patterns confuse inductor's compute_ancestors scheduler. + # Fall back to forward_native so inductor can fuse the pure-PyTorch ops + # itself. + if torch.compiler.is_compiling(): + return self.forward_native(x) try: return self._forward_fused(x) except Exception: @@ -93,6 +100,8 @@ class RMSNorm(CustomOp): self, x: torch.Tensor, ) -> torch.Tensor: + if torch.compiler.is_compiling(): + return self.forward_native(x) try: return self._forward_fused(x) except Exception: