Skip to content

[SD3] CFG Cutoff fix and official callback #11890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2025
Merged

Conversation

asomoza
Copy link
Member

@asomoza asomoza commented Jul 8, 2025

What does this PR do?

To do a CFG cutoff with the SD3 pipeline we need to also expose the pooled_prompt_embeds and also change the shape of them.

This PR also adds an official callback to test this.

How to test

import torch

from diffusers import StableDiffusion3Pipeline
from diffusers.callbacks import SD3CFGCutoffCallback


torch_dtype = torch.bfloat16

pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-medium", torch_dtype=torch_dtype)
pipe.enable_model_cpu_offload()

cfg_callback = SD3CFGCutoffCallback(cutoff_step_ratio=0.3)

prompt = "A capybara in a field of flowers"

image = pipe(
    prompt,
    num_inference_steps=28,
    callback_on_step_end=cfg_callback,
    generator=torch.Generator(device="cuda").manual_seed(42),
).images[0]

image.save("capybara_cfg_cutoff.png")
No CFG cutoff CFG cutoff
capybara_no_cfg_cutoff capybara_cfg_cutoff

Who can review?

@yiyixuxu @cubiq

Copy link
Collaborator

@yiyixuxu yiyixuxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@asomoza
Copy link
Member Author

asomoza commented Jul 9, 2025

failing test is not related.

@asomoza asomoza merged commit 48a6d29 into main Jul 9, 2025
14 of 15 checks passed
@asomoza asomoza deleted the sd3_cfg_cutout_callback branch July 9, 2025 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants