MCPcopy Create free account
hub / github.com/huggingface/diffusers / _apply_layer_skip_hook

Function _apply_layer_skip_hook

src/diffusers/hooks/layer_skip.py:199–263  ·  view source on GitHub ↗
(module: torch.nn.Module, config: LayerSkipConfig, name: str | None = None)

Source from the content-addressed store, hash-verified

197
198
199def _apply_layer_skip_hook(module: torch.nn.Module, config: LayerSkipConfig, name: str | None = None) -> None:
200 name = name or _LAYER_SKIP_HOOK
201
202 if config.skip_attention and config.skip_attention_scores:
203 raise ValueError("Cannot set both `skip_attention` and `skip_attention_scores` to True. Please choose one.")
204 if not math.isclose(config.dropout, 1.0) and config.skip_attention_scores:
205 raise ValueError(
206 "Cannot set `skip_attention_scores` to True when `dropout` is not 1.0. Please set `dropout` to 1.0."
207 )
208
209 if config.fqn == "auto":
210 for identifier in _ALL_TRANSFORMER_BLOCK_IDENTIFIERS:
211 if hasattr(module, identifier):
212 config.fqn = identifier
213 break
214 else:
215 raise ValueError(
216 "Could not find a suitable identifier for the transformer blocks automatically. Please provide a valid "
217 "`fqn` (fully qualified name) that identifies a stack of transformer blocks."
218 )
219
220 transformer_blocks = _get_submodule_from_fqn(module, config.fqn)
221 if transformer_blocks is None or not isinstance(transformer_blocks, torch.nn.ModuleList):
222 raise ValueError(
223 f"Could not find {config.fqn} in the provided module, or configured `fqn` (fully qualified name) does not identify "
224 f"a `torch.nn.ModuleList`. Please provide a valid `fqn` that identifies a stack of transformer blocks."
225 )
226 if len(config.indices) == 0:
227 raise ValueError("Layer index list is empty. Please provide a non-empty list of layer indices to skip.")
228
229 blocks_found = False
230 for i, block in enumerate(transformer_blocks):
231 if i not in config.indices:
232 continue
233
234 blocks_found = True
235
236 if config.skip_attention and config.skip_ff:
237 logger.debug(f"Applying TransformerBlockSkipHook to '{config.fqn}.{i}'")
238 registry = HookRegistry.check_if_exists_or_initialize(block)
239 hook = TransformerBlockSkipHook(config.dropout)
240 registry.register_hook(hook, name)
241
242 elif config.skip_attention or config.skip_attention_scores:
243 for submodule_name, submodule in block.named_modules():
244 if isinstance(submodule, _ATTENTION_CLASSES) and not submodule.is_cross_attention:
245 logger.debug(f"Applying AttentionProcessorSkipHook to '{config.fqn}.{i}.{submodule_name}'")
246 output_fn = AttentionProcessorRegistry.get(submodule.processor.__class__).skip_processor_output_fn
247 registry = HookRegistry.check_if_exists_or_initialize(submodule)
248 hook = AttentionProcessorSkipHook(output_fn, config.skip_attention_scores, config.dropout)
249 registry.register_hook(hook, name)
250
251 if config.skip_ff:
252 for submodule_name, submodule in block.named_modules():
253 if isinstance(submodule, _FEEDFORWARD_CLASSES):
254 logger.debug(f"Applying FeedForwardSkipHook to '{config.fqn}.{i}.{submodule_name}'")
255 registry = HookRegistry.check_if_exists_or_initialize(submodule)
256 hook = FeedForwardSkipHook(config.dropout)

Callers 4

apply_layer_skipFunction · 0.85
prepare_modelsMethod · 0.85
prepare_modelsMethod · 0.85
prepare_modelsMethod · 0.85

Calls 7

_get_submodule_from_fqnFunction · 0.85
FeedForwardSkipHookClass · 0.85
register_hookMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…