|
| 1 | +<!--Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +--> |
| 12 | + |
| 13 | +# Auto docstring and parameter templates |
| 14 | + |
| 15 | +Every [`~modular_pipelines.ModularPipelineBlocks`] has a `doc` property that is automatically generated from its `description`, `inputs`, `intermediate_outputs`, `expected_components`, and `expected_configs`. The auto docstring system keeps docstrings in sync with the block's actual interface. Parameter templates provide standardized descriptions for parameters that appear across many pipelines. |
| 16 | + |
| 17 | +## Auto docstring |
| 18 | + |
| 19 | +Modular pipeline blocks are composable — you can nest them, chain them in sequences, and rearrange them freely. Their docstrings follow the same pattern. When a [`~modular_pipelines.SequentialPipelineBlocks`] aggregates inputs and outputs from its sub-blocks, the documentation should update automatically without manual rewrites. |
| 20 | + |
| 21 | +The `# auto_docstring` marker generates docstrings from the block's properties. Add it above a class definition to mark the class for automatic docstring generation. |
| 22 | + |
| 23 | +```py |
| 24 | +# auto_docstring |
| 25 | +class FluxTextEncoderStep(SequentialPipelineBlocks): |
| 26 | + ... |
| 27 | +``` |
| 28 | + |
| 29 | +Run the following command to generate and insert the docstrings. |
| 30 | + |
| 31 | +```bash |
| 32 | +python utils/modular_auto_docstring.py --fix_and_overwrite |
| 33 | +``` |
| 34 | + |
| 35 | +The utility reads the block's `doc` property and inserts it as the class docstring. |
| 36 | + |
| 37 | +```py |
| 38 | +# auto_docstring |
| 39 | +class FluxTextEncoderStep(SequentialPipelineBlocks): |
| 40 | + """ |
| 41 | + Text input processing step that standardizes text embeddings for the pipeline. |
| 42 | +
|
| 43 | + Inputs: |
| 44 | + prompt_embeds (`torch.Tensor`) *required*: |
| 45 | + text embeddings used to guide the image generation. |
| 46 | + ... |
| 47 | +
|
| 48 | + Outputs: |
| 49 | + prompt_embeds (`torch.Tensor`): |
| 50 | + text embeddings used to guide the image generation. |
| 51 | + ... |
| 52 | + """ |
| 53 | +``` |
| 54 | + |
| 55 | +You can also check without overwriting, or target a specific file or directory. |
| 56 | + |
| 57 | +```bash |
| 58 | +# Check that all marked classes have up-to-date docstrings |
| 59 | +python utils/modular_auto_docstring.py |
| 60 | + |
| 61 | +# Check a specific file or directory |
| 62 | +python utils/modular_auto_docstring.py src/diffusers/modular_pipelines/flux/ |
| 63 | +``` |
| 64 | + |
| 65 | +If any marked class is missing a docstring, the check fails and lists the classes that need updating. |
| 66 | + |
| 67 | +``` |
| 68 | +Found the following # auto_docstring markers that need docstrings: |
| 69 | +- src/diffusers/modular_pipelines/flux/encoders.py: FluxTextEncoderStep at line 42 |
| 70 | +
|
| 71 | +Run `python utils/modular_auto_docstring.py --fix_and_overwrite` to fix them. |
| 72 | +``` |
| 73 | + |
| 74 | +## Parameter templates |
| 75 | + |
| 76 | +`InputParam` and `OutputParam` define a block's inputs and outputs. Create them directly or use `.template()` for standardized definitions of common parameters like `prompt`, `num_inference_steps`, or `latents`. |
| 77 | + |
| 78 | +### InputParam |
| 79 | + |
| 80 | +[`~modular_pipelines.InputParam`] describes a single input to a block. |
| 81 | + |
| 82 | +| Field | Type | Description | |
| 83 | +|---|---|---| |
| 84 | +| `name` | `str` | Name of the parameter | |
| 85 | +| `type_hint` | `Any` | Type annotation (e.g., `str`, `torch.Tensor`) | |
| 86 | +| `default` | `Any` | Default value (if not set, parameter has no default) | |
| 87 | +| `required` | `bool` | Whether the parameter is required | |
| 88 | +| `description` | `str` | Human-readable description | |
| 89 | +| `kwargs_type` | `str` | Group name for related parameters (e.g., `"denoiser_input_fields"`) | |
| 90 | +| `metadata` | `dict` | Arbitrary additional information | |
| 91 | + |
| 92 | +#### Creating InputParam directly |
| 93 | + |
| 94 | +```py |
| 95 | +from diffusers.modular_pipelines import InputParam |
| 96 | + |
| 97 | +InputParam( |
| 98 | + name="guidance_scale", |
| 99 | + type_hint=float, |
| 100 | + default=7.5, |
| 101 | + description="Scale for classifier-free guidance.", |
| 102 | +) |
| 103 | +``` |
| 104 | + |
| 105 | +#### Using a template |
| 106 | + |
| 107 | +```py |
| 108 | +InputParam.template("prompt") |
| 109 | +# Equivalent to: |
| 110 | +# InputParam(name="prompt", type_hint=str, required=True, |
| 111 | +# description="The prompt or prompts to guide image generation.") |
| 112 | +``` |
| 113 | + |
| 114 | +Templates set `name`, `type_hint`, `default`, `required`, and `description` automatically. Override any field or add context with the `note` parameter. |
| 115 | + |
| 116 | +```py |
| 117 | +# Override the default value |
| 118 | +InputParam.template("num_inference_steps", default=28) |
| 119 | + |
| 120 | +# Add a note to the description |
| 121 | +InputParam.template("prompt_embeds", note="batch-expanded") |
| 122 | +# description becomes: "text embeddings used to guide the image generation. ... (batch-expanded)" |
| 123 | +``` |
| 124 | + |
| 125 | +### OutputParam |
| 126 | + |
| 127 | +[`~modular_pipelines.OutputParam`] describes a single output from a block. |
| 128 | + |
| 129 | +| Field | Type | Description | |
| 130 | +|---|---|---| |
| 131 | +| `name` | `str` | Name of the parameter | |
| 132 | +| `type_hint` | `Any` | Type annotation | |
| 133 | +| `description` | `str` | Human-readable description | |
| 134 | +| `kwargs_type` | `str` | Group name for related parameters | |
| 135 | +| `metadata` | `dict` | Arbitrary additional information | |
| 136 | + |
| 137 | +#### Creating OutputParam directly |
| 138 | + |
| 139 | +```py |
| 140 | +from diffusers.modular_pipelines import OutputParam |
| 141 | + |
| 142 | +OutputParam(name="image_latents", type_hint=torch.Tensor, description="Encoded image latents.") |
| 143 | +``` |
| 144 | + |
| 145 | +#### Using a template |
| 146 | + |
| 147 | +```py |
| 148 | +OutputParam.template("latents") |
| 149 | + |
| 150 | +# Add a note to the description |
| 151 | +OutputParam.template("prompt_embeds", note="batch-expanded") |
| 152 | +``` |
| 153 | + |
| 154 | +## Available templates |
| 155 | + |
| 156 | +`INPUT_PARAM_TEMPLATES` and `OUTPUT_PARAM_TEMPLATES` are defined in [modular_pipeline_utils.py](https://github.com/huggingface/diffusers/blob/main/src/diffusers/modular_pipelines/modular_pipeline_utils.py). They include common parameters like `prompt`, `image`, `num_inference_steps`, `latents`, `prompt_embeds`, and more. Refer to the source for the full list of available template names. |
| 157 | + |
0 commit comments