M10 (partial): quantized inference primitives - #19
Merged
Conversation
Binds MLX's affine int4/int8 group-wise quantization at the Native
and Elixir levels plus a direct-call helper for eager use. Scope
narrowed vs. PLAN.md's original M10 — the Axon-layer dispatch,
Bumblebee AWQ conformance, and Backend.dot/7 routing are deferred
to M10.5 because `Nx.Defn.Evaluator` has no public hook to inject a
custom op that isn't an `Nx.Backend` callback, and `Nx.dot/2`
collapses multi-tensor containers before reaching the backend. See
RELEASE.md and PLAN.md §M10.5 for the three candidate defn-
integration approaches.
- Native NIFs (c_src/ops/linalg.cpp): quantize/3 returning a
3-tuple {w_q, scales, biases}, dequantize/5, quantized_matmul/7
with explicit transpose (AWQ layouts need transpose=false).
- Emily.QuantizedWeight — Nx.Container-derived struct with keep:
so scalar metadata (group_size, bits, transpose) survives
container traversal. Validates rank, last-axis divisibility,
dtype, bit count before dispatch.
- Emily.Quantization.quantized_matmul/2 — direct-call helper for
eager/benchmark use over materialized tensors.
- Tests: +7 cases in test/emily/native_test.exs, 13 cases across
test/emily/quantization/, and a 1k-iter memory soak in
test/soak/quantized_memory_test.exs asserting packed-weight
footprint invariants.
mix precommit: 238 tests, 0 failures, credo clean.
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
Native.quantize/3,Native.dequantize/5,Native.quantized_matmul/7— plusEmily.QuantizedWeight(Nx.Container-derived struct) andEmily.Quantization.quantized_matmul/2direct-call helper for eager use.Backend.dot/7routing, and the Qwen3-0.6B-AWQ conformance test are deferred to M10.5. Investigation uncovered thatNx.dot/2collapses multi-tensor containers before reaching the backend, andNx.Defn.Evaluatorhas no public hook to inject a custom op that isn't already aNx.Backendcallback. Three candidate approaches for M10.5 are documented inPLAN.md(defn-native dequantize, Emily.Compiler custom-op intercept, upstream Nx extension).test/emily/quantization/, and a 1k-iter memory soak.mix precommit: 238 tests, 0 failures, credo clean.Scope note
The
quantized_matmul/7NIF takestransposeas an explicit arg rather than PLAN's/6signature — AWQ packed layouts needtranspose=falsewhile fresh-from-dense weights usetranspose=true, and MLX exposes it as a required parameter.Test plan
mix precommit(compile --warnings-as-errors, format, credo --strict, full test suite)mix test test/emily/native_test.exs --only describe:quantization— 7/7mix test test/emily/quantization/— 13/13mix test test/soak/quantized_memory_test.exs— 1/1 (under 1 s wall)