Skip to content

test(qzeros): pin the symmetric-qzeros state machine invariants - #6432

Merged
Qubitium merged 2 commits into
mainfrom
test/qzeros-state-machine-invariants
Nov 29, 2026
Merged

Qubitium merged 2 commits into
mainfrom
test/qzeros-state-machine-invariants

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Test-only guard for the symmetric-qzeros state machine. No product change and no bug fix — an end-to-end audit of main (a4471c53b) found no invariant violated, so this PR only pins the invariants so the next edit to this path cannot silently break one.

Six changes landed here in one day: #6388, #6392, #6391, #6395, #6409, and the pending #6157 normalization. Each was verified individually; nothing covered the combination.

What the state machine looks like

Three markers, two encodings:

piece meaning
_HF_LEGACY_QZEROS the checkpoint came from a pre-#2983 producer using the floor width out_features // 32 * bits
_HF_CHECKPOINT_SYM that producer's sym flag
_HF_DEFER_SYM_QZEROS hide the checkpoint tensor from the loader's expected-key set and rebuild it in post-init
qzero_format() the raw _qzeros_format — a placeholder 1 until _qzeros_format_established
_load_target_qzeros_format() the layout the selected kernel actually decodes — safe to read at any time

All five checkpoint kernels declare REQUIRES_FORMAT_V2 = True, so the placeholder 1 disagrees with the decoded 2 until a load resolves it. That is #4377, and it is why qzero_format() must not be used as a decode answer before the layout is established — the exact mistake the first #6157 draft made.

Invariants asserted

  1. a 32-aligned legacy projection never defers (#6409/#6396), and a real floor-width mismatch always does;
  2. an asymmetric floor-width checkpoint is refused, never silently wrong;
  3. a deferred module materializes to qzeros_standard_shape(...), with _qzeros_format_established set and qzero_format() == _load_target_qzeros_format();
  4. materialization is idempotent across 2/3/4/8-bit;
  5. the deferral marker and a materialized buffer are mutually exclusive;
  6. a float qzeros buffer (int4pack affine offsets, which are 0.0 for a symmetric checkpoint) is never filled with a packed word — skipped until the #6157 normalizer exists;
  7. a clean checkpoint's word survives the load, and every pre-/post-fix(qlinear): size GPTQ buffers by pack_factor and skip Laguna g_proj quantization #2983 producer lands on the same decoded zero point.

Results

tree result
main a4471c53b 49 passed, 13 skipped
main + #6419 (b481a9ffc) 50 passed, 12 skipped
together with the four existing qzeros test files 68 passed, 17 skipped, 0 failed — no global-state pollution

Every skip is a kernel-geometry or environment gate with a precise reason (e.g. TorchCpuAvx512Linear does not support out_features=80: `out_features`: 80 must be divisible by [32]). None hides a failure.

Real checkpoints, loaded on main and with #6419: Qwen2.5-0.5B-Instruct-gptq-4bit (168 layers), Llama-3.2-1B-…-vortex-v1 (112), Llama-3.2-1B-Instruct-GPTQ-4bits-gp128 (112) — all uniform 0x88888888, no float qzeros, no leftover deferral, layout established on every layer, identical on both trees.

Audit notes (no action taken here)

  1. The INT3 branch of _materialize_hf_sym_qzeros is unreachable. Planar gptq_p — the only 3-bit layout any kernel accepts — requires 32-aligned in_features/out_features, and for those widths legacy_qzeros_columns(out_features, 3) == qzeros_standard_shape(...)[1], so #6409 never defers a 3-bit layer. Defensive dead code, not a defect.
  2. checkpoint_format is a trusted label with no check against the bytes. A checkpoint declaring gptq (v1) while holding v2 words loads silently shifted: measured 0x88888888 → 0x99999999 (every nibble +1). Nothing detects it. Pre-existing contract, not a regression — but it is the one remaining way to get a silently wrong model from this path.
  3. #6419 does not repair a disk-offloaded layer (if qzeros.device.type == "meta": continue), so the #6157 fix covers resident loads only. Called out on that PR; verified by reading the guard rather than by measurement, since I could not force a disk-offload cell on a model small enough to load quickly here.
  4. The floor-width repair path is unreachable for TorchCpuAvx512Linear, which requires out_features % 32 == 0 and therefore can never hold a non-aligned width. TorchAtenLinear needs % 16 and TorchFusedLinear % 8, so both do reach it.

Full report with the writer/reader table and the cell-by-cell matrix is in /root/openai-audit/audit-qzeros-state-machine.md in the session workspace; the summary above is self-contained.

Commands

cd tests && CUDA_VISIBLE_DEVICES="" \
  PYTHONPATH=<clone>:<clone>/tests \
  /root/venv/bin/python -m pytest test_qzeros_state_machine_audit.py -q -rs -p no:cacheprovider

ruff check tests/test_qzeros_state_machine_audit.py → clean.

Six changes landed on the symmetric ``qzeros`` path in one day (#6388,
#6392, #6391, #6395, #6409 and the pending #6157 normalization). Each was
verified on its own; nothing pinned the combination, and a later edit can
satisfy one fix while breaking another.

The state machine has three markers (``_HF_LEGACY_QZEROS``,
``_HF_CHECKPOINT_SYM``, ``_HF_DEFER_SYM_QZEROS``) and two word encodings,
selected by ``_load_target_qzeros_format()``. The tests assert:

- a 32-aligned legacy projection never defers, and a real floor-width
  mismatch always does (#6409 / #6396);
- an asymmetric floor-width checkpoint is refused, never silently wrong;
- a deferred module materializes to the standard shape with a resolved
  layout that agrees with the one the kernel decodes;
- materialization is idempotent across 2/3/4/8-bit;
- the deferral marker and a materialized buffer are mutually exclusive;
- a float ``qzeros`` buffer (int4pack affine offsets) is never filled with
  a packed word -- skipped until the #6157 normalizer exists;
- a clean checkpoint's word survives the load, and every pre- and
  post-#2983 producer lands on the same decoded zero point.

``qzero_format()`` is deliberately not read as a decode answer before
``_qzeros_format_established``: every kernel is ``REQUIRES_FORMAT_V2``, so
the constructor placeholder ``1`` disagrees with the decoded ``2`` until a
load resolves it (#4377). That distinction is the whole reason the first
#6157 draft corrupted clean checkpoints.

Test-only. Verified 49 passed / 13 skipped on main (a4471c5) and
50 / 12 with #6419 applied; 68 passed / 17 skipped together with the four
existing qzeros test files, so there is no global-state pollution. Every
skip is a kernel-geometry or environment gate with a precise reason.
``_legacy_module_or_skip`` ended with a bare ``pytest.skip(...)`` call, which
reads as an implicit ``return None`` to static analysis and made CodeQL flag
the function for mixing explicit and implicit returns. ``pytest.skip``
raises, so the added return is unreachable; it exists only to make every
non-exceptional path explicit, and the comment says so.

Behaviour unchanged: 49 passed, 13 skipped, identical to before.
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 20:10
@Qubitium
Qubitium merged commit b211101 into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the test/qzeros-state-machine-invariants branch November 29, 2026 20:10
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.

1 participant