Repository navigation
refactor(xtoken): move same-tokenizer KD math into shared helpers - #4721
Conversation
avenkateshha
left a comment
There was a problem hiding this comment.
Multi-agent review (RL expert, two bug-finders, test agent, design reviewer, devil's-advocate verification, completeness critic). Every claim below was reproduced on CPU in the NeMo-RL container.
Confirmed. Both bitwise-parity claims match the base implementation: 576/576 loss and grad cases equal on each of the top-K and full-vocab paths. The CP=2 path and a 4-rank TP2×CP2 gloo check of same_tokenizer_topk_kl agree with single-process references. x_token/ + test_loss_functions.py give 395 passed, 48 skipped. The ruff hooks are clean, and pinned pyrefly shows no new errors.
4 inline comments: a memory regression in the masked top-K picker (introduced by this PR), a forwarding test gap on the averaged_logits path (introduced by this PR), the same_tokenizer_full_vocab_kl docstring, and the vocab_topk field doc (predates this PR).
Generated by Claude Code
2b871b8 to
fef7b51
Compare
ca90d1d to
fc7fc7f
Compare
451625c to
ddd556f
Compare
67455e9 to
78d75bc
Compare
447cc5a to
75d5b8b
Compare
78d75bc to
6f6df9f
Compare
|
Hi, @avenkateshha. Would you please rebase this PR and solve the conflicts. Now it mixes in some previous commits. Thanks a lot. |
Move the same-tokenizer KD math out of CrossTokenizerDistillationLossFn into nemo_rl/algorithms/x_token/loss_utils.py so other losses can reuse it: - same_tokenizer_topk_kl / same_tokenizer_full_vocab_kl take the KD mask, sample mask and global KD-token count as explicit keyword arguments. - The top-K vocab list is unchanged: the per-vocab max over every teacher row (select_teacher_topk_indices), MAX-reduced across CP. X-Token passes its token_mask as the KD mask and global_valid_toks as the KD-token count. _direct_topk_kl, _direct_full_vocab_kl and _same_vocab_masked_kl are removed; _compute_same_vocab_kl and the averaged_logits fast path call the shared helpers. Losses and gradients are bitwise identical to before for any KD and sample mask. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Adithya Hanasoge <[email protected]>
The averaged_logits fast path passes temperature, reverse_kl, the KD and sample masks and the KD-token count to same_tokenizer_full_vocab_kl explicitly. Its test stubbed the helper without looking at the arguments, so hard-coding reverse_kl=False or temperature=1.0 at the call site went unnoticed. Record the stub's keyword arguments and check them against non-default values. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Adithya Hanasoge <[email protected]>
…b KD same_tokenizer_full_vocab_kl sliced the teacher to the student logits width, so columns that a student lm_head pads past the tokenizer vocab stayed in both softmaxes, while same_tokenizer_topk_kl already drops them via vocab_size. Give the full-vocab helper the same vocab_size argument: cut student and teacher to it and renormalize the student. The averaged_logits fast path passes len(student_tokenizer). Only kd_loss_mode=averaged_logits with same-tokenizer teachers and a padded student (e.g. Qwen) changes; unpadded students are bitwise unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Adithya Hanasoge <[email protected]>
vocab_topk was documented as P-KL only and inert when gold_loss=True, but every same-tokenizer teacher uses it for its top-K KL whatever gold_loss is; only the all-same-tokenizer averaged_logits path uses the full vocab. Update the CrossTokenizerDistillationLossConfig field doc, the vocab_topk comments in both X-Token exemplar configs and the guide, which also described the multi-teacher example's same-tokenizer teacher as a full-vocab KL. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Adithya Hanasoge <[email protected]>
75d5b8b to
6c27c72
Compare
|
@RayenTian Thanks! Rebased onto |
|
/ok to test 6c27c72 |
What does this PR do ?
Moves the same-tokenizer KD math out of
CrossTokenizerDistillationLossFninto shared helpers innemo_rl/algorithms/x_token/loss_utils.py, so other losses can reuse it. X-Token now calls the helpers. Apart from one lm_head-padding fix in theaveraged_logitsfull-vocab path (see Behavior), losses and gradients are bitwise identical to the base.Builds on #3286, which is now merged; this PR targets
main. #4680 will be restacked on this branch and switched to the same helpers separately.Changes
same_tokenizer_topk_klandsame_tokenizer_full_vocab_klinloss_utils.py. The KD mask, sample mask and global KD-token count are explicit keyword-only inputs, along withvocab_size,vocab_topk(top-K only),temperature,reverse_kl,tp_groupandcp_group. Both helpers cut student and teacher to the shared tokenizer vocabvocab_size, dropping lm_head padding columns.select_teacher_topk_indices, MAX-reduced across CP). The next-token-shifted KD mask times the sample mask is used only for the masked mean.CrossTokenizerDistillationLossFn:_compute_same_vocab_kland theaveraged_logitsfast path now call the helpers. They passalign.student_token_maskas the KD mask,global_valid_toksas the KD-token count andself.student_vocab_size(len(student_tokenizer)) asvocab_size._direct_topk_kl,_direct_full_vocab_kland_same_vocab_masked_klare removed.vocab_topkis documented as used by same-tokenizer teachers too, not P-KL only (config field doc, both X-Token exemplar configs, and the guide).Behavior
No change, with one exception: the
averaged_logitsfull-vocab path now drops the student's lm_head padding columns (ids pastlen(student_tokenizer)), as the top-K path already did. This changes the loss only forkd_loss_mode: averaged_logitswith all same-tokenizer teachers and a student whose lm_head is wider than its tokenizer (for example a Qwen student). No shipped X-Token config or nightly uses that combination.Everything else, including every student without lm_head padding, is bitwise identical to the base for any KD and sample mask. Cross-tokenizer P-KL / gold paths, configs and entry points are unchanged.
Testing
CPU runs in the NeMo-RL container:
tests/unit/algorithms/x_token/andtests/unit/algorithms/test_loss_functions.py: 531 passed, 49 skipped (48 GPU-only, 1 existing parametrization skip), 0 failed. Onmainat the base commit: 273 passed, 49 skipped.tests/unit/algorithms/x_token/test_loss_utils.py:test_chat_eot_supervision.py: X-Token's_compute_same_vocab_klforwards temperature /reverse_kl/vocab_topkand the masks to the helper.test_xtoken_off_policy_distillation.py: theaveraged_logitsfast path forwards temperature /reverse_kl/vocab_sizeand the masks to the helper.main's (students without lm_head padding): 18,432 single-process and 3,744 per-rank CP=2 / TP=2 gloo comparisons of loss, student gradient andkl_lossare all bitwise identical. The TP=2 full-vocab case needs Megatron and could not run on CPU; that code path is unchanged.GPU tests and the multi-teacher nightly were not re-run for this revision.
Before your PR is "Ready for review"
Pre checks:
vocab_topkfield doc, X-Token exemplar config comments, anddocs/guides/xtoken-off-policy-distillation.md)🤖 Generated with Claude Code