From 3fb82021387507e6686d48e02b5f44db852acc31 Mon Sep 17 00:00:00 2001 From: wassname Date: Wed, 27 May 2026 22:50:26 +0000 Subject: [PATCH] fix: drop nested save_file import so the closure can find it on cache-hit The redundant `from safetensors.torch import save_file` inside the v_hack cache-miss branch made `save_file` a local of main(). Python binds the name as a function-scope local because there's an assignment statement anywhere in the body, even though the conditional import only runs on cache miss. The top-level import at line 75 was shadowed for the whole function. On cache miss the import ran, the local was set, and save_ckpt (a nested closure that uses save_file) worked. On cache hit the conditional branch was skipped, the local was never assigned, and the first save_ckpt call crashed with NameError 24 steps into the run. #54 hit this. #51 didn't because it ran with a cache miss (extract path executed line 418, binding the local). --- src/projected_grpo/train.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/projected_grpo/train.py b/src/projected_grpo/train.py index 1fe49b4..fc2d22a 100644 --- a/src/projected_grpo/train.py +++ b/src/projected_grpo/train.py @@ -415,7 +415,6 @@ def main(cfg: Config) -> int: if not v_hack_path.exists(): from .extract_vhack_grad import extract_v_hack from .pairs import PAIRS as VHACK_PAIRS - from safetensors.torch import save_file logger.info(f"v_hack cache miss at {v_hack_path}; extracting (~5min)...") model.eval() # match standalone extract: deterministic backward, no dropout v_hack_cpu_dict, raw_grads, _diag = extract_v_hack(