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
5 changes: 4 additions & 1 deletion src/diffusers/hooks/taylorseer_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import torch.nn as nn

from ..utils import logging
from .hooks import HookRegistry, ModelHook, StateManager
from .hooks import CacheContext, HookRegistry, ModelHook, StateManager


logger = logging.get_logger(__name__)
Expand Down Expand Up @@ -226,6 +226,9 @@ def reset_state(self, module: torch.nn.Module) -> None:

@torch.compiler.disable
def _measure_should_compute(self) -> bool:
if self.state_manager._context is None:
self.state_manager.set_context(CacheContext(name="inference"))

state: TaylorSeerState = self.state_manager.get_state()
state.current_step += 1
current_step = state.current_step
Expand Down
30 changes: 27 additions & 3 deletions tests/models/testing_utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ class TaylorSeerCacheTesterMixin(TaylorSeerCacheConfigMixin, CacheTesterMixin):

@torch.no_grad()
def _test_cache_inference(self):
"""Test that model can run inference with TaylorSeer cache enabled (requires cache_context)."""
"""Test that model can run inference with TaylorSeer cache enabled."""
init_dict = self.get_init_dict()
inputs_dict = self.get_dummy_inputs()
model = self.model_class(**init_dict).to(torch_device)
Expand All @@ -965,7 +965,6 @@ def _test_cache_inference(self):
config = self._get_cache_config()
model.enable_cache(config)

# TaylorSeer requires cache_context to be set for inference
with model.cache_context("taylorseer_test"):
# First pass populates the cache
_ = model(**inputs_dict, return_dict=False)[0]
Expand Down Expand Up @@ -994,7 +993,7 @@ def _test_cache_inference(self):

@torch.no_grad()
def _test_reset_stateful_cache(self):
"""Test that _reset_stateful_cache resets the TaylorSeer cache state (requires cache_context)."""
"""Test that _reset_stateful_cache resets the TaylorSeer cache state."""
init_dict = self.get_init_dict()
inputs_dict = self.get_dummy_inputs()
model = self.model_class(**init_dict).to(torch_device)
Expand Down Expand Up @@ -1030,6 +1029,31 @@ def test_taylorseer_cache_inference(self):
def test_taylorseer_cache_context_manager(self):
self._test_cache_context_manager()

@require_cache_mixin
@torch.no_grad()
def test_taylorseer_cache_inference_without_context(self):
inputs_dict = self.get_dummy_inputs()
model = self.model_class(**self.get_init_dict()).to(torch_device)
model.eval()
model.enable_cache(self._get_cache_config())

# Second pass is predicted from the first, see `TAYLORSEER_CACHE_CONFIG`.
_ = model(**inputs_dict, return_dict=False)[0]
output_without_context = model(**inputs_dict, return_dict=False)[0]

model._reset_stateful_cache()
with model.cache_context("taylorseer_test"):
_ = model(**inputs_dict, return_dict=False)[0]
output_with_context = model(**inputs_dict, return_dict=False)[0]

assert_tensors_close(
output_without_context,
output_with_context,
atol=1e-5,
rtol=0,
msg="TaylorSeer output without a cache context should match the output with one.",
)

@require_cache_mixin
def test_taylorseer_cache_reset_stateful_cache(self):
self._test_reset_stateful_cache()