[DSV4 TOP 1/N] Make trainer query RMS batch invariant - #1912
Open
kaixih wants to merge 5 commits into
Open
Conversation
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
kaixih
marked this pull request as ready for review
July 29, 2026 03:10
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Contributor
Author
|
@maocheng23 @yueming-yuan to review thx. |
This was referenced Jul 30, 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.
Summary
Makes the trainer-side DeepSeek-V4 512-wide query RMS batch invariant.
wq_bwhen Megatronbatch_invariant_mode=True..meanformula when the flag is false.This is the first split from #1788.
Motivation
DeepSeek-V4 true on-policy (TOP) requires exact prefill-recompute versus
training-forward log-probabilities. The query RMS reduction must keep the same
floating-point association when the same query row is evaluated under different
outer batch shapes.
Megatron already exposes
--batch-invariant-modethroughTransformerConfig.This change makes the DSV4 trainer query RMS honor that existing opt-in mode.
Behavior
Default behavior is unchanged:
The fixed-tree path is not enabled by default because its unfused implementation
has a performance cost.
Scope
This PR contains the fixed-tree primitive and the trainer-side 512-wide query-RMS
wiring as one feature.
It intentionally does not include the matching SGLang rollout/prefill
query-RMS path. That backend change is required in a follow-up before this forms
a complete DSV4 TOP numerical contract or proves end-to-end prefill-versus-trainer
parity.
It also does not include deterministic TP collectives, FP8 alignment,
hyper-connections, MoE accumulation, log-softmax, or the final E2E.
Tests
(3, 1, 4, 512)and(3, 2, 4, 512), requiring exact fixed-tree outputequality. It intentionally does not assert native-path inequality because
that depends on the device and selected kernel.
test_dsv4_query_rms_is_batch_invariant:1 passedon the current head.fed40325) withradixark/miles:latestand PyTorch2.11.0+cu130: passed.1 passed.evaluated alone and inside a larger batch.
59.71 us, fixed tree225.73 us(
3.78x).git diff --check: cleanThe unfused fixed-tree path has a measurable performance cost, so the production
wiring in this PR remains opt-in behind
--batch-invariant-mode.