Repository navigation
Fix PipelineMixin dropping layers on uneven splits, add explicit per-rank splits - #1816
Merged
Merged
Conversation
…s r+1…size-1). The divisible case is byte-for-byte the same assignment as before.
michalk8
reviewed
Sep 8, 2026
| def size(self): | ||
| return self._size | ||
|
|
||
| arch = importlib.import_module("mlx_lm.models.qwen3_moe") |
Collaborator
There was a problem hiding this comment.
I'd just directly do from mlx_lm.models import qwen3_moe, since we're just testing one model.
michalk8
requested changes
Sep 8, 2026
Collaborator
|
Thanks a lot for catching this, I've added some comments. Once they have been addressed, happy to merge! |
michalk8
self-requested a review
September 8, 2026 13:59
Contributor
Author
|
I believe all requested changes are taken care of:
Couple of other items discovered while address 7 above:
|
Collaborator
To avoid duplication, we should subclass the mixin, will leave it for a future PR. |
twallgren
added a commit
to twallgren/mlx-lm
that referenced
this pull request
Sep 8, 2026
ml-explore#1816 already fixed PipelineMixin's own layer-dropping bug (start_idx computed from this rank's own size rather than a real prefix sum, silently omitting a layer whenever ranks have different sizes) with a cleaner API than what this branch originally proposed for that part -- dropped here in favor of it. deepseek_v32.py wasn't touched by that fix because it never used PipelineMixin at all -- it carried its own fully independent copy of the same buggy logic, unrelated to and unfixed by ml-explore#1816. Rather than patching that copy a third time, this converts DeepseekV32Model to actually inherit PipelineMixin (matching qwen3_5.py/ministral3.py), so it gets ml-explore#1816's tested fix directly instead of maintaining a parallel implementation of the same bug-prone logic. Same behavior-preserving transformation applied elsewhere: __call__ walks pipeline_layers instead of manually indexing layers[start_idx + i] over a hand-tracked num_layers, and the outer Model.layers property returns model.pipeline_layers instead of re-deriving the same slice by hand. Also added the same empty-cache guards used elsewhere in this codebase (cache and cache[-1] is not None, etc.) for a rank that ends up with zero local layers. qwen3_moe.py's sanitize() has an unrelated bug in the same problem space, not touched by ml-explore#1816: it gates MoE expert-weight-stacking on a fixed model.layers.0... key. Under pipeline sharding a rank only has its own local layers' weight files downloaded, so a rank not owning layer 0 sees that key absent and skips stacking entirely for its own local layers -- silently wrong for any raw (unconverted) checkpoint under pipeline parallelism. Fixed to detect and iterate by which layers are actually present in the local weights dict instead.
twallgren
added a commit
to twallgren/mlx-lm
that referenced
this pull request
Sep 9, 2026
ml-explore#1816 already fixed PipelineMixin's own layer-dropping bug (start_idx computed from this rank's own size rather than a real prefix sum, silently omitting a layer whenever ranks have different sizes) with a cleaner API than what this branch originally proposed for that part -- dropped here in favor of it. deepseek_v32.py wasn't touched by that fix because it never used PipelineMixin at all -- it carried its own fully independent copy of the same buggy logic, unrelated to and unfixed by ml-explore#1816. Rather than patching that copy a third time, this converts DeepseekV32Model to actually inherit PipelineMixin (matching qwen3_5.py/ministral3.py), so it gets ml-explore#1816's tested fix directly instead of maintaining a parallel implementation of the same bug-prone logic. Same behavior-preserving transformation applied elsewhere: __call__ walks pipeline_layers instead of manually indexing layers[start_idx + i] over a hand-tracked num_layers, and the outer Model.layers property returns model.pipeline_layers instead of re-deriving the same slice by hand. Also added the same empty-cache guards used elsewhere in this codebase (cache and cache[-1] is not None, etc.) for a rank that ends up with zero local layers. qwen3_moe.py's sanitize() has an unrelated bug in the same problem space, not touched by ml-explore#1816: it gates MoE expert-weight-stacking on a fixed model.layers.0... key. Under pipeline sharding a rank only has its own local layers' weight files downloaded, so a rank not owning layer 0 sees that key absent and skips stacking entirely for its own local layers -- silently wrong for any raw (unconverted) checkpoint under pipeline parallelism. Fixed to detect and iterate by which layers are actually present in the local weights dict instead.
twallgren
added a commit
to twallgren/mlx-lm
that referenced
this pull request
Sep 14, 2026
computed from this rank's own size rather than a real prefix sum, silently omitting a layer whenever ranks have different sizes) with a cleaner API than what this branch originally proposed for that part -- dropped here in favor of it. deepseek_v32.py wasn't touched by that fix because it never used PipelineMixin at all -- it carried its own fully independent copy of the same buggy logic, unrelated to and unfixed by ml-explore#1816. Rather than patching that copy a third time, this converts DeepseekV32Model to actually inherit PipelineMixin (matching qwen3_5.py/ministral3.py), so it gets ml-explore#1816's tested fix directly instead of maintaining a parallel implementation of the same bug-prone logic. Same behavior-preserving transformation applied elsewhere: __call__ walks pipeline_layers instead of manually indexing layers[start_idx + i] over a hand-tracked num_layers, and the outer Model.layers property returns model.pipeline_layers instead of re-deriving the same slice by hand. Also added the same empty-cache guards used elsewhere in this codebase (cache and cache[-1] is not None, etc.) for a rank that ends up with zero local layers. qwen3_moe.py's sanitize() has an unrelated bug in the same problem space, not touched by ml-explore#1816: it gates MoE expert-weight-stacking on a fixed model.layers.0... key. Under pipeline sharding a rank only has its own local layers' weight files downloaded, so a rank not owning layer 0 sees that key absent and skips stacking entirely for its own local layers -- silently wrong for any raw (unconverted) checkpoint under pipeline parallelism. Fixed to detect and iterate by which layers are actually present in the local weights dict instead.
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.
Bug: PipelineMixin.pipeline() assigns some layers to no rank whenever len(layers) % group.size() != 0. The per-rank count is adjusted, but start_idx still multiplies by this rank's own count. 7 layers / 2 ranks silently drops layer 3. 62/4 drops layers 30–31. Wrong output, no error.
Fix: Compute the per-rank counts once, then start_idx = sum(counts of ranks r+1…size-1). The divisible case produces the identical assignment.
Feature: Optional split=[...] - explicit per-rank layer counts for asymmetric machines. On my 2-Mac Thunderbolt/JACCL ring, an M4 Max and an M5 Max, an uneven 14/28 split put 50.2 GB on the busy dev machine vs 93.0 GB on the clean node, where TP needs 72.6 GB on each — it's what lets a smaller or loaded node participate at all; parity vs unpipelined < 1e-6 including cached decode. Measured with the same split logic in our own model port; this PR ports it to PipelineMixin
Tests: Assignment coverage over stub groups (every layer exactly once, contiguous, reverse-rank order) for 6 shapes; a real qwen3_moe 7-layer/2-rank regression (fails on current main); split semantics + three ValueError cases. model_parallel_tests.py 4 passed + 5 subtests with the change; the two new tests fail on current main. test_models.py -k qwen3 3 passed; black/isort clean.