diff --git a/python/sglang/kernels/ops/diffusion/sites/fused_linear_gelu_site.py b/python/sglang/kernels/ops/diffusion/sites/fused_linear_gelu_site.py index fc77d78095..1d8e84ca88 100644 --- a/python/sglang/kernels/ops/diffusion/sites/fused_linear_gelu_site.py +++ b/python/sglang/kernels/ops/diffusion/sites/fused_linear_gelu_site.py @@ -138,6 +138,9 @@ def can_use_linear_gelu(linear: Any, x: torch.Tensor) -> bool: return False if getattr(linear, "weight", None) is None or x.dtype != linear.weight.dtype: return False + if torch.compiler.is_compiling(): + # dynamo guards on the cache's size, so reading it recompiles per linear + return _input_independent_ok(linear) verdict = _VERDICTS.get(linear) if verdict is None: verdict = _VERDICTS[linear] = _input_independent_ok(linear) diff --git a/test/registered/kernels/ops/diffusion/test_sites.py b/test/registered/kernels/ops/diffusion/test_sites.py index 0e14388787..2293c12a66 100644 --- a/test/registered/kernels/ops/diffusion/test_sites.py +++ b/test/registered/kernels/ops/diffusion/test_sites.py @@ -22,6 +22,7 @@ from unittest.mock import MagicMock, patch import pytest import torch +import torch._dynamo.testing import torch.nn as nn import torch.nn.functional as F @@ -503,6 +504,33 @@ def test_mounted_gelu_site_compiles_fullgraph(): ) +class _GeluBlock(nn.Module): + def __init__(self): + super().__init__() + self.ff = _GeluSite() + self.audio_ff = _GeluSite() + + def forward(self, x): + return self.ff(x) + self.audio_ff(x) + + +@requires_cuda +@torch.no_grad() +def test_mounted_gelu_sites_compile_one_frame_across_linears(): + # DiT blocks share one compiled forward; a new linear must not recompile it + torch._dynamo.reset() + blocks = nn.ModuleList(_GeluBlock() for _ in range(12)) + assert linear_gelu.FUSED_LINEAR_GELU.mount(blocks) + counter = torch._dynamo.testing.CompileCounter() + for block in blocks: + block.compile(backend=counter) + x = torch.randn(16, 64, device="cuda", dtype=torch.bfloat16) + for _ in range(2): + for block in blocks: + block(x) + assert counter.frame_count == 1 + + @requires_cuda @torch.no_grad() def test_sana_video_linear_attention_quality_path_and_guards():