Skip to content

fix: UnboundLocalError for noise_pred_neg/perturbed when cfg=1.0#460

Open
Frank Greico (codeman101) wants to merge 1 commit intoLightricks:masterfrom
codeman101:fix/noise-pred-neg-unbound-cfg1
Open

fix: UnboundLocalError for noise_pred_neg/perturbed when cfg=1.0#460
Frank Greico (codeman101) wants to merge 1 commit intoLightricks:masterfrom
codeman101:fix/noise-pred-neg-unbound-cfg1

Conversation

@codeman101
Copy link
Copy Markdown

Summary

Fixes UnboundLocalError in MultimodalGuider.predict_noise() when using cfg=1.0 (distilled mode) with sampler_post_cfg_function hooks active.

Fixes #458

Bug

In predict_noise(), the variables noise_pred_neg and noise_pred_perturbed are only assigned inside conditional blocks:

  • noise_pred_neg is assigned inside if any(params.do_uncond() ...) -- skipped when cfg=1.0
  • noise_pred_perturbed is assigned inside if any(params.do_perturbed() ...) -- skipped when STG is disabled

However, both variables are unconditionally referenced later in the sampler_post_cfg_function hooks loop:

for fn in model_options.get("sampler_post_cfg_function", []):
    args = {
        ...
        "uncond_denoised": noise_pred_neg,        # <-- UnboundLocalError
        "perturbed_cond_denoised": noise_pred_perturbed,  # <-- UnboundLocalError
    }

This causes an UnboundLocalError crash when any post-CFG hook is registered (e.g. from other custom nodes) and the model runs in distilled mode (cfg=1.0).

Fix

Initialize both variables to noise_pred_pos as safe fallbacks before the conditional blocks:

noise_pred_neg = noise_pred_pos
noise_pred_perturbed = noise_pred_pos

When cfg=1.0 and no perturbation is applied, the unconditional and perturbed predictions are semantically equivalent to the positive prediction, so noise_pred_pos is the correct fallback value.

…ional blocks

When cfg=1.0 (distilled mode), do_uncond() returns False so the block
that assigns noise_pred_neg is skipped. Similarly when STG is disabled,
noise_pred_perturbed is never assigned. But both variables are
unconditionally referenced in the sampler_post_cfg_function hooks,
causing an UnboundLocalError.

Initialize both to noise_pred_pos as safe fallbacks before the
conditional blocks.

Fixes Lightricks#458
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.

UnboundLocalError: noise_pred_neg referenced before assignment when cfg=1.0 (distilled mode)

1 participant