Skip to content

fix: Pass quantized scales and biases as program inputs - #168

Merged
polvalente merged 4 commits into
elixir-nx:mainfrom
georgeguimaraes:fix-quant-captures
Nov 27, 2026
Merged

polvalente merged 4 commits into
elixir-nx:mainfrom
georgeguimaraes:fix-quant-captures

Conversation

@georgeguimaraes

Copy link
Copy Markdown
Member

A quantized dot captured the scales and biases of its weight as program constants. The dispatch key held the full Quantization.Config, with these tensors. So each new weight compiled a new program, and the cache kept that program and its scales and biases. max_items and ttl are :infinity by default, so this memory stays until the VM stops.

With HuggingFaceTB/SmolLM2-360M-Instruct (q4 ONNX, through Transmuter), each load and generate cycle kept 82.8 MB on main. Three cycles kept 250.8 MB.

This change makes the scales and biases extra program inputs after the call's own inputs. The key holds only the structure of the weight: group size, bits, mode, transpose, the shape and type of the scales and biases, and their input positions. A native :while that carries a quantized weight also carries its scales and biases.

main this branch
a new weight with the same structure 1 new program 0 new programs
4096x4096 q4 weight, memory kept after the task 4.19 MB 0 MB
SmolLM2-360M, 3 load and generate cycles 82.8 / 166.9 / 250.8 MB 0 / 0 / 0 MB
SmolLM2-360M q4 decode, 64 tokens 134.9 tok/s 135.2 tok/s

Hex 0.5.0 has a second cause for the same leak: the thread-local MLX compile cache, which #149 fixed on main. A 0.5.x release with #149 and this change fixes both.

A quantized dot captured the scales and biases of its weight, and the dispatch key held the full quantization config. So each new weight compiled a new program, and that program kept the scales and biases in device memory while the cache held it.

The scales and biases are now extra program inputs, and the key holds only the structure of the weight. A native :while that carries a quantized weight also carries its scales and biases. Weights with the same structure share one program, and no program keeps weight memory.
Comment thread emlx/lib/emlx/native/expr.ex Outdated
Comment on lines +2388 to +2389
%EMLX.Quantization.Config{} = cfg,
cfg,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's restore the match here as a typesystem hint.

polvalente and others added 3 commits November 27, 2026 12:19
Keeps the struct pattern as a type hint, as requested in review.

Co-authored-by: Paulo Valente <polvalente@users.noreply.github.com>
The cfg argument is the spec map built by quant_signature/2, not an
EMLX.Quantization.Config struct, so the struct match fails. Match the map
keys the function reads, as a type hint.

Co-authored-by: Paulo Valente <polvalente@users.noreply.github.com>
@polvalente
polvalente merged commit 5d3879c into elixir-nx:main Nov 27, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants