Skip to content

loader: do not restore an fp8 quantization_config over dequantized weights - #15364

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s7-fp8-dequant-save-config
Oct 26, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s7-fp8-dequant-save-config

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

Loading a block FP8 checkpoint with quantization_config = FineGrainedFP8Config(dequantize = True) and leaving load_in_4bit at its default goes through this sequence in both FastLanguageModel.from_pretrained and FastModel.from_pretrained:

  1. transformers dequantizes the weights to 16bit and removes quantization_config from the model config.
  2. _drop_fp8_dequantize_conversions forgets the save time conversions and marks the model _unsloth_fp8_dequantized.
  3. The load_in_4bit block then writes the caller's config back with model.config.update({"quantization_config": quantization_config.to_dict()}).

save_pretrained therefore writes only bf16 tensors, with no weight_scale_inv, under quant_method: fp8. transformers 4.57.6 ignores dequantize, builds FP8 layers that expect scales, and the reload fails with NotImplementedError: Cannot copy out of meta tensor; no data!. A default config load is not affected, since #14803 already skips the bitsandbytes stamp for a dequantized load.

Fix

Skip the explicit config restore when the model was marked _unsloth_fp8_dequantized, in both loaders. The flag is only set when transformers dropped the fp8 config, so a kept FP8 load (sm89+ without dequantize) and every other user config are restored as before.

Tests

  • tests/test_fp8_dequant_save_policy.py::test_caller_fp8_config_is_not_restored_over_dequantized_weights (CPU, both loaders): every model.config.update({"quantization_config": quantization_config...}) in from_pretrained sits behind the dequantized guard. Fails on main, passes here.
  • tests/test_fp8_16bit_text_loader_dequant.py::test_explicit_dequantize_config_saves_without_fp8_config (CUDA, both loaders): loads a tiny block FP8 Llama with FineGrainedFP8Config(dequantize = True), saves, and checks the saved config has no quantization_config, every tensor is 16bit (float16 on a T4, which has no bf16) with no scales, and a reload gives identical weights. On a Kaggle T4 (torch 2.11, transformers 5.17.0) it fails on main for both loaders, with the saved config still quant_method: fp8, and passes here.
  • transformers 4.57.6 reload: the bf16 checkpoint saved by main's loaders, with only the quantization_config entry removed (the one difference this change makes), reloads bit identical under 4.57.6.

The rest of both test files passes locally on transformers 5.17.0.

Not covered

The 4.57.6 reload was checked on CPU from a saved checkpoint, not by running the full Unsloth loader under 4.57.6 (whose FineGrainedFP8Config has no dequantize, so the explicit path does not arise there). The vision loader (FastBaseModel) does not restore a caller config at this point and is unchanged.

…ights

With FineGrainedFP8Config(dequantize = True) and load_in_4bit left at its default,
transformers drops the fp8 config and loads 16bit weights, but FastLanguageModel and
FastModel then wrote the caller's config back. save_pretrained produced bf16 weights
under quant_method fp8 with no scales, which transformers 4.57.6 cannot reload.
Skip the restore when the load was dequantized.
@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-26T12:33:12.209395Z 06896e9 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. Can't wait for the next one!

Reviewed commit: 06896e97ac

ℹ️ 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 c3a2907 into main Oct 26, 2026
@danielhanchen
danielhanchen deleted the dh/s7-fp8-dequant-save-config branch October 26, 2026 19:58
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