From 2c1e85964e80a51aa513c0b77a2af27fc7bb1ba7 Mon Sep 17 00:00:00 2001 From: Chetan Kumar Verma <39086835+ckvermaAI@users.noreply.github.com> Date: Wed, 14 Oct 2026 09:55:07 +0530 Subject: [PATCH] [diffusion] cache-dit: respect CFG parallel for dual-transformer models (#43704) Co-authored-by: Claude Opus 5.5 (1M context) --- .../runtime/cache/cache_dit_integration.py | 3 ++- .../pipelines_core/stages/denoising.py | 1 + .../test/unit/test_cache_dit_integration.py | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/python/sglang/multimodal_gen/runtime/cache/cache_dit_integration.py b/python/sglang/multimodal_gen/runtime/cache/cache_dit_integration.py index e1e8b1ef3e..e206fa83c4 100644 --- a/python/sglang/multimodal_gen/runtime/cache/cache_dit_integration.py +++ b/python/sglang/multimodal_gen/runtime/cache/cache_dit_integration.py @@ -680,6 +680,7 @@ def enable_cache_on_dual_transformer( model_name: str = "wan2.2", sp_group: Optional[torch.distributed.ProcessGroup] = None, tp_group: Optional[torch.distributed.ProcessGroup] = None, + has_separate_cfg: bool = True, ) -> tuple[torch.nn.Module, torch.nn.Module]: """Enable cache-dit on dual transformers using BlockAdapter. @@ -861,7 +862,7 @@ def enable_cache_on_dual_transformer( params_modifiers=[primary_modifier, secondary_modifier], check_forward_pattern=adapter_spec.check_forward_pattern, check_num_outputs=adapter_spec.check_num_outputs, - has_separate_cfg=adapter_spec.has_separate_cfg, + has_separate_cfg=adapter_spec.has_separate_cfg and has_separate_cfg, ), parallelism_config=None, ) 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 cfc14b8dff..8004f0bc76 100644 --- a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py +++ b/python/sglang/multimodal_gen/runtime/pipelines_core/stages/denoising.py @@ -1191,6 +1191,7 @@ class DenoisingStage(PipelineStage, RolloutDenoisingMixin): model_name=self._cache_dit_dual_model_name(), sp_group=sp_group, tp_group=tp_group, + has_separate_cfg=has_separate_cfg, ) logger.info( "cache-dit enabled on dual transformers (steps=%d, %d)", diff --git a/python/sglang/multimodal_gen/test/unit/test_cache_dit_integration.py b/python/sglang/multimodal_gen/test/unit/test_cache_dit_integration.py index 3dd87c944c..bc74cc565b 100644 --- a/python/sglang/multimodal_gen/test/unit/test_cache_dit_integration.py +++ b/python/sglang/multimodal_gen/test/unit/test_cache_dit_integration.py @@ -545,6 +545,30 @@ class TestCalibratorSelection(unittest.TestCase): ) self.assertEqual(module.cache_dit.enable_calls, []) + def test_dual_transformer_has_separate_cfg_follows_runtime(self): + """Under CFG parallel each rank makes one Wan2.2 call per step; + cache-dit must not pair consecutive calls as cond/uncond.""" + for runtime_flag in (True, False): + with self.subTest(has_separate_cfg=runtime_flag): + module = _import_module_with_stub() + module.ParamsModifier = types.SimpleNamespace + transformer = _make_transformer("AnyModel") + transformer.blocks = ["block_0"] + transformer_2 = _make_transformer("AnyModel") + transformer_2.blocks = ["block_0"] + + module.enable_cache_on_dual_transformer( + transformer, + transformer_2, + self._config(module), + self._config(module), + model_name="wan2.2", + has_separate_cfg=runtime_flag, + ) + + adapter = module.cache_dit.enable_calls[0]["target"] + self.assertEqual(adapter.has_separate_cfg, runtime_flag) + class TestCacheDitLegacyFallback(unittest.TestCase): """cache-dit < 1.5.0 (CI base jobs ship 1.3.0): fallback import + DMD guard."""