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 = [