Fix SAG crashes: disable_cfg1_optimization + clean uncond for degraded pass#487
Open
seti9585 wants to merge 2 commits into
Open
Fix SAG crashes: disable_cfg1_optimization + clean uncond for degraded pass#487seti9585 wants to merge 2 commits into
seti9585 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Self-Attention Guidance (SAG) fails in two distinct ways on reForge:
Issue 1 -
AttributeError: 'NoneType' object has no attribute 'shape'Occurs when
uncond_denoisedisNoneinpost_cfg_function. This happenswhen CFG scale is 1.0 or during the Hires Fix second pass, where the
disable_cfg1_optimizationcauses 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_batchcall (the degraded forward pass).The
uncondconditioning passed fromargs["uncond"]may contain keys such asmask,strength, orareawhose batch dimensions do not match thedegraded_noisedtensor. This causesget_area_and_multto produce amulttensor with an unexpected shape (e.g.
[2, 4, H, W]), which then fails thein-place accumulation in
_calc_cond_batch.Additionally,
model_optionsfrom the outer sampling pass may contain amodel_function_wrapperthat forces batch=2 output even when only a singlesample 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.pyAdded
unet.model_options["disable_cfg1_optimization"] = Trueafter patching.This ensures uncond is always computed when SAG is enabled, preventing the
NoneTypecrash.ldm_patched/contrib/nodes_sag.pyIn
post_cfg_function:Noneguard foruncond_predanduncond_attnto handle anyremaining edge cases where uncond is unavailable.
uncond_predandxto matchcfg_resultbatch size beforethe degraded image computation.
unconddict with a minimal copy containing onlymodel_conds(andhooksif present). This removesmask,strength,area, and other keys that carry incompatible batch dimensions.model_optionswith a minimal dict for the SAG forwardpass, avoiding wrappers that force batch=2 output.
Notes
nodes_sag.pychange strips conditioning keys that are not needed forSAG'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.
forge_sag.pyfix alone resolves Issue 1. Both files are required toresolve Issue 2.