From a3a487f415e6b04fb9e90ebfb5ff77cd1e1f17ff Mon Sep 17 00:00:00 2001 From: Chenjie Luo <108829653+cjluo-nv@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:48:46 -0800 Subject: [PATCH] Calibrate the GGML IQ2 experts-only recipes with local_hessian instead of imatrix (#3274) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### What does this PR do? Type of change: new feature (changes the default calibration of three built-in recipes) The three GGML IQ2 experts-only recipes, `iq2_xxs_experts_only`, `iq2_xs_experts_only` and `iq2_s_experts_only`, now calibrate with `local_hessian` (#3158) instead of imatrix. The IQ1 experts-only recipes, `iq1_s_experts_only` and `iq1_m_experts_only`, keep imatrix, which scored higher at IQ1 (see below). Both encode each expert weight once from one calibration forward pass, need no layerwise calibration, and pin the codes. local_hessian picks each group's local scale by the layer-output error under the 256-column Hessian of the tokens routed to that expert. imatrix picks it by the squared error weighted by `sqrt(E[x²])`, llama.cpp's statistic. Only the algorithm changes; the quantized weights (`*.experts.*`, `*block_sparse_moe*`) stay the same. The full `` recipes keep GPTQ followed by imatrix: GPTQ already encodes every weight there, and AutoQuantize accepts only `max` alongside GPTQ. **Accuracy.** SciCode subtask accuracy (%), imatrix → local_hessian, IQ2 formats, 8 runs per arm. Settings: 1,024 calibration samples, INT8 fake-quant serving (BF16 for Flash-Next), 64K token cap. Each ± is one standard error of the difference over the 8 runs per arm, not a 95% interval; |z| > 2 counts as significant. From #3158: | Model (quantized weights) | IQ2_XXS | IQ2_XS | IQ2_S | |---|---|---|---| | GLM-5.3-Flash (all routed experts, as these recipes) | 3.9 → 9.9 (**+6.0 ± 0.5**) | 12.5 → 18.3 (**+5.8 ± 0.5**) | 31.2 → 30.7 (−0.5 ± 0.6) | | Qwen3.8-Flash-Next (routed experts' `gate_up`) | 38.4 → 37.2 (−1.2 ± 0.9) | 40.2 → 39.1 (−1.1 ± 1.1) | 41.9 → 42.7 (+0.8 ± 0.8) | | Qwen3.8-27B (dense MLP, for the method) | 9.5 → 16.0 (**+6.5 ± 0.8**) | 20.6 → 26.5 (**+5.9 ± 1.0**) | 32.0 → 34.0 (+2.0 ± 1.1) | - **IQ2_S:** no significant gain on any model, at the same calibration cost as IQ2_XXS. It switches for consistency with the other IQ2 recipes; a follow-up PR that derives the super-block scale `d` is expected to make local_hessian better at IQ2_S. - **Never significantly worse than imatrix.** It is clearly better at IQ2_XXS and IQ2_XS on GLM and 27B, mostly through fewer runaway repetition loops. Flash-Next never loops under either and ties. - **GLM arm:** quantized exactly the weights these recipes quantize. - **Truncation:** 10–51% of generations per arm hit the 64K cap, so read these as relative comparisons under identical settings. - **Doom-loop detector:** on for both GLM arms and off for the Qwen arms at IQ2, the same within each model. **Why IQ1 keeps imatrix.** The same comparison at IQ1, run on `main` with the doom-loop detector on for every arm (`repetition_detection` 256/32/3): | Model (quantized weights) | IQ1_S SciCode, imatrix → local_hessian | IQ1_M SciCode, imatrix → local_hessian | KL vs BF16, IQ1_S / IQ1_M, imatrix → local_hessian | |---|---|---|---| | Qwen3.8-Flash-Next (routed experts' `gate_up`) | 8.96 → 3.03 (**−5.9 ± 0.6**) | 20.08 → 12.06 (**−8.0 ± 0.9**) | 0.543 → 0.597 / 0.447 → 0.473 | | GLM-5.3-Flash (all routed experts) | 0.0 → 0.0 (1 run each) | 0.0 → 0.0 (1 run each) | 1.495 → 1.537 / 1.253 → 1.300 | | Qwen3.8-27B (dense MLP) | 0.0 → 0.0 (1 run each) | 1.74 → 1.74 (±0.16) | 1.107 → 1.157 / 0.624 → 0.683 | - **Flash-Next is worse with local_hessian:** it is the only model that stays usable at IQ1, and local_hessian scores lower at both formats. It does not loop more; its answers are worse. - **KL agrees:** local_hessian's KL is higher in all six IQ1 pairs. - **Damping is not the cause:** on Flash-Next IQ1_M, `perc_damp` 1, 3 and 10 give KL 0.471, 0.474 and 0.467, all still above imatrix's 0.447. - **Working hypothesis:** at IQ1's coarse grid, imatrix's `sqrt(E[x²])`-weighted squared error is a better proxy for the network's output than the exact layer-output error local_hessian minimizes. **Cost.** Calibration on `main` (914e9abcb4, with the fused choice-search kernel #3218): IQ2_XXS, 1,024 × 512 samples, batch 16, 4× GB300, CUDA extension compiled ahead. Time is `mtq.quantize`, including the calibration forward pass. Memory is the peak above the loaded model, summed over GPUs. | Model, quantized weights | Time, imatrix → local_hessian | GPU memory, imatrix → local_hessian | |---|---|---| | GLM-5.3-Flash, all routed experts (this recipe's weights) | 494 s → 1,229 s (2.5×) | 207 → 279 GiB (+72 GiB, ≈18 GiB/GPU) | | Qwen3.8-Flash-Next, routed experts' `gate_up` | 302 s → 674 s (2.2×) | 21 → 70 GiB on 2 GPUs (+49 GiB) | - **Extra memory:** the Hessians, `in_features` KiB per expert weight, held on the GPU until that weight is encoded. They match the estimate from the model configs: 71 GiB for GLM. - **Host memory:** unchanged, about 5.5 GiB RSS. - **Flash-Next with the shipped recipe:** it fails before calibrating, with imatrix on `main` as well as with this change. The recipe also quantizes the experts' `down_proj`, whose 640-wide input is not a multiple of IQ2's 256-column block. That's why the Flash-Next arms above quantized `gate_up` only. - **When the Hessians don't fit:** override the algorithm with `{method: imatrix}`, as `ptq.md` now says. Layerwise calibration holds one layer's Hessians at a time, but it is not a memory fallback here. On GLM-5.3-Flash at batch 16 with `layerwise: {enable: true}`, local_hessian calibrated all 45 layers in 2,028 s, 1.65× slower. Its peak GPU memory above the model was 422 GiB, against 279 GiB without layerwise, because layerwise's own activation caching outweighs the Hessians it saves. ### Usage ```bash python examples/hf_ptq/hf_ptq.py --pyt_ckpt_path --recipe general/ptq/iq2_xxs_experts_only ... ``` To keep imatrix, override the algorithm with `algorithm: {method: imatrix}`. ### Testing - `tests/unit/recipe/test_presets.py`: the experts-only recipe test now expects `local_hessian` for the three IQ2 formats and imatrix for the two IQ1 formats. - `tests/unit/torch/quantization/test_ggml_imatrix.py`: - The per-expert imatrix test now sets `algorithm: imatrix` on the recipe's quantizers, keeping its coverage of imatrix. - The fused-expert HF export test runs the recipe's quantizers with both imatrix and local_hessian, which pin differently: imatrix without a source fingerprint, local_hessian with one. - On GB200 (oci-jhb), in the calibration container: - `tests/unit/recipe`, `test_ggml_imatrix.py`, `test_ggml_gptq.py`, `test_local_hessian.py`: 657 passed. - `test_local_hessian_gpu.py`, `test_ggml_imatrix_gpu.py`: 12 passed. - The GGML/experts case of `tests/gpu/torch/export/test_export.py`: 1 passed. - After the review fixes, with the fused-expert HF export test run for both imatrix and local_hessian: `tests/unit/recipe`, `test_ggml_imatrix.py`, `test_local_hessian.py`, `test_ggml_gptq.py`: 663 passed. - The end-to-end runs in the tables above. IQ1 run IDs and recipes are in the campaign workspace; the IQ1 SciCode runs used 8 runs per arm except where marked. ### Before your PR is "*Ready for review*" - Is this change backward compatible?: ✅ The recipes are new in 0.49.0, and existing configs and checkpoints are unaffected. - If you copied code from any other sources or added a new PIP dependency, did you follow guidance in `CONTRIBUTING.md`: N/A - Did you write any new necessary tests?: ✅ (updated the existing recipe tests) - Did you update [Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?: ✅ (the 0.49.0 entries that introduced these recipes now say which recipes use which algorithm) - Did you get Claude approval on this PR?: ❌ (pending) ### Additional Information Follows the GGML local-Hessian series: #3158, #3159, #3209, #3218. 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Summary by CodeRabbit - **New Features** - IQ2 experts-only quantization recipes now use local-Hessian calibration. IQ1 recipes continue to use imatrix calibration. - Added guidance on GPU memory requirements for IQ2 calibration and an imatrix fallback when expert Hessians do not fit. - Expanded quantization changelog coverage for weighted imatrix calibration and AutoQuantize support for GGML formats. - **Documentation** - Updated calibration guidance, including recommendations for layerwise calibration and batch size 1 during amax search. --------- Signed-off-by: Chenjie Luo Co-authored-by: Claude Opus 5.5 --- CHANGELOG.rst | 4 ++-- modelopt/torch/quantization/model_calib.py | 5 +++-- .../ptq/presets/model/iq2_s_experts_only.yaml | 6 +++--- .../ptq/presets/model/iq2_xs_experts_only.yaml | 6 +++--- .../presets/model/iq2_xxs_experts_only.yaml | 6 +++--- .../general/ptq/iq2_s_experts_only.yaml | 6 +++--- .../general/ptq/iq2_xs_experts_only.yaml | 6 +++--- .../general/ptq/iq2_xxs_experts_only.yaml | 6 +++--- modelopt_recipes/ptq.md | 14 +++++++++----- tests/unit/recipe/test_presets.py | 18 +++++++++--------- .../torch/quantization/test_ggml_imatrix.py | 10 ++++++---- 11 files changed, 47 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 94fbfc34851..7808326680e 100755 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,9 +16,9 @@ Changelog AutoQuantize path and preserve their linear-attention fusion groups during mixed-format export. - ``--use_fsdp2`` PTQ in ``examples/hf_ptq`` now supports vision-language models, not only Qwen3.5, calibrated on text or, with ``--calib_with_images``, on image-text samples; the vision tower is quantized only if the recipe enables it. A model whose forward requires images needs ``--calib_with_images``, and encoder-decoder models and Nemotron Nano VL are not supported yet. - Add experimental data-parallel/FSDP2 KV-cache AutoQuantize, including composed weight-then-KV searches in ``examples/hf_ptq`` with synchronized calibration, scoring, selection, and resumable per-rank checkpoints. FSDP2 recipe scoring uses a global sample budget and defaults to a calibration batch size of 1 per rank. -- Add imatrix calibration (``algorithm: {method: imatrix}``) for the GGML formats with a weighted search: each weight is encoded once, its search weighted by the square root of its inputs' per-column ``E[x^2]`` (llama.cpp's imatrix statistic), and HF fused-MoE experts get one statistic per expert. A weight that already holds final codes is left alone, so ``[{method: gptq}, {method: imatrix}]`` runs GPTQ where it applies and imatrix on the rest; the built-in IQ1 and IQ2 recipes now do this, and new ``_experts_only`` recipes apply imatrix to the routed MoE experts alone. +- Add imatrix calibration (``algorithm: {method: imatrix}``) for the GGML formats with a weighted search: each weight is encoded once, its search weighted by the square root of its inputs' per-column ``E[x^2]`` (llama.cpp's imatrix statistic), and HF fused-MoE experts get one statistic per expert. A weight that already holds final codes is left alone, so ``[{method: gptq}, {method: imatrix}]`` runs GPTQ where it applies and imatrix on the rest; the built-in IQ1 and IQ2 recipes now do this, and new ``iq1_s_experts_only`` and ``iq1_m_experts_only`` recipes apply imatrix to the routed MoE experts alone. - AutoQuantize now accepts GPTQ and imatrix candidates for the GGML formats (e.g. IQ2 with ``[{method: gptq}, {method: imatrix}]``), calibrating and scoring each from the original weights and keeping the selected candidates' codes in the returned model. A search with such candidates that fails before calibrating any of them leaves the model unconverted, so it can be retried on the same model. -- ``local_hessian`` now calibrates the GGML IQ formats, which it previously left unchanged: like imatrix it encodes each weight once, without layerwise calibration, and pins the codes, but picks each group's local scale by the layer-output error under its inputs' 256-column Hessian, damped by the new ``perc_damp`` (default 0.3). Use ``{method: local_hessian}`` in place of ``{method: imatrix}``, alone or after GPTQ; it helps most at IQ2_XXS and IQ2_XS, and like imatrix it skips the max-calibration forward pass when no activation quantizer needs one. +- ``local_hessian`` now calibrates the GGML IQ formats, which it previously left unchanged: like imatrix it encodes each weight once and pins the codes, but picks each group's local scale by the layer-output error under its inputs' 256-column Hessian, damped by the new ``perc_damp`` (default 0.3). Use ``{method: local_hessian}`` in place of ``{method: imatrix}``, alone or after GPTQ, as the new ``iq2_xxs_experts_only``, ``iq2_xs_experts_only`` and ``iq2_s_experts_only`` recipes do; it helps most at IQ2_XXS and IQ2_XS. - Add ``local_hessian_scales`` to the GPTQ config. GGML IQ formats then pick each block's local scales by the error GPTQ pays for that block once the later columns compensate, rather than by the squared error; on the IQ2_XXS models we tested it matched or beat both GPTQ and imatrix. - Add ``format_shard=(index, count)`` to ``mtq.auto_quantize`` to calibrate and score the searched formats in parallel processes, and ``mtq.merge_auto_quantize_checkpoints`` to combine the shard checkpoints into one that solves (and re-solves for other budgets) exactly like the unsharded search. Gradient and KL-divergence methods only. - GPTQ now also quantizes HuggingFace fused MoE experts, each with its own Hessian from the tokens routed to it; they were previously skipped. The built-in IQ1 and IQ2 recipes therefore run GPTQ on the experts too, and large MoEs need layerwise calibration to hold one layer's expert Hessians at a time. diff --git a/modelopt/torch/quantization/model_calib.py b/modelopt/torch/quantization/model_calib.py index f6e0ba3aed6..01513240cda 100644 --- a/modelopt/torch/quantization/model_calib.py +++ b/modelopt/torch/quantization/model_calib.py @@ -1352,8 +1352,9 @@ def local_hessian_calibrate( until it is encoded (1 KiB per input feature for GGML's 256-wide blocks), so all of them are held at once unless layerwise calibration bounds it to one layer's. - We recommend using Local-Hessian with layerwise calibration enabled - (``"layerwise": {"enable": True}``) and a calibration batch size of 1. + For the amax search, we recommend layerwise calibration (``"layerwise": {"enable": True}``) and + a calibration batch size of 1. Encoded weights need layerwise calibration only when their + Hessians do not fit at once. Args: model: Model to be calibrated. diff --git a/modelopt_recipes/configs/ptq/presets/model/iq2_s_experts_only.yaml b/modelopt_recipes/configs/ptq/presets/model/iq2_s_experts_only.yaml index 4602236b083..666c29bff3e 100644 --- a/modelopt_recipes/configs/ptq/presets/model/iq2_s_experts_only.yaml +++ b/modelopt_recipes/configs/ptq/presets/model/iq2_s_experts_only.yaml @@ -14,8 +14,8 @@ # limitations under the License. # QuantizeConfig preset for IQ2_S weight-only quantization of MoE routed experts only, calibrated -# with imatrix, which weights each expert's search by the inputs routed to it, collected in a -# single forward pass. +# with local_hessian, which picks each group's local scale by the layer-output error under the +# Hessian of the inputs routed to each expert, collected in a single forward pass. # modelopt-schema: modelopt.torch.quantization.config.QuantizeConfig imports: @@ -24,7 +24,7 @@ imports: iq2_s: configs/numerics/iq2_s algorithm: - method: imatrix + method: local_hessian quant_cfg: - $import: base_disable_all - quantizer_name: '*.experts.*weight_quantizer' diff --git a/modelopt_recipes/configs/ptq/presets/model/iq2_xs_experts_only.yaml b/modelopt_recipes/configs/ptq/presets/model/iq2_xs_experts_only.yaml index 9659c0e2dc1..b66ec0efb99 100644 --- a/modelopt_recipes/configs/ptq/presets/model/iq2_xs_experts_only.yaml +++ b/modelopt_recipes/configs/ptq/presets/model/iq2_xs_experts_only.yaml @@ -14,8 +14,8 @@ # limitations under the License. # QuantizeConfig preset for IQ2_XS weight-only quantization of MoE routed experts only, calibrated -# with imatrix, which weights each expert's search by the inputs routed to it, collected in a -# single forward pass. +# with local_hessian, which picks each group's local scale by the layer-output error under the +# Hessian of the inputs routed to each expert, collected in a single forward pass. # modelopt-schema: modelopt.torch.quantization.config.QuantizeConfig imports: @@ -24,7 +24,7 @@ imports: iq2_xs: configs/numerics/iq2_xs algorithm: - method: imatrix + method: local_hessian quant_cfg: - $import: base_disable_all - quantizer_name: '*.experts.*weight_quantizer' diff --git a/modelopt_recipes/configs/ptq/presets/model/iq2_xxs_experts_only.yaml b/modelopt_recipes/configs/ptq/presets/model/iq2_xxs_experts_only.yaml index 4f401bfd431..2027ed60f99 100644 --- a/modelopt_recipes/configs/ptq/presets/model/iq2_xxs_experts_only.yaml +++ b/modelopt_recipes/configs/ptq/presets/model/iq2_xxs_experts_only.yaml @@ -14,8 +14,8 @@ # limitations under the License. # QuantizeConfig preset for IQ2_XXS weight-only quantization of MoE routed experts only, calibrated -# with imatrix, which weights each expert's search by the inputs routed to it, collected in a -# single forward pass. +# with local_hessian, which picks each group's local scale by the layer-output error under the +# Hessian of the inputs routed to each expert, collected in a single forward pass. # modelopt-schema: modelopt.torch.quantization.config.QuantizeConfig imports: @@ -24,7 +24,7 @@ imports: iq2_xxs: configs/numerics/iq2_xxs algorithm: - method: imatrix + method: local_hessian quant_cfg: - $import: base_disable_all - quantizer_name: '*.experts.*weight_quantizer' diff --git a/modelopt_recipes/general/ptq/iq2_s_experts_only.yaml b/modelopt_recipes/general/ptq/iq2_s_experts_only.yaml index df8c1bb6e9d..bb208c43a88 100644 --- a/modelopt_recipes/general/ptq/iq2_s_experts_only.yaml +++ b/modelopt_recipes/general/ptq/iq2_s_experts_only.yaml @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -# IQ2_S weight-only PTQ of MoE routed experts only, with imatrix. +# IQ2_S weight-only PTQ of MoE routed experts only, with local_hessian. # modelopt-schema: modelopt.recipe.config.ModelOptPTQRecipe imports: @@ -22,7 +22,7 @@ imports: metadata: description: >- Applies GGML-compatible IQ2_S weight-only quantization to MoE routed experts only, each - expert's search weighted by imatrix statistics of the tokens routed to it. Shared experts, dense - layers and attention stay in BF16. Requires calibration data. + expert's local scales picked by its layer-output error on the tokens routed to it. Shared + experts, dense layers and attention stay in BF16. Requires calibration data. quantize: $import: preset diff --git a/modelopt_recipes/general/ptq/iq2_xs_experts_only.yaml b/modelopt_recipes/general/ptq/iq2_xs_experts_only.yaml index d1977c1e62d..efd907dc100 100644 --- a/modelopt_recipes/general/ptq/iq2_xs_experts_only.yaml +++ b/modelopt_recipes/general/ptq/iq2_xs_experts_only.yaml @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -# IQ2_XS weight-only PTQ of MoE routed experts only, with imatrix. +# IQ2_XS weight-only PTQ of MoE routed experts only, with local_hessian. # modelopt-schema: modelopt.recipe.config.ModelOptPTQRecipe imports: @@ -22,7 +22,7 @@ imports: metadata: description: >- Applies GGML-compatible IQ2_XS weight-only quantization to MoE routed experts only, each - expert's search weighted by imatrix statistics of the tokens routed to it. Shared experts, dense - layers and attention stay in BF16. Requires calibration data. + expert's local scales picked by its layer-output error on the tokens routed to it. Shared + experts, dense layers and attention stay in BF16. Requires calibration data. quantize: $import: preset diff --git a/modelopt_recipes/general/ptq/iq2_xxs_experts_only.yaml b/modelopt_recipes/general/ptq/iq2_xxs_experts_only.yaml index 075fa92488b..f2658db09f7 100644 --- a/modelopt_recipes/general/ptq/iq2_xxs_experts_only.yaml +++ b/modelopt_recipes/general/ptq/iq2_xxs_experts_only.yaml @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -# IQ2_XXS weight-only PTQ of MoE routed experts only, with imatrix. +# IQ2_XXS weight-only PTQ of MoE routed experts only, with local_hessian. # modelopt-schema: modelopt.recipe.config.ModelOptPTQRecipe imports: @@ -22,7 +22,7 @@ imports: metadata: description: >- Applies GGML-compatible IQ2_XXS weight-only quantization to MoE routed experts only, each - expert's search weighted by imatrix statistics of the tokens routed to it. Shared experts, dense - layers and attention stay in BF16. Requires calibration data. + expert's local scales picked by its layer-output error on the tokens routed to it. Shared + experts, dense layers and attention stay in BF16. Requires calibration data. quantize: $import: preset diff --git a/modelopt_recipes/ptq.md b/modelopt_recipes/ptq.md index c0173c008f1..7375eb34ca1 100644 --- a/modelopt_recipes/ptq.md +++ b/modelopt_recipes/ptq.md @@ -63,11 +63,11 @@ supported combinations. | `iq1_m` | IQ1_M W1A16 (1.75 bpw), MLP + MoE weights only | none | GPTQ (layerwise), then imatrix for weights GPTQ left | | `iq1_m_experts_only` | IQ1_M W1A16 (1.75 bpw), MoE routed experts only | none | imatrix | | `iq2_xxs` | IQ2_XXS W2A16 (2.06 bpw), MLP + MoE weights only | none | GPTQ (layerwise), then imatrix for weights GPTQ left | -| `iq2_xxs_experts_only` | IQ2_XXS W2A16 (2.06 bpw), MoE routed experts only | none | imatrix | +| `iq2_xxs_experts_only` | IQ2_XXS W2A16 (2.06 bpw), MoE routed experts only | none | local Hessian | | `iq2_xs` | IQ2_XS W2A16 (2.31 bpw), MLP + MoE weights only | none | GPTQ (layerwise), then imatrix for weights GPTQ left | -| `iq2_xs_experts_only` | IQ2_XS W2A16 (2.31 bpw), MoE routed experts only | none | imatrix | +| `iq2_xs_experts_only` | IQ2_XS W2A16 (2.31 bpw), MoE routed experts only | none | local Hessian | | `iq2_s` | IQ2_S W2A16 (2.56 bpw), MLP + MoE weights only | none | GPTQ (layerwise), then imatrix for weights GPTQ left | -| `iq2_s_experts_only` | IQ2_S W2A16 (2.56 bpw), MoE routed experts only | none | imatrix | +| `iq2_s_experts_only` | IQ2_S W2A16 (2.56 bpw), MoE routed experts only | none | local Hessian | | `q8_0` | Q8_0 W8A16 (8.5 bpw), eligible linears | none | none (no calibration) | @@ -161,8 +161,12 @@ activations and tensor-core math are what deliver the throughput. fused-MoE experts. - **`iq1_s_experts_only` / `iq1_m_experts_only` / `iq2_xxs_experts_only` / `iq2_xs_experts_only` / `iq2_s_experts_only`** — the same IQ formats on the MoE routed - experts only (`*.experts.*`, `*block_sparse_moe*`), calibrated with imatrix alone; shared - experts, dense layers and attention stay in BF16. + experts only (`*.experts.*`, `*block_sparse_moe*`), in one calibration pass; shared experts, + dense layers and attention stay in BF16. The IQ1 recipes calibrate with imatrix, which scored + higher at IQ1. The IQ2 recipes calibrate with `local_hessian`, which holds every expert weight's + Hessian blocks, `in_features` KiB each, on the GPU at once until encoding: 72 GiB over four GPUs + on GLM-5.3-Flash, more on larger MoEs, beyond what `hf_ptq.py`'s automatic batch size reserves. + If that does not fit, override the algorithm with `{method: imatrix}`. - **`q8_0`** — GGML-compatible weight-only quantization on eligible linear layers, with BF16 activations; `lm_head`, MoE routers, `conv1d` and the vision branch stay in BF16. Q8_0 uses 8.5 bits per weight and requires no calibration data. Quantized diff --git a/tests/unit/recipe/test_presets.py b/tests/unit/recipe/test_presets.py index 16f5d31a2d6..10a39f74340 100644 --- a/tests/unit/recipe/test_presets.py +++ b/tests/unit/recipe/test_presets.py @@ -201,17 +201,17 @@ def test_iq_recipe_matches_packing_contract(qformat, block_size, effective_bits) @pytest.mark.parametrize( - ("qformat", "block_size", "effective_bits"), + ("qformat", "block_size", "effective_bits", "method"), [ - ("iq1_s", IQ1_S_BLOCK_SIZE, IQ1_S_EFFECTIVE_BITS), - ("iq1_m", IQ1_M_BLOCK_SIZE, IQ1_M_EFFECTIVE_BITS), - ("iq2_xxs", IQ2_XXS_BLOCK_SIZE, IQ2_XXS_EFFECTIVE_BITS), - ("iq2_xs", IQ2_XS_BLOCK_SIZE, IQ2_XS_EFFECTIVE_BITS), - ("iq2_s", IQ2_S_BLOCK_SIZE, IQ2_S_EFFECTIVE_BITS), + ("iq1_s", IQ1_S_BLOCK_SIZE, IQ1_S_EFFECTIVE_BITS, "imatrix"), + ("iq1_m", IQ1_M_BLOCK_SIZE, IQ1_M_EFFECTIVE_BITS, "imatrix"), + ("iq2_xxs", IQ2_XXS_BLOCK_SIZE, IQ2_XXS_EFFECTIVE_BITS, "local_hessian"), + ("iq2_xs", IQ2_XS_BLOCK_SIZE, IQ2_XS_EFFECTIVE_BITS, "local_hessian"), + ("iq2_s", IQ2_S_BLOCK_SIZE, IQ2_S_EFFECTIVE_BITS, "local_hessian"), ], ) -def test_iq_experts_only_recipe_quantizes_the_routed_experts_with_imatrix( - qformat, block_size, effective_bits +def test_iq_experts_only_recipe_quantizes_the_routed_experts( + qformat, block_size, effective_bits, method ): quantize = load_recipe(f"general/ptq/{qformat}_experts_only").quantize.model_dump( exclude_unset=True @@ -227,7 +227,7 @@ def test_iq_experts_only_recipe_quantizes_the_routed_experts_with_imatrix( assert weight_cfg["backend"] == "ggml" assert weight_cfg["block_sizes"][-1] == block_size assert weight_cfg["effective_bits"] == effective_bits - assert quantize["algorithm"] == {"method": "imatrix"} + assert quantize["algorithm"] == {"method": method} # --- RecipeSupersededAction: the flags --recipe replaces ---------------------------------------- diff --git a/tests/unit/torch/quantization/test_ggml_imatrix.py b/tests/unit/torch/quantization/test_ggml_imatrix.py index ce59f059bf0..8edb0b37994 100644 --- a/tests/unit/torch/quantization/test_ggml_imatrix.py +++ b/tests/unit/torch/quantization/test_ggml_imatrix.py @@ -206,9 +206,9 @@ def test_imatrix_weights_each_fused_expert_by_its_own_tokens(monkeypatch, moe): original = copy.deepcopy(moe.mlp) inputs = _inputs(rows=96, columns=HIDDEN) calls = _record_importance(monkeypatch) - # The shipped experts-only recipe, which calibrates in a single pass. + # The shipped experts-only recipe's quantizers, calibrated with imatrix in a single pass. recipe = load_recipe("general/ptq/iq2_xxs_experts_only").quantize.model_dump() - assert recipe["algorithm"]["method"] == "imatrix" + recipe["algorithm"] = {"method": "imatrix"} mtq.quantize(moe, recipe, lambda m: m(inputs)) # The shared expert stays in BF16. @@ -242,9 +242,11 @@ def test_imatrix_weights_each_fused_expert_by_its_own_tokens(monkeypatch, moe): assert torch.equal(weight[idx], original_weight[idx]) -def test_hf_export_writes_each_fused_expert_s_pinned_rows(moe): - # imatrix leaves the experts' weights unrounded, and export splits gate_up into gate and up. +@ENCODERS +def test_hf_export_writes_each_fused_expert_s_pinned_rows(moe, algorithm): + # Both encoders leave the experts' weights unrounded, and export splits gate_up into gate and up. recipe = load_recipe("general/ptq/iq2_xxs_experts_only").quantize.model_dump() + recipe["algorithm"] = algorithm mtq.quantize(moe, recipe, lambda m: m(_inputs(rows=96, columns=HIDDEN))) experts = moe.mlp.experts pins = [