Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions scripts/run_causal_intervention.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,20 @@ def hook(module, args, output):
)
state["printed"] = True

if torch.any(rho_tokens_after > target_rho_tensor + 1e-6):
# The rho proxy is approximate, so keep a small numerical margin.
rho_margin = 1e-3
max_rho_after = float(rho_tokens_after.max().item())
if max_rho_after > float(target_rho_tensor.item()) + rho_margin:
raise RuntimeError(
f"Layer {layer_idx} clamp failed: "
f"rho_after={rho_after['max']:.6f} > target={float(target_rho_tensor.item()):.6f}"
f"rho_after={max_rho_after:.6f} > "
f"target={float(target_rho_tensor.item()):.6f} "
f"(margin={rho_margin:.1e})"
)
if debug and not state["printed"] and max_rho_after > float(target_rho_tensor.item()):
print(
f"[layer {layer_idx}] rho_after overshoot "
f"{max_rho_after:.6f} within margin={rho_margin:.1e}"
)

dt_raw_clamped = dt_raw_work.clone()
Expand Down
Loading