Skip to content

Full finetuning: dequantize block FP8 checkpoints on load - #15367

Merged
danielhanchen merged 5 commits into
mainfrom
dh/s7-fp8-full-finetune-dequant
Oct 27, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
dh/s7-fp8-full-finetune-dequant

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

FastModel.from_pretrained(fp8_checkpoint, full_finetuning = True, load_in_4bit = False, dtype = torch.bfloat16)

FastModel only passes dequantize = True to transformers' FineGrainedFP8Config for an explicit load_in_16bit = True load. Full finetuning does not set load_in_16bit, so transformers keeps FP8Linear layers. Training preparation then casts the fp8 weight storage to bf16 without applying weight_scale_inv, and three things go wrong:

  • the trained weights are the raw fp8 values, not the dequantized ones;
  • FP8Linear.forward treats a two-byte weight as a plain F.linear weight, so outputs are off by the block scales;
  • save_pretrained writes bf16 tensors under a quant_method: fp8, dequantize: false config, so a reload changes every projection again.

This predates #14803. The same load with load_in_16bit = True was already correct.

Fix

In FastBaseModel.from_pretrained, full finetuning of a quant_method: fp8 checkpoint now requests dequantize = True, as the explicit 16bit path does. It also runs _dequantize_leftover_fp8_params after load, again as the 16bit path does. A caller's own quantization_config still wins.

If the installed transformers has no dequantize flag on FineGrainedFP8Config, the load is refused with a clear message instead of training unscaled weights. LoRA loads and every other quant method are unchanged.

Tests

tests/test_fp8_full_finetune_dequant.py:

  • Routing (no GPU needed; a subprocess records the config FastModel hands transformers and stops before any weight is read):
    • full_finetuning = True with load_in_4bit = False, and with the default load_in_4bit, requests dequantize = True. Both fail on main.
    • full_finetuning with load_in_16bit = True passes on main and here.
    • A LoRA load keeps dequantize = False.
    • A transformers whose config has no dequantize flag refuses full finetuning (fails on main, which loads) and still allows a LoRA load.
  • GPU (skipped without CUDA): loaded projection weights match the dequantized checkpoint, no fp8 parameters remain, the saved config has no quantization_config, and a reload changes nothing.

On main: 3 failed, 3 passed, 1 skipped (no GPU). With this change: 6 passed, 1 skipped. The existing FP8 loader suites (test_fp8_16bit_text_loader_dequant, test_fp8_dequant_save_policy, test_fp8_leftover_dequant_16bit, test_fp8_moe_fast_inference_gate, test_fp8_to_nf4_load, test_modelopt_fp8_loading) pass: 92 passed, 21 skipped.

GPU check

L4 (sm_89), torch 2.14.1, transformers 5.17.0. The model is a locally generated two-layer block-FP8 Llama with 14 projections, loaded with full_finetuning = True, load_in_4bit = False, dtype = torch.bfloat16:

Check main this PR main, load_in_16bit = True
Projection module FP8Linear Linear Linear
Weights equal to the unscaled fp8 cast 14 / 14 0 / 14 0 / 14
Max weight rel. L2 error vs dequantized checkpoint 5672.6 0 0
Projection forward rel. L2 error vs dequantized checkpoint 5261.8 1.7e-3 1.7e-3
Saved quantization_config fp8, dequantize: false none none
Projections changed by save and reload 14 / 14 0 / 14 0 / 14

The 1.7e-3 forward error is bf16 matmul rounding against an fp32 reference, and it is the same with load_in_16bit = True.

Not covered

  • The refusal path was only exercised with a simulated FineGrainedFP8Config that has no dequantize flag, not on a real transformers 4.57.x install.
  • Not tested: MoE FP8 checkpoints under full finetuning, multi-GPU device maps, and an actual training run after the load.

FastModel only asked transformers to dequantize a fine-grained FP8 checkpoint for an explicit
load_in_16bit load. full_finetuning = True kept the FP8Linear layers, and training preparation
then cast the fp8 storage to 16bit without the scales, so the trained weights, the forward and
the saved checkpoint were all wrong.

Full finetuning now passes dequantize = True too and dequantizes leftover fp8 parameters, as the
16bit path does. A transformers without the dequantize flag is refused with a clear message
instead of training unscaled weights.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T14:49:59.046110Z c8241aa 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 9bee4f8d9c

ℹ️ 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".

pre-commit-ci Bot and others added 2 commits October 26, 2026 12:01
…oRA check on transformers without dequantize

The test imported test_fp8_16bit_text_loader_dequant without its directory on sys.path, so it failed to collect on its own. On transformers 4.57.x the spoofed sm80 device trips the fp8 capability check before the simulated old config is reached, so that case is skipped there.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4b8f7cdfd7

ℹ️ 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".

Comment on lines +127 to +128
model, _ = FastModel.from_pretrained(src, max_seq_length = 64, dtype = torch.bfloat16,
full_finetuning = True, load_in_4bit = False, device_map = "cuda:0")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Supply a tokenizer for the GPU round-trip test

On CUDA with a transformers version supporting dequantization, this subprocess cannot reach the weight or save/reload assertions: _write_tiny_block_fp8_llama writes only config.json and model.safetensors, but FastModel.from_pretrained also loads a tokenizer before returning. The tokenizer fallback in unsloth/models/vision.py raises when none is available, so this test fails at assert lines even when FP8 dequantization works correctly. Save a minimal tokenizer into the fixture directory or provide a local tokenizer fixture explicitly so the GPU test exercises the intended round trip.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed and fixed in c8241aa: the fixture now writes a word level PreTrainedTokenizerFast (built offline with tokenizers) next to the checkpoint, so the subprocess gets past from_pretrained to the weight and save / reload assertions. The whole file passes on a B200 (transformers 5.17, torch 2.13), including test_full_finetuning_loads_and_saves_dequantized_weights: 7 passed.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: c8241aa709

ℹ️ 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".

[skip ci]
@danielhanchen
danielhanchen merged commit 839a500 into main Oct 27, 2026
@danielhanchen
danielhanchen deleted the dh/s7-fp8-full-finetune-dequant branch October 27, 2026 10:55
danielhanchen added a commit that referenced this pull request Oct 27, 2026
…f it (#15558)

Seven tests in `tests/test_mxfp4_lora_packed_experts_dequantize.py` fail
on main (`Repo tests (CPU, rest)`, e.g. job 122197461249 on #15514, and
every PR based after #15367):

```
unsloth/models/vision.py:4132: NameError: name '_fp8_full_finetune' is not defined
```

## Cause

The test compiles only the `if` in `FastBaseModel.from_pretrained` that
sets `quantizer_kwargs["dequantize"]`, with a hand-written namespace of
its inputs. #15367 added a local just ahead of it,

```python
_fp8_full_finetune = full_finetuning and str(quant_method).lower() == "fp8"
```

and reads it in that `if`'s condition. The product is right; the harness
namespace was missing the new local, so every case that reaches the
condition stopped on the NameError.

## Change

The harness now also runs the `name = ...` statements ahead of the
branch in each block enclosing it (within the function) whose name the
branch reads, outermost first, skipping any name the test already
provides. Today that adds only the `_fp8_full_finetune` line; a future
local computed the same way is picked up instead of breaking the file
again, and the stubs keep precedence.

## Verification (local, CPU)

- Whole file: main 7 failed, 37 passed; this branch 44 passed.
- Sabotage: replacing that line in `vision.py` with `_fp8_full_finetune
= True` fails 5 tests on this branch
(`test_native_load_unchanged_without_packed_path`,
`test_offloading_device_map_keeps_native_load`, ...), so the lifted line
is the real one from `vision.py` and it is evaluated on every case.
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