A head in a later layer composes with a head in an earlier layer when its query, key or value side reads the subspace the earlier head's OV circuit wrote. Measure this from the weights.
composition_score(W_later, W_OV_earlier, decimals=4)
(d, d). Return , rounded; 0.0 if either norm is zero.composition_matrix(ov_layer1, layer2, kind, decimals=4)
ov_layer1 is a list of (d, d) OV circuits of the earlier layer's heads. layer2 is a list of dicts with "QK" and "OV" (d, d) matrices for the later layer's heads. kind is "Q", "K" or "V":
"Q": the later head reads through its query side, use W_later = QK.T (so the product is QK.T @ OV1),"K": use W_later = QK (product QK @ OV1),"V": use W_later = OV2 (product OV2 @ OV1).(H1, H2) array of rounded scores, entry (i, j) for earlier head i and later head j.strongest_pairs(C, k)
k largest entries of a composition matrix as a list of (i, j, score) tuples sorted by descending score, ties by (i, j).random_baseline(rng, d, d_h, trials, decimals=4)
rng.normal matrices of shapes (d, d_h), (d, d_h) for V/O and Q/K, with W_O of shape (d_h, d)), over trials draws; the level a real score has to beat.np.linalg.norm(M) for a 2-D array.H1 * H2 products of d x d matrices are cheap.rng.