Skip to content

Fix SAG crashes: disable_cfg1_optimization + clean uncond for degraded pass#487

Open
seti9585 wants to merge 2 commits into
Panchovix:mainfrom
seti9585:main
Open

Fix SAG crashes: disable_cfg1_optimization + clean uncond for degraded pass#487
seti9585 wants to merge 2 commits into
Panchovix:mainfrom
seti9585:main

Conversation

@seti9585

@seti9585 seti9585 commented Jun 7, 2026

Copy link
Copy Markdown

Problem

Self-Attention Guidance (SAG) fails in two distinct ways on reForge:

Issue 1 - AttributeError: 'NoneType' object has no attribute 'shape'

Occurs when uncond_denoised is None in post_cfg_function. This happens
when CFG scale is 1.0 or during the Hires Fix second pass, where the
disable_cfg1_optimization causes uncond to be skipped entirely.

Issue 2 - RuntimeError: output with shape [1, 4, H, W] doesn't match the broadcast shape [2, 4, H, W]

Occurs during SAG's internal calc_cond_batch call (the degraded forward pass).
The uncond conditioning passed from args["uncond"] may contain keys such as
mask, strength, or area whose batch dimensions do not match the
degraded_noised tensor. This causes get_area_and_mult to produce a mult
tensor with an unexpected shape (e.g. [2, 4, H, W]), which then fails the
in-place accumulation in _calc_cond_batch.

Additionally, model_options from the outer sampling pass may contain a
model_function_wrapper that forces batch=2 output even when only a single
sample is passed, compounding the shape mismatch.

Both issues are reproducible on reForge with SDXL models, LoRAs, and
various standard extensions and samplers active.

Fixes #140

Changes

extensions-builtin/sd_forge_sag/scripts/forge_sag.py

Added unet.model_options["disable_cfg1_optimization"] = True after patching.
This ensures uncond is always computed when SAG is enabled, preventing the
NoneType crash.

ldm_patched/contrib/nodes_sag.py

In post_cfg_function:

  • Added None guard for uncond_pred and uncond_attn to handle any
    remaining edge cases where uncond is unavailable.
  • Normalised uncond_pred and x to match cfg_result batch size before
    the degraded image computation.
  • Replaced the raw uncond dict with a minimal copy containing only
    model_conds (and hooks if present). This removes mask, strength,
    area, and other keys that carry incompatible batch dimensions.
  • Replaced the outer model_options with a minimal dict for the SAG forward
    pass, avoiding wrappers that force batch=2 output.

Notes

  • The nodes_sag.py change strips conditioning keys that are not needed for
    SAG's degraded forward pass. This means SAG guidance will not reflect
    inpainting masks or regional conditioning weights, but these are not relevant
    to the self-attention guidance signal and their presence was causing crashes.
  • Tested with SDXL models, multiple LoRAs, Hires Fix, and various standard samplers and extensions.
  • The forge_sag.py fix alone resolves Issue 1. Both files are required to
    resolve Issue 2.

@seti9585 seti9585 changed the title Fix SAG crash when uncond is None (disable_cfg1_optimization) Fix SAG crashes: disable_cfg1_optimization + clean uncond for degraded pass Jun 8, 2026
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.

[Bug]: SAG not working?

1 participant