Repository navigation
fix: Pass quantized scales and biases as program inputs - #168
Merged
Merged
Conversation
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.
polvalente
reviewed
Nov 27, 2026
Comment on lines
+2388
to
+2389
| %EMLX.Quantization.Config{} = cfg, | ||
| cfg, |
Member
There was a problem hiding this comment.
Let's restore the match here as a typesystem hint.
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>
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.
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_itemsandttlare:infinityby 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
:whilethat carries a quantized weight also carries its scales and biases.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.