Given the attention patterns of every head on a prompt, compute the scores interpretability tools use to label heads.
pattern_stats(A, decimals=4)
A is an (H, T, T) array of attention weights (row t of head h sums to one, causal: zero above the diagonal).H, rounded:
"prev_token": for each head the mean over queries of ,"self": the mean over all queries of the diagonal ,"first_token": the mean over queries of (attention to the first position, the usual "sink"),"entropy": the mean over queries of in nats, treating as .repeated_sequence(rng, n, vocab)
n random token ids in [0, vocab) with rng.integers (one call) and return the list of length 2n consisting of the sequence followed by itself.label_heads(stats, prev_thresh=0.5, self_thresh=0.5, first_thresh=0.5)
"previous" if its previous-token score is at least prev_thresh, else "self" if its self score is at least self_thresh, else "sink" if its first-token score is at least first_thresh, else "other".np.arange to pick the sub-diagonal entries in one indexing operation.np.where(A > 0, A * np.log(A), 0).T * T entries.