Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After we ship cache_context support to all core pipelines, we will remove this. For now, xfail is better than skipping the tests.

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
Expand Down
15 changes: 15 additions & 0 deletions tests/modular_pipelines/testing_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/modular_pipelines/wan/test_modular_pipeline_wan_vace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
73 changes: 73 additions & 0 deletions tests/pipelines/testing_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Loading