2828 from diffusers import FluxTransformer2DModel
2929
3030 model = FluxTransformer2DModel.from_pretrained("black-forest-labs/FLUX.1-dev")
31-
31+
3232 with TeaCache(model, num_steps=50, rel_l1_thresh=0.6, skip_steps=10):
3333 # Model forward passes will use temporal caching
3434 for step in range(50):
5959def make_teacache_forward (num_steps : int = 50 , rel_l1_thresh : float = 0.6 , skip_steps : int = 0 ) -> Callable :
6060 """
6161 Create a cached forward method for Flux transformers using TeaCache.
62-
62+
6363 This factory function creates a modified forward method that implements temporal
6464 caching based on the relative L1 distance of modulated inputs. The caching
6565 decision is made by analyzing the first transformer block's modulated input
6666 and comparing it to the previous timestep.
67-
67+
6868 Args:
6969 num_steps (int, optional): Total number of inference steps. Used to determine
7070 when to reset the counter. Defaults to 50.
7171 rel_l1_thresh (float, optional): Relative L1 distance threshold for caching.
7272 Lower values mean more aggressive caching. Defaults to 0.6.
7373 skip_steps (int, optional): Number of initial steps to skip caching.
7474 Useful for allowing the model to stabilize. Defaults to 0.
75-
75+
7676 Returns:
7777 Callable: A cached forward method that can be bound to a transformer model
78-
78+
7979 Example:
8080 >>> model = FluxTransformer2DModel.from_pretrained("model_name")
8181 >>> cached_forward = make_teacache_forward(num_steps=50, rel_l1_thresh=0.6)
8282 >>> model.forward = cached_forward.__get__(model, type(model))
83-
83+
8484 Note:
8585 The rescaling function uses polynomial coefficients optimized for Flux models.
8686 The accumulated distance is reset when it exceeds the threshold or at the
8787 beginning/end of the inference sequence.
8888 """
89+
8990 def teacache_forward (
9091 self : Union [FluxTransformer2DModel , NunchakuFluxTransformer2dModel ],
9192 hidden_states : torch .Tensor ,
@@ -405,14 +406,14 @@ def custom_forward(*inputs): # type: ignore
405406class TeaCache :
406407 """
407408 Context manager for applying TeaCache temporal caching to transformer models.
408-
409+
409410 This class provides a context manager that temporarily modifies a Flux transformer
410411 model to use TeaCache temporal caching. When entering the context, the model's
411412 forward method is replaced with a cached version that tracks temporal changes
412413 and skips computation when appropriate.
413-
414+
414415 Args:
415- model (Union[FluxTransformer2DModel, NunchakuFluxTransformer2dModel]):
416+ model (Union[FluxTransformer2DModel, NunchakuFluxTransformer2dModel]):
416417 The transformer model to apply caching to
417418 num_steps (int, optional): Total number of inference steps. Defaults to 50.
418419 rel_l1_thresh (float, optional): Relative L1 distance threshold for caching.
@@ -421,32 +422,33 @@ class TeaCache:
421422 Useful for model stabilization. Defaults to 0.
422423 enabled (bool, optional): Whether caching is enabled. If False, the model
423424 behaves normally. Defaults to True.
424-
425+
425426 Attributes:
426427 model: Reference to the transformer model
427428 num_steps (int): Total number of inference steps
428429 rel_l1_thresh (float): Caching threshold
429430 skip_steps (int): Number of steps to skip caching
430431 enabled (bool): Caching enabled flag
431432 previous_model_forward: Original forward method (for restoration)
432-
433+
433434 Example:
434435 Basic usage::
435-
436+
436437 with TeaCache(model, num_steps=50, rel_l1_thresh=0.6):
437438 for step in range(50):
438439 output = model(inputs[step])
439-
440+
440441 Disabling caching conditionally::
441-
442+
442443 with TeaCache(model, enabled=use_caching):
443444 # Model will use caching only if use_caching is True
444445 output = model(inputs)
445-
446+
446447 Note:
447448 The context manager automatically restores the original forward method
448449 when exiting, ensuring the model can be used normally afterward.
449450 """
451+
450452 def __init__ (
451453 self ,
452454 model : Union [FluxTransformer2DModel , NunchakuFluxTransformer2dModel ],
@@ -465,14 +467,14 @@ def __init__(
465467 def __enter__ (self ) -> "TeaCache" :
466468 """
467469 Enter the TeaCache context and apply caching to the model.
468-
470+
469471 This method is called when entering the 'with' block. It replaces the
470472 model's forward method with a cached version and initializes the
471473 necessary state variables for tracking temporal changes.
472-
474+
473475 Returns:
474476 TeaCache: Self reference for context manager protocol
475-
477+
476478 Note:
477479 If caching is disabled (enabled=False), the model is left unchanged.
478480 """
@@ -490,16 +492,16 @@ def __enter__(self) -> "TeaCache":
490492 def __exit__ (self , exc_type : Any , exc_value : Any , traceback : Any ) -> None :
491493 """
492494 Exit the TeaCache context and restore the original model.
493-
495+
494496 This method is called when exiting the 'with' block. It restores the
495497 model's original forward method and cleans up the state variables
496498 that were added for caching.
497-
499+
498500 Args:
499501 exc_type: Exception type (if any occurred)
500- exc_value: Exception value (if any occurred)
502+ exc_value: Exception value (if any occurred)
501503 traceback: Exception traceback (if any occurred)
502-
504+
503505 Note:
504506 If caching was disabled (enabled=False), no cleanup is performed.
505507 """
0 commit comments