Repository navigation
QAT rounding and NF4 dequant cache: inference-mode reloads, recipe marker, bit-exact checksums - #18630
Merged
Conversation
_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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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.
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.
Tests (this branch vs main
54af93780b, CPU)prepush_gate.pypasses.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._checksumviews 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.tests/test_qat_round_rewrite.py+tests/test_qat_weight_memo_guards.py)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 undertorch.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(), thenload_state_dicta different base weight into the same storage, still under inference mode.round_frozen_qat_weightsboth treat the weight as unchanged.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 onround_frozen_qat_weights.Tests
New
tests/test_qat_round_inference_reload.py(CPU, asserts it imports its own tree):load_state_dictorcopy_thenround_frozen_qat_weightsmatchesUNSLOTH_QAT_ROUND_INPLACE=0bitwise, and the quantizer is off again (rounded in place).tests/test_qat_round_rewrite.py+tests/test_qat_weight_memo_guards.py)Not covered
Not run end to end with
fast_inference=Trueand 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):
state_dict()into an int4-prepared model.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 throughtorch.save/torch.load. The destination must matchUNSLOTH_QAT_ROUND_INPLACE=0bitwise and end with its quantizer off.tests/test_qat_round_rewrite.py+tests/test_qat_weight_memo_guards.py)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):
offsetan inference tensor and admit the weight to the cache.state2.absmaxin place, outside inference mode.absmaxthe inference tensor instead behaves the same.Separately, an inference packed weight raised
RuntimeError: Inference tensors do not track version counteron admission.No default training path was found that builds these states. They need weights or scales created under
torch.inference_modethat outlive it.Fix
_version(t)returnsNonefor an inference tensor andt._versionotherwise._scales_keyreads it per tensor, so one inference tensor no longer hides writes to the others._lookupdecodes 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):offsetorabsmaxwith an in place write tostate2.absmaxis served fresh.tests/test_nf4_dequant_cache*.py)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.