Instead of rotating queries and keys, a model can put position straight into the attention scores: after the scaled query-key dot product, add a learned number chosen by how many positions back the key is, then apply the causal mask and softmax. Implement two versions: a T5-style bias that is one learned scalar per head and distance, and a query-dependent bias that also reads the query token's hidden state.
For query position and key position the distance is (how many positions back the key is), and a table of size is indexed with : every distance of or more shares the last entry. Keys with are future keys and get weight exactly 0.
t5_bias_attention(q, k, positions, table) -> np.ndarray
q, k: (H, T, d); positions: integers (T,), strictly increasing; table: (H, D).(H, T, T): softmax over keys of the masked .query_bias_attention(q, k, hidden, positions, u) -> np.ndarray
q, k: (T, d), a single head; hidden: (T, d_model), the hidden state of each token; u: (D, d_model), one learned vector per distance.(T, T).positions, not from row indices: with positions [0, 1, 4], the key at 1 is 3 positions back from the query at 4.