In asynchronous RL for language models, one system (the sampler, an inference engine) generates the completions and another (the trainer) recomputes the log-probability of every sampled token to form the update. With identical weights the two numbers should be equal, but they are computed by different code, often in 16-bit floats, and they drift apart. The last step of the model is the most sensitive: the LM head multiplies the final hidden state by a weight matrix to get one logit per vocabulary token, and a log-softmax turns logits into log-probabilities. The MiniMax-M1 paper reported that running this head in 32-bit floats (an "FP32 LM head") brings the two sets of probabilities much closer. Here you measure the effect of the head's precision against a float64 reference.
H has shape (T, d) (final hidden state at each of T positions), W has shape (V, d) (head weight, one row per vocabulary token), and tokens holds the T sampled token ids. dtype is one of the strings "float16", "float32", "float64".
token_logprobs(H, W, tokens, dtype="float32")
Do every step below in the given dtype, in this order. The float16 result depends on the order of operations, so follow it exactly.
H and W to the dtype.H with the transpose of W, computed with the @ operator (np.matmul), shape (T, V).np.sum along the last axis, and take the log of the sum.t, the entry of token tokens[t].Return the T values as a list of Python floats rounded to 4 decimals. Raise ValueError for any other dtype string or when len(tokens) is not T.
head_precision_gap(H, W, tokens, dtype)
dtype and at "float64" (the reference), convert both to float64 and exponentiate them to get probabilities."mean_abs_diff" and "max_abs_diff": the mean and the maximum over the T positions of the absolute difference of the two probabilities, each rounded to 6 decimals. With T = 0 both are 0.0.