Repository navigation
Fix tensor-parallel gradient scaling in Top-K KL distillation - #2988
Conversation
Signed-off-by: wiederse <[email protected]>
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Scale gradients and validate distributed loss modelopt/torch/distill/plugins/megatron.py, tests/gpu_megatron/torch/distill/plugins/test_distill_megatron.py, CHANGELOG.rst |
For multiple tensor-parallel ranks, the loss adjustment scales gradients by the inverse of the tensor-parallel size while preserving the reported loss. Distributed tests compare shard losses and gradients with a full-vocabulary reference across top-P, reverse-KL, and temperature settings. The changelog records the fix. |
Priority:
Estimated code review effort: 2 (Simple) | ~15 minutes
Change: Bug fix
Merge Risk: 
Minimal · up to e02c3
-
The change corrects Top-K distillation gradients without changing reported losses, and the distributed reference test covers the scaling error. It is ready to merge after normal checks.
Pre-merge checks: 
5 · 
1
-
Failed checks: 1 warning Passed checks: 5 passed-
Full details: Title check
-
Explanation
The title "Fix tensor-parallel gradient scaling in Top-K KL distillation" names the main change: the Top-K KL loss now scales its backward gradient by 1/TP to correct gradients replicated across tensor-parallel ranks. The changeset adds this scaling in TopLogitsKLLoss.forward, and the test changes check gradient shards against a full-vocabulary reference under tensor parallelism. The title is concise, specific, and free of file lists or vague terms. The changelog and test updates are supporting changes that the title does not need to cover.
Full details: Security Anti-Patterns
-
Explanation
PASS. The PR changes only
modelopt/torch/distill/plugins/megatron.py, one changelog entry, and tests. The modelopt addition only applies backward gradient scaling. Added Python lines contain no unsafetorch.load,numpy.load(..., allow_pickle=True), hardcodedtrust_remote_code=True, external-inputeval()/exec(), or# nosec. The diff adds nopyproject.tomlor requirements dependencies.
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1 unsupported.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment @coderabbitai help to get the list of available commands.
cjluo-nv
left a comment
There was a problem hiding this comment.
Bot review (gpt-6.1-sol) — DM the bot to share feedback.
LGTM: backward-only scaling corrects replicated TP gradients without changing forward losses; the justified reference-test rewrite needs human sign-off.
Needs action:
- Confirm the test rewrite in
tests/gpu_megatron/torch/distill/plugins/test_distill_megatron.py: it retains forward and probability-mass checks while adding shard-gradient comparisons, Top-P truncation, temperature scaling, and reverse KL.
|
/claude review |
|
/ok to test 5ec737a |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2988 +/- ##
==========================================
+ Coverage 77.50% 79.15% +1.65%
==========================================
Files 661 661
Lines 74949 74951 +2
==========================================
+ Hits 58086 59331 +1245
+ Misses 16863 15620 -1243
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Claude review — PR #2988Scope: full review (trigger comment was a bare Findings: CRITICAL: 0 · IMPORTANT: 0 · SUGGESTION: 3 (all test-only, all posted inline) The fix is correct — I verified the
|
kevalmorabia97
left a comment
There was a problem hiding this comment.
LGTM. Root cause confirmed in the code: the Top-K path's dist_nn.functional.all_gather and _tp_logsumexp's dist_nn.functional.all_reduce sum the gradient of the replicated loss across TP ranks, so scaling only the backward by 1 / tp_size is correct. loss - loss.detach() is exactly 0 in the forward, so the reported loss and loss-balancer weights are unchanged, and the dense LogitsKLLoss is untouched. The rewrite of the ghost-token reference test is justified: the forward and probability-mass checks are kept and shard-gradient checks are added (Top-P, temperature, reverse KL). I did not run the GPU tests; the PR reports TP=1/2/4 passing and the negative control failing.
Minor, non-blocking:
- The PR body still says "this PR is a draft"; it is ready for review.
- Anyone who tuned the learning rate under the previously inflated TP gradients will see a change; the changelog entry already says gradients change.
Signed-off-by: Keval Morabia <[email protected]>
|
/ok to test aed85b5 |
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Signed-off-by: Keval Morabia <[email protected]>
Use equal vocab shards so torch.chunk never splits unevenly, and request dist_workers_size_2 so a single-GPU run skips instead of passing without entering the tp_size > 1 branch. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: Keval Morabia <[email protected]>
|
/ok to test e02c3f2 |
#2928 #2885 #2908 #2929 #2950 #2972 #2877 #2951 #2816 #3005 #3012 #2971 #3015 #3006 #3026 #2988 #3028 #1860 #3030 #2612 #3033 #2653 #2599 #2655 #2618 #2868 #3032 #3027 #2869 #2984 #2975 #2345 #3020 #2997 (#3072) ## Cherry-picked PRs - #2817 - #2671 - #2892 - #2876 - #2910 - #2911 - #2919 - #2904 - #2937 - #2928 - #2885 - #2908 - #2929 - #2950 - #2972 - #2877 - #2951 - #2816 - #3005 - #3012 - #2971 - #3015 - #3006 - #3026 - #2988 - #3028 - #1860 - #3030 - #2612 - #3033 - #2653 - #2599 - #2655 - #2618 - #2868 - #3032 - #3027 - #2869 - #2984 - #2975 - #2345 - #3020 - #2997 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Diffusion exports now offer more control over attention-quantizer metadata and preserve sparse-attention settings in a format compatible with downstream tooling. * Model loading better handles select bundled model implementations, and Docker guidance now covers writable CUDA-extension cache locations. * **Bug Fixes** * Improved reliability for model export and checkpoint saving, quantization across devices, ONNX processing, and calibration. * Temporary TensorRT and autotuning artifacts now use private directories by default. * **Documentation** * Updated installation guidance, compatibility notes, and links; corrected typos across guides and examples. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Chad Voegele <[email protected]> Signed-off-by: Rohan Joshi <[email protected]> Signed-off-by: LinCanNerd <[email protected]> Signed-off-by: Keval Morabia <[email protected]> Signed-off-by: weimingc <[email protected]> Signed-off-by: Noey Yang <[email protected]> Signed-off-by: Kai Xu <[email protected]> Signed-off-by: Zhehao Hu <[email protected]> Signed-off-by: noeyy-mino <[email protected]> Signed-off-by: joe-nv <[email protected]> Signed-off-by: yingguo-trt <[email protected]> Signed-off-by: Shengliang Xu <[email protected]> Signed-off-by: Chenjie Luo <[email protected]> Signed-off-by: anmaurya001 <[email protected]> Signed-off-by: Connor Baker <[email protected]> Signed-off-by: wiederse <[email protected]> Signed-off-by: Vu Dinh <[email protected]> Signed-off-by: Pratik Gandhi <[email protected]> Signed-off-by: Michael Rusu <[email protected]> Signed-off-by: Michael-Rusu <[email protected]> Signed-off-by: namgyu-youn <[email protected]> Signed-off-by: Sylvester Kaczmarek <[email protected]> Signed-off-by: Javier de Jesus <[email protected]> Signed-off-by: javierdejesusda <[email protected]> Signed-off-by: Zhiyu Cheng <[email protected]> Signed-off-by: Juhi Mittal <[email protected]> Signed-off-by: Dmitry Rantovov <[email protected]> Signed-off-by: h-guo18 <[email protected]> Co-authored-by: Chad Voegele <[email protected]> Co-authored-by: LinCanNerd <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Keval Morabia <[email protected]> Co-authored-by: Wei-Ming Chen <[email protected]> Co-authored-by: noeyy-mino <[email protected]> Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: kaix-nv <[email protected]> Co-authored-by: Zhehao Hu <[email protected]> Co-authored-by: joe <[email protected]> Co-authored-by: yingguo-trt <[email protected]> Co-authored-by: Shengliang Xu <[email protected]> Co-authored-by: Chenjie Luo <[email protected]> Co-authored-by: anmaurya001 <[email protected]> Co-authored-by: Connor Baker <[email protected]> Co-authored-by: Jiao Mingyang <[email protected]> Co-authored-by: Vu Dinh <[email protected]> Co-authored-by: Pratik Gandhi <[email protected]> Co-authored-by: Michael R <[email protected]> Co-authored-by: Michael-Rusu <[email protected]> Co-authored-by: namgyu-youn <[email protected]> Co-authored-by: Sylvester Kaczmarek <[email protected]> Co-authored-by: Javier De Jesus <[email protected]> Co-authored-by: Zhiyu <[email protected]> Co-authored-by: Juhi Mittal <[email protected]> Co-authored-by: Dmitry Rantovov <[email protected]> Co-authored-by: h-guo18 <[email protected]>
What does this PR do?
Type of change: Bug fix
With Top-K logits distillation and tensor parallelism enabled, each TP rank evaluates the same global KL loss. The autograd all-gather and full-vocabulary normalizer all-reduce sum gradients from these replicated losses, multiplying student gradients by the TP size even though the reported loss is correct.
Scale only the Top-K loss's backward contribution by
1 / TP. Forward loss values, loss-balancer weights, and dense KL behavior remain unchanged. Extend the existing ghost-token reference test to compare each vocabulary shard's gradients against a full-vocabulary reference, including Top-P truncation, non-default temperature, and reverse KL.Usage
No API or command-line changes. Existing
logit_kl_topk/--logit_kl_top_kconfigurations automatically receive the corrected gradients.Testing
pre-commit run --files modelopt/torch/distill/plugins/megatron.py tests/gpu_megatron/torch/distill/plugins/test_distill_megatron.py CHANGELOG.rst: passed.GPU test selection:
Run with 1, 2, or 4 visible GPUs.
Before your PR is "Ready for review"
CONTRIBUTING.md: N/A.Additional Information
No additional dependencies or communication collectives.
Summary by CodeRabbit