Conversation
Adds a hidden, settable-but-not-public `_sparsity` field to QuantizationSpec and PalettizationSpec: weights are pre-sparsified before fake-quant/palettize, and finalize() inserts sparse_to_dense (plus lut_to_dense for palettization) in the correct op order. Validation of unsupported combinations (asymmetric quant, unsigned/FP4 dtypes, quantized LUTs, per-channel scale, non-per-tensor granularity) now happens at spec-construction time via pydantic.
Moves the [0, 1] range check for _sparsity into the existing _validate_sparsity(_zero_preserving) methods so each spec has a single validation entrypoint, and shortens a couple of over-long comments.
Resolves conflicts from main's graph-mode rewrite (qspec reconciliation pipeline, ensure_initialized-based palettization clustering, factory/import renames) and threads the sparsity plumbing through the new code paths: - _KMeansFakePalettize.__init__ now takes sparsity explicitly and forwards it to the base class, since upstream removed the old **kwargs passthrough. - The graph-mode qspec reconciliation pipeline (_qspec_types.py, _provisional_qspec_generation.py, _qspec_constraints.py, _qspec_resolution.py) rebuilds a fresh QuantizationSpec from reconciled fields; added a SPARSITY field so the private _sparsity value round-trips through that rebuild instead of silently reverting to None. Verified: tests/export/test_joint_sparsity.py (4/4), tests/quantization/ + tests/palettization/ (2059/2059, excluding slow), tests/export/ full suite (all passing, excluding slow).
…rse ops Spec construction no longer validates sparsity-compatibility; instead _validate_sparsity_for_export (in each technique's _prepare_for_export.py) checks at finalize time: quantization requires a zero (or absent) zero_point, palettization requires per-tensor granularity and scalar (cluster_dim=1) palettization. The same check now gates both backends -- CoreML's export path registers a PRUNING-first compression_type list so coremltools' own torch-frontend converter auto-detects the sparsity pattern from the traced weight's real zeros and builds the matching constexpr_sparse_to_dense / constexpr_sparse_blockwise_shift_scale / constexpr_lut_to_sparse chain, mirroring CoreAI's sparse_to_dense / lut_to_dense chain. Adds tests/export/test_joint_compression.py, covering the quantization dtype/qscheme/granularity matrix and the palettization n_bits/cluster_dim/ granularity matrix against both export backends, on MNIST and ResNet.
u-simha
marked this pull request as ready for review
September 11, 2026 22:51
…-support' into u/usimha/add-quant-plus-sparsity-support
u-simha
commented
Sep 15, 2026
| FieldName.QPARAM_CALCULATOR_CLS: "qparam_calculator_cls", | ||
| FieldName.RANGE_CALCULATOR_CLS: "range_calculator_cls", | ||
| FieldName.SCALE_DTYPE: "scale_dtype", | ||
| FieldName.SPARSITY: "_sparsity", |
Contributor
Author
There was a problem hiding this comment.
@anotheranshu - I realized adding a new spec value is pretty cumbersome; is there a way we can have this as **args or something? There are 4 files I needed to update, which is not ideal.
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.
What:
How:
_sparsityfield to theQuantizationSpecandPalettizationSpecto sparsity the weights before the fake quantization & fake palettization is appliedprepare_for_exportfor both quantization & palettization to insert thesparse_to_denseops in the correct orderConstraints:
prepare()call)Example quant + sparsity config:
Example palett + sparsity config: