EmbeddingGemma is a Gemma decoder whose attention was made bidirectional. The weights are the same; what changes is which query-key pairs are allowed. Implement the two masks, an attention function that takes any allowed-pair mask, and a counter for how many pairs each mask lets through.
causal_mask(T)
(T, T) where entry [i, j] is True when query i may attend to key j, which for a decoder means j <= i.bidirectional_mask(pad)
pad has shape (B, T) with 1 for real tokens and 0 for padding. Return a boolean array of shape (B, T, T) where every query may attend to every real key: [b, i, j] is True exactly when pad[b, j] == 1.masked_attention(Q, K, V, allowed)
Q, K, V have shape (B, T, d); allowed is (T, T) or (B, T, T) and broadcasts. Scores are , disallowed entries are set to before the softmax, and the result is the weighted sum of V. Return the (B, T, d) output rounded to 4 decimals. Every query row has at least one allowed key.allowed_pairs(allowed)
True entries in the mask as a Python int.np.where(allowed, scores, -np.inf) then subtract the row max before exponentiating.(T, T) mask broadcasts against (B, T, T) scores without any reshaping.