Skip to content

Commit f782ca1

Browse files
authored
[docs] Callbacks (huggingface#5735)
* updates * feedback
1 parent 80e78d2 commit f782ca1

22 files changed

+42
-37
lines changed

docs/source/en/_toctree.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@
7878
title: Kandinsky
7979
- local: using-diffusers/controlnet
8080
title: ControlNet
81-
- local: using-diffusers/callback
82-
title: Callback
8381
- local: using-diffusers/shap-e
8482
title: Shap-E
8583
- local: using-diffusers/diffedit
8684
title: DiffEdit
8785
- local: using-diffusers/distilled_sd
8886
title: Distilled Stable Diffusion inference
87+
- local: using-diffusers/callback
88+
title: Pipeline callbacks
8989
- local: using-diffusers/reproducibility
9090
title: Create reproducible pipelines
9191
- local: using-diffusers/custom_pipeline_examples

docs/source/en/using-diffusers/callback.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o
1010
specific language governing permissions and limitations under the License.
1111
-->
1212

13-
# Using callback
13+
# Pipeline callbacks
1414

15-
[[open-in-colab]]
15+
The denoising loop of a pipeline can be modified with custom defined functions using the `callback_on_step_end` parameter. This can be really useful for *dynamically* adjusting certain pipeline attributes, or modifying tensor variables. The flexibility of callbacks opens up some interesting use-cases such as changing the prompt embeddings at each timestep, assigning different weights to the prompt embeddings, and editing the guidance scale.
1616

17-
Most 🤗 Diffusers pipelines now accept a `callback_on_step_end` argument that allows you to change the default behavior of denoising loop with custom defined functions. Here is an example of a callback function we can write to disable classifier-free guidance after 40% of inference steps to save compute with a minimum tradeoff in performance.
17+
This guide will show you how to use the `callback_on_step_end` parameter to disable classifier-free guidance (CFG) after 40% of the inference steps to save compute with minimal cost to performance.
18+
19+
The callback function should have the following arguments:
20+
21+
* `pipe` (or the pipeline instance) provides access to useful properties such as `num_timestep` and `guidance_scale`. You can modify these properties by updating the underlying attributes. For this example, you'll disable CFG by setting `pipe._guidance_scale=0.0`.
22+
* `step_index` and `timestep` tell you where you are in the denoising loop. Use `step_index` to turn off CFG after reaching 40% of `num_timestep`.
23+
* `callback_kwargs` is a dict that contains tensor variables you can modify during the denoising loop. It only includes variables specified in the `callback_on_step_end_tensor_inputs` argument, which is passed to the pipeline's `__call__` method. Different pipelines may use different sets of variables, so please check a pipeline's `_callback_tensor_inputs` attribute for the list of variables you can modify. Some common variables include `latents` and `prompt_embeds`. For this function, change the batch size of `prompt_embeds` after setting `guidance_scale=0.0` in order for it to work properly.
24+
25+
Your callback function should look something like this:
1826

1927
```python
2028
def callback_dynamic_cfg(pipe, step_index, timestep, callback_kwargs):
@@ -29,14 +37,9 @@ def callback_dynamic_cfg(pipe, step_index, timestep, callback_kwargs):
2937
return callback_kwargs
3038
```
3139

32-
Your callback function has below arguments:
33-
* `pipe` is the pipeline instance, which provides access to useful properties such as `num_timestep` and `guidance_scale`. You can modify these properties by updating the underlying attributes. In this example, we disable CFG by setting `pipe._guidance_scale` to be `0`.
34-
* `step_index` and `timestep` tell you where you are in the denoising loop. In our example, we use `step_index` to decide when to turn off CFG.
35-
* `callback_kwargs` is a dict that contains tensor variables you can modify during the denoising loop. It only includes variables specified in the `callback_on_step_end_tensor_inputs` argument passed to the pipeline's `__call__` method. Different pipelines may use different sets of variables so please check the pipeline class's `_callback_tensor_inputs` attribute for the list of variables that you can modify. Common variables include `latents` and `prompt_embeds`. In our example, we need to adjust the batch size of `prompt_embeds` after setting `guidance_scale` to be `0` in order for it to work properly.
40+
Now, you can pass the callback function to the `callback_on_step_end` parameter and the `prompt_embeds` to `callback_on_step_end_tensor_inputs`.
3641

37-
You can pass the callback function as `callback_on_step_end` argument to the pipeline along with `callback_on_step_end_tensor_inputs`.
38-
39-
```python
42+
```py
4043
import torch
4144
from diffusers import StableDiffusionPipeline
4245

@@ -51,10 +54,12 @@ out = pipe(prompt, generator=generator, callback_on_step_end=callback_custom_cfg
5154
out.images[0].save("out_custom_cfg.png")
5255
```
5356

54-
Your callback function will be executed at the end of each denoising step and modify pipeline attributes and tensor variables for the next denoising step. We successfully added the "dynamic CFG" feature to the stable diffusion pipeline without having to modify the code at all.
57+
The callback function is executed at the end of each denoising step, and modifies the pipeline attributes and tensor variables for the next denoising step.
58+
59+
With callbacks, you can implement features such as dynamic CFG without having to modify the underlying code at all!
5560

5661
<Tip>
5762

58-
Currently we only support `callback_on_step_end`. If you have a solid use case and require a callback function with a different execution point, please open a [Feature Request](https://github.com/huggingface/diffusers/issues/new?assignees=&labels=&projects=&template=feature_request.md&title=) so we can add it!
63+
🤗 Diffusers currently only supports `callback_on_step_end`, but feel free to open a [feature request](https://github.com/huggingface/diffusers/issues/new/choose) if you have a cool use-case and require a callback function with a different execution point!
5964

6065
</Tip>

src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ def __call__(
728728
callback_on_step_end_tensor_inputs (`List`, *optional*):
729729
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
730730
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
731-
`._callback_tensor_inputs` attribute of your pipeine class.
731+
`._callback_tensor_inputs` attribute of your pipeline class.
732732
733733
Examples:
734734

src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def __call__(
780780
callback_on_step_end_tensor_inputs (`List`, *optional*):
781781
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
782782
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
783-
`._callback_tensor_inputs` attribute of your pipeine class.
783+
`._callback_tensor_inputs` attribute of your pipeline class.
784784
Examples:
785785
786786
Returns:

src/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def __call__(
181181
callback_on_step_end_tensor_inputs (`List`, *optional*):
182182
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
183183
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
184-
`._callback_tensor_inputs` attribute of your pipeine class.
184+
`._callback_tensor_inputs` attribute of your pipeline class.
185185
186186
Examples:
187187

src/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def __call__(
283283
callback_on_step_end_tensor_inputs (`List`, *optional*):
284284
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
285285
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
286-
`._callback_tensor_inputs` attribute of your pipeine class.
286+
`._callback_tensor_inputs` attribute of your pipeline class.
287287
288288
Examples:
289289
@@ -759,7 +759,7 @@ def __call__(
759759
prior_callback_on_step_end_tensor_inputs (`List`, *optional*):
760760
The list of tensor inputs for the `prior_callback_on_step_end` function. The tensors specified in the
761761
list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in
762-
the `._callback_tensor_inputs` attribute of your pipeine class.
762+
the `._callback_tensor_inputs` attribute of your pipeline class.
763763
callback_on_step_end (`Callable`, *optional*):
764764
A function that calls at the end of each denoising steps during the inference. The function is called
765765
with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
@@ -768,7 +768,7 @@ def __call__(
768768
callback_on_step_end_tensor_inputs (`List`, *optional*):
769769
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
770770
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
771-
`._callback_tensor_inputs` attribute of your pipeine class.
771+
`._callback_tensor_inputs` attribute of your pipeline class.
772772
773773
774774
Examples:

src/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def __call__(
255255
callback_on_step_end_tensor_inputs (`List`, *optional*):
256256
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
257257
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
258-
`._callback_tensor_inputs` attribute of your pipeine class.
258+
`._callback_tensor_inputs` attribute of your pipeline class.
259259
260260
Examples:
261261

src/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpainting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def __call__(
362362
callback_on_step_end_tensor_inputs (`List`, *optional*):
363363
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
364364
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
365-
`._callback_tensor_inputs` attribute of your pipeine class.
365+
`._callback_tensor_inputs` attribute of your pipeline class.
366366
367367
Examples:
368368

src/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def __call__(
423423
callback_on_step_end_tensor_inputs (`List`, *optional*):
424424
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
425425
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
426-
`._callback_tensor_inputs` attribute of your pipeine class.
426+
`._callback_tensor_inputs` attribute of your pipeline class.
427427
428428
Examples:
429429

src/diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ def __call__(
659659
callback_on_step_end_tensor_inputs (`List`, *optional*):
660660
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
661661
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
662-
`._callback_tensor_inputs` attribute of your pipeine class.
662+
`._callback_tensor_inputs` attribute of your pipeline class.
663663
664664
Examples:
665665

0 commit comments

Comments
 (0)