[PyTorch] Fix mutable QB bounds in CUDA graphs - #3426
Open
harryzhou2000 wants to merge 5 commits into
Open
Conversation
Signed-off-by: Harry Zhou <hhanyu@nvidia.com>
Contributor
Greptile SummaryThe PR adds an explicit validation-version handshake for mutable Quantile Balancing bounds used during CUDA graph capture.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains. Important Files Changed
Reviews (5): Last reviewed commit: "[PyTorch] Allow trusted QB updates durin..." | Re-trigger Greptile |
Signed-off-by: Harry Zhou <hhanyu@nvidia.com>
Signed-off-by: Harry Zhou <hhanyu@nvidia.com>
Signed-off-by: Harry Zhou <hhanyu@nvidia.com>
Signed-off-by: Harry Zhou <hhanyu@nvidia.com>
This was referenced Aug 26, 2026
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.
Description
Quantile Balancing keeps
qb_bin_boundsas a persistent CUDA tensor. In MCore's full-iteration CUDA graph, the router reads the current bounds near the start of an iteration and QB finalization updates the same storage near the end of that iteration. The graph executable is stable, but the state behind its captured device pointer is intentionally mutated once per replayed global batch.The host validation cache introduced in #3395 keys eager validation to the tensor's PyTorch version. Each eager
bounds.copy_()advances that version while preserving the pointer, so the last warmup leaves a valid but stale cached version and capture attempts a forbidden device-to-host validation.Graph replay has a second important property:
_versiononce;cudaGraphLaunchexecutes the captured update and changes device contents;_versionremains fixed.The fix therefore uses an explicit trusted-producer handshake rather than pretending host version bookkeeping can inspect replayed data:
mark_qb_bin_bounds_validated()records the current version without synchronizing or inspecting values;MCore's QB updater is such a trusted producer: starting from finite ordered bounds, it computes finite expert biases and constructs the next range as
[bias_min - 1, bias_max + 1]. The companion feature-detected MCore call is harryzhou2000/Megatron-LM@78e971efa.This PR intentionally does not use a device assertion. Invalid replay-time data cannot become a recoverable Python exception without a caller-owned device status tensor and a later host observation point; an assertion or trap would poison the CUDA context. If an untrusted graph-time producer is added later, it should use that explicit status-buffer design. The current trusted updater needs no extra kernel, global write, or replay-time host synchronization.
The final Transformer Engine diff is Python and tests only; it does not change a CUDA kernel, C++ binding, public C ABI, or fused-router launch sequence. Kimi K3's Quantile Balancing algorithm is described in https://github.com/MoonshotAI/Kimi-K3/blob/main/k3_tech_report.pdf.
Tests
Validated on one NVIDIA B200 using the
dev_2604container:22 passed.47 passed, 2 skipped; the skips require multiple GPUs.two_kernelandfused_atomichistogram modes._versionadvanced once during capture and remained fixed across all replays.