Skip to content

feat(gptq): load-time GPTQ repack onto the AWQ path - #5160

Open
windreamer wants to merge 6 commits into
stack/c2-ct-normalizefrom
stack/c3-gptq-repack
Open

windreamer wants to merge 6 commits into
stack/c2-ct-normalizefrom
stack/c3-gptq-repack

Conversation

@windreamer

@windreamer windreamer commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

TurboMind's strongest-performing assets are its mixed-precision W4A16 GEMM kernels, but the TurboMind runtime is being retired in favor of the PyTorch engine. This stack grafts the W4A16 GEMM family into the PyTorch engine: weights are packed into TurboMind kernels at load time, forward runs on TurboMind kernels, and the PyTorch engine keeps owning scheduling, KV-cache management, and CUDA graphs.

This PR teaches the load path to serve GPTQ dense checkpoints: the GPTQ pack axis, nibble order, and zero-point offset are absorbed at load time, landing on the same canonical AWQ layout as PRs 1-2 — and GPTQ MoE checkpoints route through the same AWQ path (grouped-MoE specifics land in PR 4 and #5169).

Modification

  • gptq_repack + normalize_gptq_weights: GPTQ/AWQ pack discrimination is decisive (qwidth/scales relation); symmetric checkpoints synthesize the zero reference on flush.
  • Loud rejections: desc_act (act-order) checkpoints and bits != 4 fail at parse/load with clear errors instead of deep inside kernels.
  • Dispatch: GPTQ dense linears route to the awq family; GPTQ MoE routes through the same AWQ path, so the grouped W4A16 route (PR 4) serves it.
  • Review hardening (3 fixes from the series' independent review): the GPTQ
    update-path staging defensively clones tensors against IPC-buffer reuse (a module
    whose scales arrive in a later bucket than its weights converted from stale buffer
    contents — silent wrong weights on hot updates); _gptq_sym_qzeros synthesizes the
    correct (g, n//8) int32 word shape (verify=True crashed on exactly the
    symmetric-no-qzeros checkpoints this module documents); convert_checkpoint
    rewrites only the exact config.json basename (sibling configs pass through
    byte-for-byte). Three regression tests pin all three.

BC-breaking

None. Additive; non-GPTQ checkpoints behave exactly as before.

Use cases

GPTQ W4A16 models (mainstream GPTQ exports without desc_act) run on the TM W4A16 channel with zero offline conversion; GPTQ MoE checkpoints dispatch through the same path (grouped route in PR 4, full pipeline wiring in #5169).

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests: repack round-trips and trap guards (nibble order, zero offset, geometry rejections, desc_act), kernel parity on CUDA, config dispatch tests, and the memdecode build-model fake tracking the normalize_quant rename this PR introduces.
  3. No new downstream dependencies.
  4. Docstrings updated accordingly.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical loading and conversion defects reject valid GPTQ checkpoints, mishandle sharded weights, ignored layers, and CUDA tensors, and produce AWQ exports with incompatible metadata and unsupported group-index tensors.

8 open issues
What changed in this PR

This PR adds load-time GPTQ-to-AWQ repacking so the PyTorch engine can serve GPTQ dense models through the existing W4A16 kernels without offline conversion.

Changes:

  • Adds GPTQ packing, dequantization, verification, and offline conversion utilities.
  • Routes GPTQ dense linears through AWQ and integrates normalization into model loading and weight updates.
  • Adds CPU layout and validation tests plus CUDA kernel parity and routing tests.
File Description
tests/​pytorch/​kernel/​test_tm_gemm_gptq.py Tests CUDA kernel parity and supported-shape routing.
tests/​pytorch/​kernel/​test_gptq_repack.py Tests packing semantics, geometry checks, normalization, and dispatch.
lmdeploy/​turbomind/​gptq_repack.py Implements GPTQ-to-AWQ conversion and reference utilities.
lmdeploy/​pytorch/​weight_loader/​model_weight_loader.py Adds GPTQ stream normalization to checkpoint loading.
lmdeploy/​pytorch/​spec_decode/​proposers/​base.py Enables quantization normalization for draft-model loading.
lmdeploy/​pytorch/​memdecode/​agent.py Enables normalization for memory-model loading.
lmdeploy/​pytorch/​engine/​model_agent/​agent.py Integrates normalization into initial loading and online updates.
lmdeploy/​pytorch/​config.py Adds GPTQ configuration parsing, dtype handling, and dense-layer dispatch.

Review effort: Balanced

Comment thread lmdeploy/pytorch/config.py Resolved
Comment thread lmdeploy/turbomind/gptq_repack.py Resolved
Comment thread lmdeploy/turbomind/gptq_repack.py Resolved
Comment thread lmdeploy/turbomind/gptq_repack.py Resolved
Comment thread lmdeploy/turbomind/gptq_repack.py Resolved
Comment thread lmdeploy/turbomind/gptq_repack.py Resolved
- GPTQ dense checkpoints repack to the canonical AWQ layout at load time (gptq_repack): pack axis, nibble order, and the zero-point offset are absorbed in-flight, so the AWQ and TurboMind W4A16 channels serve GPTQ without an offline conversion.
- The repack rejects desc_act (act-order) checkpoints loudly and verifies geometry on demand with a synthesized symmetric zero reference.
- GPTQ dispatches dense linears to the awq family and honors ignored modules; MoE keeps the raw method and fails loudly (no GPTQ MoE implementation). bits != 4 is rejected at parse.
- The load-time repack gate is QuantizationConfig.load_normalize(), shared by the initial, draft, memdecode, and hot-update paths; fp8-static and group-32 ct keys pass through untouched.
- Tests: repack round-trips and trap guards (nibble order, zero offset, geometry rejections, desc_act), kernel parity on CUDA, and config dispatch; the TM cross-check skips cleanly without a built extension.
… route MoE through AWQ

Accept AutoGPTQ's identity group-index buffers: a non-decreasing consecutive g_idx is the default no-act-order export and is consumed; only a reordered mapping raises. desc_act is also rejected at config parse.
Keep a staged GPTQ module until its components arrive instead of flushing on the next prefix: shard and bucket boundaries can interleave a module's tensors, and an early flush synthesized a wrong asymmetric pad.
Allocate repacked outputs on the input device so hot-update IPC tensors do not mix CPU and CUDA operands, and gather quant components across all safetensors shards in convert_checkpoint: the converted output is a single model.safetensors with the quant config rewritten to AWQ and identity g_idx dropped.
Route GPTQ MoE through the AWQ family like dense linears: normalize_gptq_weights covers expert prefixes, so grouped experts land on the same verified route as ct group-128.
…oad fake

- c3 renamed the load-time normalization kwarg normalize_ct -> normalize_quant (d4fea0f); the fake fixed for the c2 signature went stale again from this branch up.
- Follows the c2 fix (8cf6d79 pre-restack) which is still correct at its own layer; this commit adapts the same fake to the c3+ signature so the test stays green through c4/c5.
…shape, and pin the config.json match

Three defects from the C3 serial review:
- normalize_gptq_weights staged update-path tensors without cloning. Update buckets deliver views into a reused IPC buffer, so a module whose scales arrive in a later bucket converted from the buffer's stale contents - silent wrong weights on GPTQ hot updates. The ct normalizer already made this defensive copy; the gptq half now mirrors it.
- _gptq_sym_qzeros synthesized (g, n) instead of (g, n//8) int32 words, so gptq_to_awq(verify=True) crashed on exactly the symmetric-no-qzeros checkpoints the module documents as supported. Default loads were unaffected (verify=False).
- convert_checkpoint's fname.endswith('config.json') also rewrote tokenizer_config.json / generation_config.json, contradicting the byte-for-byte passthrough docstring. Basename pinned; passthrough regression-tested.
Regression tests: verify-accepts-synthesized-zeros, update-path buffer-reuse clone, sibling-config byte passthrough.
- checkpoint_format threads through gptq_to_awq/convert_shard and the load-time
  normalize stream: v1 applies the +1 offset, v2 keeps the stored symmetric zero;
  the nibble-overflow guard now evaluates after the format-correct offset and
  names the format (PR 5160 review thread 4691021304).
- convert_checkpoint reads the source config.json format and drops the stale v2
  marker from the output AWQ config; unknown checkpoint formats are rejected
  loudly at parse.
- Regression: reviewer's symmetric case (all codes/zeros = 8, unit scales)
  dequantizes to zero through the full v2 path; first-principles mixed-zeros v2
  reference; v1 offset pinned by a guard test.
- _finalize_updated_weights passes the transaction's checkpoint_format into
  finalize_gptq_weights instead of letting the leftover flush default to v1
  while the shard streams used the parsed format (audit note N2 on PR 5160;
  unreachable today, consistency only).

This branch has not been deployed

No deployments
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.

3 participants