You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -12,237 +12,77 @@ specific language governing permissions and limitations under the License.
12
12
13
13
# Pipeline callbacks
14
14
15
-
The denoising loop of a pipeline can be modified with custom defined functions using the `callback_on_step_end` parameter. The callback function is executed at the end of each step, and modifies the pipeline attributes and variables for the next step. This is really useful for *dynamically*adjusting certain pipeline attributes or modifying tensor variables. This versatility allows for interesting use cases such as changing the prompt embeddings at each timestep, assigning different weights to the prompt embeddings, and editing the guidance scale. With callbacks, you can implement new features without modifying the underlying code!
15
+
A callback is a function that modifies [`DiffusionPipeline`] behavior and it is executed at the end of a denoising step. The changes are propagated to subsequent steps in the denoising process. It is useful for adjusting pipeline attributes or tensor variablesto support new features without rewriting the underlying pipeline code.
16
16
17
-
> [!TIP]
18
-
> 🤗 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!
17
+
Diffusers provides several callbacks in the pipeline [overview](../api/pipelines/overview#callbacks).
19
18
20
-
This guide will demonstrate how callbacks work by a few features you can implement with them.
19
+
To enable a callback, configure when the callback is executed after a certain number of denoising steps with one of the following arguments.
21
20
22
-
## Official callbacks
21
+
-`cutoff_step_ratio` specifies when a callback is activated as a percentage of the total denoising steps.
22
+
-`cutoff_step_index` specifies the exact step number a callback is activated.
23
23
24
-
We provide a list of callbacks you can plug into an existing pipeline and modify the denoising loop. This is the current list of official callbacks:
24
+
The example below uses `cutoff_step_ratio=0.4`, which means the callback is activated once denoising reaches 40% of the total inference steps. [`~callbacks.SDXLCFGCutoffCallback`] disables classifier-free guidance (CFG) after a certain number of steps, which can help save compute without significantly affecting performance.
25
25
26
-
-`SDCFGCutoffCallback`: Disables the CFG after a certain number of steps for all SD 1.5 pipelines, including text-to-image, image-to-image, inpaint, and controlnet.
27
-
-`SDXLCFGCutoffCallback`: Disables the CFG after a certain number of steps for all SDXL pipelines, including text-to-image, image-to-image, inpaint, and controlnet.
28
-
-`IPAdapterScaleCutoffCallback`: Disables the IP Adapter after a certain number of steps for all pipelines supporting IP-Adapter.
26
+
Define a callback with either of the `cutoff` arguments and pass it to the `callback_on_step_end` parameter in the pipeline.
29
27
30
-
> [!TIP]
31
-
> If you want to add a new official callback, feel free to open a [feature request](https://github.com/huggingface/diffusers/issues/new/choose) or [submit a PR](https://huggingface.co/docs/diffusers/main/en/conceptual/contribution#how-to-open-a-pr).
32
-
33
-
To set up a callback, you need to specify the number of denoising steps after which the callback comes into effect. You can do so by using either one of these two arguments
34
-
35
-
-`cutoff_step_ratio`: Float number with the ratio of the steps.
36
-
-`cutoff_step_index`: Integer number with the exact number of the step.
37
-
38
-
```python
28
+
```py
39
29
import torch
40
-
41
30
from diffusers import DPMSolverMultistepScheduler, StableDiffusionXLPipeline
42
31
from diffusers.callbacks import SDXLCFGCutoffCallback
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/without_cfg_callback.png" alt="generated image of a sports car at the road" />
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/with_cfg_callback.png" alt="generated image of a sports car at the road with cfg callback" />
Dynamic classifier-free guidance (CFG) is a feature that allows you to disable CFG after a certain number of inference steps which can help you save compute with minimal cost to performance. The callback function for this should have the following arguments:
86
-
87
-
-`pipeline` (or the pipeline instance) provides access to important properties such as `num_timesteps` and `guidance_scale`. You can modify these properties by updating the underlying attributes. For this example, you'll disable CFG by setting `pipeline._guidance_scale=0.0`.
88
-
-`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_timesteps`.
89
-
-`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.
90
-
91
-
Your callback function should look something like this:
> The interruption callback is supported for text-to-image, image-to-image, and inpainting for the [StableDiffusionPipeline](../api/pipelines/stable_diffusion/overview) and [StableDiffusionXLPipeline](../api/pipelines/stable_diffusion/stable_diffusion_xl).
55
+
If you want to add a new official callback, feel free to open a [feature request](https://github.com/huggingface/diffusers/issues/new/choose) or [submit a PR](https://huggingface.co/docs/diffusers/main/en/conceptual/contribution#how-to-open-a-pr). Otherwise, you can also create your own callback as shown below.
132
56
133
-
Stopping the diffusion process early is useful when building UIs that work with Diffusers because it allows users to stop the generation process if they're unhappy with the intermediate results. You can incorporate this into your pipeline with a callback.
57
+
## Early stopping
134
58
135
-
This callback function should take the following arguments: `pipeline`, `i`, `t`, and `callback_kwargs` (this must be returned). Set the pipeline's `_interrupt` attribute to `True` to stop the diffusion process after a certain number of steps. You are also free to implement your own custom stopping logic inside the callback.
59
+
Early stopping is useful if you aren't happy with the intermediate results during generation. This callback sets a hardcoded stop point after which the pipeline terminates by setting the `_interrupt` attribute to `True`.
136
60
137
-
In this example, the diffusion process is stopped after 10 steps even though `num_inference_steps` is set to 50.
IP Adapter is an image prompt adapter that can be used for diffusion models without any changes to the underlying model. We can use the IP Adapter Cutoff Callback to disable the IP Adapter after a certain number of steps. To set up the callback, you need to specify the number of denoising steps after which the callback comes into effect. You can do so by using either one of these two arguments:
163
-
164
-
-`cutoff_step_ratio`: Float number with the ratio of the steps.
165
-
-`cutoff_step_index`: Integer number with the exact number of the step.
166
-
167
-
We need to download the diffusion model and load the ip_adapter for it as follows:
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/without_callback.png" alt="generated image of a tiger sitting in a chair drinking orange juice" />
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/with_callback2.png" alt="generated image of a tiger sitting in a chair drinking orange juice with ip adapter callback" />
> This tip was contributed by [asomoza](https://github.com/asomoza).
81
+
## Display intermediate images
242
82
243
-
Display an image after each generation step by accessing and converting the latents after each step into an image. The latent space is compressed to 128x128, so the images are also 128x128 which is useful for a quick preview.
83
+
Visualizing the intermediate images is useful for progress monitoring and assessing the quality of the generated content. This callback decodes the latent tensors at each step and converts them to images.
244
84
245
-
1. Use the function below to convert the SDXL latents (4 channels) to RGB tensors (3 channels) as explained in the [Explaining the SDXL latent space](https://huggingface.co/blog/TimothyAlexisVass/explaining-the-sdxl-latent-space)blog post.
85
+
[Convert](https://huggingface.co/blog/TimothyAlexisVass/explaining-the-sdxl-latent-space)the Stable Diffusion XL latents from latents (4 channels) to RGB tensors (3 tensors).
246
86
247
87
```py
248
88
deflatents_to_rgb(latents):
@@ -260,7 +100,7 @@ def latents_to_rgb(latents):
260
100
return Image.fromarray(image_array)
261
101
```
262
102
263
-
2. Create a function to decode and save the latents into an image.
103
+
Extract the latents and convert the first image in the batch to RGB. Save the image as a PNG file with the step number.
3. Pass the `decode_tensors` function to the `callback_on_step_end`parameter to decode the tensors after each step. You also need to specify what you want to modify in the `callback_on_step_end_tensor_inputs` parameter, which in this case are the latents.
115
+
Use the `callback_on_step_end_tensor_inputs`parameter to specify what input type to modify, which in this case, are the latents.
0 commit comments