refactor: make QuantizationMode an algebraic enum with associated values (#285)#393
Open
VDurocher wants to merge 3 commits intoml-explore:mainfrom
Open
refactor: make QuantizationMode an algebraic enum with associated values (#285)#393VDurocher wants to merge 3 commits intoml-explore:mainfrom
VDurocher wants to merge 3 commits intoml-explore:mainfrom
Conversation
…ues (ml-explore#285) - Convert QuantizationMode from String raw value enum to algebraic enum - Add case affine(groupSize: Int = 64, bits: Int = 4) with associated values - Keep mxfp4, mxfp8, nvfp4 as simple cases (fixed parameters) - Add cName computed property to replace rawValue for C API calls - Add groupSize and bits computed properties on QuantizationMode - Add manual Codable conformance (required since associated values prevent String raw type) - Add Equatable conformance (auto-synthesized by Swift) - Update all call sites: mode.rawValue → mode.cName - Update all default parameter values: .affine → .affine() - Update Quantized.swift with matching .affine() defaults
Update all default parameter values from .affine to .affine() to match the new algebraic QuantizationMode enum where affine carries associated values (groupSize and bits).
- Convert QuantizationMode from String raw value enum to algebraic enum - Add case affine(groupSize: Int = 64, bits: Int = 4) with associated values - Keep mxfp4, mxfp8, nvfp4 as simple cases with fixed parameters - Add cName computed property to replace rawValue for C API calls - Add groupSize and bits computed properties on QuantizationMode - Add manual Codable conformance (associated values prevent String raw type) - Add Equatable conformance (auto-synthesized by Swift) - Update all call sites: mode.rawValue -> mode.cName - Update all default parameter values: .affine -> .affine() Closes ml-explore#285
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.
Closes #285
What
Converts
QuantizationModefrom a scalar enum (backed byStringraw values) to an algebraic enum, allowing theaffinecase to carry its own configuration parameters.Why
Different quantization schemes have fundamentally different parameters. Previously,
groupSizeandbitswere passed as separate top-level arguments alongside the mode, creating an implicit coupling: callers had to know which parameters applied to which mode. With associated values, the configuration is explicit, exhaustively type-checked, and co-located with the mode it configures.As noted in the issue,
mxfp4only allowsgroupSize = 32andbits = 4— encoding these as separate top-level properties forces callers to reason about which combinations are valid. Associated values eliminate that ambiguity.Changes
Source/MLX/Ops.swiftQuantizationModecases now:case affine(groupSize: Int = 64, bits: Int = 4)— carries its parameters with sensible defaultscase mxfp4,case mxfp8,case nvfp4— no associated values (parameters are fixed by format spec): String, Codable, Sendablewith: Equatable, Sendable(associated values are incompatible withStringraw type)var cName: Stringcomputed property to bridge to the C API (replaces.rawValue)public var groupSize: Intandpublic var bits: Intcomputed properties — these return the associated values for.affineand the spec-mandated values for other modesextension QuantizationMode: Codablewith manualencode/init(from:)implementationsmode.rawValuecall sites updated tomode.cName(7 occurrences)mode: QuantizationMode = .affinedefault parameters updated to.affine()(6 occurrences)Source/MLXNN/Quantized.swiftmode: QuantizationMode = .affinedefault parameters updated to.affine()(12 occurrences acrossquantizeSingle,quantize,QuantizedEmbedding, andQuantizedLinear)Migration
The existing
groupSizeandbitstop-level parameters on public functions are preserved for backward compatibility. TheQuantizationMode.groupSizeandQuantizationMode.bitscomputed properties provide a unified way to query a mode's effective parameters.Notes
QuantizationMode.affine(bare), as noted in the issueCodableencoding format changes from a plain string ("affine") to a keyed container ({"type":"affine","groupSize":64,"bits":4}) — existing serialized data using the old format would need migrationEquatableconformance is auto-synthesized by Swift for enums withEquatable-conforming associated values