Skip to content

QAT rounding and NF4 dequant cache: inference-mode reloads, recipe marker, bit-exact checksums - #18630

Merged
danielhanchen merged 4 commits into
mainfrom
kr4-qat-nf4-inference
Nov 20, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
kr4-qat-nf4-inference

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up fixes for #18434, #18433, found while re-checking the PRs merged on 2026-11-19. One commit per fix; every fix has a test that fails on main and passes here.

Fix Follows up Severity
QAT round checksum hashes 8-byte dtypes by their bits #18434 P2
QAT round: re-round after an in-place reload under inference mode #18434 P2
QAT round: cross-recipe state_dict loads keep the target fake quantizer #18434 P2
NF4 dequant cache: inference-tensor scales and packed weights #18433 P2

Tests (this branch vs main 54af93780b, CPU)

  • New and changed tests: main 10 failed / 8 passed, this branch 18 passed. Each test loads the code of its own tree.
  • Neighbouring suites on this branch: QAT and NF4 dequant cache suites: 243 passed, 16 skipped.
  • prepush_gate.py passes.

QAT round checksum hashes 8-byte dtypes by their bits (follows up #18434)

QAT round: checksum the bits of 8-byte dtypes, not their values

What broke

#18434 marks a rounded QAT base weight with _checksum (unsloth/models/_qat_round.py) so a later device move or reload of the same values is recognised as already rounded. _checksum views 1, 2 and 4 byte dtypes as integers, but an 8-byte dtype (float64) is converted by value with .to(torch.int64). Every float64 tensor below 1 in magnitude therefore hashes to 0 and matches any other float64 tensor of the same shape: two random 64 x 64 float64 weights give the same checksum.

Reachability today: none found. torchao fake quantization rejects float64 inputs (Unsupported input dtype: torch.float64), so no QAT model rounds a float64 weight yet. This is a latent fix so the checksum means what its docstring says.

Fix

View every dtype wider than 2 bytes as int32 words (4-byte dtypes exactly as before; 8-byte dtypes as two words). Checksums of 1, 2 and 4 byte dtypes are unchanged.

Tests

New tests/test_qat_round_checksum_bits.py (CPU): different values give different checksums and a clone gives the same one, for float64 / float32 / bfloat16 / float16; the file asserts it imports its own tree.

Arm Result (new file + tests/test_qat_round_rewrite.py + tests/test_qat_weight_memo_guards.py)
origin/main 635532f 1 failed (float64 checksums equal), 30 passed, 1 skipped
this branch 31 passed, 1 skipped

QAT round: re-round after an in-place reload under inference mode (follows up #18434)

QAT round: detect an in place reload of an inference-tensor base weight by value

What broke

#18434 re-rounds a rounded QAT base weight that was rewritten after preparation. It detects the rewrite from (data_ptr, _version). An inference tensor (built under torch.inference_mode, as base weights shared with vLLM can be) has no version counter, so its state is (data_ptr, None) before and after an in place reload.

Repro (CPU): prepare a real torchao int8 QAT LoRA model under torch.inference_mode(), then load_state_dict a different base weight into the same storage, still under inference mode.

  • The post-load hook and a later round_frozen_qat_weights both treat the weight as unchanged.
  • The raw incoming weight stays in service with fake quantization off.
  • It is 0.0162 away (max abs) from per call fake quantization of the same weight.

The parent behaves the same way.

Fix

For an unversioned weight whose (pointer, None) state did not change, compare its values with the checksum recorded at rounding time. A mismatch is handled like a versioned rewrite: the quantizer comes back and the weight is rounded again, in place under inference mode. Versioned weights take exactly the old path. The extra checksum runs only for inference-tensor weights, on load and on round_frozen_qat_weights.

Tests

New tests/test_qat_round_inference_reload.py (CPU, asserts it imports its own tree):

  • An inference weight reloaded through load_state_dict or copy_ then round_frozen_qat_weights matches UNSLOTH_QAT_ROUND_INPLACE=0 bitwise, and the quantizer is off again (rounded in place).
  • An unchanged inference weight is not rounded twice. int8 rounding is not idempotent, so a second rounding would show.
Arm Result (new file + tests/test_qat_round_rewrite.py + tests/test_qat_weight_memo_guards.py)
origin/main 635532f 2 failed, 28 passed, 1 skipped
this branch 30 passed, 1 skipped
Not covered

Not run end to end with fast_inference=True and a live vLLM engine.

QAT round: cross-recipe state_dict loads keep the target fake quantizer (follows up #18434)

QAT round: name the fake quantization recipe in the rounded-weight marker

What broke

#18434 marks a rounded QAT base weight in state_dict() metadata with a checksum of its values. A load whose incoming weight matches the marker is kept as already rounded. The marker does not say which grid the values were rounded to.

Repro (CPU, real torchao QAT layers):

  1. Load an int8-prepared model's state_dict() into an int4-prepared model.
  2. The checksum matches, so the destination keeps the int8-rounded values verbatim with its int4 quantizer off.
  3. Its output differs from per call int4 fake quantization of the same weights (up to 0.0059 on the weight).

The reverse direction behaves the same. The parent also left the destination disabled.

Fix

The marker now carries the quantizer type and its config repr (dtype, granularity, mapping, precision). A marked weight from another recipe fails the check and is rounded for this module like any new weight. Same-recipe loads, including through torch.save / torch.load(weights_only = True), are still kept as is. Markers written by the previous format no longer match, so those loads are rounded again (the pre-marker behaviour).

Tests

New tests/test_qat_round_recipe_marker.py (CPU, asserts it imports its own tree): int8 to int4 and int4 to int8, in memory and through torch.save / torch.load. The destination must match UNSLOTH_QAT_ROUND_INPLACE=0 bitwise and end with its quantizer off.

Arm Result (new file + tests/test_qat_round_rewrite.py + tests/test_qat_weight_memo_guards.py)
origin/main 635532f 4 failed, 27 passed, 1 skipped
this branch 31 passed, 1 skipped

NF4 dequant cache: inference-tensor scales and packed weights (follows up #18433)

NF4 dequant cache: read each scale's version on its own; never cache an inference packed weight

What broke

#18433 keys a cached dense NF4 weight on the address and version of every scale tensor (_scales_key). When any of them was an inference tensor (no version counter), the whole key fell back to (absmax pointer, None). That dropped the versions of the ordinary scales too.

Repro (CPU, real bitsandbytes nested NF4 state):

  1. Make only offset an inference tensor and admit the weight to the cache.
  2. Double state2.absmax in place, outside inference mode.
  3. The next lookup is a hit and returns the old dense weight, 1.47 away (max abs) from a fresh decode. Making absmax the inference tensor instead behaves the same.

Separately, an inference packed weight raised RuntimeError: Inference tensors do not track version counter on admission.

No default training path was found that builds these states. They need weights or scales created under torch.inference_mode that outlive it.

Fix
  • _version(t) returns None for an inference tensor and t._version otherwise. _scales_key reads it per tensor, so one inference tensor no longer hides writes to the others.
  • _lookup decodes an inference packed weight afresh instead of admitting it.

The ordinary path is unchanged apart from one is_inference() call per tensor on the hit path, which is host only.

Tests

New tests/test_nf4_dequant_cache_inference_tensors.py (CPU, reuses the scales test fixtures, asserts it imports its own tree):

  • An inference offset or absmax with an in place write to state2.absmax is served fresh.
  • An inference packed weight is decoded and not admitted.
Arm Result (new file + the three existing tests/test_nf4_dequant_cache*.py)
origin/main 635532f 3 failed, 42 passed
this branch 45 passed

Not proven here

GPU runs of the QAT reload paths and the NF4 cache with inference tensors; the double rounding after a safetensors round trip described in #18434 is unchanged.

_checksum viewed 1, 2 and 4 byte dtypes as integers but converted float64 by
value, so every float64 tensor below 1 in magnitude hashed to 0 and matched any
other. View 8-byte dtypes as two int32 words. Other dtypes hash as before.
…ht by value

An inference tensor has no version counter, so a reload into its storage under
inference mode kept its (pointer, None) state. The load_state_dict hook and
round_frozen_qat_weights both took it as unchanged and left the raw incoming
weight in service with fake quantization off. For unversioned weights compare
the rounding checksum instead, then round again as for a versioned rewrite.
…rker

The state_dict marker held only a checksum of the rounded values, so an int8
model's state_dict loaded into an int4-prepared model (or the reverse) passed
the check, kept the other grid's values verbatim and left this model's
quantizer off. Add the quantizer type and config to the marker; a load from
another recipe is rounded for this module like any new weight.
…an inference packed weight

_scales_key fell back to (absmax pointer, None) when any scale was an inference
tensor, dropping the versions of the others, so an in place write to an
ordinary scale (for example state2.absmax) still hit the stale dense weight.
Key every scale with its own version (None for an inference tensor). An
inference packed weight raised on admission (no version counter); decode it
afresh instead.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T13:56:25.252900Z 86f18c2 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 86f18c2ec3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 291e95d into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the kr4-qat-nf4-inference branch November 20, 2026 14:54
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