Repository navigation
gpt_oss: stop_gradient on top-k indices so LoRA backward works (fixes #1814) - #1843
Merged
michalk8 merged 2 commits intoSep 4, 2026
Merged
Conversation
…l-explore#1814) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
michalk8
self-requested a review
September 4, 2026 18:41
Collaborator
|
LGTM, thanks @dellapolla-lab. And for future contributions, please consult the CONTRIBUTING.md. |
michalk8
approved these changes
Sep 4, 2026
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.
Fixes #1814.
Problem
LoRA / any backward pass through
gpt_ossfails inmlx_topk:mlx_topkfeeds the indices returned bymx.argpartitionstraight intomx.take_along_axis; in the backward pass MLX then tries to differentiate with respect to those integer indices.Fix
Wrap the top-k indices in
mx.stop_gradient, exactly asqwen3_moe.pydoes since #1787 (inds = mx.stop_gradient(inds)after theargpartition), and as the open PR #1238 does for gemma4. Gradient still flows through the router via the softmax over the selected values (expert_weights); the discrete expert selection has no gradient by construction, so this changes no numerics of the forward pass or of the weights that do get gradients.Verification
Minimal model built from
mlx_lm/models/gpt_oss.py(2 layers, 8 local experts, 2 experts per token, hidden 32, vocab 64, no weights downloaded),nn.value_and_grad(model, loss)on a 6-token batch, mlx 0.32.2 / mlx-lm 0.31.3, macOS arm64:main(6651d2e)ValueError: [gather_axis] Cannot calculate VJP with respect to indices…Inference path is unchanged (
mx.stop_gradientis a no-op in the forward pass).🤖 Generated with Claude Code