Repository navigation
fix(models): honor third-party checkpoint interop in save, Marlin, and export (#4053) - #4059
Merged
Merged
Conversation
…d export (#4053) - keep the caller's live model across save_quantized() instead of swapping in the CPU save shell - decode a non-contiguous g_idx through Marlin's activation-order path - accept is_marlin_format: false, reject only true - route GPTQModel.export(..., "hf") through utils.model_dequant - resolve per-module dynamic bits for GPTQ dequantization - map torch backend for qqq/gguf/bitsandbytes and normalize string bits / null sym
This was referenced Nov 14, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 14, 2026
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.
Part of #4053
This is the second half of #4053: items 4-8 and the Minor list. Items 1-3 are covered by the sibling PR.
TL;DR
Five interop paths now trust the checkpoint instead of a runtime default:
save()keeps the caller's live model, Marlin decodes a non-contiguousg_idx,is_marlin_format: falseis accepted,export(..., "hf")routes through the checkpoint dequantizer, and GPTQ dequantization resolves per-moduledynamicbits. 12 files, +~250/-~45, plus 4 new test files anddocs/checkpoint-interop.md.1. Root causes and fixes
models/writer.py:2027assigned the save shell toself.model, leaving a CPUTorchCpuAvx512Linearmodel behind aftersave().model_for_save(models/writer.py:2029) and use it for every save step;self.modelis untouched.nn_modules/qlinear/marlin.py:413replacedg_idxwith an empty tensor wheneverdesc_act=False, ignoring a block-permuted stored index._g_idx_is_contiguous()(marlin.py:65) detects the mismatch inpost_init(marlin.py:374); the module switches tomarlin_sort_g_idx+ repack, or fails closed on misaligned shapes.quantization/config.py:4045/:4633raised for anyis_marlin_formatvalue._is_truthy_compat_flag()(config.py:4126) acceptsfalse/"false"silently; onlytrueraises (config.py:4092,:4751).models/auto.py:1101dequantized the runtime model withnn_modules/qlinear/torch.dequantize_model, which accepts onlyTorchLinear, turns quantized embeddings intonn.Linear, and hard-codes fp16. Legacyquantize_config.json-only checkpoints failed earlier atauto.py:1058.format="hf"goes straight toutils/model_dequant.dequantize_model(auto.py:1068); that path handles AWQ, GPTQ, embeddings, and merges legacyquantize_config.json(model_dequant.py:1975). Auxiliary.cache/.gitdirs are skipped (model_dequant.py:1939).utils/model_dequant.py:1815used the globalbitsfor every GPTQ module._gptq_module_int()(model_dequant.py:1573) resolvesdynamicper module; the default is normalized withquant_bits_width(model_dequant.py:1827)._LEGACY_BACKEND_BY_METHODhad noqqq/gguf/bitsandbytesentry; string bits failed inside packers;sym: nullsurvived parsing.utils/backend.py:169), normalized bits in dequant, and defaultedsym=NonetoTrue(config.py:4490).2. New flow: save keeps the live model
flowchart TD A["save_quantized()"] --> B{"load_quantized_model<br/>and format != EXL3?"} B -- no --> C["model_for_save = self.model"] B -- yes --> D["model_for_save = get_model_with_quantize(qcfg, model_local_path)"] D --> E["write config from model_for_save"] C --> E E --> F["get_state_dict_for_save(model_for_save)"] F --> G["write safetensors shards"] G --> H["self.model unchanged"] H --> I["caller inference keeps live kernel"] style D fill:#fff4cc,stroke:#b8860b style H fill:#d9f2d9,stroke:#2e7d323. New flow: Marlin
g_idxflowchart TD A["MarlinLinear.post_init()"] --> B{"self.desc_act?"} B -- yes --> F["activation-order path"] B -- no --> C{"_g_idx_is_contiguous(g_idx, K, group_size)?"} C -- yes --> D["contiguous path: empty g_idx"] C -- no --> E{"marlin_is_tile_aligned(N, K)?"} E -- no --> X["raise NotImplementedError<br/>load a non-Marlin backend"] E -- yes --> G["set desc_act = True<br/>is_k_full = True"] G --> F F --> H["marlin_sort_g_idx + gptq_marlin_repack"] style X fill:#f8d7da,stroke:#b02a37 style G fill:#fff4cc,stroke:#b8860b4. New flow: export
flowchart TD A["GPTQModel.export(model, target, format)"] --> B{"format == hf?"} B -- yes --> C["resolve local snapshot"] C --> D["dequantize_model(source, target, trust_remote_code)"] D --> E["config.json without quantization_config"] E --> F["dense safetensors + copied aux files"] B -- no --> G["AutoConfig + backend selection"] G --> H["GPTQModel.load"] H --> I["format == mlx: convert_gptq_to_mlx_weights"] style D fill:#fff4cc,stroke:#b8860b style F fill:#d9f2d9,stroke:#2e7d325. New flow: per-module GPTQ bits
flowchart TD A["convert_gptq_file(path, config)"] --> B["default_bits = quant_bits_width(config.bits)"] B --> C["for each module prefix"] C --> D["bits = _gptq_module_int(config, prefix, bits, default_bits)"] D --> E{"bits == 3?"} E -- yes --> F["3-bit unpack path"] E -- no --> G["standard unpack_rows / unpack_cols"] F --> H["fp64 weight reconstruction"] G --> H H --> I["finalize_for_save(target_dtype)"] style D fill:#fff4cc,stroke:#b8860bThe same diagrams and contracts are committed in
docs/checkpoint-interop.md, linked fromREADME.md.6. Failure and ambiguity contract
save()on a loaded Marlin checkpointself.modelbecomes a CPU shell; logits rel 1.47 off (issue)self.modelunchanged; new test asserts rel < 1e-3g_idx,desc_act=Falseg_idx, misaligned shapesNotImplementedError(use a non-Marlin backend)is_marlin_format: trueis_marlin_format: falseexport("hf")on AWQValueError: Only models loaded using TorchLinear...export("hf")on quantized embeddingsnn.Linearshape errorexport("hf")with onlyquantize_config.jsonAttributeError: 'BloomConfig' object has no attribute 'quantization_config'dynamicbits7. Fail-before / pass-after
test_save_keeps_live_model_inferencefailed with the CPU shell (Expected all tensors to be on the same device); issue measured rel 1.47.q_projrel err 0.13885; whole-model Marlin vs Torch rel 0.8756, top-1 0%.QuantizeConfig.from_quant_config(... is_marlin_format=False)raisedValueError.falseaccepted; gemma-1.1-2b-it-GPTQ and hugging-quants Llama-3.1-8B load with Marlin and return finite logits.AttributeError: 'BloomConfig' object has no attribute 'quantization_config'; AWQ export:ValueError: Only models loaded using TorchLinear....AutoModelForCausalLM.from_pretrainedand return finite logits; synthetic 3-bit/4-bit and 4-bit/8-bit exports match the fp64 oracle at bf16 precision.RuntimeError: The size of tensor a (64) must match ... (128); string bits:TypeError: unsupported operand type(s) for //: 'int' and 'str'.bits: "4"decode exactly against an fp64 oracle.normalize_backend("torch", quant_method=QQQ)returnedBACKEND.TORCH.QQQ_TORCH/GGUF_TORCH/BITSANDBYTES;sym: null->True; string bits -> int.8. Test commands and results
CUDA venv (
/opt/venvs/cu130t/bin/python,CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d):ROCm venv (
/opt/venvs/rocm101t/bin/python,HIP_VISIBLE_DEVICES=2):Item 5 PPL, 32 non-overlapping 512-token wikitext-2 windows, Qwen2-1.5B-Instruct-GPTQ-4bits-gp32:
The issue's absolute 48.3/141.8 split could not be reproduced with this windowing; the correctness invariant is that Marlin now matches the Torch decode (0.06% apart) and the layer error drops from 1.4e-1 to 0. Before the fix Marlin ignored
g_idxand produced different tokens.tests/test_q4_marlin.pycannot be collected on the CUDA venv because the environment lacksparameterized(pre-existing, same astests/kernels/test_gptq.py); the gemma case was exercised directly withGPTQModel.load(..., backend=BACKEND.MARLIN)and returned finite logits.9. Hardware
GPU-d458708b-c760-bbf1-08fb-8b8803a2157d(never the RTX 5090).HIP_VISIBLE_DEVICES=2for CPU-verifiable tests.10. Reviewer notes
get_model_with_quantizeitself is owned by the sibling PR; this PR only stops assigning its result toself.model.g_idxon tile-misaligned shapes fails closed rather than silently decoding contiguous groups.export("hf")keeps the empty-directory case working (resume=True) and refuses a non-empty target, matchingdequantize_model's contract.