From 1c0a5bb9d1a92b49576c59e86e1fc152abfddbeb Mon Sep 17 00:00:00 2001 From: trevorgordon981 Date: Thu, 2 Jul 2026 03:22:15 -0700 Subject: [PATCH 1/2] Stop gradients through DSA indexer top-k indices to fix LoRA training crash Training a LoRA on deepseek_v32 / glm_moe_dsa crashes in the backward pass with 'ValueError: [scatter_axis] Cannot calculate VJP with respect to indices' whenever a batch is longer than index_topk (2048). The indexer's argpartition output derives from trainable projections, so autograd asks the consuming put_along_axis/take_along_axis for an indices VJP, which MLX does not define. Integer top-k indices carry no gradient, so stopping gradients on them is exact: forward is unchanged and inference is unaffected. --- mlx_lm/models/deepseek_v32.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/mlx_lm/models/deepseek_v32.py b/mlx_lm/models/deepseek_v32.py index d696c02c5..a11cccccc 100644 --- a/mlx_lm/models/deepseek_v32.py +++ b/mlx_lm/models/deepseek_v32.py @@ -109,9 +109,16 @@ def __call__( scores = scores.sum(axis=1, keepdims=True) if mask is not None: scores = mx.where(mask, scores, -float("inf")) - return mx.argpartition(scores, kth=-self.index_topk, axis=-1)[ - ..., -self.index_topk : - ] + # The top-k indices are an integer selection and carry no gradient. + # Stop gradients here so that when training (e.g. LoRA) with + # sequences longer than index_topk, autograd does not request an + # indices VJP from the scatter/gather ops that consume them + # ("Cannot calculate VJP with respect to indices"). + return mx.stop_gradient( + mx.argpartition(scores, kth=-self.index_topk, axis=-1)[ + ..., -self.index_topk : + ] + ) class DeepseekV32Attention(nn.Module): @@ -206,6 +213,11 @@ def __call__( topk_indices = self.indexer(x, qr, mask, cache=cache[1]) if topk_indices is not None: + # Also stop gradients at the point of use: if the indices ever + # arrive through a traced boundary (e.g. as a checkpointed + # function input with grad_checkpoint), the producer-side + # stop_gradient is not visible to the re-traced graph. + topk_indices = mx.stop_gradient(topk_indices) if L == 1: idx = topk_indices[:, :, 0, :, None] kv_latent = mx.take_along_axis( From a7c88eb731e9d9062d105ed7010701c67ecfc553 Mon Sep 17 00:00:00 2001 From: Michal Klein <46717574+michalk8@users.noreply.github.com> Date: Wed, 9 Sep 2026 17:06:17 +0200 Subject: [PATCH 2/2] Prune comments --- mlx_lm/models/deepseek_v32.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/mlx_lm/models/deepseek_v32.py b/mlx_lm/models/deepseek_v32.py index 92e341a97..e8d22d3f0 100644 --- a/mlx_lm/models/deepseek_v32.py +++ b/mlx_lm/models/deepseek_v32.py @@ -111,16 +111,8 @@ def __call__( scores = scores.sum(axis=1, keepdims=True) if mask is not None: scores = mx.where(mask, scores, -float("inf")) - # The top-k indices are an integer selection and carry no gradient. - # Stop gradients here so that when training (e.g. LoRA) with - # sequences longer than index_topk, autograd does not request an - # indices VJP from the scatter/gather ops that consume them - # ("Cannot calculate VJP with respect to indices"). - return mx.stop_gradient( - mx.argpartition(scores, kth=-self.index_topk, axis=-1)[ - ..., -self.index_topk : - ] - ) + indices = mx.argpartition(scores, kth=-self.index_topk, axis=-1) + return mx.stop_gradient(indices[..., -self.index_topk :]) class DeepseekV32Attention(nn.Module): @@ -215,10 +207,6 @@ def __call__( topk_indices = self.indexer(x, qr, mask, cache=cache[1]) if topk_indices is not None: - # Also stop gradients at the point of use: if the indices ever - # arrive through a traced boundary (e.g. as a checkpointed - # function input with grad_checkpoint), the producer-side - # stop_gradient is not visible to the re-traced graph. topk_indices = mx.stop_gradient(topk_indices) if L == 1: idx = topk_indices[:, :, 0, :, None]