Skip to content

Stop gradients through MoE routing indices in granitemoe, granitemoehybrid, lfm2_moe - #1795

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
adityak74:fix-moe-routing-stop-gradient
Sep 1, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
adityak74:fix-moe-routing-stop-gradient

Conversation

@adityak74

Copy link
Copy Markdown
Contributor

What

Wraps the integer top-k expert-routing indices in mx.stop_gradient in three MoE model families: granitemoe, granitemoehybrid, and lfm2_moe.

Why

LoRA fine-tuning (mlx_lm.lora --train) on any of these families currently crashes in the backward pass:

ValueError: [gather_axis] Cannot calculate VJP with respect to indices. Use stop_gradient on indices to stop gradients from being computed.

The indices derive from differentiable router logits and are consumed by take_along_axis and 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 with mx.stop_gradient; these three were missing it.

This is the same fix as #1787 (qwen3_moe), applied to the remaining affected families. A survey of mlx_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 real mlx_lm.lora --train runs (found while fine-tuning Qwen3-30B-A3B / surveying MoE trainability on an M5 Pro).

https://claude.ai/code/session_01GZWrsNwempeEyaCaQiZbWH

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you move the tests to tests/test_tuner_trainer.py like https://github.com/ml-explore/mlx-lm/pull/1787/changes?

@zcbenz zcbenz added the await response This pull request is waiting for response from the author. label Aug 29, 2026
@adityak74

Copy link
Copy Markdown
Contributor Author

Done — moved the three backward-pass tests into tests/test_tuner_trainer.py following the #1787 structure (same _assert_backward helper shape), and removed the separate test file. Full test_tuner_trainer.py passes locally (5 passed).

…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
adityak74 force-pushed the fix-moe-routing-stop-gradient branch from f9c2244 to 4b06786 Compare August 29, 2026 19:27
@adityak74
adityak74 requested a review from zcbenz August 30, 2026 00:32
@zcbenz zcbenz removed the await response This pull request is waiting for response from the author. label Sep 1, 2026
@zcbenz
zcbenz merged commit acb0240 into ml-explore:main Sep 1, 2026
2 checks passed
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