Skip to content

Fix PipelineMixin dropping layers on uneven splits, add explicit per-rank splits - #1816

Merged
michalk8 merged 4 commits into
ml-explore:mainfrom
azamamirza:pipeline-uneven-split
Sep 8, 2026
Merged

michalk8 merged 4 commits into
ml-explore:mainfrom
azamamirza:pipeline-uneven-split

Conversation

@azamamirza

Copy link
Copy Markdown
Contributor

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.

  • ☑️ I understand it is strictly prohibited to use AI to write PR descriptions

…s r+1…size-1). The divisible case is byte-for-byte the same assignment as before.
Comment thread tests/model_parallel_tests.py Outdated
def size(self):
return self._size

arch = importlib.import_module("mlx_lm.models.qwen3_moe")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just directly do from mlx_lm.models import qwen3_moe, since we're just testing one model.

Comment thread tests/model_parallel_tests.py Resolved
Comment thread tests/model_parallel_tests.py Resolved
Comment thread tests/model_parallel_tests.py Resolved
Comment thread tests/model_parallel_tests.py Resolved
Comment thread mlx_lm/models/pipeline.py Resolved
Comment thread mlx_lm/models/pipeline.py Resolved
@michalk8

michalk8 commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Thanks a lot for catching this, I've added some comments. Once they have been addressed, happy to merge!

@michalk8
michalk8 self-requested a review September 8, 2026 13:59
@azamamirza

Copy link
Copy Markdown
Contributor Author

I believe all requested changes are taken care of:

  1. Group helper to top of file - done
  2. Stage to top of file - done
  3. direct from mlx_lm.models import qwen3_moe - done, import lib is removed from the test
  4. size = 2 variable passed to Group - done
  5. assertRaisesRegex - done
  6. divmod suggestion in pipeline.py - done as suggested
  7. ministral3 + qwen3_5 override pipeline() - fixed both

Couple of other items discovered while address 7 above:

  1. deepseek_v32 also does the exact pre-fix math and has the same layer-dropping bug, but it is not a PipelineMixin subclass, so fixing it means either adding the mixin to its bases or duplicating the corrected math.
  2. kimi_k3 also does its own split but its math is already correct (the cumulative divmod form), so no action needed there.

@michalk8 michalk8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

@michalk8

michalk8 commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

deepseek_v32 also does the exact pre-fix math and has the same layer-dropping bug, but it is not a PipelineMixin subclass, so fixing it means either adding the mixin to its bases or duplicating the corrected math.

To avoid duplication, we should subclass the mixin, will leave it for a future PR.
As for Kimi3, would leave it for now if it's correct.

@michalk8
michalk8 merged commit 6c5d3a2 into ml-explore:main Sep 8, 2026
2 checks passed
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.
@azamamirza
azamamirza deleted the pipeline-uneven-split branch September 8, 2026 19:16
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants