RL icon indicating copy to clipboard operation
RL copied to clipboard

feat(xtoken): support TP/CP/diff-DP sharded cross-tokenizer distillation

Open RayenTian opened this issue 5 months ago • 4 comments

What does this PR do ?

Adds TP / CP / heterogeneous-DP sharding support to the cross-tokenizer (xtoken) off-policy distillation loss (P-KL + gold) on the DTensor v2 worker, fixes a context-parallel loss-invariance bug, and adds a parallelism-invariance nightly.

Issues

Closes https://github.com/NVIDIA-NeMo/RL/issues/2682

Following https://github.com/NVIDIA-NeMo/RL/issues/2792 (Make building the (student, teacher) projection matrix inflight )

Result

Teacher : Qwen/Qwen3-4B TP2 CP2 DP2 Student : meta-llama/Llama-3.2-1B TP4 CP2 DP1

KL Loss

image

Gold Loss

image

XT Loss

image

Usage

# 1. Build the (student, teacher) projection matrix from the tokenizer pair (one-time, offline) -> writes <output-dir>/<output-filename>_special.pt.
uv run python -m tools.x_token.minimal_projection_via_multitoken --student-model meta-llama/Llama-3.2-1B --teacher-model Qwen/Qwen3-4B --top-k 4 --enable-special-token-mapping --enable-exact-match --disable-reverse-pass --disable-scale-trick --output-filename xtoken_proj --output-dir /tmp/xtoken_proj
# 2. Distill: student TP4×CP2 from a teacher running TP2×CP2 (heterogeneous layout); the loss stays parallelism-invariant across TP/CP/DP.
uv run examples/run_xtoken_off_policy_distillation.py --config examples/configs/recipes/llm/xtoken-off-policy-distillation-qwen3-4b-to-llama3.2-1b-1n8g-dtensor-tp4cp2.yaml loss_fn.projection_matrix_path=/tmp/xtoken_proj/xtoken_proj_special.pt
# key knobs: policy.dtensor_cfg.{tensor_parallel_size=4,context_parallel_size=2}, teacher.dtensor_cfg.{tensor_parallel_size=2,context_parallel_size=2}

Before your PR is "Ready for review"

Pre checks:

  • [ ] Make sure you read and followed Contributor guidelines
  • [ ] Did you write any new necessary tests?
  • [ ] Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • [ ] Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

  • ...

RayenTian avatar Jun 09 '26 08:06 RayenTian

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.

copy-pr-bot[bot] avatar Jun 09 '26 08:06 copy-pr-bot[bot]

/ok to test 040969c

RayenTian avatar Jun 12 '26 02:06 RayenTian

/ok to test 17bec2f

RayenTian avatar Jun 12 '26 02:06 RayenTian

/ok to test b02211f

RayenTian avatar Jun 12 '26 06:06 RayenTian

/ok to test 1513dd6

RayenTian avatar Jun 23 '26 04:06 RayenTian

/ok to test 18ebb7f

RayenTian avatar Jun 24 '26 07:06 RayenTian

/ok to test ca46ce9

RayenTian avatar Jun 24 '26 14:06 RayenTian

/ok to test 0cffdaa

RayenTian avatar Jun 24 '26 14:06 RayenTian

/ok to test 75e43f9

RayenTian avatar Jun 25 '26 00:06 RayenTian

RL POR Test Summary — NVIDIA-NeMo/RL PR #2745

feat(xtoken): support TP/CP/diff-DP sharded cross-tokenizer distillation

Result: ✅ PASS

Field Value
Testcase test_xtoken_off_policy_distillation_basic (suite rl, tier function, 8 GPU)
run_id 20260629T210828Z-963551a9
SLURM job 5531857
host ipp2-1658
cluster eos (coreai_dlalgo_ci)
duration 353s
exit_code 0
summary.json status passed
container nemo-rl-nightly-20260629.sqsh
pinned merge SHA 9e6c2d5ffe25296bbd70e7e4926957e8a894f0a9
commit e23cd89 on qiaochuz/por_2745_xtoken (DLQASH/nmfw_tests)

What this PR adds (feature surface under test)

PR #2745 introduces off-policy cross-tokenizer distillation where the student and teacher use different tokenizers AND different parallelism layouts, with the distillation loss computed identically (parallelism-invariant) across mismatched TP/CP/DP sharding. New code surface:

  1. Product entrypoint — examples/run_xtoken_off_policy_distillation.py
    • Enforces the cross-tokenizer contract: distinct policy/teacher tokenizers
      • a non-null projection_matrix_path.
  2. Shipped recipe (heterogeneous layout) — examples/configs/recipes/llm/distillation-xtoken-off-policy-qwen3-4b-to-llama3.2-1b-1n8g-dtensor-tp4cp2.yaml
    • student Llama-3.2-1B sharded TP4×CP2
    • teacher Qwen3-4B sharded TP2×CP2
    • differing DP across student/teacher (diff-DP) on one 8-GPU node
    • mixed-KL cross-tokenizer loss (loss_fn.projection_matrix_path)
  3. Projection-matrix build tool — tools/x_token/build_projection_matrix.sh
    • Constructs the (student-vocab × teacher-vocab) token-alignment projection matrix that maps teacher logits into student token space.

What the test exercises (step by step)

Step 1 — Projection matrix build (PR tool): Runs build_projection_matrix.sh for (student Llama-3.2-1B, teacher Qwen3-4B), runtime-top-k 4. Asserts the .pt artifact is produced. → Observed: 128×256 projection built, 0.05% rounding; BOTH directions ran (student→teacher pass + teacher→student pass).

Step 1.5 — Offline dataset staging: Materializes nvidia/Nemotron-Pretraining-Specialized-v1.1 (Nemotron-Pretraining-Formal-Logic/part_000000.parquet) via named-file hf_hub_download, rewrites data.train.data_files to a LOCAL glob so the recipe's hf://...*.parquet wildcard doesn't hit the Hub under HF_HUB_OFFLINE=1.

Step 2 — Heterogeneous cross-tokenizer distillation (the feature): Drives the SHIPPED recipe through the new entrypoint. Overrides limited to: projection_matrix_path (required), local data glob, and CI-shrink knobs (max_num_steps=3, val_period=3, seq_len=256). The feature-defining knobs — TP4/CP2 student, TP2/CP2 teacher, diff-DP, mixed-KL loss config — are inherited unchanged from the recipe, so the heterogeneous sharded loss path the PR adds is genuinely exercised.

Step 3 — Metric extraction + smoke assertion: Dumps TB logs → metrics.json, then asserts the cross-tokenizer loss produced a finite train/loss at the final step.


Assertions & evidence (stdout.log lines 1331–1332)

PASS  data["train/loss"]["3"] > 0.0      → 2.6068239212036133
PASS  data["train/loss"]["3"] < 1000.0   → 2.6068239212036133

(validation/loss 2.58722, validation/kl_loss 0.0707011 at step 3)

Policy: presence + finiteness only — NOT a distillation-gain claim (3 steps is far too short to assert convergence).


What is / isn't covered

Covered:

  • New entrypoint end-to-end on the shipped heterogeneous recipe
  • Projection-matrix build tool (both alignment directions)
  • Cross-tokenizer mixed-KL loss across TP4/CP2 ↔ TP2/CP2 + diff-DP
  • train + validation forward/backward over 3 steps, finite loss

NOT covered (out of scope for a function-tier smoke):

  • Convergence / distillation-quality gain
  • Multi-node (e2e tier) scaling of the same layout
  • Alternative TP/CP combinations or other model pairs

Guards (false-signal protection)

  1. Source-level (quoted heredoc): SKIP if entrypoint / recipe / build tool absent, or if entrypoint no longer references projection_matrix_path.
  2. Runtime cherry-pick: pins PR merge SHA 9e6c2d5 → clean SKIP if a checkout predates the merged behavior.

Both → clean SKIP (exit 0), never a false PASS/FAIL.

CI-shrink (within policy): only step count + seq length reduced; disk-precheck fix is env-only (HF_DATASETS_CACHE → tmpfs). Feature knobs untouched.

RCA note: runs 1–4 failed on infra only (offline-cache / disk-precheck), NOT PR defects; run 5 is the clean green after the tmpfs fix. ~2.5h delay was eos scheduler congestion, not a test issue.


Artifacts

  • stdout.log — …/logs/20260629T210828Z-963551a9/tests/test_xtoken_off_policy_distillation_basic/stdout.log
  • summary.json — …/logs/20260629T210828Z-963551a9/.../summary.json

MR

Branch pushed; create the MR (pre-filled, targets main): https://gitlab-master.nvidia.com/dl/DLQASH/nmfw_tests/-/merge_requests/new?merge_request%5Bsource_branch%5D=qiaochuz%2Fpor_2745_xtoken&merge_request%5Btarget_branch%5D=main

qiaochuz-nv avatar Jun 30 '26 00:06 qiaochuz-nv