Skip to content

Formats: keep unquantized tensors loading under NVFP4 / MXFP4 compressed-tensors checkpoints, keep grouped Linears out of QAT - #17775

Merged
danielhanchen merged 8 commits into
mainfrom
formats-unquantized-converters-qat-grouped
Nov 15, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
formats-unquantized-converters-qat-grouped

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

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 matches weight_packed / weight_scale keys. 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 the Concatenate after it.

fix_transformers_quantizer_unquantized_converters (in import_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.convert pass plain .weight sources 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 .weight pattern with $ when transformers' older unanchored matching (5.20 and below) would let it capture _global_scale / _zero_point / _scale / _packed companions. On 5.21+ the original converter is restored unchanged.

  • Raises a clear ValueError naming 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 matched nn.Linear for a dense fake-quantized Linear. DeepSeek-V4 / V4.1 o_a_proj is a grouped Linear (n_groups and its own block-diagonal forward). After the swap it lost both, and GroupedLinearLoRA failed on the first forward under qat_scheme nvfp4 / mxfp8 / int4.

Grouped Linears now stay in full precision through every QAT step, using one predicate (is_grouped_linear, by module structure, so it survives deepcopy):

  • prepare;
  • the filters stored in the model's QAT config;
  • the whole-model and module-by-module convert;
  • the merged reload metadata (modules_to_not_convert);
  • the per-group and block filter builders, and so the microscaling validator;
  • the int8, int8-int4 and cactus schemes.

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 = True keeps 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):
    • builds NVFP4 and MXFP4 compressed-tensors quantizers and checks that the n-gram shard converter survives the rewrite;
    • checks that quantized keys still go to the rewritten converter;
    • checks that FP8 adds nothing back;
    • checks that the dequantize op passes plain shards through.
  • tests/test_qat_grouped_linear.py (new): a grouped Linear survives qat_scheme preparation with its n_groups and forward, while plain Linears are still fake-quantized.
  • tests/test_fp8_to_nf4_load.py: the packed-expert message.

Results:

Run Result
CPU, transformers 4.57.6 (PR files plus existing QAT / precision / FSDP2-export tests) 169 passed, 30 skipped
CPU, transformers 5.22.1 (same files) 189 passed, 22 skipped
CPU, converter tests on 5.18.0 / 5.20.0 / 5.23.0 6 / 6 / 10 passed
B200 (correctness only), 17 test files, 5.22.1 407 passed
  • The skips fire where a check does not apply to that transformers version, mostly the 5.x-only loader hooks this patches.
  • Every new test fails on the previous head of this branch and passes here.
  • The existing QAT / precision / FSDP2-export tests give identical results on base and head.
  • On B200, 6 tests/test_mxfp4_compressed_linear.py tests fail with or without this PR: the same 6 IDs fail on main at 3051c0b.
  • End to end, all five formats train on both models on B200, L4 and T4 (tiny and 1.45B fake checkpoints).
  • Before fix 1, the NVFP4 / MXFP4 Qwen4Exp checkpoints loaded with a random n-gram table on 5.21-5.22. They now load it exactly, in the requested dtype, on 5.22.1 and 5.23.0.

…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.
@danielhanchen danielhanchen self-assigned this Nov 14, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 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-15T02:22:04.800114Z e411f4d 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.

…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]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: e411f4d419

ℹ️ 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 3b50686 into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the formats-unquantized-converters-qat-grouped branch November 15, 2026 07:56
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...`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant