Computation of merged probabilities for GOLD
Hi,
I'm looking into GOLD for logit distillation and believe there is a bug in the way that the merged probability of tokens in a group is being computed. See the function here. Extracting the marginal of the first position and multiplying by the conditionals of the subsequent tokens only results in the correct probability for the actual sequence, as mentioned in the comments. Couldn't this be fixed by applying Bayes' rule?
For instance, assume a group consists of two tokens t1 = "a" and t2 = "b" at the first and second positions, respectively. To get the distribution over the vocabulary, instead of doing P(t1 | context) * P(t2 = "b" | t1 = "a", context) (treating t1 as variable), which is only correct when t1 = "a" and incorrect otherwise, shouldn't we compute P(t1 = "a" | context) * P(t2 | t1 = "a", context) (treating t2 as variable)? This would be correct for all values of t2.
Hi @vman049,
Took a look and you're right. Do you want to upload a PR with the fix?
I'm thinking we can modify the loop that goes through the group to go from the first element and stop before getting to the last.
We then would replace
first_pos = group[0]
marginal_probs = probs[first_pos]
with
last_pos = group[-1]
marginal_probs = probs[last_pos]
I think everything else should work well with that change, but let me know what you think.
Thank you for submitting this issue!
Thanks, @cmpatino , for following up on this! I thought more about this. Want to share my findings before going ahead with any changes.
In the example below, assume all distributions are conditional on the context before the current group.
Take, for example, a string "abcde" generated by student during a rollout. Let's assume this was generated with the tokens ["a", "bcd", "e"]. The teacher retokenizes with its tokenizer this as ["abc", "de"].
- Under the current implementation, we would be comparing:
- $p^{student}_{merged}$:
p(y0) * [p(y1 = "bcd" | y0 = "a") * p(y2 = "e" | y0 = "a", y1 = "bcd")] - $p^{teacher}_{merged}$:
p(y0) * [p(y1 = "de" | y0 = "abc")].
- $p^{student}_{merged}$:
- With the proposed implementation, we would be comparing:
- $p^{student}_{merged}$:
p(y2 | y0 = "a", y1 = "bcd") * [p(y0 = "a") * p(y1 = "bcd" | y0 = "a")] - $p^{teacher}_{merged}$:
p(y1 | y0 = "abc") * [p(y0 = "abc")].
- $p^{student}_{merged}$:
Everything in square brackets [] given above is constant scaling factor with respect to the token over which we're getting the distribution.
- In the existing approach, we are comparing p(y0) of the student against p(y0) of the teacher, scaled by a constant w.r.t. y0 (the part in square brackets
[]). Both of these have the same context (i.e., whatever came before "abcde"). - In the proposed approach, we are comparing a conditional distribution of y2 for the student against a conditional distribution of y1 for the teacher, also scaled by constants w.r.t. y2 and y1, respectively.
Important observations from this:
- In the proposed approach, the conditional distributions over y2 and y1 for the student and teacher are conditional on different tokens. These tokens include the context preceding "abcde" as well as the tokens within each group which precede the last token.
- This is different from the existing approach in which the marginal distributions over y0 have the same context (only the context preceding "abcde").
As mentioned in the code comments / initial post, the existing code does not result in a proper joint distribution over the sequence, and the probability of (y0, y1, y2) is only correct for the observed value of y0 (for the student; similarly over (y0, y1) for the teacher). In the proposed approach, the joint distribution is exact for all combinations of (y0, y1, y2) (again, for the student; similarly for the teacher).
Furthermore, in the existing approach, there is no normalization before the loss is computed. To incorporate information about all tokens other tokens in the group besides the first, this is actually necessary. Although there is the counterfactual token bias because of the uniform scaling, if we were to normalize, the terms which are constant w.r.t. y0 completely disappear, so we are left comparing just p(y0) for the student against p(y0) for the teacher. Hence, while the alignment is still useful for identifying which tokens for which to compare distributions, we would be dropping information about everything else within the span. On the other hand, with the proposed approach, we can get a proper distribution over the last token in each group, conditional on everything before, including the tokens within the group, just by dropping the scaling factors.
Both approaches, however, suffer from a bias towards the tokenizer with the smaller vocabulary. A smaller vocabulary would result in coarser groups and larger scaling factors (on average); thus, the tokenizer with the smaller vocabulary would generally have larger values. Hence, another point I would like to make here is about normalizing the distributions over the matched and unmatched subsets prior to computing KL/JSD and ULD/Wasserstein loss, respectively.
So, in summary, here are my recommendations:
- Computing merged probabilities:
- Option A: We can keep the existing code for computing the merged probabilities without normalization as it retains the information about all tokens within the group, albeit introducing bias to counterfactual tokens.
- Option B: We can switch to the proposed approach, which also retains information about all tokens within the group. This results in a proper probability distribution and is simpler to compute since we just need to take the conditional probability of the last token (no scaling needed). It does not introduce counterfactual bias, but the distributions we're comparing may have slightly different context depending on what came before the last token within a group.
- Regardless of which we select from (1), we should normalize the subsets of scores/probabilities before computing KL/JSD and ULD/Wasserstein loss to avoid biasing towards models with smaller vocabularies.
Please let me know what you think. Thank you!
Hi @cmpatino , was wondering if you had any thoughts on the above. Could you please let me know when you get a chance? Thanks!
Thank you for sharing the detailed analysis!
I agree with moving forward with Option B + normalization. Feel free to upload a PR and assign me as a reviewer when it's ready.
What I like about the approach is that it results in a proper probability distribution, which is a central piece in the distillation approach. It comes at a cost of not having the same context between the models, but hopefully the student can figure it out.
I'm on the fence about the normalization for Option A, because I'm inclined to think it's more important to avoid cancelling out the context information. However, it doesn't matter much because we're going with Option B, and I don't see a downside to normalizing in that scenario.
Looking forward to your PR 🚀
Awesome, thanks for your feedback! Will share the PR soon.
Hey @vman049, sorry for the slow response on our end, catching up on the backlog. Thanks to you and @cmpatino for the thorough analysis, the math checks out and Option B + normalization sounds right. Are you still planning to open the PR? Happy to wait, but if you'd rather hand it off, just say the word and we'll pick it up.
Yes, sorry for the super-late response! Work has been super-busy, but I will cut the PR next week. Thank you for your patience!
On Wed, May 27, 2026 at 8:44 PM Quentin Gallouédec @.***> wrote:
qgallouedec left a comment (huggingface/trl#5152) https://github.com/huggingface/trl/issues/5152#issuecomment-4559869061
Hey @vman049 https://github.com/vman049, sorry for the slow response on our end, catching up on the backlog. Thanks to you and @cmpatino https://github.com/cmpatino for the thorough analysis, the math checks out and Option B + normalization sounds right. Are you still planning to open the PR? Happy to wait, but if you'd rather hand it off, just say the word and we'll pick it up.
— Reply to this email directly, view it on GitHub https://github.com/huggingface/trl/issues/5152?email_source=notifications&email_token=AB4VMFYYKZAGNHMD3GJH5ND446DXBA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTINJVHE4DMOJQGYY2M4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-4559869061, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4VMFZZYCMTGYIVFFCKJR3446DXBAVCNFSM6AAAAACV47UOC2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DKNJZHA3DSMBWGE . You are receiving this because you were mentioned.Message ID: @.***>
Created the PR here. @cmpatino / @qgallouedec: could one of you please take a look?
Note that I fixed one other related thing, which is that the probabilities we were using were assumed to be for that token itself while they were actually for the next token. We needed to left shift all the probabilities so they were computed correctly and so that we could get the probability of the first token in the group.
Please let me know if you have any questions!