From f654a43ac3186b010fd0ec7333021a08d53c8772 Mon Sep 17 00:00:00 2001 From: Olekssy Date: Tue, 21 Apr 2026 05:10:29 +0200 Subject: [PATCH] fix: prevent UnboundLocalError when analyzer is not initialized (#301) * fix: prevent UnboundLocalError when analyzer is not initialized Move cleanup of analyzer and residuals inside the conditional block where they are actually defined to avoid crashing when --print-residual-geometry or --plot-residuals are not used. Co-Authored-By: Claude Opus 4.7 * fix: address AI review feedback on residual cleanup --------- Co-authored-by: Claude Opus 4.7 --- src/heretic/main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/heretic/main.py b/src/heretic/main.py index ea6fc2f..8492ac6 100644 --- a/src/heretic/main.py +++ b/src/heretic/main.py @@ -443,6 +443,9 @@ def run(): if settings.plot_residuals: analyzer.plot_residuals() + + # We don't need the full residuals after computing their means and analyzing geometry. + del good_residuals, bad_residuals, analyzer else: print("* Obtaining residual mean for good prompts...") good_means = model.get_residuals_mean(good_prompts) @@ -462,8 +465,7 @@ def run(): ) refusal_directions = F.normalize(refusal_directions, p=2, dim=1) - # We don't need the residuals after computing refusal directions. - del good_residuals, bad_residuals, analyzer + # Clear cache before starting the optimization study. empty_cache() trial_index = 0