summaryrefslogtreecommitdiff
path: root/sdil/probes.py
diff options
context:
space:
mode:
authorYurenHao0426 <Blackhao0426@gmail.com>2026-07-22 04:01:23 -0500
committerYurenHao0426 <Blackhao0426@gmail.com>2026-07-22 04:01:23 -0500
commit3942eda789a64dbc4e213cb9ca2f376c1f334501 (patch)
tree9f3fc556b3536c18668a037d27d8c6f7c99bfbcd /sdil/probes.py
parent7eb40a516849cbc3e93540d4fdf40366f6a8df9e (diff)
baseline: add unamortized node perturbation
Diffstat (limited to 'sdil/probes.py')
-rw-r--r--sdil/probes.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/sdil/probes.py b/sdil/probes.py
index 0075bca..efa52e6 100644
--- a/sdil/probes.py
+++ b/sdil/probes.py
@@ -116,6 +116,26 @@ def fa_alignment_report(net, x, y, y_onehot):
@torch.no_grad()
+def nodepert_alignment_report(net, x, y, cfg):
+ """Alignment of the unamortized perturbation signal actually used."""
+ grads, loss = true_hidden_grads(net, x, y)
+ estimator = (core.simultaneous_node_perturbation_targets
+ if cfg.pert_mode == "simultaneous"
+ else core.node_perturbation_targets)
+ targets = estimator(net, x, y, sigma=cfg.pert_sigma, n_dirs=cfg.pert_ndirs)
+ fwd = net.forward(x)
+ cosines = []
+ q_norm = []
+ g_norm = []
+ for layer, (target, grad) in enumerate(zip(targets, grads)):
+ gain = net.act_prime(fwd["u"][layer])
+ cosines.append(_row_cos(target * gain, -grad * gain))
+ q_norm.append(target.norm(dim=1).mean().item())
+ g_norm.append(grad.norm(dim=1).mean().item())
+ return {"cos_q_negg": cosines, "q_norm": q_norm, "g_norm": g_norm, "loss": loss}
+
+
+@torch.no_grad()
def loss_decrease_ratio(net, x, y, y_onehot, cfg, step):
"""Single-step descent quality: apply one SDIL update to a scratch copy and
measure the actual loss drop on the same batch, compared to one plain-SGD