Skip to content

feat(sinq): add NF4/NF3 non-uniform codebooks - #6608

Merged
Qubitium merged 2 commits into
mainfrom
feat/sinq-nf-codebooks
Dec 1, 2026
Merged

Qubitium merged 2 commits into
mainfrom
feat/sinq-nf-codebooks

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

Adds opt-in non-uniform NF4/NF3 codebooks to SINQ while keeping the existing
uniform affine grid as the default. At 3 bits, NF3 improves WikiText-2
perplexity by 11.0% relative to uniform SINQ in the same run, using the same
checkpoint/reload and evaluation path.

What Changed

  • SINQConfig.codebook (uniform default, nf4 at 4 bits, nf3 at 3 bits),
    carried through config serialization, meta.weight_only, dynamic-override
    rejection, and module construction.
  • Published NF4/NF3 codebook tables and a clean-room fit on Sinkhorn-normalized
    tiles: per-row mean shift, max-abs step, nearest-level selection, then
    (level - zero) * step * row_scale.
  • SinqLinear packs codebook indices directly, stores the continuous row shift
    in a new FP32 sinq_zero buffer, decodes with a gather, and stays on the
    eager path because the compiled affine decoder cannot represent a codebook.
  • Checkpoint schema, required buffers, post_init validation, and dense file
    export support sinq_zero.
  • Tests cover the codebook constants and an independent FP64 nearest-level
    oracle, exact code recovery after packing (including non-32-aligned NF4),
    HF pack parity, save/reload, dense export, config round trips, and
    accelerator forward parity.
  • Docs: SINQ algorithm, checkpoint layout, flow diagrams, limitations,
    quantization guide, citations, and kernel compile matrix.

Existing uniform checkpoints keep their layout; sinq_zero is required only
for NF4/NF3.

Tests

Executed and passed:

python -m pytest tests/test_sinq.py -q -ra
# 80 passed

python -m pytest tests/test_sinq_gpu.py -q -ra
# 12 passed on CUDA; 12 passed on ROCm

python -m pytest tests/test_docs_contract_refs.py -q -ra
# 260 passed

python -m pytest tests/test_gptq.py tests/test_asym_gptq_v1.py \
  tests/test_dequantize_model_dtype_4455.py tests/test_dequant_sharded_gptq_awq_5398.py -q -ra
# 89 passed, 1 skipped

ruff check gptqmodel gptqmodel/models/ports/native/auto/__init__.py \
  gptqmodel/models/ports/native/auto/_registry.py docs/eora tests setup.py
# All checks passed

The single skip is the existing
TestGPTQProcessorStreaming::test_gptq_processor_async_d2h_streaming_roundtrip
case whose subprocess helper is unavailable in this environment; it is
unrelated to SINQ.

Measured Impact

WikiText-2, Qwen2.5-0.5B-Instruct, 3-bit, group size 128, default gate
(gate_scope="group", kurtosis_threshold=10.0), 16 × 2,048-token evaluation
windows. Both rows come from the same run, with the checkpoint saved and
reloaded before scoring:

Config PPL Delta vs uniform
SINQ uniform 32.115 —
SINQ NF3 28.578 -11.0%

Reference rows from the same evaluation setup: asymmetric RTN 60.677 and
calibrated GPTQ 23.292. Uniform remains the default; NF4/NF3 are opt-in.

Model Support / Module Plan

Not applicable: no new model, alias, or definition.

AWQ onboarding safety

Not applicable: no model or AWQ grouping, search, replay, or folding change.

Logic Flow Documentation

The quantizer gains a codebook branch and the checkpoint gains an optional
sinq_zero tensor. docs/sinq.md contains the updated before/after Mermaid
flow and the checkpoint layout table; the page is linked from README.md.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • Codebook indices are compared exactly with an independent FP64
    nearest-level oracle after packing; decoded outputs are compared against
    the quantizer's reconstructed weight and the dense export path.
  • No fusion support change.
  • Kernel inventory is unchanged; the codebook path is a new eager decode.

Notes

  • NF4 requires 4 bits and NF3 requires 3 bits; other combinations are rejected.
  • NF4/NF3 checkpoints store the continuous shift in sinq_zero; uniform
    checkpoints keep the existing layout.
  • The codebook path uses a Torch gather and does not use the compiled affine
    decoder.

Add SINQConfig.codebook ("uniform" default, "nf4" at 4-bit, "nf3" at 3-bit) and fit the published NormalFloat codebooks on the Sinkhorn-normalized tiles. Non-uniform checkpoints store the continuous row shift in a new FP32 sinq_zero tensor; the kernel gathers codebook levels and the dense export path reconstructs the same weights.
@ZX-ModelCloud
ZX-ModelCloud marked this pull request as ready for review December 1, 2026 06:26
@Qubitium
Qubitium merged commit 1f5f2ff into main Dec 1, 2026
6 checks passed
@Qubitium
Qubitium deleted the feat/sinq-nf-codebooks branch December 1, 2026 06:34
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.

2 participants