A toy residual model: at each of positions the residual is the embedding plus the writes of components; a reader head at the last position attends over all positions with scores that depend on the residuals and writes the attended mixture, which is read out as a logit difference. Patching one component at one position from the clean run into the corrupt run changes both the attention and the values, so the effect is non-linear and must be measured by rerunning the reader.
reader_metric(R, W_r, d_vec)
R is (P, d) residuals. Scores over all positions , softmax over , output , metric . Return m as a float.assemble(embed, writes)
embed is (P, d), writes is (C, P, d). Return the residuals embed + writes.sum(axis=0).patching_grid(embed_clean, writes_clean, embed_corrupt, writes_corrupt, W_r, d_vec, decimals=4)
m_clean and m_corrupt from the two assembled runs. For every component c and position p, take the corrupt writes, replace entry [c, p] with the clean one, assemble with the corrupt embedding, and compute the metric m_patched. Return a (C, P) array of normalised scores (m_patched - m_corrupt) / (m_clean - m_corrupt), rounded; if the denominator is zero return zeros.best_patch(grid)
(c, p) of the largest grid entry (first occurrence in row-major order) and its value, as a tuple (c, p, value).1; one that overshoots can exceed 1.