MCPcopy Index your code
hub / github.com/huggingface/diffusers / MagCacheConfig

Class MagCacheConfig

src/diffusers/hooks/mag_cache.py:84–137  ·  view source on GitHub ↗

r""" Configuration for [MagCache](https://github.com/Zehong-Ma/MagCache). Args: threshold (`float`, defaults to `0.06`): The threshold for the accumulated error. If the accumulated error is below this threshold, the block computation is skipped. A higher thre

Source from the content-addressed store, hash-verified

82
83@dataclass
84class MagCacheConfig:
85 r"""
86 Configuration for [MagCache](https://github.com/Zehong-Ma/MagCache).
87
88 Args:
89 threshold (`float`, defaults to `0.06`):
90 The threshold for the accumulated error. If the accumulated error is below this threshold, the block
91 computation is skipped. A higher threshold allows for more aggressive skipping (faster) but may degrade
92 quality.
93 max_skip_steps (`int`, defaults to `3`):
94 The maximum number of consecutive steps that can be skipped (K in the paper).
95 retention_ratio (`float`, defaults to `0.2`):
96 The fraction of initial steps during which skipping is disabled to ensure stability. For example, if
97 `num_inference_steps` is 28 and `retention_ratio` is 0.2, the first 6 steps will never be skipped.
98 num_inference_steps (`int`, defaults to `28`):
99 The number of inference steps used in the pipeline. This is required to interpolate `mag_ratios` correctly.
100 mag_ratios (`torch.Tensor`, *optional*):
101 The pre-computed magnitude ratios for the model. These are checkpoint-dependent. If not provided, you must
102 set `calibrate=True` to calculate them for your specific model. For Flux models, you can use
103 `diffusers.hooks.mag_cache.FLUX_MAG_RATIOS`.
104 calibrate (`bool`, defaults to `False`):
105 If True, enables calibration mode. In this mode, no blocks are skipped. Instead, the hook calculates the
106 magnitude ratios for the current run and logs them at the end. Use this to obtain `mag_ratios` for new
107 models or schedulers.
108 """
109
110 threshold: float = 0.06
111 max_skip_steps: int = 3
112 retention_ratio: float = 0.2
113 num_inference_steps: int = 28
114 mag_ratios: Optional[Union[torch.Tensor, List[float]]] = None
115 calibrate: bool = False
116
117 def __post_init__(self):
118 # User MUST provide ratios OR enable calibration.
119 if self.mag_ratios is None and not self.calibrate:
120 raise ValueError(
121 " `mag_ratios` must be provided for MagCache inference because these ratios are model-dependent.\n"
122 "To get them for your model:\n"
123 "1. Initialize `MagCacheConfig(calibrate=True, ...)`\n"
124 "2. Run inference on your model once.\n"
125 "3. Copy the printed ratios array and pass it to `mag_ratios` in the config.\n"
126 "For Flux models, you can import `FLUX_MAG_RATIOS` from `diffusers.hooks.mag_cache`."
127 )
128
129 if not self.calibrate and self.mag_ratios is not None:
130 if not torch.is_tensor(self.mag_ratios):
131 self.mag_ratios = torch.tensor(self.mag_ratios)
132
133 if len(self.mag_ratios) != self.num_inference_steps:
134 logger.debug(
135 f"Interpolating mag_ratios from length {len(self.mag_ratios)} to {self.num_inference_steps}"
136 )
137 self.mag_ratios = nearest_interp(self.mag_ratios, self.num_inference_steps)
138
139
140class MagCacheState(BaseState):

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…