Scatter embeddings for sequence parallelism in standalone LM forwards#5628
Open
kevalmorabia97 wants to merge 1 commit into
Open
Scatter embeddings for sequence parallelism in standalone LM forwards#5628kevalmorabia97 wants to merge 1 commit into
kevalmorabia97 wants to merge 1 commit into
Conversation
Models can build their GPTModel embedding with scatter_embedding_sequence_parallel=False so that the embedding output stays un-scattered for a caller that merges/scatters it -- e.g. VLM language models, whose outer multimodal model calls .embedding() directly, merges vision/audio + text embeddings, and then scatters the combined sequence for sequence parallelism. When such a language model is run standalone (GPTModel.forward with input_ids and no external decoder_input) -- for example distilling or quantizing only the language-model tower of a VLM -- the outer scatter is bypassed. Under sequence parallelism the embeddings then stay full-length on every TP rank, and the output-side sequence-parallel gather doubles the sequence, producing a tensor of length TP_size x seq_length vs seq_length downstream (observed as a shape mismatch in knowledge-distillation loss masking). Scatter the internally-embedded sequence in _preprocess when sequence parallelism is on and the embedding was built not to scatter. This only affects models that set scatter_embedding_sequence_parallel=False; standard models (scatter=True) are unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.com>
Contributor
|
This PR has been automatically converted to draft because all PRs must start as drafts. When you are ready for review, click Ready for Review to begin the review process. This will:
See the contribution guide for more details. |
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
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
Fix a sequence-parallel correctness bug for standalone language-model forwards of models whose embedding is built with
scatter_embedding_sequence_parallel=False.Why
Some models build their
GPTModelembedding withscatter_embedding_sequence_parallel=Falseso the embedding output stays un-scattered for a caller that merges/scatters it. The prime example is vision-language (and omni/audio) models: the outer multimodal model callslanguage_model.embedding()directly, merges the vision/audio embeddings with the text embeddings, and only then scatters the combined sequence for sequence parallelism (and passes it in asdecoder_input).When such a language model is run standalone —
GPTModel.forward(input_ids=..., decoder_input=None), e.g. distilling or PTQ-ing only the language-model tower of a VLM — that outer scatter is bypassed. Under sequence parallelism the embeddings then stay full-length on every TP rank, the decoder runs the full sequence, and the output-side sequence-parallel gather doubles the sequence. Downstream this shows up as aTP_size × seq_lengthvsseq_lengthshape mismatch.Concretely, ModelOpt language-model distillation of a VLM (Qwen3-VL/Qwen3.5-VL, Gemma3-VL, …) at TP=2 + SP fails in the KD loss-mask step:
(32 = TP_size(2) × seq_length(16).) Plain TP (no SP) works; standard LMs work; only the standalone-LM + SP case of these
scatter=Falsemodels is affected.Fix
In
GPTModel._preprocess, when the model embeds internally under sequence parallelism and the embedding was built not to scatter, scatter the sequence here:This only affects models that set
scatter_embedding_sequence_parallel=False; standard models (scatter=True) are unchanged (the guard is false), and thedecoder_input-provided path (normal VLM inference) is untouched.Testing
Validated on
nvcr.io/nvidia/nemo:26.06via ModelOpt/Megatron-Bridge language-model distillation: Gemma3-VL and Qwen3.5-VL both pass at TP=2 + SP with this change (previously both failed at the loss-mask step). Single-GPU and TP-without-SP were already passing and remain so.🤖 Generated with Claude Code