Skip to content

fix(w4a): revalidate the packed layout in NVFP4 post_init - #3481

Merged
Qubitium merged 3 commits into
mainfrom
fix-nvfp4-post-init-layout-check
Nov 2, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix-nvfp4-post-init-layout-check

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

W4ANVFP4Linear.post_init restores the packed-checkpoint validation it lost in #3459, plus follow-up fixes for the zero-point format bookkeeping, stale-cache, and format-conversion issues that the validation surfaced:

  1. NVFP4 layout validation (original fix): fix(w4a): stop caching the E4M3 weight expansion #3459 moved the GPTQ layout checks (meta qweight, contiguous g_idx/desc_act=False, symmetric zero point 8) out of _centered_int4_codes into _check_native_weight_layout, which only W4AFP8Linear.post_init calls. NVFP4 deliberately bypasses W4AFP8Linear.post_init to stage its own operand caches, so a malformed checkpoint — a permuted group map or non-symmetric zeros — was decoded silently into the FP8/FP4 operands instead of raising.
  2. qzero-format bookkeeping: raw GPTQ packers write logical zero points (the internal/v2 layout), but hf_pack_layer marked them format=1 — so a gptq_v2 request applied +1 to already-v2 words and a gptq request skipped the needed v1 conversion, leaving NVFP4 post_init to reject freshly packed layers. hf_pack_layer now marks format=2 and only converts to the v1 checkpoint convention (stored zp−1) for REQUIRES_FORMAT_V2 kernels requesting gptq, matching pack_module. The pack_module-bypassing raw packers on W4AFP8Linear (pack_block/pack_gpu/pack_original) now mark format=2 like pack() already did, and the loader marks GPTQQuantLinear modules format=2 after loading a native GPTQ_V2 checkpoint.
  3. Stale operand caches after failed re-init: a post_init that failed validation left previously staged _weight_* buffers populated, so forward() could serve operands decoded from a stale layout. Both post_init implementations now invalidate caches before validating, so a failed re-init leaves the module unusable instead of silently corrupt.
  4. v1↔v2 conversion correctness (review round 3): convert_gptq_v1_to_v2_format_module/convert_gptq_v2_to_v1_format_module used whole-word ±0x11111111-style corrections for bits 2/4/8, which borrows/carries across packed fields whenever a zero point is 0 — silently corrupting neighboring zero points at field boundaries. Both now decode/shift/repack per field via _correct_gptq_v1_qzeros/_revert_gptq_v1_qzeros_correction for bits 2/4/8 and int32 INT3.
  5. Format propagation gaps (review round 3): hf_pack_layer(..., post_init=True) now restores the v2 runtime layout after a v1-checkpoint pack (zp−1 words are export-only; flag-blind decoders need logical zero points). GPTQQuantLinear._load_from_state_dict restores _qzeros_format from the declared checkpoint format so state_dict()/load_state_dict() round-trips keep the v2 domain. The BitBLAS repack_from_gptq hook — which bypasses _align_qzero_format — now shifts zp−1 fields back to logical zero points when the source is a v2-requiring module still in the v1 checkpoint domain.

What Changed

  • gptqmodel/nn_modules/qlinear/w4a_nvfp4.py: call self._check_native_weight_layout() in post_init before _centered_int4_codes, and _invalidate_weight_cache() first.
  • gptqmodel/nn_modules/qlinear/w4a_floatx.py: _invalidate_weight_cache() at the top of post_init; pack_block/pack_gpu/pack_original overrides mark qzero_format(2); pack_block/pack_gpu forward *args so pack()'s positional block_in/workers reach the base implementation.
  • gptqmodel/nn_modules/qlinear/__init__.py: GPTQQuantLinear._load_from_state_dict restores _qzeros_format from the declared checkpoint format (FORMAT.GPTQ/gptq/gemm → 1, FORMAT.GPTQ_V2/gptq_v2 → 2).
  • gptqmodel/nn_modules/qlinear/bitblas.py: repack_from_gptq shifts zp−1 fields to logical zero points for REQUIRES_FORMAT_V2 sources in the v1 checkpoint domain.
  • gptqmodel/utils/model.py: per-field v1↔v2 qzero conversion via _correct_gptq_v1_qzeros/_revert_gptq_v1_qzeros_correction for bits 2/4/8 and int32 INT3 (whole-word ±1 was not equivalent once a field was 0/maxed).
  • gptqmodel/utils/packing.py: hf_pack_layer marks raw-packer output format=2, gates the v1 conversion on REQUIRES_FORMAT_V2 + checkpoint_format="gptq", and restores the v2 runtime layout before post_init().
  • gptqmodel/models/loader.py: mark GPTQQuantLinear modules format=2 after loading a native GPTQ_V2 checkpoint.
  • tests/kernels/test_w4a_cache_lifecycle.py: NVFP4 hf_pack_layer regression for both checkpoint flavors, state_dict round-trip into a fresh format=GPTQ_V2 module, failed-re-init cache-clearing coverage.
  • tests/test_hf_packing_api.py: field-boundary conversion coverage (bits 2/4/8, zero field next to nonzero fields, both directions), default-format post_init forward regression, updated reuse/pack-parity expectations.

Tests

Every working PR must include at least one new simple, fast, targeted unit test when the change affects behavior, a bug fix, or a regression path.

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

Paste the exact test commands and results here:

cd tests && python -m pytest test_hf_packing_api.py kernels/test_w4a_cache_lifecycle.py -x -q \
  -k "not block_impl and not cuda and not gpu and (not native_weight_changes_invalidate_readiness or not pack)"
# 78 passed, 9 skipped (CUDA-only), 9 deselected

The new tests fail on the pre-fix code (NVFP4 post_init rejects a valid gptq_v2/gptq packed layer; failed re-init leaves caches populated; packed-word conversion corrupts boundary fields; v2 state_dict round-trip is rejected) and pass with the fix. pack_block-impl cases were deselected locally: the native extension raises Illegal instruction on this box (no GPU, mismatched ISA) — a pre-existing environment issue, unrelated to this change.

Review Requirements

AI-assisted code is welcome. The PR must still be reviewed before it is opened as ready for review.

I or an AI agent (such as Codex or Claude) have:

  • 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

  • For kernel-related changes, accuracy drift was measured against an applicable independent Torch FP32/FP64 oracle, or the reason this is not applicable is documented.

Not applicable: no kernel or numerics change — this restores/extends checkpoint validation, format bookkeeping, and per-field conversion; decode math is unchanged.

Notes

  • post_init=False still leaves zp−1 words for a gptq request (export semantics); post_init=True restores the v2 runtime layout since the caller wants a usable module.
  • Non-v2 kernels keep the legacy flag=1-with-zp-words convention, so the BitBLAS normalization is gated on REQUIRES_FORMAT_V2 + flag==1 — shifting a non-v2 flag-1 module would corrupt its (already logical) zero points.
  • The BitBLAS hook path has no runnable coverage on CPU (the kernel needs its GPU toolchain); it is covered by the flag/domain logic only.

Link to Devin session: https://app.devin.ai/sessions/4e39c8fbf89b45229af250ac5b93eb41
Open in Devin Desktop: https://app.devin.ai/desktop/session/4e39c8fbf89b45229af250ac5b93eb41?variant=devin
Requested by: @Qubitium

W4ANVFP4Linear.post_init calls PackableQuantLinear.post_init directly and
stages its own operand caches, so it never ran the native-layout checks that
moved into _check_native_weight_layout in #3459. A desc_act group map or a
non-symmetric zero point was decoded silently into the FP8/FP4 operands.
Restore the shared validation before _centered_int4_codes, matching the
checks the old decode used to perform, and cover both rejections on CPU.
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Incorrect zero-point format bookkeeping rejects valid packed layers and accepts invalid native GPTQ_V2 checkpoints, while failed reinitialization leaves stale operand caches usable.

3 open issues
What changed in this PR

Restores packed-checkpoint validation in the NVFP4 backend after #3459 moved those checks out of the shared decoder.

Changes:

  • Validate the GPTQ layout before decoding NVFP4 weight operands.
  • Add CPU regression tests rejecting permuted group mappings and non-symmetric zero points.
File Description
tests/​kernels/​test_w4a_cache_lifecycle.py Adds two NVFP4 checkpoint-validation regression tests.
gptqmodel/​nn_modules/​qlinear/​w4a_nvfp4.py Calls the shared layout validator before decoding weights.

Review effort: Max

… stale operand caches on re-init

- hf_pack_layer: mark raw-packer output as the v2 (logical zero point)
  layout it actually produces, and only convert to the v1 checkpoint
  convention (stored zp-1) for kernels that require the v2 runtime
  layout, matching pack_module. Previously a gptq_v2 request added one
  to already-v2 words and a gptq request skipped the needed v1
  conversion, so NVFP4 post_init rejected freshly packed layers.
- loader: mark GPTQQuantLinear modules format=2 after loading a native
  GPTQ_V2 checkpoint; the flag previously kept its format=1 default even
  though no v1->v2 conversion ran, so format-aware decoders read every
  zero point one high.
- W4AFP8Linear/W4ANVFP4Linear post_init: invalidate staged operand
  caches before validating, so a failed re-post_init leaves forward()
  unusable instead of serving operands from a stale layout.
- W4AFP8Linear: mark format=2 in pack_block/pack_gpu/pack_original like
  pack() already did, covering the pack_module entry points that bypass
  pack().
- Tests: NVFP4 hf_pack_layer regression for both checkpoint flavors,
  failed re-post_init cache-clearing coverage, and updated reuse/
  pack-parity expectations for the corrected format bookkeeping.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved packing and zero-point handling defects cause packing failures, incorrect runtime weights, and corrupted asymmetric checkpoints.

6 open issues

Review effort: Max

Comment thread gptqmodel/utils/packing.py Resolved
Comment thread gptqmodel/utils/packing.py Resolved
Comment thread gptqmodel/utils/packing.py Resolved
Review follow-ups:

- convert_gptq_v1_to_v2/v2_to_v1_format_module: use the field-aware
  _correct/_revert helpers for bits 2/4/8 (and int32 INT3); a packed-word
  +-1 borrows/carries across fields, corrupting zero points at field
  boundaries.
- hf_pack_layer(post_init=True): restore the v2 runtime layout after a
  v1-checkpoint pack so flag-blind decoders read logical zero points.
- GPTQQuantLinear._load_from_state_dict: restore _qzeros_format from the
  declared checkpoint format so state_dict round-trips keep the v2 domain.
- W4AFP8Linear pack_block/pack_gpu: forward positional args (block_in,
  workers) so pack() no longer raises TypeError.
- BitBLAS repack_from_gptq: shift zp-1 fields to logical zero points when
  repacking a v2-requiring source still in the v1 checkpoint domain.
- tests: field-boundary conversion coverage for bits 2/4/8, default-format
  post_init forward regression, NVFP4 state_dict round-trip; drop asserts
  that required expanded operand caches to persist (forward is CUDA-gated,
  post_init revalidation is the CPU-usable signal).
@Qubitium
Qubitium merged commit 8765753 into main Nov 2, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix-nvfp4-post-init-layout-check branch November 2, 2026 06:12
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