Repository navigation
Formats: keep unquantized tensors loading under NVFP4 / MXFP4 compressed-tensors checkpoints, keep grouped Linears out of QAT - #17775
Merged
Conversation
…kpoints transformers rewrites a model's weight converters to collect the quantizer's serialized names. For NVFP4 / MXFP4 compressed-tensors checkpoints the rewritten converter only matches weight_packed / weight_scale keys, so a tensor the checkpoint keeps in 16 bit was randomly initialized: Qwen4Exp's n-gram table (transformers 5.21-5.22) or bf16 experts beside quantized attention. On 5.23 the dequantize op drops the plain shards and the load fails. Put the replaced converter back after the rewritten ones when no rewritten copy still loads its sources, and let the compressed-tensors dequantize op pass plain sources without a packed or scale companion through.
torchao swaps every matched Linear for a dense fake-quantized Linear. A block-diagonal grouped Linear (DeepSeek-V4 / V4.1 o_a_proj: n_groups and its own forward) lost n_groups and its grouped matmul, and GroupedLinearLoRA failed on the first forward under qat_scheme nvfp4 / mxfp8 / int4. Grouped Linears now stay in full precision with a warning; every other Linear keeps its scheme.
DeepSeek-V4 / V4.1 releases store fp8 linears and experts already packed to 4 bits. load_in_4bit = True keeps them as released (no FP4 -> NF4 double rounding); the message now says that LoRA trains on the frozen fp8 / FP4 weights at about the memory of a 4-bit load, instead of reading like a failure.
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. |
…load metadata The grouped o_a_proj guard only wrapped the filter handed to quantize_ during prepare. The config kept on the model, the whole-model and module-by-module convert, the merged reload metadata, the per-group / block filter builders, the microscaling validator and the int8 / int8-int4 / cactus scans still selected it. One predicate now decides, and the stored config carries a structural filter wrapper (survives deepcopy) when the model has grouped Linears, so every consumer agrees and modules_to_not_convert lists the grouped layers. Models without grouped Linears keep their exact filters and metadata. The warning names only grouped layers a filter would otherwise have selected.
…restore, mixed patterns - Plain tensors the compressed-tensors dequantize op passes through now land in the target parameter's dtype, as the rest of the load does (bf16 shards in an fp16 / fp32 model stayed bf16 on transformers 5.23). - The CPU init route and the MLX branch install the fix too; both skip _gpu_init. - On transformers <= 5.20, which searches source patterns unanchored, a restored converter that would also take a quantized tensor's companions (weight_global_scale, weight_zero_point, ...) is restored anchored at the bare name. - One converter pattern that collects both 16-bit and packed shards now raises a clear error instead of silently dropping the 16-bit ones. - Tests call update_weight_conversions with the arity the installed transformers takes (5.18 - 5.20 pass no model).
…n_4bit message The loader keeps the release's own fp8 / FP4 formats and does not re-quantize the experts to NF4; hardware fallback or dequantize = True can still expand weights, so the message no longer claims packed residency or 4-bit memory. [skip ci]
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Nov 15, 2026
…7812) Test-only change. The three dense-checkpoint tests in `tests/test_fp8_to_nf4_load.py` (from unslothai#17775) each loaded the same tiny fp8 checkpoint to NF4. They now share one load through a module-scoped fixture. On a shared B200, which is too noisy for timing, the saving is small: | | before | after | |---|---|---| | three affected tests, CPU s | 29.4 / 29.1 | 24.1 / 26.6 | | three affected tests, wall s | 38.3 / 54.7 | 38.8 / 53.0 | The CPU time drops by about 3 to 5 s. The wall time is inside the noise. The whole file passes in both arms: 21 passed, 1 skipped. The tests still catch the bugs they guard: - Forcing 16-bit layers to quantize fails all three (`14 == 13`, and `Linear4bit` is not `Linear`). - Dropping the reached-keys filter fails `test_fp8_keys_the_model_does_not_have_are_ignored`. - Scaling the dequant by 1.01 fails `test_dense_fp8_loads_nf4_bit_identical...`.
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.
Three fixes found while running every training format (16-bit LoRA, QLoRA NF4, NVFP4, MXFP4, MXFP8) on Qwen3.8-Flash-Next (
qwen4_exp) and DeepSeek-V4.1 tiny and medium checkpoints.1. Unquantized tensors in packed compressed-tensors checkpoints were randomly initialized
transformers 5.21+ (
HfQuantizer.update_weight_conversions, and the compressed-tensors override on earlier 5.x builds) rewrites the model's weight converters so they collect the quantizer's serialized names. For a packed format (NVFP4 / MXFP4), the rewritten converter only matchesweight_packed/weight_scalekeys. Any tensor the checkpoint keeps in 16 bit then matches nothing, gets randomly initialized, and the load does not complain. Examples are Qwen4Exp's n-gram table shards (ngram_embedding.shard_*.weight) and bf16 experts beside quantized attention. On 5.23 the compressed-tensors dequantize op drops those plain shards instead, and the load fails in theConcatenateafter it.fix_transformers_quantizer_unquantized_converters(inimport_fixes.py, called from_gpu_init.py):Puts each replaced converter back after the rewritten ones, but only when no rewritten copy still loads its sources. That is the case for FP8, and for 5.23, which also collects the bare name. Converters match first-wins in list order, so quantized keys still go to the rewritten copy.
Lets
CompressedTensorsDequantize.convertpass plain.weightsources that have no packed or scale companion straight through, as its FP8 branch already does for weights without a scale.Casts passed-through plain shards to the target parameter's dtype.
Anchors a restored
.weightpattern with$when transformers' older unanchored matching (5.20 and below) would let it capture_global_scale/_zero_point/_scale/_packedcompanions. On 5.21+ the original converter is restored unchanged.Raises a clear
ValueErrornaming the keys when one pattern mixes 16-bit and packed shards, instead of silently dropping the 16-bit ones.Is installed on the GPU, CPU (
_cpu_init.py) and MLX init paths.Both patches are idempotent and do nothing on transformers builds without these hooks.
2. QAT broke block-diagonal grouped Linears
torchao's
quantize_(..., QATConfig(...))swaps every matchednn.Linearfor a dense fake-quantized Linear. DeepSeek-V4 / V4.1o_a_projis a grouped Linear (n_groupsand its own block-diagonal forward). After the swap it lost both, andGroupedLinearLoRAfailed on the first forward underqat_schemenvfp4 / mxfp8 / int4.Grouped Linears now stay in full precision through every QAT step, using one predicate (
is_grouped_linear, by module structure, so it survivesdeepcopy):modules_to_not_convert);The wrapper is stored only when the model has grouped Linears, so other models keep exactly their current filters and export metadata. The warning names a grouped layer only when some filter would have selected it.
3. Clearer message for fp8 releases with packed FP4 experts
DeepSeek-V4 / V4.1 releases store fp8 Linears and experts already packed to 4 bits.
load_in_4bit = Truekeeps them as released and does not round the FP4 experts to NF4 a second time. The old message read like a failure. It now says the release's own fp8 / FP4 formats are kept and the experts are not re-quantized to NF4, without a memory claim (GPUs without FP8 kernels dequantize).Tests
tests/test_quantizer_unquantized_converters.py(new):tests/test_qat_grouped_linear.py(new): a grouped Linear survivesqat_schemepreparation with itsn_groupsand forward, while plain Linears are still fake-quantized.tests/test_fp8_to_nf4_load.py: the packed-expert message.Results:
tests/test_mxfp4_compressed_linear.pytests fail with or without this PR: the same 6 IDs fail onmainat 3051c0b.