Skip to content

Commit 1c3b909

Browse files
yiyi@huggingface.coclaude
andcommitted
[docs] add modular pipeline conventions and gotchas
Create .ai/modular.md as a shared reference for modular pipeline conventions, patterns, and common mistakes β€” parallel to the existing models.md for model conventions. Consolidates content from the former modular-conversion.md skill file and adds gotchas identified from reviewing recent modular pipeline PRs (LTX #13378, SD3 #13324). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fbe8a75 commit 1c3b909

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

β€Ž.ai/AGENTS.mdβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Strive to write code as simple and explicit as possible.
3535
- Use `self.progress_bar(timesteps)` for progress tracking
3636
- Don't subclass an existing pipeline for a variant β€” DO NOT use an existing pipeline class (e.g., `FluxPipeline`) to override another pipeline (e.g., `FluxImg2ImgPipeline`) which will be a part of the core codebase (`src`)
3737

38+
### Modular Pipelines
39+
40+
- See [modular.md](modular.md) for modular pipeline conventions, patterns, and gotchas.
41+
3842
## Skills
3943

4044
Task-specific guides live in `.ai/skills/` and are loaded on demand by AI agents. Available skills include:

.ai/skills/model-integration/modular-conversion.md renamed to .ai/modular.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
# Modular Pipeline Conversion Reference
1+
# Modular pipeline conventions and rules
22

3-
## When to use
4-
5-
Modular pipelines break a monolithic `__call__` into composable blocks. Convert when:
6-
- The model supports multiple workflows (T2V, I2V, V2V, etc.)
7-
- Users need to swap guidance strategies (CFG, CFG-Zero*, PAG)
8-
- You want to share blocks across pipeline variants
3+
Shared reference for modular pipeline conventions, patterns, and gotchas.
94

105
## File structure
116

@@ -14,7 +9,7 @@ src/diffusers/modular_pipelines/<model>/
149
__init__.py # Lazy imports
1510
modular_pipeline.py # Pipeline class (tiny, mostly config)
1611
encoders.py # Text encoder + image/video VAE encoder blocks
17-
before_denoise.py # Pre-denoise setup blocks
12+
before_denoise.py # Pre-denoise setup blocks (timesteps, latent prep, noise)
1813
denoise.py # The denoising loop blocks
1914
decoders.py # VAE decode block
2015
modular_blocks_<model>.py # Block assembly (AutoBlocks)
@@ -81,15 +76,21 @@ for i, t in enumerate(timesteps):
8176
latents = components.scheduler.step(noise_pred, t, latents, generator=generator)[0]
8277
```
8378

84-
## Key pattern: Chunk loops for video models
79+
## Key pattern: Denoising loop
80+
81+
All models use `LoopSequentialPipelineBlocks` for the denoising loop (iterating over timesteps):
82+
```python
83+
class MyModelDenoiseLoopWrapper(LoopSequentialPipelineBlocks):
84+
block_classes = [LoopBeforeDenoiser, LoopDenoiser, LoopAfterDenoiser]
85+
```
8586

86-
Use `LoopSequentialPipelineBlocks` for outer loop:
87+
Autoregressive video models (e.g. Helios) also use it for an outer chunk loop:
8788
```python
88-
class ChunkDenoiseStep(LoopSequentialPipelineBlocks):
89-
block_classes = [PrepareChunkStep, NoiseGenStep, DenoiseInnerStep, UpdateStep]
89+
class HeliosChunkDenoiseStep(LoopSequentialPipelineBlocks):
90+
block_classes = [ChunkHistorySlice, ChunkNoiseGen, ChunkDenoiseInner, ChunkUpdate]
9091
```
9192

92-
Note: blocks inside `LoopSequentialPipelineBlocks` receive `(components, block_state, k)` where `k` is the loop iteration index.
93+
Note: sub-blocks inside `LoopSequentialPipelineBlocks` receive `(components, block_state, i, t)` for denoise loops or `(components, block_state, k)` for chunk loops.
9394

9495
## Key pattern: Workflow selection
9596

@@ -136,6 +137,26 @@ ComponentSpec(
136137
)
137138
```
138139

140+
## Gotchas
141+
142+
1. **Importing from standard pipelines.** The modular and standard pipeline systems are parallel β€” modular blocks must not import from `diffusers.pipelines.*`. For shared utility methods (e.g. `_pack_latents`, `retrieve_timesteps`), either redefine as standalone functions or use `# Copied from diffusers.pipelines.<model>...` headers. See `wan/before_denoise.py` and `helios/before_denoise.py` for examples.
143+
144+
2. **Cross-importing between modular pipelines.** Don't import utilities from another model's modular pipeline (e.g. SD3 importing from `qwenimage.inputs`). If a utility is shared, move it to `modular_pipeline_utils.py` or copy it with a `# Copied from` header.
145+
146+
3. **Accepting `guidance_scale` as a pipeline input.** Users configure the guider separately (see [guider docs](https://huggingface.co/docs/diffusers/main/en/api/guiders)). Different guider types have different parameters; forwarding them through the pipeline doesn't scale. Don't manually set `components.guider.guidance_scale = ...` inside blocks. Same applies to computing `do_classifier_free_guidance` β€” that logic belongs in the guider.
147+
148+
4. **Accepting pre-computed outputs as inputs to skip encoding.** In standard pipelines we accept `prompt_embeds`, `negative_prompt_embeds`, `image_latents`, etc. so users can skip encoding steps. In modular pipelines this is unnecessary β€” users just pop out the encoder block and run it separately. Encoder blocks should only accept raw inputs (`prompt`, `image`, etc.).
149+
150+
5. **VAE encoding inside prepare-latents.** Image encoding should be its own block in `encoders.py` (e.g. `MyModelVaeEncoderStep`). The prepare-latents block should accept `image_latents`, not raw images. This lets users run encoding standalone. See `WanVaeEncoderStep` for reference.
151+
152+
6. **Instantiating components inline.** If a class like `VideoProcessor` is needed, register it as a `ComponentSpec` and access via `components.video_processor`. Don't create new instances inside block `__call__`.
153+
154+
7. **Deeply nested block structure.** Prefer flat sequences over nesting Auto blocks inside Sequential blocks inside Auto blocks. Put the `Auto` selection at the top level and make each workflow variant a flat `InsertableDict` of leaf blocks. See `flux2/modular_blocks_flux2_klein.py` for the pattern.
155+
156+
8. **Using `InputParam.template()` / `OutputParam.template()` when semantics don't match.** Templates carry predefined descriptions β€” e.g. the `"latents"` output template means "Denoised latents". Don't use it for initial noisy latents from a prepare-latents step. Use a plain `InputParam(...)` / `OutputParam(...)` with an accurate description instead.
157+
158+
9. **Test model paths pointing to contributor repos.** Tiny test models must live under `hf-internal-testing/`, not personal repos like `username/tiny-model`. Move the model before merge.
159+
139160
## Conversion checklist
140161

141162
- [ ] Read original pipeline's `__call__` end-to-end, map stages

β€Ž.ai/review-rules.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Review-specific rules for Claude. Focus on correctness β€” style is handled by r
55
Before reviewing, read and apply the guidelines in:
66
- [AGENTS.md](AGENTS.md) β€” coding style, copied code
77
- [models.md](models.md) β€” model conventions, attention pattern, implementation rules, dependencies, gotchas
8+
- [modular.md](modular.md) β€” modular pipeline conventions, patterns, common mistakes
89
- [skills/parity-testing/SKILL.md](skills/parity-testing/SKILL.md) β€” testing rules, comparison utilities
910
- [skills/parity-testing/pitfalls.md](skills/parity-testing/pitfalls.md) β€” known pitfalls (dtype mismatches, config assumptions, etc.)
1011

β€Ž.ai/skills/model-integration/SKILL.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ See [../../models.md](../../models.md) for the attention pattern, implementation
8282

8383
## Modular Pipeline Conversion
8484

85-
See [modular-conversion.md](modular-conversion.md) for the full guide on converting standard pipelines to modular format, including block types, build order, guider abstraction, and conversion checklist.
85+
See [modular.md](../../modular.md) for the full guide on modular pipeline conventions, block types, build order, guider abstraction, gotchas, and conversion checklist.
8686

8787
---
8888

0 commit comments

Comments
Β (0)