2026-08-15 14:41:09 +08:00
|
|
|
# Layerwise Offloading
|
|
|
|
|
|
|
|
|
|
Layerwise, or blockwise, offloading keeps one transformer block on the
|
|
|
|
|
accelerator and prefetches the next block while the current block computes.
|
|
|
|
|
It is best suited to compute-heavy video DiTs whose block execution can hide
|
|
|
|
|
host-to-device transfers.
|
|
|
|
|
|
|
|
|
|
## Execution flow
|
|
|
|
|
|
|
|
|
|
Each block has a pre-forward and post-forward hook. Parameters are consolidated
|
|
|
|
|
in pinned host tensors and rematerialized for execution on a dedicated copy
|
|
|
|
|
stream.
|
|
|
|
|
|
|
|
|
|
| Block | Pre-forward hook | Forward | Post-forward hook |
|
|
|
|
|
| --- | --- | --- | --- |
|
|
|
|
|
| block 0 | Prefetch block 1 | Compute block 0 | Free block 0 |
|
|
|
|
|
| block 1 | Prefetch block 2 | Compute block 1 | Free block 1 |
|
|
|
|
|
| ... | ... | ... | ... |
|
|
|
|
|
| last block | Prefetch block 0 | Compute last block | Free last block |
|
|
|
|
|
|
2026-09-06 05:40:09 +08:00
|
|
|
Selected, plan-declared text-encoder blocks can use the same rank-local
|
|
|
|
|
streaming mechanism. Image encoders, VAEs, and non-block DiT modules remain
|
|
|
|
|
device resident.
|
2026-08-15 14:41:09 +08:00
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from vllm_omni import Omni
|
|
|
|
|
|
|
|
|
|
omni = Omni(
|
|
|
|
|
model="Wan-AI/Wan2.2-T2V-A14B-Diffusers",
|
2026-09-06 05:40:09 +08:00
|
|
|
diffusion_offload_config={
|
|
|
|
|
"mode": "layer",
|
|
|
|
|
"components": ["dit"],
|
|
|
|
|
},
|
2026-08-15 14:41:09 +08:00
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
vllm serve Wan-AI/Wan2.2-T2V-A14B-Diffusers \
|
2026-09-06 05:40:09 +08:00
|
|
|
--omni \
|
|
|
|
|
--diffusion-offload-config '{"mode":"layer","components":["dit"]}'
|
2026-08-15 14:41:09 +08:00
|
|
|
```
|
|
|
|
|
|
2026-09-06 05:40:09 +08:00
|
|
|
## Component selection
|
|
|
|
|
|
|
|
|
|
Add `dit`, `text_encoder`, or both to the `components` list. Omitting
|
|
|
|
|
`layer_options` uses safe rank-local defaults:
|
|
|
|
|
|
|
|
|
|
- `["dit"]` streams only DiT blocks.
|
|
|
|
|
- `["text_encoder"]` streams only declared text-encoder blocks.
|
|
|
|
|
- Listing both streams both components.
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# DiT-only layer offload
|
|
|
|
|
vllm serve /path/to/model --omni \
|
|
|
|
|
--diffusion-offload-config \
|
|
|
|
|
'{"mode":"layer","components":["dit"]}'
|
|
|
|
|
|
|
|
|
|
# Stream a model-declared text encoder while keeping the DiT resident
|
|
|
|
|
vllm serve /path/to/model --omni \
|
|
|
|
|
--diffusion-offload-config \
|
|
|
|
|
'{"mode":"layer","components":["text_encoder"]}'
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Encoder categories are resolved from `OffloadPlan.encoder_component_types`
|
|
|
|
|
first. A name-based fallback is retained for pipelines that predate
|
|
|
|
|
`OffloadPlan`.
|
|
|
|
|
|
2026-08-15 14:41:09 +08:00
|
|
|
## Model integration
|
|
|
|
|
|
|
|
|
|
Transformer classes declare containers of executable blocks:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
class WanTransformer3DModel(nn.Module):
|
|
|
|
|
_layerwise_offload_blocks_attrs = ["blocks"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Flux2Transformer2DModel(nn.Module):
|
|
|
|
|
_layerwise_offload_blocks_attrs = [
|
|
|
|
|
"transformer_blocks",
|
|
|
|
|
"single_transformer_blocks",
|
|
|
|
|
]
|
|
|
|
|
```
|
|
|
|
|
|
2026-09-06 05:40:09 +08:00
|
|
|
Auxiliary components use declarative pipeline metadata:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from vllm_omni.diffusion.offloader import OffloadPlan
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyPipeline(nn.Module):
|
|
|
|
|
_encoder_modules = ["prompt_model"]
|
|
|
|
|
_offload_plan = OffloadPlan(
|
|
|
|
|
encoder_component_types={"prompt_model": "text_encoder"},
|
|
|
|
|
encoder_block_attrs={"prompt_model": ("encoder.layers",)},
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
See the [layerwise design](../../../design/feature/offloader/layerwise_offload.md)
|
|
|
|
|
for the discovery and hook invariants. Both ordinary and distributed layerwise
|
|
|
|
|
offload consume the same `OffloadPlan` metadata.
|
2026-08-15 14:41:09 +08:00
|
|
|
|
|
|
|
|
## Limitations
|
|
|
|
|
|
2026-09-06 05:40:09 +08:00
|
|
|
- The default weight transfer is `rank-local`. Set a selected component's
|
|
|
|
|
`weight_transfer` to `allgather` to shard its host weights across a
|
|
|
|
|
compatible multi-device group; backend selection is automatic.
|
2026-08-15 14:41:09 +08:00
|
|
|
- Setup consolidates and pins block parameters, increasing cold-start time.
|
|
|
|
|
- Performance depends on block compute time and host-to-device bandwidth;
|
|
|
|
|
lightweight blocks may not hide transfers.
|