[MCore] Add global-batch quantile balancing - #6638
Open
harryzhou2000 wants to merge 1 commit into
Open
Conversation
This was referenced Aug 19, 2026
harryzhou2000
force-pushed
the
hhanyu/k3-quantile-balancing-main
branch
from
August 24, 2026 09:07
fe35272 to
35d1280
Compare
harryzhou2000
marked this pull request as ready for review
August 24, 2026 09:22
harryzhou2000
force-pushed
the
hhanyu/k3-quantile-balancing-main
branch
from
August 24, 2026 14:04
35d1280 to
889ea1a
Compare
Member
Author
|
Applied the same review fixes as the paired dev PR: #6637. Changes:
Validation on NVIDIA B200 with PyTorch 2.13 nightly and Transformer Engine 2.19 development:
|
Signed-off-by: Harry Zhou <hhanyu@nvidia.com>
harryzhou2000
force-pushed
the
hhanyu/k3-quantile-balancing-main
branch
from
August 25, 2026 04:57
889ea1a to
5a88a7c
Compare
Member
Author
|
Mirrored the QB review fixes from the paired dev PR #6637 in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Extend
main's existing Quantile Balancing router with Kimi K3 global-batch histogram estimation while preserving the current exactmicro_batchestimator and its default behavior.Paired dev-target PR: #6637
Quantile estimator
For token
iand expertj, lets[i,j]be the raw sigmoid router score and letalpha[i]be the(k+1)-th largest value ofs[i,:] + b[:]. K3 defines:The histogram range for the next global batch is
[min(b_next)-1, max(b_next)+1]. The default is 1000 uniform bins per expert, matching the report.Reference: Kimi K3 technical report, Quantile Balancing.
Implementation
--moe-router-quantile-balancing-estimation-scope {micro_batch,global_batch}:micro_batchremains the default and retains the existing exact estimator;global_batchenables K3's pooled histogram estimator.--moe-aux-loss-coeff 0for QB.[num_experts, num_bins]per MoE router across all gradient-accumulation microbatches.expert_biasbuffer without enabling--moe-router-enable-expert-bias; that flag continues to select the independent signed-count updater.fused_atomicpath;(k+1)and histogram accumulation with PyTorch operations.No full score tensor is retained. The fused path adds shared-memory bin classification and histogram atomics to the existing router kernel. The global-batch finalizer communicates
num_experts * num_binsint32 counters per router; the non-fused fallback additionally launches PyTorch top-(k+1)and scatter operations.Validation
Validated on NVIDIA B300 with NVIDIA/TransformerEngine#3395 installed:
micro_batchbehavior plus report-derived histogram recovery, pooled-global-batch semantics, fused-vs-non-fused parity, paged-stash retry reset, and real MCore router finalization.qb_histogram_mode="fused_atomic"and compares its histogram exactly with the non-fused implementation.Additional eight-rank validation on NVIDIA B300 exercised the production
finalize_model_gradspath for:For every topology, the test verifies model-parallel group sizes, per-microbatch accumulation, non-identical pre-reduction rank-local histograms, the all-reduced histogram against an independent quantile-update oracle, identical bias and bounds on all ranks, in-place reset, stable CUDA-graph-visible addresses, and exactly one gradient synchronization. All six fused/unfused parameterizations passed on all eight ranks. The focused Transformer Engine router suite passed 38 tests with 2 skips.
The commits are SSH-signed and signed off.
git diff --checkand the repositorytools/autoformat.shpass; pylint rates the changed files 10/10 and Ruff reports no errors.Scope and dependencies
The existing
micro_batchpath and non-fusedglobal_batchpath do not depend on NVIDIA/TransformerEngine#3395. If fused global-batch QB is requested, MCore checks the installed TE signature and reports a targeted error when the histogram API is unavailable.This PR does not depend on SiTU-GLU or #6674. It has no SiTU activation imports or configuration coupling; the paired integration checkout was used only to verify compatibility between the independent branches.
Issue tracking
For PRs from open-source community contributors:
Linked issue: TODO — link a Megatron-LM feature-request issue before marking this draft ready for review.
Contribution process
Pre-checks
Code review
Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.
Step 1: Mark PR as "Ready for Review"
.github/CODEOWNERS.Final Review might get declined if these requirements are not fulfilled.
Step 2: Final Review
For PRs that change
megatron/core, once all expert reviewers have approved, theFinal Reviewlabel is applied automatically and final reviewers are assigned.For PRs outside
megatron/core, this step is skipped.Step 3: Approved
Once all required reviewers have approved, the
Approvedlabel is applied automatically.Merge
Any member of mcore-engineers will be able to merge your PR.