[Fix] diffusion: Keep the linear-GELU verdict cache out of torch.compile traces (#45944)

Co-authored-by: Mick Qian <mickqian@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Mick
2026-10-23 17:01:51 +08:00
committed by GitHub
co-authored by Mick Qian Claude Opus 5.5
parent f9129915a3
commit 494eeecb6c
2 changed files with 31 additions and 0 deletions
@@ -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)
@@ -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():