Skip to content

feat: optimize cfg special cases with guidance schdeule - #2033

Open
stduhpf wants to merge 2 commits into
leejet:masterfrom
stduhpf:guidance-schedule
Open

stduhpf wants to merge 2 commits into
leejet:masterfrom
stduhpf:guidance-schedule

Conversation

@stduhpf

@stduhpf stduhpf commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Avoid running unnecessary diffusion model forward passes when using guidance schedule with scale == 1 (or rather scale == img_cfg)

Related Issue / Discussion

Additional Information

Checklist

@leejet leejet left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The CFG simplification is valid, but skipping these branches also skips work required by other features:

  1. Preserve APG behavior
    With APG momentum enabled, removing the unconditional prediction at scale 1 makes AdaptiveProjectedGuidance::forward() return before updating its momentum buffer. Subsequent guided steps therefore produce different results. Removing uncond in three-condition APG also changes the output formula after projection/clipping. Please restrict this optimization to plain CFG until these cases are handled.

  2. Keep the ControlNet computation needed by img_uncond
    When text_cfg == img_cfg != 1, skipping the uncond branch also skips computing negative-prompt controls. The remaining img_uncond pass then receives the positive-prompt controls instead. Its required ControlNet computation must remain even when the unconditional diffusion pass is skipped.

  3. Invalidate caches for skipped conditions
    With a schedule such as 2 → 1 → 2, the positive cache can be refreshed while the negative cache remains unchanged. When guidance resumes, EasyCache/UCache can reuse the older negative residual based on the newer positive cache state. Please invalidate the skipped condition’s cache or force a refresh when it becomes active again.

@stduhpf

stduhpf commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

Like this?

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.

2 participants