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

Function apply_faster_cache

src/diffusers/hooks/faster_cache.py:486–574  ·  view source on GitHub ↗

r""" Applies [FasterCache](https://huggingface.co/papers/2410.19355) to a given pipeline. Args: module (`torch.nn.Module`): The pytorch module to apply FasterCache to. Typically, this should be a transformer architecture supported in Diffusers, such as `CogVi

(module: torch.nn.Module, config: FasterCacheConfig)

Source from the content-addressed store, hash-verified

484
485
486def apply_faster_cache(module: torch.nn.Module, config: FasterCacheConfig) -> None:
487 r"""
488 Applies [FasterCache](https://huggingface.co/papers/2410.19355) to a given pipeline.
489
490 Args:
491 module (`torch.nn.Module`):
492 The pytorch module to apply FasterCache to. Typically, this should be a transformer architecture supported
493 in Diffusers, such as `CogVideoXTransformer3DModel`, but external implementations may also work.
494 config (`FasterCacheConfig`):
495 The configuration to use for FasterCache.
496
497 Example:
498 ```python
499 >>> import torch
500 >>> from diffusers import CogVideoXPipeline, FasterCacheConfig, apply_faster_cache
501
502 >>> pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-5b", torch_dtype=torch.bfloat16)
503 >>> pipe.to("cuda")
504
505 >>> config = FasterCacheConfig(
506 ... spatial_attention_block_skip_range=2,
507 ... spatial_attention_timestep_skip_range=(-1, 681),
508 ... low_frequency_weight_update_timestep_range=(99, 641),
509 ... high_frequency_weight_update_timestep_range=(-1, 301),
510 ... spatial_attention_block_identifiers=["transformer_blocks"],
511 ... attention_weight_callback=lambda _: 0.3,
512 ... tensor_format="BFCHW",
513 ... )
514 >>> apply_faster_cache(pipe.transformer, config)
515 ```
516 """
517
518 logger.warning(
519 "FasterCache is a purely experimental feature and may not work as expected. Not all models support FasterCache. "
520 "The API is subject to change in future releases, with no guarantee of backward compatibility. Please report any issues at "
521 "https://github.com/huggingface/diffusers/issues."
522 )
523
524 if config.attention_weight_callback is None:
525 # If the user has not provided a weight callback, we default to 0.5 for all timesteps.
526 # In the paper, they recommend using a gradually increasing weight from 0 to 1 as the inference progresses, but
527 # this depends from model-to-model. It is required by the user to provide a weight callback if they want to
528 # use a different weight function. Defaulting to 0.5 works well in practice for most cases.
529 logger.warning(
530 "No `attention_weight_callback` provided when enabling FasterCache. Defaulting to using a weight of 0.5 for all timesteps."
531 )
532 config.attention_weight_callback = lambda _: 0.5
533
534 if config.low_frequency_weight_callback is None:
535 logger.debug(
536 "Low frequency weight callback not provided when enabling FasterCache. Defaulting to behaviour described in the paper."
537 )
538
539 def low_frequency_weight_callback(module: torch.nn.Module) -> float:
540 is_within_range = (
541 config.low_frequency_weight_update_timestep_range[0]
542 < config.current_timestep_callback()
543 < config.low_frequency_weight_update_timestep_range[1]

Used in the wild real call sites across dependent graphs

searching dependent graphs…