From f0ddf5475b354273c4debb3bee8735392fbfaec3 Mon Sep 17 00:00:00 2001 From: Sayak Paul Date: Wed, 18 Nov 2026 05:58:46 +0000 Subject: [PATCH] tests: add pipeline-level testing for cache context. --- .../test_modular_pipeline_kandinsky_s2v.py | 11 +++ .../modular_pipelines/testing_utils/common.py | 15 ++++ .../wan/test_modular_pipeline_wan_vace.py | 11 +++ tests/pipelines/testing_utils/common.py | 73 +++++++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/tests/modular_pipelines/kandinsky_s2v/test_modular_pipeline_kandinsky_s2v.py b/tests/modular_pipelines/kandinsky_s2v/test_modular_pipeline_kandinsky_s2v.py index 8e665f82f21f..a6a155353a5c 100644 --- a/tests/modular_pipelines/kandinsky_s2v/test_modular_pipeline_kandinsky_s2v.py +++ b/tests/modular_pipelines/kandinsky_s2v/test_modular_pipeline_kandinsky_s2v.py @@ -255,9 +255,20 @@ def get_dummy_inputs(self, seed=0): return inputs +# A `src/` gap, so the test is marked `xfail`: it will XPASS once the call is wrapped, and the marker can go. +DISTILLED_NO_CACHE_CONTEXT = pytest.mark.xfail( + reason="`KandinskyS2VDistilledChunkDenoiseInner` calls the transformer outside `cache_context`.", + strict=True, +) + + class TestKandinskyS2VDistilledModularPipelineFast( KandinskyS2VDistilledModularPipelineTesterConfig, ModularPipelineTesterMixin ): + @DISTILLED_NO_CACHE_CONTEXT + def test_cache_context_receives_required_fields(self): + super().test_cache_context_receives_required_fields() + @pytest.mark.skip(reason=FAST_SKIPS["test_inference_batch_consistent"]) def test_inference_batch_consistent(self): pass diff --git a/tests/modular_pipelines/testing_utils/common.py b/tests/modular_pipelines/testing_utils/common.py index 3ea912a6db5c..0ccf5621acd8 100644 --- a/tests/modular_pipelines/testing_utils/common.py +++ b/tests/modular_pipelines/testing_utils/common.py @@ -23,6 +23,11 @@ from diffusers import ModularPipeline, ModularPipelineBlocks from diffusers.utils import logging +from ...pipelines.testing_utils.common import ( + REQUIRED_CACHE_CONTEXT_FIELDS, + assert_cache_contexts_have, + register_cache_context_probes, +) from ...testing_utils import ( CaptureLogger, backend_empty_cache, @@ -397,6 +402,16 @@ def test_num_images_per_prompt(self, batch_sizes=[1, 2], num_images_per_prompts= assert images.shape[0] == batch_size * num_images_per_prompt + def test_cache_context_receives_required_fields(self): + pipe = self.get_pipeline().to(torch_device) + probes = register_cache_context_probes(pipe) + if not probes: + pytest.skip(f"Skipping test because {self.pipeline_blocks_class} has no `CacheMixin` component.") + + pipe(**self.get_dummy_inputs(), output=self.output_name) + + assert_cache_contexts_have(probes, REQUIRED_CACHE_CONTEXT_FIELDS) + def test_latents_output_in_canonical_form(self): # The denoise group must hand latents back in the family's one canonical form, so any block (a decoder, a # latent upsampler, a second denoise group) can consume them without geometry inputs. The decoder reads diff --git a/tests/modular_pipelines/wan/test_modular_pipeline_wan_vace.py b/tests/modular_pipelines/wan/test_modular_pipeline_wan_vace.py index 3b22389ec632..d53b1755e37f 100644 --- a/tests/modular_pipelines/wan/test_modular_pipeline_wan_vace.py +++ b/tests/modular_pipelines/wan/test_modular_pipeline_wan_vace.py @@ -29,6 +29,13 @@ ) +# A `src/` gap, so the test is marked `xfail`: it will XPASS once the call is wrapped, and the marker can go. +NO_CACHE_CONTEXT = pytest.mark.xfail( + reason="`Wan22LoopDenoiser` calls the transformer outside `cache_context`.", + strict=True, +) + + class Wan22VaceModularPipelineTesterConfig(BaseModularPipelineTesterConfig): pipeline_class = Wan22VaceModularPipeline pipeline_blocks_class = Wan22VaceBlocks @@ -61,6 +68,10 @@ def get_dummy_inputs(self, seed=0): class TestWan22VaceModularPipelineFast(Wan22VaceModularPipelineTesterConfig, ModularPipelineTesterMixin): + @NO_CACHE_CONTEXT + def test_cache_context_receives_required_fields(self): + super().test_cache_context_receives_required_fields() + def test_inference_with_reference_image(self): pipe = self.get_pipeline().to("cpu") diff --git a/tests/pipelines/testing_utils/common.py b/tests/pipelines/testing_utils/common.py index 57801494de0c..65691a5a1033 100644 --- a/tests/pipelines/testing_utils/common.py +++ b/tests/pipelines/testing_utils/common.py @@ -26,6 +26,8 @@ import diffusers from diffusers import DiffusionPipeline +from diffusers.hooks.hooks import BaseState, HookRegistry, ModelHook, StateManager +from diffusers.models.cache_utils import CacheMixin from diffusers.utils import logging from diffusers.utils.source_code_parsing_utils import ReturnNameVisitor @@ -95,6 +97,67 @@ def assert_mean_pixel_difference(image, expected_image, expected_max_diff=10): assert avg_diff < expected_max_diff, f"Error image deviates {avg_diff} pixels on average" +# `CacheContext` fields every denoiser call must be given. PAB and FasterCache read `timestep` from the context and +# raise without it. +REQUIRED_CACHE_CONTEXT_FIELDS = ("timestep",) + + +class CacheContextProbeHook(ModelHook): + """Records the `CacheContext` in effect on every forward of the module it is registered on. + + It is stateful and owns a `StateManager`, so the real `cache_context` hands it the context the same way it does + to a cache hook: what gets recorded is what a cache hook on that module would read. + """ + + _is_stateful = True + + def __init__(self): + super().__init__() + self.state_manager = StateManager(BaseState) + self.contexts = [] + + def pre_forward(self, module, *args, **kwargs): + self.contexts.append(self.state_manager._context) + return args, kwargs + + def reset_state(self, module): + self.state_manager.reset() + return module + + +def register_cache_context_probes(pipe): + """Register a `CacheContextProbeHook` on every `CacheMixin` component of `pipe`, keyed by component name.""" + probes = {} + for name, component in pipe.components.items(): + if isinstance(component, CacheMixin): + probes[name] = CacheContextProbeHook() + HookRegistry.check_if_exists_or_initialize(component).register_hook(probes[name], "cache_context_probe") + return probes + + +def assert_cache_contexts_have(probes, fields): + """Assert every forward the probes saw ran inside a `cache_context` that was given each of `fields`. + + `fields` are `CacheContext` field names. A forward that ran outside any `cache_context` has none of them. + """ + assert any(probe.contexts for probe in probes.values()), ( + f"None of {sorted(probes)} was called, so no `cache_context` call was checked." + ) + for name, probe in probes.items(): + incomplete = [] + for call, context in enumerate(probe.contexts): + if context is None: + incomplete.append(f"call {call} (no `cache_context`): {', '.join(fields)}") + continue + missing = [field for field in fields if getattr(context, field) is None] + if missing: + incomplete.append(f"call {call} (`{context.name}`): {', '.join(missing)}") + assert not incomplete, ( + f"{len(incomplete)} of {len(probe.contexts)} `{name}` calls ran without cache context fields the cache " + f"hooks read. Pass them to `{name}.cache_context(name, ...)`. Missing: {'; '.join(incomplete)}" + ) + + class BasePipelineTesterConfig: """ Base class defining the configuration interface for pipeline testing. @@ -652,6 +715,16 @@ def test_cfg(self): assert out_cfg.shape == out_no_cfg.shape + def test_cache_context_receives_required_fields(self): + pipe = self.get_pipeline().to(torch_device) + probes = register_cache_context_probes(pipe) + if not probes: + pytest.skip(f"Skipping test because {self.pipeline_class} has no `CacheMixin` component.") + + pipe(**self.get_dummy_inputs()) + + assert_cache_contexts_have(probes, REQUIRED_CACHE_CONTEXT_FIELDS) + def test_callback_inputs(self): sig = inspect.signature(self.pipeline_class.__call__) has_callback_tensor_inputs = "callback_on_step_end_tensor_inputs" in sig.parameters