M9: gradient conformance and training primitives - #18
Merged
Conversation
Lifts indexed_add/indexed_put/gather off the via_binary fallback onto
native mx::scatter_add / mx::scatter / mx::gather, and adds the test
scaffolding needed to trust a gradient on Emily.
Backend: shared apply_scatter/6 + index-split + updates-shape rewrap
helpers translate Nx's {N, rank} indices + {batch ++ non_indexed_dims}
updates into MLX's per-axis index arrays + {batch ++ per_axis_slot}
updates. The single-axis gather fast path now reshapes to out.shape —
fixes a latent shape lie that only surfaced when grad chains dot
downstream of gather.
Tests:
- grad_equivalence_test.exs: 8-property zoo (sum, dot, reshape∘transpose,
broadcast, gather, indexed_add + two composition cases), each run
under Emily.Compiler and Nx.Defn.Evaluator, plus a PRNG-key-threading
test through a grad-computed dropout.
- finite_diff_test.exs + support/grad_helper.ex: 4-op numerical oracle
with per-op tolerance tables.
- mlp_curve_test.exs + transformer_block_curve_test.exs +
support/training_helper.ex: 50-step SGD curves matched per-step
(rtol=1e-3, drift canary) and final-loss (rtol=1e-4, convergence).
- soak/training_test.exs: 1k training-step memory-stability soak.
- mnist_full_test.exs: opt-in `:training_full` MNIST Axon MLP →
>97% test accuracy in 5 epochs under Emily.Compiler.
Docs: RELEASE.md updated; PLAN.md renumbered — M10 (conv-pool) and
M11 (1.0 release) follow.
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.
Summary
indexed_add/indexed_put/gatheroff thevia_binaryfallback onto nativemx::scatter_add/mx::scatter/mx::gather; grad through these ops no longer round-trips toNx.BinaryBackend.gatherfast path that only surfaced onceNx.Defn.gradchaineddotdownstream ofgather.See
RELEASE.mdfor the full M9 entry andPLAN.md § M9for scope.Test plan
mix precommit— 1 doctest, 64 properties, 219 tests, 0 failuresmix test --only conformance— 16 tests, 0 failures (DistilBERT / Qwen3 / ViT / Whisper tiny-random unaffected by the gather shape-lie fix)mix test --only training_full— 1 test, 0 failures; Axon MLP on MNIST converges to ~97% in 5 epochs underEmily.Compiler(~10 s wall time on Apple Silicon)