From f96a7326e601f0a7f57aacab15f366cddf537c78 Mon Sep 17 00:00:00 2001 From: "jacky.cheng" Date: Sun, 18 Oct 2026 15:13:25 +0800 Subject: [PATCH] [Fix] [AMD] diffusion: Prevent ComponentResidencyError after compile warmup (#43952) Co-authored-by: Mick Qian --- .../pipelines_core/stages/denoising.py | 17 ++++-- ...ompile_offload_keeps_explicit_layerwise.py | 53 +++++++++++++++++++ .../test/unit/test_offload_during_compile.py | 8 +-- 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 python/sglang/multimodal_gen/test/unit/test_compile_offload_keeps_explicit_layerwise.py diff --git a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py b/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py index 205b032daf..4ee2e65f7d 100644 --- a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py +++ b/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py @@ -565,6 +565,13 @@ class DenoisingStage(PipelineStage, RolloutDenoisingMixin): moved.append(module) return moved + def _explicit_dit_layerwise_offload(self) -> bool: + """True when the user asked for layerwise DiT residency, not a compile-only window.""" + args = self.server_args + if args.dit_layerwise_offload: + return True + return bool(getattr(args, "is_dit_layerwise_offload_selected", False)) + def _maybe_torch_compile(self, module: object) -> None: """ Compile a module with torch.compile, and enable inductor overlap tweak if available. @@ -2073,9 +2080,13 @@ class DenoisingStage(PipelineStage, RolloutDenoisingMixin): for module in moved: module.to(device) return - # prepare for first real request: restore the user-configured residency - for module in self._offloaded_dit_modules_for_compile: - module.disable_offload() + # Compile-only offload is undone so the real request is GPU-resident. + # An explicit layerwise request must stay enabled: disable_offload() + # clears manager.enabled, and the next use-site then raises + # ComponentResidencyError (Wan transformer_2). + if not self._explicit_dit_layerwise_offload(): + for module in self._offloaded_dit_modules_for_compile: + module.disable_offload() # clear the list for avoid overhead during real request self._offloaded_dit_modules_for_compile.clear() # The warmup traced the offload hooks, whose graph breaks inside the diff --git a/python/sglang/multimodal_gen/test/unit/test_compile_offload_keeps_explicit_layerwise.py b/python/sglang/multimodal_gen/test/unit/test_compile_offload_keeps_explicit_layerwise.py new file mode 100644 index 0000000000..8bcc608967 --- /dev/null +++ b/python/sglang/multimodal_gen/test/unit/test_compile_offload_keeps_explicit_layerwise.py @@ -0,0 +1,53 @@ +# SPDX-License-Identifier: Apache-2.0 +"""Compile warmup must not clear a layerwise DiT the user asked to keep.""" + +import unittest +from types import SimpleNamespace + +from sglang.multimodal_gen.runtime.pipelines_core.stages.denoising import ( + DenoisingStage, +) + + +class _OffloadModule: + def __init__(self): + self.disable_calls = 0 + + def disable_offload(self): + self.disable_calls += 1 + + +class TestCompileWarmupKeepsExplicitLayerwise(unittest.TestCase): + def _stage(self, **server_args): + stage = DenoisingStage.__new__(DenoisingStage) + stage.server_args = SimpleNamespace(**server_args) + module = _OffloadModule() + stage._offloaded_dit_modules_for_compile = [module] + return stage, module + + def _finish_compile_window(self, stage): + batch = SimpleNamespace(is_warmup=False) + with stage._offload_for_torch_compile_warmup(batch): + pass + + def test_explicit_dit_layerwise_offload_stays_enabled(self): + stage, module = self._stage( + dit_layerwise_offload=True, + is_dit_layerwise_offload_selected=False, + ) + self._finish_compile_window(stage) + self.assertEqual(module.disable_calls, 0) + self.assertEqual(stage._offloaded_dit_modules_for_compile, []) + + def test_compile_only_offload_is_disabled_for_the_real_request(self): + stage, module = self._stage( + dit_layerwise_offload=False, + is_dit_layerwise_offload_selected=False, + ) + self._finish_compile_window(stage) + self.assertEqual(module.disable_calls, 1) + self.assertEqual(stage._offloaded_dit_modules_for_compile, []) + + +if __name__ == "__main__": + unittest.main() diff --git a/python/sglang/multimodal_gen/test/unit/test_offload_during_compile.py b/python/sglang/multimodal_gen/test/unit/test_offload_during_compile.py index bf53ef9ac3..0fe9bac48c 100644 --- a/python/sglang/multimodal_gen/test/unit/test_offload_during_compile.py +++ b/python/sglang/multimodal_gen/test/unit/test_offload_during_compile.py @@ -47,10 +47,12 @@ class TestOffloadDuringCompile(CustomTestCase): self.dit = _DiT() self.counter = CompileCounter() self.dit.compile(backend=self.counter) - self.stage = SimpleNamespace( - _offloaded_dit_modules_for_compile=[self.dit], - _move_resident_components_for_warmup=lambda: [], + self.stage = DenoisingStage.__new__(DenoisingStage) + self.stage.server_args = SimpleNamespace( + dit_layerwise_offload=False, is_dit_layerwise_offload_selected=False ) + self.stage._offloaded_dit_modules_for_compile = [self.dit] + self.stage._move_resident_components_for_warmup = lambda: [] def _forward(self, *, is_warmup): with DenoisingStage._offload_for_torch_compile_warmup(