Diffusers 文档
管道回调
并获得增强的文档体验
开始使用
管道回调
可以使用自定义函数通过callback_on_step_end
参数修改管道的去噪循环。回调函数在每个步骤结束时执行,并修改管道属性和变量以用于下一个步骤。这对于*动态*调整某些管道属性或修改张量变量非常有用。这种多功能性允许有趣的用例,例如在每个时间步改变提示嵌入,为提示嵌入分配不同的权重,以及编辑引导比例。通过回调,您可以在不修改底层代码的情况下实现新功能!
🤗 Diffusers 目前只支持 callback_on_step_end
,但如果您有很棒的用例并且需要一个具有不同执行点的回调函数,请随时打开功能请求!
本指南将通过您可以实现的一些功能来演示回调的工作原理。
官方回调
我们提供了一系列回调,您可以将其插入现有管道并修改去噪循环。这是当前官方回调列表:
SDCFGCutoffCallback
:在一定步数后禁用所有 SD 1.5 管道的 CFG,包括文本到图像、图像到图像、图像修复和 ControlNet。SDXLCFGCutoffCallback
:在一定步数后禁用所有 SDXL 管道的 CFG,包括文本到图像、图像到图像、图像修复和 ControlNet。IPAdapterScaleCutoffCallback
:在一定步数后禁用所有支持 IP-Adapter 的管道的 IP Adapter。
要设置回调,您需要指定回调生效的去噪步数。您可以通过以下两个参数之一来完成此操作:
cutoff_step_ratio
:浮点数,表示步数比例。cutoff_step_index
:整数,表示确切的步数。
import torch
from diffusers import DPMSolverMultistepScheduler, StableDiffusionXLPipeline
from diffusers.callbacks import SDXLCFGCutoffCallback
callback = SDXLCFGCutoffCallback(cutoff_step_ratio=0.4)
# can also be used with cutoff_step_index
# callback = SDXLCFGCutoffCallback(cutoff_step_ratio=None, cutoff_step_index=10)
pipeline = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
variant="fp16",
).to("cuda")
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config, use_karras_sigmas=True)
prompt = "a sports car at the road, best quality, high quality, high detail, 8k resolution"
generator = torch.Generator(device="cpu").manual_seed(2628670641)
out = pipeline(
prompt=prompt,
negative_prompt="",
guidance_scale=6.5,
num_inference_steps=25,
generator=generator,
callback_on_step_end=callback,
)
out.images[0].save("official_callback.png")


动态分类器无关引导
动态分类器无关引导(CFG)是一种功能,允许您在一定数量的推理步骤后禁用 CFG,这有助于您节省计算量,同时对性能影响最小。此回调函数应具有以下参数:
pipeline
(或管道实例)提供对重要属性的访问,例如num_timesteps
和guidance_scale
。您可以通过更新底层属性来修改这些属性。对于此示例,您将通过设置pipeline._guidance_scale=0.0
来禁用 CFG。step_index
和timestep
告诉您在去噪循环中的位置。使用step_index
在达到num_timesteps
的 40% 后关闭 CFG。callback_kwargs
是一个字典,其中包含您可以在去噪循环期间修改的张量变量。它只包含在callback_on_step_end_tensor_inputs
参数中指定的变量,该参数传递给管道的__call__
方法。不同的管道可能使用不同的变量集,因此请检查管道的_callback_tensor_inputs
属性以获取您可以修改的变量列表。一些常见的变量包括latents
和prompt_embeds
。对于此函数,在设置guidance_scale=0.0
后更改prompt_embeds
的批次大小,以使其正常工作。
您的回调函数应该如下所示:
def callback_dynamic_cfg(pipe, step_index, timestep, callback_kwargs):
# adjust the batch_size of prompt_embeds according to guidance_scale
if step_index == int(pipeline.num_timesteps * 0.4):
prompt_embeds = callback_kwargs["prompt_embeds"]
prompt_embeds = prompt_embeds.chunk(2)[-1]
# update guidance_scale and prompt_embeds
pipeline._guidance_scale = 0.0
callback_kwargs["prompt_embeds"] = prompt_embeds
return callback_kwargs
现在,您可以将回调函数传递给 callback_on_step_end
参数,并将 prompt_embeds
传递给 callback_on_step_end_tensor_inputs
。
import torch
from diffusers import StableDiffusionPipeline
pipeline = StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16)
pipeline = pipeline.to("cuda")
prompt = "a photo of an astronaut riding a horse on mars"
generator = torch.Generator(device="cuda").manual_seed(1)
out = pipeline(
prompt,
generator=generator,
callback_on_step_end=callback_dynamic_cfg,
callback_on_step_end_tensor_inputs=['prompt_embeds']
)
out.images[0].save("out_custom_cfg.png")
中断扩散过程
中断回调支持文本到图像、图像到图像和图像修复功能,适用于 StableDiffusionPipeline 和 StableDiffusionXLPipeline。
提前停止扩散过程对于构建与 Diffusers 配合使用的 UI 非常有用,因为它允许用户在对中间结果不满意时停止生成过程。您可以通过回调将其整合到管道中。
此回调函数应接受以下参数:pipeline
、i
、t
和 callback_kwargs
(此参数必须返回)。将管道的 _interrupt
属性设置为 True
以在一定步数后停止扩散过程。您也可以在回调内部自由实现自己的自定义停止逻辑。
在此示例中,即使 num_inference_steps
设置为 50,扩散过程也在 10 步后停止。
from diffusers import StableDiffusionPipeline
pipeline = StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5")
pipeline.enable_model_cpu_offload()
num_inference_steps = 50
def interrupt_callback(pipeline, i, t, callback_kwargs):
stop_idx = 10
if i == stop_idx:
pipeline._interrupt = True
return callback_kwargs
pipeline(
"A photo of a cat",
num_inference_steps=num_inference_steps,
callback_on_step_end=interrupt_callback,
)
IP 适配器截止
IP Adapter 是一个图像提示适配器,可用于扩散模型,而无需对底层模型进行任何更改。我们可以使用 IP Adapter Cutoff Callback 在一定步数后禁用 IP Adapter。要设置回调,您需要指定回调生效的去噪步数。您可以通过以下两个参数之一来完成此操作:
cutoff_step_ratio
:浮点数,表示步数比例。cutoff_step_index
:整数,表示确切的步数。
我们需要下载扩散模型并为其加载 ip_adapter,如下所示:
from diffusers import AutoPipelineForText2Image
from diffusers.utils import load_image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda")
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
pipeline.set_ip_adapter_scale(0.6)
回调的设置应如下所示:
from diffusers import AutoPipelineForText2Image
from diffusers.callbacks import IPAdapterScaleCutoffCallback
from diffusers.utils import load_image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16
).to("cuda")
pipeline.load_ip_adapter(
"h94/IP-Adapter",
subfolder="sdxl_models",
weight_name="ip-adapter_sdxl.bin"
)
pipeline.set_ip_adapter_scale(0.6)
callback = IPAdapterScaleCutoffCallback(
cutoff_step_ratio=None,
cutoff_step_index=5
)
image = load_image(
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ip_adapter_diner.png"
)
generator = torch.Generator(device="cuda").manual_seed(2628670641)
images = pipeline(
prompt="a tiger sitting in a chair drinking orange juice",
ip_adapter_image=image,
negative_prompt="deformed, ugly, wrong proportion, low res, bad anatomy, worst quality, low quality",
generator=generator,
num_inference_steps=50,
callback_on_step_end=callback,
).images
images[0].save("custom_callback_img.png")


每步生成后显示图像
此提示由asomoza贡献。
通过访问和转换每个步骤后的潜变量为图像,在每个生成步骤后显示图像。潜变量空间压缩为 128x128,因此图像也是 128x128,这对于快速预览很有用。
- 使用以下函数将 SDXL 潜变量(4 通道)转换为 RGB 张量(3 通道),如“解释 SDXL 潜变量空间”博客文章中所述。
def latents_to_rgb(latents):
weights = (
(60, -60, 25, -70),
(60, -5, 15, -50),
(60, 10, -5, -35),
)
weights_tensor = torch.t(torch.tensor(weights, dtype=latents.dtype).to(latents.device))
biases_tensor = torch.tensor((150, 140, 130), dtype=latents.dtype).to(latents.device)
rgb_tensor = torch.einsum("...lxy,lr -> ...rxy", latents, weights_tensor) + biases_tensor.unsqueeze(-1).unsqueeze(-1)
image_array = rgb_tensor.clamp(0, 255).byte().cpu().numpy().transpose(1, 2, 0)
return Image.fromarray(image_array)
- 创建一个函数来解码潜变量并将其保存为图像。
def decode_tensors(pipe, step, timestep, callback_kwargs):
latents = callback_kwargs["latents"]
image = latents_to_rgb(latents[0])
image.save(f"{step}.png")
return callback_kwargs
- 将
decode_tensors
函数传递给callback_on_step_end
参数,以便在每一步之后解码张量。您还需要在callback_on_step_end_tensor_inputs
参数中指定要修改的内容,在此示例中是潜变量。
from diffusers import AutoPipelineForText2Image
import torch
from PIL import Image
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True
).to("cuda")
image = pipeline(
prompt="A croissant shaped like a cute bear.",
negative_prompt="Deformed, ugly, bad anatomy",
callback_on_step_end=decode_tensors,
callback_on_step_end_tensor_inputs=["latents"],
).images[0]




