From 05f9dda43fa4dbe7abb56606b9af1fc31979cf31 Mon Sep 17 00:00:00 2001 From: Enoque Duarte Date: Mon, 28 Sep 2026 23:36:12 +0100 Subject: [PATCH] Fix gradients through Mistral4 and MiniMax indices Assisted-by: Codex:GPT-5.6 --- mlx_lm/models/minimax_m3_vl.py | 2 ++ mlx_lm/models/mistral4.py | 1 + tests/test_tuner_trainer.py | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/mlx_lm/models/minimax_m3_vl.py b/mlx_lm/models/minimax_m3_vl.py index c9688a296..1990641ec 100644 --- a/mlx_lm/models/minimax_m3_vl.py +++ b/mlx_lm/models/minimax_m3_vl.py @@ -169,6 +169,7 @@ def __call__(self, x: mx.array) -> mx.array: k = self.num_experts_per_tok inds = mx.argpartition(-scores, kth=k - 1, axis=-1)[..., :k] + inds = mx.stop_gradient(inds) scores = mx.take_along_axis(orig_scores, inds, axis=-1) scores = scores / (mx.sum(scores, axis=-1, keepdims=True) + 1e-20) scores = (scores * self.routed_scaling_factor).astype(x_flat.dtype) @@ -295,6 +296,7 @@ def _block_mask( block_scores = mx.where(is_local, mx.inf, block_scores) inds = mx.argpartition(-block_scores, kth=topk - 1, axis=-1)[..., :topk] + inds = mx.stop_gradient(inds) keep = mx.put_along_axis( mx.zeros(block_scores.shape, dtype=mx.bool_), inds, diff --git a/mlx_lm/models/mistral4.py b/mlx_lm/models/mistral4.py index 572060c31..b3a71f2d7 100644 --- a/mlx_lm/models/mistral4.py +++ b/mlx_lm/models/mistral4.py @@ -220,6 +220,7 @@ def mistral4_expert_select( scores_for_choice = scores inds = mx.argpartition(-scores_for_choice, kth=top_k - 1, axis=-1)[..., :top_k] + inds = mx.stop_gradient(inds) selected_scores = mx.take_along_axis(scores, inds, axis=-1) if norm_topk_prob: diff --git a/tests/test_tuner_trainer.py b/tests/test_tuner_trainer.py index fda8295b0..60b5c00b9 100644 --- a/tests/test_tuner_trainer.py +++ b/tests/test_tuner_trainer.py @@ -228,6 +228,45 @@ def loss_fn(gates): mx.eval(mx.grad(loss_fn)(mx.random.normal((2, 3, 8)))) + def test_mistral4_expert_select_backward(self): + from mlx_lm.models.mistral4 import mistral4_expert_select + + for n_group in (1, 2): + with self.subTest(n_group=n_group): + + def loss_fn(gates): + _, scores = mistral4_expert_select(gates, 2, n_group, 1, 1.0, True) + return scores.sum() + + mx.eval(mx.grad(loss_fn)(mx.random.normal((4, 8)))) + + def test_minimax_m3_vl_backward(self): + from mlx_lm.models import minimax_m3_vl + + args = minimax_m3_vl.TextArgs( + hidden_size=16, + intermediate_size=8, + shared_intermediate_size=8, + num_attention_heads=4, + num_key_value_heads=2, + num_local_experts=8, + num_experts_per_tok=2, + head_dim=4, + rotary_dim=4, + mlp_layer_types=["sparse"], + layer_types=["minimax_m3_sparse"], + index_n_heads=2, + index_head_dim=4, + index_block_size=2, + index_topk_blocks=1, + index_local_blocks=0, + ) + layer = minimax_m3_vl.DecoderLayer(args, 0) + # Three key blocks exceed the top-1 budget and exercise sparse selection. + inputs = mx.random.normal((1, 6, args.hidden_size)) + + mx.eval(mx.grad(lambda x: layer(x).sum())(inputs)) + def test_gemma4_per_layer_inputs_backward(self): # With embeddings instead of token ids, the lookup gathers with argmin output. from mlx_lm.models import gemma4_text