Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/source/en/api/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ Cache methods speedup diffusion transformers by storing and reusing intermediate
[[autodoc]] MagCacheConfig

[[autodoc]] apply_mag_cache

## SeaCacheConfig

[[autodoc]] SeaCacheConfig

[[autodoc]] apply_sea_cache
25 changes: 25 additions & 0 deletions docs/source/en/api/pipelines/cosmos3.md
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,31 @@ if result.action is not None:
</hfoption>
</hfoptions>

## SeaCache

SeaCache is disabled by default. Cosmos 3 supports enabling it explicitly with [`SeaCacheConfig`]. SeaCache reuses
transformer residuals when the Spectral-Evolution-Aware indicator changes slowly, reducing the number of full
transformer executions. Enable it on the transformer with scheduler metadata callbacks from the pipeline:

```python
from diffusers import SeaCacheConfig

pipe.transformer.enable_cache(
SeaCacheConfig(
threshold=0.2,
max_consecutive_cached=2,
current_step_callback=lambda: pipe.current_step_index,
current_sigma_callback=lambda: pipe.current_sigma,
num_inference_steps_callback=lambda: pipe.num_timesteps,
Comment on lines +676 to +678

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to provide actual values here? Or maybe even just specify what pipe is supposed to be?

)
)
```

The same model-level API works with [`Cosmos3OmniPipeline`], [`Cosmos3OmniModularPipeline`], and
[`Cosmos3DistilledModularPipeline`]. SeaCache is approximate and can change generated outputs. Disable it with
`pipe.transformer.disable_cache()` when you need every denoising step to execute the full transformer. Cache state is
reset after each pipeline call, and conditional and unconditional guidance branches keep independent histories.

## Context parallelism

For long videos or high resolutions, a single forward pass can exceed the memory and latency budget of one GPU. Cosmos 3 supports **context parallelism (CP)** to shard the sequence dimension across multiple GPUs, splitting the attention computation so each device holds only a slice of the tokens.
Expand Down
28 changes: 28 additions & 0 deletions docs/source/en/optimization/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ config = FasterCacheConfig(
pipeline.transformer.enable_cache(config)
```

## SeaCache

[SeaCache](https://huggingface.co/papers/2602.18993) compares Spectral-Evolution-Aware (SEA) indicators between
successive denoising steps. When the accumulated indicator change remains below a threshold, it skips the expensive
transformer block stack and predicts its output from cached residuals.

SeaCache is disabled by default. Enable it on the transformer and provide callbacks for the active scheduler step,
sigma, and number of inference steps:

```python
from diffusers import Cosmos3OmniPipeline, SeaCacheConfig

pipe = Cosmos3OmniPipeline.from_pretrained("nvidia/Cosmos3-Nano")
pipe.transformer.enable_cache(
SeaCacheConfig(
threshold=0.2,
max_consecutive_cached=2,
current_step_callback=lambda: pipe.current_step_index,
current_sigma_callback=lambda: pipe.current_sigma,
num_inference_steps_callback=lambda: pipe.num_timesteps,
)
)
```

This model-level API works with [`Cosmos3OmniPipeline`], [`Cosmos3OmniModularPipeline`], and
[`Cosmos3DistilledModularPipeline`]. SeaCache is an approximate optimization and may change generated outputs. Call
`pipe.transformer.disable_cache()` when you need every denoising step to execute the full transformer.
Comment on lines +95 to +97

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for the note! From a quick skim of the paper, it doesn't look like it needs to be Cosmos3 specific no?


## FirstBlockCache

[FirstBlock Cache](https://huggingface.co/docs/diffusers/main/en/api/cache#diffusers.FirstBlockCacheConfig) checks how much the early layers of the denoiser changes from one timestep to the next. If the change is small, the model skips the expensive later layers and reuses the previous output.
Expand Down
4 changes: 4 additions & 0 deletions src/diffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
"LayerSkipConfig",
"MagCacheConfig",
"PyramidAttentionBroadcastConfig",
"SeaCacheConfig",
"SmoothedEnergyGuidanceConfig",
"TaylorSeerCacheConfig",
"TextKVCacheConfig",
Expand All @@ -212,6 +213,7 @@
"apply_layer_skip",
"apply_mag_cache",
"apply_pyramid_attention_broadcast",
"apply_sea_cache",
"apply_taylorseer_cache",
"apply_text_kv_cache",
]
Expand Down Expand Up @@ -1081,6 +1083,7 @@
LayerSkipConfig,
MagCacheConfig,
PyramidAttentionBroadcastConfig,
SeaCacheConfig,
SmoothedEnergyGuidanceConfig,
TaylorSeerCacheConfig,
TextKVCacheConfig,
Expand All @@ -1089,6 +1092,7 @@
apply_layer_skip,
apply_mag_cache,
apply_pyramid_attention_broadcast,
apply_sea_cache,
apply_taylorseer_cache,
apply_text_kv_cache,
)
Expand Down
1 change: 1 addition & 0 deletions src/diffusers/hooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .layerwise_casting import apply_layerwise_casting, apply_layerwise_casting_hook
from .mag_cache import MagCacheConfig, apply_mag_cache
from .pyramid_attention_broadcast import PyramidAttentionBroadcastConfig, apply_pyramid_attention_broadcast
from .sea_cache import SeaCacheConfig, apply_sea_cache
from .smoothed_energy_guidance_utils import SmoothedEnergyGuidanceConfig
from .taylorseer_cache import TaylorSeerCacheConfig, apply_taylorseer_cache
from .tensor_parallel import apply_tensor_parallel
Expand Down
15 changes: 15 additions & 0 deletions src/diffusers/hooks/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class TransformerBlockMetadata:
return_hidden_states_index: int = None
return_encoder_hidden_states_index: int = None
hidden_states_argument_name: str = "hidden_states"
encoder_hidden_states_argument_name: str = "encoder_hidden_states"
hidden_states_norm_module_name: str = None

_cls: Type = None
_cached_parameter_indices: dict[str, int] = None
Expand Down Expand Up @@ -174,6 +176,7 @@ def _register_transformer_blocks_metadata():
from ..models.transformers.cogvideox_transformer_3d import CogVideoXBlock
from ..models.transformers.transformer_bria import BriaTransformerBlock
from ..models.transformers.transformer_cogview4 import CogView4TransformerBlock
from ..models.transformers.transformer_cosmos3 import Cosmos3VLTextMoTDecoderLayer
from ..models.transformers.transformer_flux import FluxSingleTransformerBlock, FluxTransformerBlock
from ..models.transformers.transformer_hunyuan_video import (
HunyuanVideoSingleTransformerBlock,
Expand Down Expand Up @@ -230,6 +233,18 @@ def _register_transformer_blocks_metadata():
),
)

# Cosmos 3
TransformerBlockRegistry.register(
model_class=Cosmos3VLTextMoTDecoderLayer,
metadata=TransformerBlockMetadata(
return_hidden_states_index=1,
return_encoder_hidden_states_index=0,
hidden_states_argument_name="gen_seq",
encoder_hidden_states_argument_name="und_seq",
hidden_states_norm_module_name="input_layernorm_moe_gen",
),
)

# Flux
TransformerBlockRegistry.register(
model_class=FluxTransformerBlock,
Expand Down
Loading
Loading