An encoder returns one hidden state per token, shape (B, T, d). An embedding model has to turn that into one vector per text, and padded positions must not count. EmbeddingGemma 2 mean-pools the real tokens, applies a single linear projection to the output width, and L2-normalizes. Implement the pooling strategies the v1 paper ablated and the head on top.
masked_mean_pool(H, mask)
H has shape (B, T, d), mask has shape (B, T) with 1 for real tokens and 0 for padding. Return the (B, d) mean over real tokens only. Divide by max(count, 1) so an all-padding row gives zeros rather than NaN.last_token_pool(H, mask)
mask is 1).first_token_pool(H)
0 of each row.embed(H, mask, W, pooling="mean")
"mean", "last" or "first"), project with W of shape (d, d_out), and L2-normalize every row with eps = 1e-12 inside the square root. Return the (B, d_out) array rounded to 4 decimals.mask[..., None] broadcasts the mask over the feature dimension.mask.sum(axis=1) - 1 when padding is on the right.