feat(xtoken): add multi-teacher support to cross-tokenizer off-policy distillation
Builds on #2757 (CE + H-KL gold loss). Extends cross-tokenizer off-policy distillation to support multiple teachers via a teachers: list of TeacherConfig.
What
- Same-vocab teachers (
projection_matrix_path=null) use direct per-position KL with no alignment; cross-tokenizer teachers keep the projection + alignment path. - Per-teacher indexed data-dict keys and serial teacher forward passes.
kd_loss_modeaggregation knob:sum/averaged_logits/select_teacher.- New
examples/configs/multi_teacher_cross_tokenizer_smoke.yamlexemplar (Qwen3-4B cross-tokenizer teacher + Llama-3.2-3B same-vocab teacher). - Updated unit tests in
tests/unit/algorithms/x_token/test_xtoken_off_policy_distillation.py.
🤖 Generated with Claude Code
This pull request requires additional validation before any workflows can run on NVIDIA's runners.
Pull request vetters can view their responsibilities here.
Contributors can view more details about this message here.
@terrykong could you take a review as well?
Update — this PR is the active one again.
#2745's TP/CP cross-tokenizer transport has now been merged into this branch directly (git merge main), and the multi-teacher loss / orchestration adapted to its always-full transport on top. So the full TP/CP + multi-teacher work lives here, with this review thread intact — please continue review on this PR rather than #2952 (which was an exploratory re-home).
/ok to test 93bd16bc081aaed2635ad0f6ad7cccdb754e0fa4
/ok to test 56c7fe2df2687136b10940b6334377205b9c0236
/ok to test 6521d1694225c989cacb42043a1cb6085888fd2a
/ok to test 94098bc
/ok to test aebc36b
/ok to test f966370
/ok to test cb2f8ba
Test Summary — NeMo-RL PR #2797 multi-teacher xToken off-policy distillation
PR: https://github.com/NVIDIA-NeMo/RL/pull/2797 nmfw_tests MR: https://gitlab-master.nvidia.com/dl/DLQASH/nmfw_tests/-/merge_requests/277
Testcases:
- test_xtoken_off_policy_distillation_basic
- test_xtoken_off_policy_distillation_multiteacher_pr2797
Run:
- run_id: 20260707T050109Z-d7eb5c87
- artifacts: /home/qiaochuz/nemo/nmfw_tests_por_2797/nemo_llm/test_suite/rl/logs/20260707T050109Z-d7eb5c87
- SLURM job: 5582163
- container: /lustre/fsw/coreai_dlalgo_ci/qiaochuz/containers/nemo-rl-v0.7.0.rc0.sqsh
- container commit: d1dde1bb63f48b8dbddaadb1913e21cdc2da347d
- product PR commit inspected: ef5dd9a56ae4212f7e12ec6d4af7d48c167d28f2
- topology: 1 node, 8 GPUs, student TP4/CP2, teachers TP2/CP2
Result: PASSED — 2/2 tests passed, 0 failed.
Report.md:
- Total tests: 2
- Passed: 2
- Failed: 0
- Training-like cases detected: 2
Per-test results:
| Testcase | Result | Exit | Runtime | Key signal |
|---|---|---|---|---|
| test_xtoken_off_policy_distillation_basic | pass | 0 | 389s | train/loss[3] = 2.6068239212036133 |
| test_xtoken_off_policy_distillation_multiteacher_pr2797 | pass | 0 | 305s | train/loss[3] = 2.5912489891052246 |
- Scope covered
PR #2797 adds multi-teacher support to cross-tokenizer off-policy distillation. The new POR/function testcase validates the real entrypoint and recipe in a released r0.7.0 container instead of unit-test fakes.
Covered source units:
| Source | Role | Evidence exercised |
|---|---|---|
| examples/run_xtoken_off_policy_distillation.py | Multi-teacher entrypoint; loads config, tokenizers, data, setup, train loop | Test calls this entrypoint with the PR #2797 multi-teacher recipe |
| examples/configs/recipes/llm/distillation-xtoken-off-policy-multiteacher-qwen3-4b-llama3.2-3b-to-llama3.2-1b-1n8g-dtensor-tp4cp2.yaml | Product multi-teacher recipe | Test uses this recipe with short smoke overrides |
| nemo_rl/algorithms/xtoken_off_policy_distillation.py | setup, teacher policy construction, collator/loss wiring, train loop | stdout shows "Setting up 2 teacher policies" and train metrics for both teacher paths |
| nemo_rl/data/cross_tokenizer_collate.py | Per-teacher tokenization/alignment payload; same-vocab teacher skip path | teacher0 emits projection/alignment metric; teacher1 emits same-vocab KD metric |
| nemo_rl/algorithms/loss/loss_functions.py | CrossTokenizerDistillationLossFn multi-teacher aggregation and per-teacher metrics | check_metrics asserts kl_loss_t0, kl_loss_t1, weight_t0, weight_t1, aggregate kl_loss |
Coverage gap closed:
- Existing PR/unit coverage validates algorithm pieces with mocks/fakes.
- This POR testcase validates the integrated path with real HF tokenizers, real models, real projection generation, real dataset staging, Ray/DTensor policy workers, CUDA IPC teacher logits, tensorboard metrics, and the released container image.
- Test design
Existing baseline case:
- test_xtoken_off_policy_distillation_basic
- Purpose: keeps PR #2745 single-teacher xToken smoke passing after PR #2797 moved projection/max-sequence overrides into per-teacher config shape.
- Runtime signal: train/loss[3] = 2.6068239212036133, loss > 0 and < 1000.
New PR #2797 case:
- test_xtoken_off_policy_distillation_multiteacher_pr2797
- Uses product recipe: examples/configs/recipes/llm/distillation-xtoken-off-policy-multiteacher-qwen3-4b-llama3.2-3b-to-llama3.2-1b-1n8g-dtensor-tp4cp2.yaml
- Keeps feature-defining topology:
- student: meta-llama/Llama-3.2-1B, TP4/CP2
- teacher0: Qwen/Qwen3-4B, TP2/CP2, cross-tokenizer projection matrix
- teacher1: meta-llama/Llama-3.2-3B, TP2/CP2, projection_matrix_path=null same-vocab direct KL
- Shortens runtime for CI/POR signal:
- max_num_steps=3
- sequence length=256
- validation/checkpointing/W&B disabled
- tensorboard enabled for metric assertions
- Stages one offline dataset parquet locally so HF_HUB_OFFLINE=1 does not fail resolving hf:// glob.
- Function/path coverage
| Area | Covered | Evidence |
|---|---|---|
| Entry parse/config load/override | Yes | Entry script executed with recipe + CLI overrides |
| Multi-teacher config validation | Yes | MasterConfig printed with teachers[0] and teachers[1] |
| Student + per-teacher tokenizer loading | Yes | setup progresses through token aligner/collator construction |
| Same-vocab teacher validation | Yes | teacher1 has projection_matrix_path=None and run proceeds to train |
| Cross-tokenizer aligner path | Yes | teacher0 projection matrix generated/used; proj_accuracy_t0 emitted |
| Same-vocab direct KL path | Yes | kl_loss_t1 emitted with no projection metric for t1 |
| Teacher policy construction loop | Yes | stdout: "Setting up 2 teacher policies" |
| Per-teacher full-logit export/pack | Yes | training reaches loss metrics for t0/t1 |
| Sum-mode multi-teacher aggregation | Yes | kd_loss_mode=sum in config; aggregate kl_loss > each teacher term |
| Student train loop | Yes | 3 training steps complete with loss metric |
| Validation path | No, intentionally disabled | val_period=0, val_at_start=false, val_at_end=false to keep POR smoke short |
| Checkpoint path | No, intentionally disabled | checkpointing.enabled=false |
| Alternate KD modes | No | averaged_logits/select_teacher are unit-test/follow-up scope, not this POR smoke |
| Dynamic teacher weighting | No | sum_weights_metric=None; static weights asserted |
Function coverage estimate for PR #2797 integration path:
- Covered primary integrated functions: parse_args, main, setup, export_teacher_logits_and_pack, xtoken_off_policy_distillation_train, CrossTokenizerCollator.call, CrossTokenizerDistillationLossFn.call, _sum_kd, _compute_teacher_kd, _compute_p_kl, _compute_same_vocab_kl/_direct_topk_kl.
- Not covered by this smoke: validate, checkpoint save/resume, averaged_logits, select_teacher, dynamic-weight scoring, negative error branches.
This is integration/path coverage, not coverage.py instrumentation.
- PR #2797-specific metric assertions
From test_xtoken_off_policy_distillation_multiteacher_pr2797 stdout:
- PASS train/kl_loss_t0[3] > 0.0 = 0.6393919587135315
- PASS train/proj_accuracy_t0[3] >= 0.0 = 6.15949821472168
- PASS train/kl_loss_t1[3] > 0.0 = 0.20682896673679352
- PASS train/weight_t0[3] > 0.0 = 8.0
- PASS train/weight_t1[3] > 0.0 = 8.0
- PASS train/kl_loss[3] > train/kl_loss_t0[3] = 0.8462209105491638
- PASS train/kl_loss[3] > train/kl_loss_t1[3] = 0.8462209105491638
- PASS train/loss[3] > 0.0 = 2.5912489891052246
- PASS train/loss[3] < 1000.0 = 2.5912489891052246
Why these assertions matter:
- kl_loss_t0 proves teacher0 contributed a cross-tokenizer KD term.
- proj_accuracy_t0 proves teacher0 used projection/alignment, not same-vocab shortcut.
- kl_loss_t1 proves teacher1 contributed a second KD term.
- absence of proj_accuracy_t1 is expected because teacher1 is same-vocab direct KL.
- weight_t0 and weight_t1 prove per-teacher weighting metrics are emitted.
- aggregate kl_loss greater than each teacher term proves sum-mode multi-teacher aggregation is active.
- train/loss bounded positive catches obvious no-op/NaN/exploding loss failures.
- Model / architecture coverage
Covered model topology:
- Student: meta-llama/Llama-3.2-1B
- Teacher 0: Qwen/Qwen3-4B
- Teacher 1: meta-llama/Llama-3.2-3B
- GPUs: 8 on one node
- Student parallelism: TP=4, CP=2
- Teacher parallelism: TP=2, CP=2 for both teachers
Covered teacher modes:
- cross-tokenizer teacher with projection matrix: teacher0
- same-vocab teacher with projection_matrix_path=null: teacher1
- static weighted sum aggregation: kd_loss_mode=sum, weights positive for both teachers
Not covered:
- multi-node IPC layout
- more than 2 teachers
- heterogeneous teacher DP beyond this recipe
- averaged_logits/select_teacher KD modes
- Additional validation performed
Static/MR validation:
- bash -n OK for changed RL shell scripts
- testcase_list.json parses OK
- git diff --check OK
- duplicate-key JSON check OK
- grep confirms no RL testcase runtime cherry-pick / ensure_commits / PR_MERGE_SHA ancestry guards remain
pytest status:
- repo-wide pytest is not a valid gate in this workspace for these shell cases.
python3 -m pytest -q --maxfail=1 nemo_llm/test_suite/rl/testcases/algorithmsreturns no tests ran because algorithms cases are shell scripts.- broader pytest collection is blocked by unrelated env/import issues (
pandasmissing under curator tests, ornemo_rlmissing under RL Python tests depending scope).
- Known limits / follow-up
Intentional limits:
- 3-step smoke, not convergence validation.
- Validation disabled.
- Checkpointing disabled.
- W&B disabled.
- No multi-node run.
Possible follow-ups:
- Add a negative config smoke for mismatched same-vocab teacher and non-null projection requirements.
- Add a separate POR case for averaged_logits or select_teacher if those modes become release-critical.
- Run under coverage.py inside the container if hard line coverage is required.
- Verdict
PR #2797 is covered by a real 8-GPU integrated POR/function smoke that exercises the shipped multi-teacher recipe in the v0.7.0.rc0 container. The test validates both teacher modes in one run: Qwen3-4B cross-tokenizer projection KL and Llama-3.2-3B same-vocab direct KL, with sum-mode aggregation producing per-teacher and aggregate metrics. The run passed 2/2 selected cases with 0 failures.