Exact activation patching costs one model run per cell. Attribution patching estimates every cell from the gradient of the metric at the corrupt run. Implement both on the toy residual model and measure how well the estimate ranks the cells.
reader_metric(R, W_r, d_vec) and assemble(embed, writes) as before.
metric_gradient(R, W_r, d_vec, eps=1e-5)
(P, d) gradient of reader_metric with respect to the residuals, by central finite differences on every entry.attribution_grid(embed_clean, writes_clean, embed_corrupt, writes_corrupt, W_r, d_vec, decimals=4)
G the gradient at the corrupt residuals, the estimate for patching component c at position p is G[p] @ (writes_clean[c, p] - writes_corrupt[c, p]). Return the (C, P) grid of estimates normalised by (m_clean - m_corrupt) (zeros if that is zero), rounded.exact_grid(...) with the same signature: the exact normalised patching grid (replace one cell of the corrupt writes with the clean one, assemble with the corrupt embedding, rerun the metric).
top_k_overlap(grid_a, grid_b, k)
k largest cells (row-major ties by index) that the two grids share, as a float.2 * P * d metric evaluations, still far fewer than a patching sweep when C is large.W_r to zero to check).