Repository navigation
fix(w4a): revalidate the packed layout in NVFP4 post_init - #3481
Merged
Merged
Conversation
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.
Contributor
|
I'll fix CI failures and address comments from users with write access that start with 'Devin'.
|
There was a problem hiding this comment.
🟡 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.
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved packing and zero-point handling defects cause packing failures, incorrect runtime weights, and corrupted asymmetric checkpoints.
6 open issues
Fix pack_block argument forwarding · New Convert packed zero points per field to prevent borrow corruption · New Restore v2 zero-point layout before TorchLinear initialization · New Honor zero-point format when repacking for BitBLAS · New Preserve zero-point format for state-dict round trips · New Avoid retaining expanded NVFP4 weights between forwards · New
Review effort: Max
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
W4ANVFP4Linear.post_initrestores 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:qweight, contiguousg_idx/desc_act=False, symmetric zero point 8) out of_centered_int4_codesinto_check_native_weight_layout, which onlyW4AFP8Linear.post_initcalls. NVFP4 deliberately bypassesW4AFP8Linear.post_initto 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.hf_pack_layermarked them format=1 — so agptq_v2request applied+1to already-v2 words and agptqrequest skipped the needed v1 conversion, leaving NVFP4post_initto reject freshly packed layers.hf_pack_layernow marks format=2 and only converts to the v1 checkpoint convention (stored zp−1) forREQUIRES_FORMAT_V2kernels requestinggptq, matchingpack_module. Thepack_module-bypassing raw packers onW4AFP8Linear(pack_block/pack_gpu/pack_original) now mark format=2 likepack()already did, and the loader marksGPTQQuantLinearmodules format=2 after loading a nativeGPTQ_V2checkpoint.post_initthat failed validation left previously staged_weight_*buffers populated, soforward()could serve operands decoded from a stale layout. Bothpost_initimplementations now invalidate caches before validating, so a failed re-init leaves the module unusable instead of silently corrupt.convert_gptq_v1_to_v2_format_module/convert_gptq_v2_to_v1_format_moduleused 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_correctionfor bits 2/4/8 and int32 INT3.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_dictrestores_qzeros_formatfrom the declared checkpoint format sostate_dict()/load_state_dict()round-trips keep the v2 domain. The BitBLASrepack_from_gptqhook — 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: callself._check_native_weight_layout()inpost_initbefore_centered_int4_codes, and_invalidate_weight_cache()first.gptqmodel/nn_modules/qlinear/w4a_floatx.py:_invalidate_weight_cache()at the top ofpost_init;pack_block/pack_gpu/pack_originaloverrides markqzero_format(2);pack_block/pack_gpuforward*argssopack()'s positionalblock_in/workersreach the base implementation.gptqmodel/nn_modules/qlinear/__init__.py:GPTQQuantLinear._load_from_state_dictrestores_qzeros_formatfrom the declared checkpoint format (FORMAT.GPTQ/gptq/gemm → 1,FORMAT.GPTQ_V2/gptq_v2 → 2).gptqmodel/nn_modules/qlinear/bitblas.py:repack_from_gptqshifts zp−1 fields to logical zero points forREQUIRES_FORMAT_V2sources in the v1 checkpoint domain.gptqmodel/utils/model.py: per-field v1↔v2 qzero conversion via_correct_gptq_v1_qzeros/_revert_gptq_v1_qzeros_correctionfor 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_layermarks raw-packer output format=2, gates the v1 conversion onREQUIRES_FORMAT_V2+checkpoint_format="gptq", and restores the v2 runtime layout beforepost_init().gptqmodel/models/loader.py: markGPTQQuantLinearmodules format=2 after loading a nativeGPTQ_V2checkpoint.tests/kernels/test_w4a_cache_lifecycle.py: NVFP4 hf_pack_layer regression for both checkpoint flavors,state_dictround-trip into a freshformat=GPTQ_V2module, 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-formatpost_initforward 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.
Paste the exact test commands and results here:
The new tests fail on the pre-fix code (NVFP4
post_initrejects a validgptq_v2/gptqpacked layer; failed re-init leaves caches populated; packed-word conversion corrupts boundary fields; v2state_dictround-trip is rejected) and pass with the fix.pack_block-impl cases were deselected locally: the native extension raisesIllegal instructionon 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:
Kernel Accuracy Requirements
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=Falsestill leaves zp−1 words for agptqrequest (export semantics);post_init=Truerestores the v2 runtime layout since the caller wants a usable module.REQUIRES_FORMAT_V2+ flag==1 — shifting a non-v2 flag-1 module would corrupt its (already logical) zero points.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