Skip lm_head on non-rank-0 pipeline-parallel ranks#1228
Open
lawcontinue wants to merge 1 commit intoml-explore:mainfrom
Open
Skip lm_head on non-rank-0 pipeline-parallel ranks#1228lawcontinue wants to merge 1 commit intoml-explore:mainfrom
lawcontinue wants to merge 1 commit intoml-explore:mainfrom
Conversation
In pipeline-parallel inference, non-final ranks compute identical lm_head logits that are never used downstream. Skip the projection on ranks > 0 to avoid a redundant large matmul (vocab_size can be 262K+). Measured on a 2-node Gemma-3-12B setup: ~30% per-step time saved by skipping the lm_head weight read on the remote rank. Affects all PipelineMixin models: deepseek_v2, deepseek_v3, deepseek_v32, glm4_moe, glm4_moe_lite, ministral3. Closes ml-explore#1203
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
In pipeline-parallel inference, every rank runs
lm_headon the same broadcast hidden state. For non-rank-0 ranks, the logits are identical to rank 0's and never used — the call is pure waste.This PR adds a
pipeline_rank != 0guard beforelm_headin all sixPipelineMixinmodels, skipping the projection on non-final ranks.Closes #1203.
Changes
Each model's
Model.__call__now returns early whenpipeline_rank != 0:deepseek_v2.pydeepseek_v3.pydeepseek_v32.pyglm4_moe.pyglm4_moe_lite.pyministral3.pytie_word_embeddingsbranch)Performance
Measured on a 2-node Gemma-3-12B setup (Thunderbolt, MLX 0.31.1): skipping
lm_headon the remote rank saves ~30% of per-step time. The savings scale withvocab_size— DeepSeek V3 with 129K vocab would see a larger benefit.Design notes
(B, L, hidden_size)instead of(B, L, vocab_size). Each rank already compiles a different graph (different layer slices), so heterogeneous output shapes should not be an issue.pipeline_rankdefaults to 0 inPipelineMixin.__init__).ministral3has atie_word_embeddingsbranch — the guard is placed before both paths, keeping the patch uniform across all six models.