Lower indexed_put / indexed_add (scatter) in the native Expr compiler - #184
Merged
Merged
Conversation
Nx.indexed_put and Nx.indexed_add (scatter) now lower under
`compiler: Emily.Compiler, native: true` for MLX-scatter-compatible index
layouts, instead of forcing a graceful fallback to the evaluator. The MLX
ops and eager NIFs already existed (Native.{scatter,scatter_add} ->
mx::{scatter,scatter_add}); only the compiler path was missing.
- opcodes.hpp: add Scatter/ScatterAdd (92-93), bump kOpcodeCount to 94,
dispatch to mx::scatter (overwrite) / mx::scatter_add (accumulate).
operands [target, updates, idx0, ...]; iattrs [[axes...]].
- ir.ex: add the opcodes; lower :indexed_put/:indexed_add (one shared
clause), mirroring Emily.Backend's apply_scatter — reuse the existing
gather index-split + scatter_gather_compatible? helpers, port
updates_shape_for_scatter. Incompatible index layouts raise (no
fallback), matching the native gather; the evaluator handles them under
native_fallback: :eval.
- compiler_equivalence_test.exs: native-vs-evaluator bit-identical cases
for indexed_put/indexed_add (2-D grid, duplicate-index accumulation,
partial-axis whole-row writes).
Another op surfaced while running the native compiler on the Whisper
livebook.
This was referenced Jun 6, 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.
Nx.indexed_putandNx.indexed_add(scatter) now lower undercompiler: Emily.Compiler, native: trueinstead of forcing a gracefulfallback. Same shape as the FFT /
take_along_axisadditions — the MLXops and eager NIFs already existed (
Native.{scatter,scatter_add}→mx::{scatter,scatter_add}); only the compiler path was missing.Implementing the one shared scatter path closes both ops.
What surfaced this
Continuing to run the Whisper
speech_to_textserving withnative: truelogged a fallback on
:indexed_put. With this change that path compilesnative for the common (MLX-scatter-compatible) index layout.
Changes
c_src/emily/opcodes.hpp— addScatter/ScatterAdd(92–93), bumpkOpcodeCountto 94, dispatch tomx::scatter(overwrite) /mx::scatter_add(accumulate). operands[target, updates, idx0, ...];iattrs
[[axes...]].lib/emily/ir.ex— add the opcodes; onelower_opclause for:indexed_put/:indexed_add, mirroringEmily.Backend'sapply_scatter:reuse the existing gather index-split +
scatter_gather_compatible?helpers, port
updates_shape_for_scatter, reshape updates into MLX'slayout, emit the scatter opcode, coerce to
out.type.compiler_equivalence_test.exs— native-vs-evaluator bit-identicalcases: 2-D grid put/add, duplicate-index accumulation, partial-axis
(
axes: [0]) whole-row writes.Behaviour note
Like the native gather, only MLX-scatter-compatible index layouts
(trailing dim == #axes, ≥1 batch dim) lower; other layouts
raiseduringlowering → the evaluator handles them under
native_fallback: :eval(unchanged from today, where any
indexed_putfalls back). Strictimprovement for the common layout.
Verification
native: true(suite defaultnative_fallback: :raise, so a fallback would raise — passing provesfull native lowering). Opcode-parity test confirms the IR↔C++ lockstep
(92–93 /
kOpcodeCount94).mix precommitgreen: compile (warnings-as-errors), format, credo--strict, docs, 689 tests, 0 failures.