Skip to content

Commit 6bf17dd

Browse files
committed
guiders
1 parent ca2b9b3 commit 6bf17dd

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed

docs/source/en/_toctree.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@
130130
title: ModularPipeline
131131
- local: modular_diffusers/components_manager
132132
title: Components Manager
133+
- local: modular_diffusers/guiders
134+
title: Guiders
133135

134136
- title: Training
135137
isExpanded: false
@@ -294,6 +296,8 @@
294296
title: States
295297
- local: api/modular_diffusers/pipeline_components
296298
title: Components and configs
299+
- local: api/modular_diffusers/guiders
300+
title: Guiders
297301
- title: Loaders
298302
sections:
299303
- local: api/loaders/ip_adapter
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Guiders
2+
3+
Guiders are components in Modular Diffusers that control how the diffusion process is guided during generation. They implement various guidance techniques to improve generation quality and control.
4+
5+
## BaseGuidance
6+
7+
[[autodoc]] diffusers.guiders.guider_utils.BaseGuidance
8+
9+
## ClassifierFreeGuidance
10+
11+
[[autodoc]] diffusers.guiders.classifier_free_guidance.ClassifierFreeGuidance
12+
13+
## ClassifierFreeZeroStarGuidance
14+
15+
[[autodoc]] diffusers.guiders.classifier_free_zero_star_guidance.ClassifierFreeZeroStarGuidance
16+
17+
## SkipLayerGuidance
18+
19+
[[autodoc]] diffusers.guiders.skip_layer_guidance.SkipLayerGuidance
20+
21+
## SmoothedEnergyGuidance
22+
23+
[[autodoc]] diffusers.guiders.smoothed_energy_guidance.SmoothedEnergyGuidance
24+
25+
## PerturbedAttentionGuidance
26+
27+
[[autodoc]] diffusers.guiders.perturbed_attention_guidance.PerturbedAttentionGuidance
28+
29+
## AdaptiveProjectedGuidance
30+
31+
[[autodoc]] diffusers.guiders.adaptive_projected_guidance.AdaptiveProjectedGuidance
32+
33+
## AutoGuidance
34+
35+
[[autodoc]] diffusers.guiders.auto_guidance.AutoGuidance
36+
37+
## TangentialClassifierFreeGuidance
38+
39+
[[autodoc]] diffusers.guiders.tangential_classifier_free_guidance.TangentialClassifierFreeGuidance
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
# Guiders
14+
15+
[Classifier-free guidance](https://huggingface.co/papers/2207.12598) steers model generation that better match a prompt and is commonly used to improve generation quality, control, and adherence to prompts. There are different types of guidance methods, and in Diffusers, they are known as *guiders*. Like blocks, it is easy to switch and use different guiders for different use cases without rewriting the pipeline.
16+
17+
This guide will show you how to switch guiders, adjust guider parameters, and load and share them to the Hub.
18+
19+
## Switching guiders
20+
21+
[`ClassifierFreeGuidance`] is the default guider and created when a pipeline is initialized with [`~ModularPipelineBlocks.init_pipeline`]. It is created by `from_config` which means it doesn't require loading specifications from a modular repository. A guider won't be listed in `modular_model_index.json`.
22+
23+
Use [`~ModularPipeline.get_component_spec`] to inspect a guider.
24+
25+
```py
26+
t2i_pipeline.get_component_spec("guider")
27+
ComponentSpec(name='guider', type_hint=<class 'diffusers.guiders.classifier_free_guidance.ClassifierFreeGuidance'>, description=None, config=FrozenDict([('guidance_scale', 7.5), ('guidance_rescale', 0.0), ('use_original_formulation', False), ('start', 0.0), ('stop', 1.0), ('_use_default_values', ['start', 'guidance_rescale', 'stop', 'use_original_formulation'])]), repo=None, subfolder=None, variant=None, revision=None, default_creation_method='from_config')
28+
```
29+
30+
Switch to a different guider by passing the new guider to [`~ModularPipeline.update_components`].
31+
32+
> [!TIP]
33+
> Changing guiders will return some text letting you know you're changing the guider type.
34+
> ```bash
35+
> ModularPipeline.update_components: adding guider with new type: PerturbedAttentionGuidance, previous type: ClassifierFreeGuidance
36+
> ```
37+
38+
```py
39+
from diffusers import LayerSkipConfig, PerturbedAttentionGuidance
40+
41+
config = LayerSkipConfig(indices=[2, 9], fqn="mid_block.attentions.0.transformer_blocks", skip_attention=False, skip_attention_scores=True, skip_ff=False)
42+
guider = PerturbedAttentionGuidance(
43+
guidance_scale=5.0, perturbed_guidance_scale=2.5, perturbed_guidance_config=config
44+
)
45+
t2i_pipeline.update_components(guider=guider)
46+
```
47+
48+
Use [`~ModularPipeline.get_component_spec`] again to verify the guider type is different.
49+
50+
```py
51+
t2i_pipeline.get_component_spec("guider")
52+
ComponentSpec(name='guider', type_hint=<class 'diffusers.guiders.perturbed_attention_guidance.PerturbedAttentionGuidance'>, description=None, config=FrozenDict([('guidance_scale', 5.0), ('perturbed_guidance_scale', 2.5), ('perturbed_guidance_start', 0.01), ('perturbed_guidance_stop', 0.2), ('perturbed_guidance_layers', None), ('perturbed_guidance_config', LayerSkipConfig(indices=[2, 9], fqn='mid_block.attentions.0.transformer_blocks', skip_attention=False, skip_attention_scores=True, skip_ff=False, dropout=1.0)), ('guidance_rescale', 0.0), ('use_original_formulation', False), ('start', 0.0), ('stop', 1.0), ('_use_default_values', ['perturbed_guidance_start', 'use_original_formulation', 'perturbed_guidance_layers', 'stop', 'start', 'guidance_rescale', 'perturbed_guidance_stop']), ('_class_name', 'PerturbedAttentionGuidance'), ('_diffusers_version', '0.35.0.dev0')]), repo=None, subfolder=None, variant=None, revision=None, default_creation_method='from_config')
53+
```
54+
55+
## Loading custom guiders
56+
57+
Guiders that are already saved on the Hub with a `modular_model_index.json` file are considered a `from_pretrained` component now instead of a `from_config` component.
58+
59+
```json
60+
{
61+
"guider": [
62+
null,
63+
null,
64+
{
65+
"repo": "YiYiXu/modular-loader-t2i-guider",
66+
"revision": null,
67+
"subfolder": "pag_guider",
68+
"type_hint": [
69+
"diffusers",
70+
"PerturbedAttentionGuidance"
71+
],
72+
"variant": null
73+
}
74+
]
75+
}
76+
```
77+
78+
The guider is only created after calling [`~ModularPipeline.load_default_components`] based on the loading specification in `modular_model_index.json`.
79+
80+
```py
81+
t2i_pipeline = t2i_blocks.init_pipeline("YiYiXu/modular-doc-guider")
82+
# not created during init
83+
assert t2i_pipeline.guider is None
84+
t2i_pipeline.load_default_components()
85+
# loaded as PAG guider
86+
t2i_pipeline.guider
87+
```
88+
89+
90+
## Changing guider parameters
91+
92+
The guider parameters can be adjusted with either the [`~ComponentSpec.create`] method or with [`~ModularPipeline.update_components`]. The example below changes the `guidance_scale` value.
93+
94+
<hfoptions id="switch">
95+
<hfoption id="create">
96+
97+
```py
98+
guider_spec = t2i_pipeline.get_component_spec("guider")
99+
guider = guider_spec.create(guidance_scale=10)
100+
t2i_pipeline.update_components(guider=guider)
101+
```
102+
103+
</hfoption>
104+
<hfoption id="update_components">
105+
106+
```py
107+
guider_spec = t2i_pipeline.get_component_spec("guider")
108+
guider_spec.config["guidance_scale"] = 10
109+
t2i_pipeline.update_components(guider=guider_spec)
110+
```
111+
112+
</hfoption>
113+
</hfoptions>
114+
115+
## Uploading custom guiders
116+
117+
Call the [`~utils.PushToHubMixin.push_to_hub`] method on a custom guider to share it to the Hub.
118+
119+
```py
120+
guider.push_to_hub("YiYiXu/modular-loader-t2i-guider", subfolder="pag_guider")
121+
```
122+
123+
To make this guider available to the pipeline, either modify the `modular_model_index.json` file or use the [`~ModularPipeline.update_components`] method.
124+
125+
<hfoptions id="upload">
126+
<hfoption id="modular_model_index.json">
127+
128+
Edit the `modular_model_index.json` file and add a loading specification for the guider by pointing to a folder containing the guider config.
129+
130+
```json
131+
{
132+
"guider": [
133+
"diffusers",
134+
"PerturbedAttentionGuidance",
135+
{
136+
"repo": "YiYiXu/modular-loader-t2i-guider",
137+
"revision": null,
138+
"subfolder": "pag_guider",
139+
"type_hint": [
140+
"diffusers",
141+
"PerturbedAttentionGuidance"
142+
],
143+
"variant": null
144+
}
145+
],
146+
```
147+
148+
</hfoption>
149+
<hfoption id="update_components">
150+
151+
Change the [`~ComponentSpec.default_creation_method`] to `"from_pretrained"` and use [`~ModularPipeline.update_components`] to update the guider and component specifications as well as the pipeline config.
152+
153+
> [!TIP]
154+
> Changing the creation method will return some text letting you know you're changing the creation type to `from_pretrained`.
155+
> ```bash
156+
> ModularPipeline.update_components: changing the default_creation_method of guider from from_config to from_pretrained.
157+
> ```
158+
159+
```py
160+
guider_spec = t2i_pipeline.get_component_spec("guider")
161+
guider_spec.default_creation_method="from_pretrained"
162+
guider_spec.repo="YiYiXu/modular-loader-t2i-guider"
163+
guider_spec.subfolder="pag_guider"
164+
pag_guider = guider_spec.load()
165+
t2i_pipeline.update_components(guider=pag_guider)
166+
```
167+
168+
To make it the default guider for a pipeline, call [`~utils.PushToHubMixin.push_to_hub`]. This is an optional step and not necessary if you only want to experiment locally.
169+
170+
```py
171+
t2i_pipeline.push_to_hub("YiYiXu/modular-doc-guider")
172+
```
173+
174+
</hfoption>
175+
</hfoptions>

0 commit comments

Comments
 (0)