Repository navigation
fix(nf12): keep excluded-layer replay dense and fail closed on leaked AUTO_NF12 buffers (#4007) - #4012
Merged
Conversation
… AUTO_NF12 buffers (#4007) A dynamic `-:` rule that excludes every module of a non-final layer still replays that layer once so the next decoder layer receives activations. The replay prepared its projections through the `forward` role, which installs AUTO_NF12 source storage, but an excluded layer never takes the `quant_source` role that restores the dense module. The packed modules stayed in the model tree, so save() wrote nf12_packed/nf12_escapes/nf12_exp_base instead of weight while quantize_config.json declared the layer unquantized; a reload left the dense weights uninitialised and produced NaN logits. - Disable AUTO_NF12 casting for the excluded-layer replay only (stage_layer.py replay_skipped_layer). - Make save() raise when nf12_* buffers belong to a module the config does not declare as NF12, so this corruption can never ship silently. - Add GPU regression coverage for layers 0, 1, 2 and 1+2 with GPTQ and AWQ, plus the last-layer, single-module, sub-block and auto_dtype_cast=off cases. - Document the flow and the fail-closed save contract.
Qubitium
added a commit
that referenced
this pull request
Nov 14, 2026
This was referenced Nov 14, 2026
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.
Fixes #4007
TL;DR
A
dynamicrule that excludes every module of a non-final layer used to leave AUTO_NF12 source storage installed in that layer, sosave()wrotenf12_packed/nf12_escapes/nf12_exp_baseinstead ofweightwhilequantize_config.jsondeclared the layer unquantized, and reloading produced NaN logits. Two changes: the excluded-layer replay no longer installs NF12 storage, andsave()now refuses to writenf12_*buffers for a module the config does not declare as NF12. +79 lines of code, +1 test module, +1 docs page.1. The problem
An excluded layer still runs one forward so the next layer receives activations (
replay_skipped_layer,gptqmodel/looper/stage_layer.py:1060). That replay prepared each projection through theforwardrole, which is the role that packs a dense source into NF12.flowchart TD A["stage_layer.py:1060 replay_skipped_layer"] --> B["_prepare_layer_direct_state_for_forward"] B --> C["_prepare_named_module_for_forward"] C --> D["shell_module_materialize role=forward"] D --> E["base.py:3529 _maybe_auto_dtype_cast_forward_module"] E --> F["cast_source_module installs AutoNF12HookedLinear"] F --> G["_replay_layer_outputs advances inputs"] F --> H["save(): nf12_* keys, no weight"] H --> I["reload: dense weight uninitialised"] I --> J["NaN logits"] style H fill:#fee,stroke:#c00 style I fill:#fee,stroke:#c00 style J fill:#fee,stroke:#c00The dense restore only happens on the
role == "quant_source"path (gptqmodel/models/base.py:4625), which an excluded layer never takes.Before/after of the mechanisms involved:
stage_layer.py:1088)forwardrole packsstate["auto_dtype_cast_forward_disabled"] = True(stage_layer.py:1100)_maybe_auto_dtype_cast_forward_module(base.py:3529)base.py:3548) but never set for this replaysave_quantized(writer.py:2104)_validate_nf12_payload_ownershipraises for undeclared NF12 payloadssave_quantized_embeddings(writer.py:1678)Measured on the issue's repro (tiny BF16 Llama, 4 layers,
dynamic={"-:.*layers\\.1\\..*": {}}, GPTQ 4-bit group 32):mlp.up_proj.nf12_packed,.nf12_escapes,.nf12_exp_basenanauto_dtype_cast="off"mlp.up_proj.weight0.0384mlp.up_proj.weight0.03842. The new flow
flowchart TD A["stage_layer.py:1060 replay_skipped_layer"] --> B["NamedModule for each projection"] B --> C["state auto_dtype_cast_forward_disabled = True"] C --> D["_prepare_named_module_for_forward"] D --> E["shell_module_materialize role=forward"] E --> F{"base.py:3548 flag set?"} F -- Yes --> G["dense source module, no cast"] F -- No --> H["quantized-subset path still packs, then quant_source restores"] G --> I["_replay_layer_outputs advances inputs"] H --> I I --> J["save(): dense weight, no nf12_* keys"] style G fill:#dfd,stroke:#080 style J fill:#dfd,stroke:#080The flag is set only on the wrappers this replay creates, so the quantized-subset path keeps packing its source and keeps restoring it through
quant_source.3. Save contract (fail closed)
flowchart TD A["save_quantized / save_quantized_embeddings"] --> B["_validate_nf12_payload_ownership writer.py:436"] B --> C{"module has nf12_* buffers?"} C -- No --> G["stream shards"] C -- Yes --> D["_module_declares_nf12_checkpoint writer.py:418"] D --> E{"resolved format is NF12 and the dynamic rule is not excluded?"} E -- Yes --> G E -- No --> F["ValueError: Save aborted"] style F fill:#fee,stroke:#c00 style G fill:#dfd,stroke:#080With the guard in place and the replay fix reverted,
save()raises:4. Change inventory
gptqmodel/looper/stage_layer.pygptqmodel/models/writer.pytests/test_auto_nf12_excluded_layers.pydocs/auto-nf12-excluded-layers.mdREADME.md5. Tests
tests/test_auto_nf12_excluded_layers.py:test_fully_excluded_non_final_layer_saves_dense_weights[layers0/1/2/1+2-gptq/awq]*.weight, nonf12_*, reloads finite with rel-err < 0.5, remaining layers still quantizedtest_other_exclusions_never_serialize_nf12_buffers[last-layer/single-module/sub-block/cast-off]test_save_guard_rejects_nf12_buffers_under_an_excluded_moduletest_save_guard_rejects_nf12_buffers_for_a_non_nf12_configtest_save_guard_allows_modules_the_config_declares_as_nf12NF12Configcheckpoint still savestest_save_guard_ignores_modules_outside_the_requested_prefixesFail-before (fix reverted, guard reverted):
Pass-after (CUDA, merged with current
origin/main):Pass-after (ROCm):
Lint:
6. Reviewer notes
restore_source_moduleafter the replay would also work but would pack and immediately unpack the source.nf12_*payloads with an unquantized config cannot be reloaded correctly by any loader.replay_skipped_layerbranch only. The legacy catchup replay (stage_layer.py:693) is unchanged; it restores already-packed modules whose live tree entry already reportsQUANT_TYPE, so the cast is skipped there by the existing check atbase.py:3559.nf12_*buffers with empty storage; that state would still serialize NF12 keys without aweight, so failing is intended.