√dₖ and softmax saturation (row entropy)
Background
Without the 1/√d_k scale, attention scores grow with the head dimension and softmax collapses to a near one-hot — "hard" attention with dead gradients. A clean way to measure softness is the average entropy of the attention rows: high entropy = broad/soft, near-zero = saturated.
For raw scores S and head dimension d_k:
Problem statement
Implement attention_entropy(scores, head_size) returning the mean per-row entropy of the scaled-softmax attention.
Input
scores— rawQKᵀscores, shape(T, T).head_size—int, the per-head dimd_k.
Output
Returns a float: the mean over rows of the row entropy (natural log).
Examples
Example 1 — uniform scores → maximal entropy
Input: scores = [[0, 0, 0, 0]], head_size = 64
Output: 1.3862943611... # ln(4)
Explanation: equal scores → uniform softmax over 4 keys → entropy ln 4.
Example 2 — large unscaled spread → saturated
Input: scores = [[100, 0, 0, 0]], head_size = 1
Output: ~0.0
Constraints
- Scale:
scores / sqrt(head_size). - Row-wise (numerically stable) softmax, then
-Σ A·ln Aper row (use a small epsilon soln 0is safe). - Return the mean row entropy as a float.
Notes
- Bigger
head_sizedivides the scores down, raising entropy back towardln T— which is exactly why√d_kkeeps softmax in its learnable regime.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example: uniform scores over 4 keys give entropy ln(4)
- •Reference: large unscaled spread saturates to ~0
- •Sample: two uniform rows average to ln(2)