Token 1 attends equally to values 2 and 6, so the plain layer gives 4 and 40 for it. Token 0 has score 0.5 and token 1 a score of about 1. Gating the values halves token 0's value before the average, giving 3.5; gating the attention output scales each token's own result, so token 1 keeps 4 and token 0 drops from 2 to 1.
Before settling on one design, Qiu and colleagues try a sigmoid gate at five positions of a multi-head attention layer and train a model for each. Implement one causal attention layer in which the gate can be moved between those positions.
X has shape (n,dmodel). W_Q, W_K, W_V have shape (h,dmodel,dk), one projection per head, and W_O has shape (hdk,dmodel).
Without a gate (position=None) the layer is standard: for head k, Qk=XWQk, Kk=XWKk, Vk=XWVk, then
Yk=softmax(dkQk(Kk)⊤)Vk
with a causal mask (token i attends to j≤i), and O=Concat(Y1,…,Yh)WO.
A gate replaces a tensor Z by Z⊙σ(XWθ) with σ(z)=1/(1+e−z) and no bias. Row i of the score always comes from row i of X, and it multiplies row i of Z. All gates here are elementwise and head-specific.
position selects Z, with the paper's names:
"G4": the queries. "G3": the keys. "G2": the values. For these three and for "G1", W_theta has shape (dmodel,hdk) and columns kdk to (k+1)dk−1 belong to head k.
"G1": each head's attention output Yk, before concatenation.
"G5": the layer output O, after W_O. Here W_theta has shape (dmodel,dmodel).
Return O, a NumPy array of shape (n,dmodel), unrounded.
Notes
With position=None, W_theta is ignored and may be None.