Repository navigation
Stop gradients through MoE routing indices in granitemoe, granitemoehybrid, lfm2_moe - #1795
Merged
Merged
Conversation
zcbenz
reviewed
Aug 28, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
Can you move the tests to tests/test_tuner_trainer.py like https://github.com/ml-explore/mlx-lm/pull/1787/changes?
Contributor
Author
|
Done — moved the three backward-pass tests into |
…ybrid, lfm2_moe
LoRA fine-tuning (mlx_lm.lora --train) crashes on these three model families
with:
ValueError: [gather_axis] Cannot calculate VJP with respect to indices.
The integer top-k expert indices derive from differentiable router logits and
flow into take_along_axis / the switch MLP, so backward propagation requests a
VJP w.r.t. the indices unless explicitly barriered. Most sibling MoE models
(olmoe, mixtral, qwen2_moe, phimoe, glm4_moe, ...) already wrap their routing
indices in mx.stop_gradient; these three were missing the guard.
Same fix as ml-explore#1787 (qwen3_moe), applied to the remaining affected families.
Adds a backward-pass regression test for all three.
Claude-Session: https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH
Per review: match the test placement used by ml-explore#1787 instead of a separate test file. Claude-Session: https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH
adityak74
force-pushed
the
fix-moe-routing-stop-gradient
branch
from
August 29, 2026 19:27
f9c2244 to
4b06786
Compare
zcbenz
approved these changes
Sep 1, 2026
2 tasks done
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
Wraps the integer top-k expert-routing indices in
mx.stop_gradientin three MoE model families:granitemoe,granitemoehybrid, andlfm2_moe.Why
LoRA fine-tuning (
mlx_lm.lora --train) on any of these families currently crashes in the backward pass:The indices derive from differentiable router logits and are consumed by
take_along_axisand the switch MLP, so without an explicit barrier MLX is asked for a VJP w.r.t. integer indices. Most sibling MoE models in the repo (olmoe,mixtral,qwen2_moe,phimoe,glm4_moe,bailing_moe,ernie4_5_moe,exaone_moe,afmoe, ...) already guard their routing indices withmx.stop_gradient; these three were missing it.This is the same fix as #1787 (
qwen3_moe), applied to the remaining affected families. A survey ofmlx_lm/models/shows these three are the last index-selecting MoE models without the guard.Test
Adds
tests/test_moe_routing_gradients.py: a tiny-config backward pass through each of the three models (no weight downloads). Fails 3/3 before the fix, passes 3/3 after. Also verified end-to-end that the same one-line pattern unblocks realmlx_lm.lora --trainruns (found while fine-tuning Qwen3-30B-A3B / surveying MoE trainability on an M5 Pro).https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH