Skip to content

fix(packing): restore GPTQ runtime zeros and preserve repack buffer dtypes - #3845

Merged
Qubitium merged 1 commit into
mainfrom
fix/3822-hf-packing-helpers
Nov 13, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/3822-hf-packing-helpers

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

TL;DR

Fixes three backend-contract failures in the stable hf_ packing helpers. Net diff: 3 files, +310/−17. The helpers now (1) restore the GPTQ v2 runtime zero-point layout in the standalone hf_post_init, (2) preserve source checkpoint buffer dtypes across hf_repack_layer, and (3) dispatch AWQ kernels by declared method instead of a QUANT_TYPE name prefix.

Closes #3822.

1. The problem

1.1 Split pack/init decoded GPTQ zero points one scale high

hf_pack_layer(..., post_init=False) (the default) leaves a gptq checkpoint request in the v1 export layout. The inline post_init=True path converts v1→v2 before post_init(), but the standalone hf_post_init only called post_init().

Mechanism Before After
Standalone init hf_post_init called post_init() directly (gptqmodel/utils/packing.py:721 and :726 at base) _restore_gptq_runtime_qzeros runs first (:248)
Inline init converted v1→v2 before post_init() same correction, now shared through the helper

A zero-weight 128×64 TorchLinear(bits=4, group_size=32, sym=True) returned 128 per output channel after hf_post_init instead of 0.

1.2 Repack narrowed checkpoint buffers to FP16

hf_repack_layer's dtype argument selects the target kernel's activation/output dtype. The copy path used plain load_state_dict into buffers the target allocated at its own width (the AWQ target allocates FP16), so a BF16 bias 65536 became inf and a 2**-30 FP32 scale became 0.

Mechanism Before After
Buffer copy load_state_dict cast into target allocations (:785-786 at base) _preserve_checkpoint_buffer_dtypes retargets shared floating buffers to the source dtype first (:819, :828)

1.3 AllSpark AWQ was rejected by the dispatcher

AllSparkAwqLinear declares METHOD.AWQ and QUANT_TYPE="allspark" and provides pack(). The dispatcher recognized AWQ only by the awq_/llm-awq name prefix, then required pack_original and raised.

Mechanism Before After
AWQ recognition QUANT_TYPE prefix only (:577 at base) prefix or METHOD.AWQ in SUPPORTS_METHODS (:609)
AllSpark ValueError: does not implement a supported packing path routed to its AWQ pack()

2. The new flow

flowchart TD
    A["hf_pack_layer(module, ...)"] --> B{"QUANT_TYPE == 'qqq'?"}
    B -->|yes| C["module.pack(linear, scales, s_extra)"]
    B -->|no| D{"AWQ backend?<br/>QUANT_TYPE prefix OR METHOD.AWQ declared"}
    D -->|yes| E["module.pack(linear, scales, zeros, g_idx)"]
    D -->|no| F{"hasattr(module, 'pack_original')?"}
    F -->|no| G["raise ValueError"]
    F -->|yes| H["pack_gpu / pack_block / pack_original"]
Loading
flowchart LR
    A["hf_pack_layer(post_init=False)"] --> B["v1 checkpoint words<br/>qzero_format() == 1"]
    B --> C["hf_post_init(module or container)"]
    C --> D{"GPTQ and REQUIRES_FORMAT_V2<br/>and qzero_format() == 1?"}
    D -->|yes| E["convert_gptq_v1_to_v2_format_module"]
    D -->|no| F["skip"]
    E --> G["module.post_init()"]
    F --> G
Loading
flowchart TD
    A["hf_repack_layer(source, backend, dtype=...)"] --> B["_instantiate_kernel(target_cls)"]
    B --> C["target buffers at kernel default width"]
    C --> D["_copy_packed_state(source, target)"]
    D --> E{"shared floating buffer<br/>source dtype != target dtype?"}
    E -->|yes| F["retarget buffer to source dtype"]
    E -->|no| G["keep target width"]
    F --> H["load_state_dict copies checkpoint values"]
    G --> H
    H --> I["post_init() applies kernel ABI conversion"]
Loading

3. Failure and ambiguity contract

Situation Before After
hf_post_init on a v1-layout REQUIRES_FORMAT_V2 GPTQ layer decoded zp-1 words converted to v2, then post_init()
hf_post_init on a kernel without REQUIRES_FORMAT_V2 (Marlin, ...) unchanged unchanged (v1 is correct for them)
hf_repack_layer with BF16/FP32 source buffers narrowed to target FP16 source width preserved; post_init() still applies its ABI conversion
hf_pack_layer on a kernel declaring METHOD.AWQ without an awq_ prefix ValueError AWQ pack()
hf_pack_layer on a GPTQ kernel without pack_original ValueError unchanged

4. Change inventory

Area File ± lines Purpose
Packing helpers gptqmodel/utils/packing.py +96/−17 qzero restore, dtype preservation, method-based AWQ dispatch
Tests tests/test_hf_packing_api.py +163 split-init, BF16/FP32 repack, AllSpark dispatch regressions
Docs docs/hf_packing_api.md +68 dispatch/lifecycle diagrams and contracts

5. Tests

Test Invariant
test_hf_post_init_restores_gptq_v2_runtime_layout[layer/container] split pack/init returns exact zeros and qzero_format() == 2
test_hf_repack_layer_preserves_awq_bfloat16_checkpoint_buffers BF16 bias 65536 survives; output finite and equal to source
test_hf_repack_layer_preserves_awq_fp32_checkpoint_scales 2**-30 FP32 scale does not underflow to zero
test_hf_pack_layer_dispatches_allspark_awq hf_pack_layer output matches direct pack()

6. Validation

7. Reviewer notes

  • The dtype rule is applied at the copy boundary, not in one target constructor: the source kernel owns the checkpoint storage width, and every copy target benefits. Kernels that need a specific runtime width still convert in their own post_init() (for example ExLlamaV2's FP16 scales).
  • The AWQ dispatch keeps the legacy QUANT_TYPE prefix match as a fallback so existing awq_* kernels are unaffected.
  • No quantization math, kernel arithmetic, or checkpoint container changed.

…types

- hf_post_init restores the v2 runtime zero-point layout for GPTQ kernels
  that require it, matching hf_pack_layer(post_init=True) after a split
  pack/initialize lifecycle.
- hf_repack_layer keeps floating checkpoint buffers at the source storage
  width instead of narrowing them into FP16 target allocations; dtype only
  selects the activation/output dtype.
- hf_pack_layer routes kernels that declare METHOD.AWQ to their AWQ packer,
  so AllSpark (QUANT_TYPE="allspark") is no longer rejected for lacking
  pack_original.
@ZX-ModelCloud
ZX-ModelCloud force-pushed the fix/3822-hf-packing-helpers branch from f7e546f to 92167ad Compare November 13, 2026 09:02
@Qubitium
Qubitium merged commit 64236bc into main Nov 13, 2026
4 of 6 checks passed
@Qubitium
Qubitium deleted the fix/3822-hf-packing-helpers branch November 13, 2026 09:04
ZX-ModelCloud added a commit that referenced this pull request Nov 13, 2026
Restore the import order required by ruff after #3845 so the repository
lint gate passes again.
Qubitium pushed a commit that referenced this pull request Nov 13, 2026
…ough (#3846)

* fix(writer): accept safe nested per-layer shard paths in EXL3 passthrough

The EXL3 native checkpoint passthrough builder rejected every index shard
entry whose name was not a bare basename. The public reshard() per-layer
layout emits nested paths such as model.layers.0/layer.safetensors, so a
valid resharded source could not be published.

Accept safe relative paths while still rejecting absolute or
drive-qualified paths, backslashes, NUL bytes, and empty, "." or ".."
components. Hugging Face snapshot symlinks that resolve outside the source
root remain permitted.

Adds regression coverage for the reshard() layout and for rejected unsafe
paths.

* chore(lint): sort quantizer imports in gptq.py

Restore the import order required by ruff after #3845 so the repository
lint gate passes again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Public hf_ packing helpers mishandle GPTQ initialization, AWQ dtype preservation, and AllSpark dispatch

2 participants