r""" Enable caching techniques on the model. Args: config (`PyramidAttentionBroadcastConfig | FasterCacheConfig | FirstBlockCacheConfig | TextKVCacheConfig`): The configuration for applying the caching technique. Currently supported caching techniques are
(self, config)
| 37 | return self._cache_config is not None |
| 38 | |
| 39 | def enable_cache(self, config) -> None: |
| 40 | r""" |
| 41 | Enable caching techniques on the model. |
| 42 | |
| 43 | Args: |
| 44 | config (`PyramidAttentionBroadcastConfig | FasterCacheConfig | FirstBlockCacheConfig | TextKVCacheConfig`): |
| 45 | The configuration for applying the caching technique. Currently supported caching techniques are: |
| 46 | - [`~hooks.PyramidAttentionBroadcastConfig`] |
| 47 | - [`~hooks.FasterCacheConfig`] |
| 48 | - [`~hooks.FirstBlockCacheConfig`] |
| 49 | - [`~hooks.TextKVCacheConfig`] |
| 50 | |
| 51 | Example: |
| 52 | |
| 53 | ```python |
| 54 | >>> import torch |
| 55 | >>> from diffusers import CogVideoXPipeline, PyramidAttentionBroadcastConfig |
| 56 | |
| 57 | >>> pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-5b", torch_dtype=torch.bfloat16) |
| 58 | >>> pipe.to("cuda") |
| 59 | |
| 60 | >>> config = PyramidAttentionBroadcastConfig( |
| 61 | ... spatial_attention_block_skip_range=2, |
| 62 | ... spatial_attention_timestep_skip_range=(100, 800), |
| 63 | ... current_timestep_callback=lambda: pipe.current_timestep, |
| 64 | ... ) |
| 65 | >>> pipe.transformer.enable_cache(config) |
| 66 | ``` |
| 67 | """ |
| 68 | |
| 69 | from ..hooks import ( |
| 70 | FasterCacheConfig, |
| 71 | FirstBlockCacheConfig, |
| 72 | MagCacheConfig, |
| 73 | PyramidAttentionBroadcastConfig, |
| 74 | TaylorSeerCacheConfig, |
| 75 | TextKVCacheConfig, |
| 76 | apply_faster_cache, |
| 77 | apply_first_block_cache, |
| 78 | apply_mag_cache, |
| 79 | apply_pyramid_attention_broadcast, |
| 80 | apply_taylorseer_cache, |
| 81 | apply_text_kv_cache, |
| 82 | ) |
| 83 | |
| 84 | if self.is_cache_enabled: |
| 85 | raise ValueError( |
| 86 | f"Caching has already been enabled with {type(self._cache_config)}. To apply a new caching technique, please disable the existing one first." |
| 87 | ) |
| 88 | |
| 89 | if isinstance(config, FasterCacheConfig): |
| 90 | apply_faster_cache(self, config) |
| 91 | elif isinstance(config, FirstBlockCacheConfig): |
| 92 | apply_first_block_cache(self, config) |
| 93 | elif isinstance(config, MagCacheConfig): |
| 94 | apply_mag_cache(self, config) |
| 95 | elif isinstance(config, TextKVCacheConfig): |
| 96 | apply_text_kv_cache(self, config) |