Skip to content

Expose stable hf_ kernel/packing/repacking APIs (#726) - #3251

Merged
Qubitium merged 3 commits into
mainfrom
feat/hf-packing-api
Oct 11, 2026
Merged

Qubitium merged 3 commits into
mainfrom
feat/hf-packing-api

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Implements the stable hf_ kernel/packing/repacking API requested in #726 for external integrators.

API surface

  • hf_check_packing_feasibility(...) and hf_check_best_packing_format(...)
  • hf_pack_layer(...) and BaseQuantLinear.hf_pack(...)
  • hf_post_init(...)
  • hf_repack_layer(...)

Correctness fixes

  • GPTQ qzero alignment uses runtime qzero_format() metadata instead of inferring the source domain from REQUIRES_FORMAT_V2.
  • Repacking propagates qzero-format metadata and converts v1/v2 domains for the target kernel.
  • hf_pack_layer accepts checkpoint_format and performs the requested GPTQ v1/v2 conversion.
  • Reusing a GPTQQuantLinear module for a second raw pack resets qzero metadata before conversion, preventing stale v2 state from corrupting freshly packed v1 qzeros.
  • AUTO feasibility with repack=True evaluates all eligible candidates.
  • Raw packing feasibility no longer advertises TorchInt8Linear or TorchInt8AwqLinear, which are repack-only targets.
  • Missing scales return a clear ValueError.
  • hf_select_quant_linear honors checkpoint_format.
  • Documentation uses hf_select_quant_linear_v2 for format-aware selection.
  • ABI-transforming runtime kernels remain rejected as repack sources.

Validation

Environment:

  • conda: gp_314t
  • CUDA_VISIBLE_DEVICES=0,1,2,3
  • 4 x NVIDIA GeForce RTX 4090 (SM89)
  • PyTorch 2.14.0+cu130

Installed and exercised optional backends:

  • bitblas 0.1.0.post1
  • bitsandbytes 0.50.2
  • llama-cpp-python 0.3.36

Results:

  • tests/test_hf_packing_api.py: 43 passed, 1 skipped
  • tests/kernels/test_selection.py: 85 passed, 21 skipped
  • BitBLAS/AWQ BitBLAS/BitsAndBytes/GGUF unit coverage: 48 passed
  • Machete/Swordfish/AWQ kernel coverage: 42 passed, 19 skipped

The remaining Machete and Swordfish skips are hardware-gated: RTX 4090 is SM89, while Machete requires Hopper SM90 and Swordfish requires Blackwell SM100/SM103/SM110. MLX kernels have no supported device in this CUDA/NVIDIA environment.

Documentation: docs/hf_packing_api.md

Closes #726.

Add `gptqmodel.utils.packing` with stable, `hf_`-prefixed entry points for
external integrators (optimum, auto-round) as requested in #726:

- `hf_check_packing_feasibility()`: probe whether a kernel can pack a
  quantization contract on the target device. `repack=True` probes repack
  targets instead of raw-weight packers (Marlin/ExllamaV2 consume GPTQ
  tensors and convert them in `post_init()`).
- `hf_check_best_packing_format()`: rank formats by fastest feasible kernel
  priority and return the preferred feasible format.
- `hf_pack_layer()` / `BaseQuantLinear.hf_pack()`: layer-wise packing that
  mirrors the internal pack path (original/block/gpu), synthesizes symmetric
  zero points when omitted, and requires explicit zeros for `sym=False`.
- `hf_post_init()`: layer-scoped `post_init()` for a layer or every quantized
  child of a container, with kwargs for kernels such as ExllamaV2 that need a
  scratch space.
- `hf_repack_layer()`: repack a quantized layer for another backend via native
  `repack_from_gptq`/`repack_from_awq` hooks or layout-compatible copies,
  aligning GPTQ v1/v2 qzero flavors; sources whose buffers are a private
  runtime ABI (Marlin/Machete/Swordfish/BitBLAS) are rejected.

Add `tests/test_hf_packing_api.py` with CPU and CUDA coverage (pack parity
against `pack_original`/`pack_block`, feasibility/best-format probing, GPU
packing, Marlin and ExllamaV2 repack targets, multi-GPU device forms) and
document the API in `docs/hf_packing_api.md`, linked from the README.
@ZX-ModelCloud
ZX-ModelCloud marked this pull request as ready for review October 11, 2026 05:44
@Qubitium
Qubitium merged commit 5cef672 into main Oct 11, 2026
6 checks passed
@Qubitium
Qubitium deleted the feat/hf-packing-api branch October 11, 2026 06:48
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.

[INTEGRATION] Expose stable kernel/packing/repacking apis

2 participants