Repository navigation
Fix DSA indexer LoRA-training crash: stop gradients through sparse-attention top-k indices - #1452
Merged
michalk8 merged 4 commits intoSep 9, 2026
Conversation
… 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.
This was referenced Jul 2, 2026
This was referenced Aug 26, 2026
michalk8
self-requested a review
September 9, 2026 15:07
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
LoRA fine-tuning of DSA sparse-attention models (
deepseek_v32, andglm_moe_dsawhich subclasses it) crashes deterministically in the backward pass:Fixes #1449. Companion core issue: ml-explore/mlx#3787.
Root cause
The DSA
Indexeronly activates when the key length exceedsindex_topk(2048); below that it returnsNoneand training works fine. Once a batch crosses that width, the indexer computestopk_indicesviamx.argpartitionover scores that derive from trainable projections (wq_b,wk,weights_proj, and the sharedq_a_proj— all typical LoRA targets). Those integer indices then feedmx.put_along_axis(training/prefill path) andmx.take_along_axis(decode path) in the attention. Because the indices lie on the autodiff path, autograd requests a VJP with respect to the scatter's index argument, andScatterAxis::vjpthrows instead of returning a zero cotangent (ml-explore/mlx#3787).This explains the reproduction in #1449 exactly: the crash lands on the first batch whose sequence width crosses 2048 — the same step every run under a fixed seed — and is unaffected by which LoRA keys are trained, the learning rate, or excluding
switch_mlp. (My original report blamed the MoE routing scatter; that was wrong — the MoE gate's scatter is already protected bymx.stop_gradientongroup_idx. The unprotected site is the indexer.)Fix
mx.stop_gradienton the top-k indices at two sites:Indexer.__call__return. Integer top-k indices are a non-differentiable selection, so this is semantically exact: the forward pass is bit-identical, and inference never differentiates so serving is unaffected.topk_indicesis consumed inDeepseekV32Attention.__call__. This is defensive: if the indices ever reach the attention through a traced boundary (e.g. as a checkpointed function input undergrad_checkpointin a variant that passes indices between layers), a producer-side barrier is not visible to the re-traced graph and the crash returns. It is a no-op when the indices come straight from the producer, as here.A zero-cotangent fix in core MLX (ml-explore/mlx#3787) would also resolve the throw, but the model-side barrier is exact regardless, avoids computing gradients that are discarded, and unblocks training today.
Validation
mlx_lm.loraon GLM-5.2 (4-bit,glm_moe_dsa) withgrad_checkpoint: true,max_seq_length: 4096on real data: previously crashed at the first >2048-token batch, now trains cleanly (30-iteration smoke run, healthy loss descent).python -m py_compileandblack --checkpass on the changed file.