An attention head's behaviour factors into a QK circuit (where it looks) and an OV circuit (what it writes). Build both from the weights and compute the diagnostics that label a head as a copier.
qk_circuit(W_Q, W_K)
W_Q, W_K are (d, d_h). Return W_Q @ W_K.T, the (d, d) bilinear form such that the unscaled score is x_i @ W_QK @ x_j.ov_circuit(W_V, W_O)
W_V is (d, d_h), W_O is (d_h, d). Return W_V @ W_O, the (d, d) map a source residual passes through on its way into the stream.circuit_rank(M, tol=1e-8)
np.linalg.matrix_rank(M, tol=tol).copying_score(W_OV)
W_OV (possibly complex); return as a float rounded to 4 decimals, or 0.0 if the denominator is zero. Positive eigenvalues mean the head writes back along what it read.full_ov_circuit(W_E, W_OV, W_U)
(V, V) matrix W_E @ W_OV @ W_U: entry (s, t) is how much attending to token s raises the logit of token t. Also return the fraction of rows whose argmax lies on the diagonal, as (matrix, diag_fraction) with the fraction rounded to 4 decimals.d_h; check it on random weights.np.linalg.eigvals and take .real.W_V @ W_O equal to a positive multiple of the identity has copying score 1 and diagonal fraction 1 when W_E @ W_U is diagonally dominant.