Deep-ML | A Non-Linearity Between the Value and Output Matrices of an Attention HeadA Non-Linearity Between the Value and Output Matrices of an Attention Head
A Non-Linearity Between the Value and Output Matrices of an Attention Head
The values are 1 and 2, so the attention outputs are 1 and 1.5. Token 0 has score 0.5 and token 1 score 0.75. The output gate scales each token's own result (0.5 and 1.125); the value gate scales the values first (0.5 and 1.5), so token 1 gets 0.5 * 0.5 + 0.5 * 1.5 = 1. With head dimension 1 the merged matrix has rank 1.
A Non-Linearity Between the Value and Output Matrices
Inside one attention head a value is multiplied by the value matrix WV, averaged with the attention weights, and multiplied by that head's part of the output matrix, WO. With nothing in between, the two matrices act as one. Qiu and colleagues explain part of the benefit of gating by this fact, and test it by placing different operations between the two matrices. Implement the head with each of those operations, and the rank of the merged matrix.
merged_rank(W_V, W_O): W_V has shape (dmodel,dk) and W_O has shape (dk,dmodel). Return the rank of the dmodel×dmodel matrix WVWO as a Python int (use numpy.linalg.matrix_rank).
S has shape (n,n): row i holds the attention weights of token i (already masked and normalised). X has shape (n,dmodel). W_theta, when used, has shape (dmodel,dk).
Write V=XWV for the values and Y=SV for the attention output, and let σ(z)=1/(1+e−z) and SiLU(z)=zσ(z). The function returns Y~WO, shape (n,dmodel), where Y~ depends on kind:
"none": Y~=Y.
"gate": Y~=Y⊙σ(XWθ), the multiplicative gate on the attention output.
"value_gate": Y~=S(V⊙σ(XWθ)), the gate on the values.
"additive_silu": Y~=Y+SiLU(XWθ).
"additive_identity": Y~=Y+XWθ.
"rmsnorm": each row of Y divided by mean(y2)+eps, the mean taken over that row's dk entries. No learned gain.