Skip to content

fix(nf12): refresh cached exponent when loader replaces buffer - #3629

Merged
Qubitium merged 2 commits into
mainfrom
fix/nf12-exp-base-buffer-replacement
Nov 8, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/nf12-exp-base-buffer-replacement

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

NF12ADLinear._exp_base() cached the exponent from its placeholder buffer. Accelerate's set_module_tensor_to_device replaces that buffer directly during checkpoint loading, without calling _load_from_state_dict. Inspecting the module before load (for example, repr(module)) pinned exponent 0 even after the checkpoint installed exponent 54. A non-escaped BF16 weight of -0.000244140625 then decoded as about -7.52e-37.

Fix

Track the exponent buffer by identity, and its PyTorch version when available. A replacement or versioned in-place update refreshes the cached scalar. clear_weight_cache() also joins the base module's state-dict/device-move invalidation hooks and handles explicit updates to inference tensors, which have no version counter. The packed representation and inference weight lifetime are unchanged.

Correct the vendored NF12 README's public format spelling from nf12_ad to nf12, matching #3627 and the saved config.

This is a localized cache-invalidation bug fix; the loading and dispatch flow is unchanged.

Validation

  • New regression test failed on main at the bit-exact decoded-weight assertion; it now uses Accelerate's tensor setter and checks all 1,024 BF16 bits plus two repeated forwards against an independent FP32 Torch matmul oracle. No dense weight is retained.
  • Exponent-cache test covers a normal tensor's in-place version change and explicit clearing after an inference-tensor update.
  • python -m pytest tests/test_nf12_ad_codec.py -q -o log_cli=false --disable-warnings --tb=short: 13 passed, 1 skipped (HIP kernel unavailable here).
  • uvx ruff check gptqmodel/nn_modules/qlinear/nf12_ad.py tests/test_nf12_ad_codec.py: passed.
  • git diff --check: passed.

For the 16x64 fixture, persistent NF12 tensors are 1,537 bytes (1,536 packed + one exponent byte; zero escapes), and the decoded BF16 matrix is 2,048 bytes of temporary CPU scratch per forward. This test does not measure throughput; native NF12 acceleration is unavailable in this environment.

@Qubitium
Qubitium merged commit 8cd72ee into main Nov 8, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/nf12-exp-base-buffer-replacement branch November 8, 2026 12:44
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