perf(megatron): keep policy logits in model precision - #2764
Merged
Merged
Conversation
YolandaLyj
requested review from
Shi-Dong,
Zhichenzzz,
fzyzcjy,
maocheng23,
yueming-yuan and
yushengsu-thu
as code owners
August 26, 2026 13:14
Contributor
Author
|
G2 validation (1x NVIDIA H200, PyTorch 2.11.0+cu129, Megatron TP=1) passed.
The training result confirms the intended combination is BF16 model output plus --recompute-loss-function and a positive --log-probs-chunk-size. |
yueming-yuan
force-pushed
the
fix/chunked-bf16-logits
branch
from
August 31, 2026 02:30
a714d56 to
1be6f92
Compare
yueming-yuan
approved these changes
Aug 31, 2026
yueming-yuan
left a comment
Collaborator
There was a problem hiding this comment.
fixes bugs and added UT & snapshot test. approved now
This was referenced Aug 31, 2026
This was referenced Aug 31, 2026
xiaohong42
added a commit
to xiaohong42/miles
that referenced
this pull request
Sep 1, 2026
Float16Module upcasts the last pipeline stage's output to fp32 by default, which for a language model is a second copy of the entire [T, V] logit tensor -- 7.9 GiB at T=16384 over a 129280-entry vocabulary. Nothing in the log-prob path needs it: calculate_log_probs_and_entropy copies each response chunk to fp32 itself, and the fused vocab-parallel cross entropy opens with a cast. At 128K context the response is a few percent of T, so the upcast is paid on the whole tensor to serve a sliver of it. Measured on a 4-layer DeepSeek-V4 at 128K: -7.9 GiB of peak allocated per rank at CP=4, -9.3 GiB at CP=8, identical log-probs. Requires the preceding empty-chunk dtype fix. Without it, bf16 logits make a CP rank that holds no response token return bf16 while its peers return fp32, and their all-reduce hangs. Backport of the mechanism that landed upstream in radixark#2764, so this branch's pinned base carries it. Co-authored-by: Cursor <cursoragent@cursor.com>
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
Motivation
Float16Module.forward()defaults tofp32_output=True. For policy scoring withlabels=None, that upcasts the complete per-rank[T, V/TP]logits tensor beforelog_probs_chunk_sizecan take effect. Large vocabularies and long packed trajectories can therefore OOM on the full FP32 output even though the loss already processes logits in chunks.With this change, the normal path is:
Critic/value paths retain the existing FP32 output behavior.
The combined 1F1B schedule is not changed here because its Megatron
PostProcessNodeperforms a separate unconditionalfloat16_to_fp32; this PR only changes the normal model-forward paths.Tests
Added regression coverage:
fp32_output=FalseG2 GPU validation passed on 1x NVIDIA H200 with PyTorch 2.11.0+cu129 and Megatron TP=1:
[512, 8192], chunk size 128):00/0[2048, 32768], chunk size 256):412.01 MiB, forward+backward768.00 MiB32.02 MiB, forward+backward304.03 MiB256.04 MiB, forward+backward304.03 MiBThe intended memory-saving training configuration therefore combines a positive
--log-probs-chunk-sizewith--recompute-loss-function.