1+ """
2+ Flux Pipeline Caching Adapter.
3+
4+ This module provides caching adapters specifically for Flux diffusion pipelines.
5+ It implements both transformer-level and pipeline-level caching integration,
6+ enabling efficient inference through advanced first-block caching strategies.
7+
8+ The module supports both single and double first-block caching for Flux models,
9+ with automatic context management to ensure proper cache lifecycle during
10+ inference.
11+
12+ Key Functions:
13+ apply_cache_on_transformer: Apply caching directly to a FluxTransformer2DModel
14+ apply_cache_on_pipe: Apply caching to a complete Flux pipeline
15+
16+ Caching Features:
17+ - Single first-block caching: Caches the first transformer block only
18+ - Double first-block caching: Caches both multi-head and single-head attention blocks
19+ - Dynamic threshold adjustment: Automatically adjusts similarity thresholds
20+ - Context management: Ensures proper cache setup and cleanup
21+ - Shallow patching: Optional lightweight patching for testing
22+
23+ Example:
24+ Apply caching to a Flux transformer::
25+
26+ from diffusers import FluxTransformer2DModel
27+ from nunchaku.caching.diffusers_adapters.flux import apply_cache_on_transformer
28+
29+ transformer = FluxTransformer2DModel.from_pretrained("model_name")
30+ cached_transformer = apply_cache_on_transformer(
31+ transformer,
32+ use_double_fb_cache=True,
33+ residual_diff_threshold_multi=0.12,
34+ residual_diff_threshold_single=0.09
35+ )
36+
37+ Apply caching to a complete pipeline::
38+
39+ from diffusers import FluxPipeline
40+ from nunchaku.caching.diffusers_adapters.flux import apply_cache_on_pipe
41+
42+ pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev")
43+ cached_pipe = apply_cache_on_pipe(
44+ pipe,
45+ use_double_fb_cache=True,
46+ residual_diff_threshold=0.12
47+ )
48+
49+ Note:
50+ The caching is applied in-place and uses mock patching to temporarily replace
51+ transformer components during inference. The original functionality is preserved
52+ when not using caching context.
53+ """
54+
155import functools
256import unittest
357
@@ -15,6 +69,51 @@ def apply_cache_on_transformer(
1569 residual_diff_threshold_multi : float | None = None ,
1670 residual_diff_threshold_single : float = 0.1 ,
1771):
72+ """
73+ Apply caching to a Flux transformer model.
74+
75+ This function modifies a FluxTransformer2DModel to use cached transformer blocks
76+ for improved inference performance. It supports both single and double first-block
77+ caching strategies with configurable similarity thresholds.
78+
79+ Args:
80+ transformer (FluxTransformer2DModel): The Flux transformer model to apply caching to
81+ use_double_fb_cache (bool, optional): Whether to use double first-block caching.
82+ If True, caches both multi-head and single-head attention blocks. Defaults to False.
83+ residual_diff_threshold (float, optional): Default similarity threshold for caching.
84+ Used for residual_diff_threshold_multi if not explicitly provided. Defaults to 0.12.
85+ residual_diff_threshold_multi (float, optional): Similarity threshold for multi-head
86+ attention blocks. If None, uses residual_diff_threshold. Defaults to None.
87+ residual_diff_threshold_single (float, optional): Similarity threshold for single-head
88+ attention blocks. Defaults to 0.1.
89+
90+ Returns:
91+ FluxTransformer2DModel: The same transformer instance with caching applied
92+
93+ Example:
94+ Basic caching setup::
95+
96+ transformer = FluxTransformer2DModel.from_pretrained("model_name")
97+ cached_transformer = apply_cache_on_transformer(
98+ transformer,
99+ use_double_fb_cache=True,
100+ residual_diff_threshold=0.12
101+ )
102+
103+ Advanced configuration::
104+
105+ cached_transformer = apply_cache_on_transformer(
106+ transformer,
107+ use_double_fb_cache=True,
108+ residual_diff_threshold_multi=0.15,
109+ residual_diff_threshold_single=0.08
110+ )
111+
112+ Note:
113+ If the transformer is already cached, the function updates the thresholds
114+ instead of reapplying caching. The caching only activates when a cache
115+ context is present.
116+ """
18117 if residual_diff_threshold_multi is None :
19118 residual_diff_threshold_multi = residual_diff_threshold
20119
@@ -60,6 +159,56 @@ def new_forward(self, *args, **kwargs):
60159
61160
62161def apply_cache_on_pipe (pipe : DiffusionPipeline , * , shallow_patch : bool = False , ** kwargs ):
162+ """
163+ Apply caching to a complete Flux diffusion pipeline.
164+
165+ This function modifies a Flux diffusion pipeline to use caching during inference.
166+ It wraps the pipeline's __call__ method to automatically create and manage cache
167+ contexts, and optionally applies transformer-level caching.
168+
169+ Args:
170+ pipe (DiffusionPipeline): The Flux diffusion pipeline to apply caching to
171+ shallow_patch (bool, optional): If True, only applies pipeline-level caching
172+ without modifying the transformer. Useful for testing. Defaults to False.
173+ **kwargs: Additional keyword arguments passed to apply_cache_on_transformer,
174+ including:
175+ - use_double_fb_cache (bool): Whether to use double first-block caching
176+ - residual_diff_threshold (float): Similarity threshold for caching
177+ - residual_diff_threshold_multi (float): Multi-head attention threshold
178+ - residual_diff_threshold_single (float): Single-head attention threshold
179+
180+ Returns:
181+ DiffusionPipeline: The same pipeline instance with caching applied
182+
183+ Example:
184+ Basic usage::
185+
186+ from diffusers import FluxPipeline
187+ pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev")
188+ cached_pipe = apply_cache_on_pipe(pipe)
189+
190+ # Use normally - caching is transparent
191+ image = cached_pipe(prompt="A beautiful landscape")
192+
193+ Advanced configuration::
194+
195+ cached_pipe = apply_cache_on_pipe(
196+ pipe,
197+ use_double_fb_cache=True,
198+ residual_diff_threshold=0.1,
199+ residual_diff_threshold_single=0.05
200+ )
201+
202+ Shallow patching for testing::
203+
204+ cached_pipe = apply_cache_on_pipe(pipe, shallow_patch=True)
205+
206+ Note:
207+ The function modifies the pipeline class's __call__ method, affecting all
208+ instances of the same pipeline class. If the pipeline is already cached,
209+ it skips the pipeline-level patching but still applies transformer caching
210+ unless shallow_patch is True.
211+ """
63212 if not getattr (pipe , "_is_cached" , False ):
64213 original_call = pipe .__class__ .__call__
65214
0 commit comments