Skip to content

Load FP8 checkpoints dequantized to 16-bit on GPUs below sm89 - #14531

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s2-w21-fp8t4
Oct 21, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s2-w21-fp8t4

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

FP8 checkpoints (quant_method: fp8) could not be loaded at all on T4, A100 or other GPUs below sm89, not even with load_in_16bit = True:

ValueError: Unsloth: FP8 quantization is only supported on L4 and higher GPUs with compute capability 8.9 or higher. You are using Tesla T4.

On transformers 5.x nothing needs FP8 hardware there. FineGrainedFP8HfQuantizer dequantizes the weights to 16-bit with their scales whenever the GPU is below sm89. Only verify_fp8_support_if_applicable stopped the load.

Summary

  • verify_fp8_support_if_applicable no longer refuses below sm89 when:

    • the installed transformers has the 16-bit fallback (FineGrainedFP8Config has dequantize, transformers 5.0+);
    • and it is not a vLLM load.

    It logs that the checkpoint is dequantized instead.

  • Unchanged:

    • vLLM (fast_inference = True) loads;
    • transformers 4.57.x, which has no fallback;
    • the FBGEMM rule;
    • non-CUDA devices.
  • FastModel and FastLanguageModel pass fast_inference to the check.

Tests

  • tests/test_fp8_device_gate.py:

    • dequantized loads below sm89;
    • vLLM still refused;
    • FBGEMM and other devices unchanged;
    • fallback detection against the installed transformers.

    main: 17 failed, 14 passed. This PR: 31 passed. It also passes on torch 2.10 + transformers 4.57.6, where the refusal is kept.

  • Regression cases on a Colab T4 (fp16, transformers 5.17.0), tiny block-FP8 Llama:

case main this PR
load_in_16bit = True ValueError pass, no float8 / scale tensors left, rel err vs bf16 twin 0.009
default load, LoRA 2 steps ValueError trains (fp16 weights)
  • Same cases on an L4 (FP8 capable, bf16): same outcomes and losses as main.
  • 8 FP8 test files on B200: 183 passed on main, 196 passed with this PR (the extra 13 are the new cases).

Not covered

  • A100 / sm86 only through the unit test with a patched capability.
  • FastLanguageModel end to end on T4.

transformers 5.x dequantizes a fine-grained FP8 checkpoint to 16-bit with its
scales on GPUs without FP8 compute (T4, A100, sm86), but
verify_fp8_support_if_applicable refused every such load first, including an
explicit load_in_16bit. The sm89 floor now applies only where FP8 weights are
kept: a vLLM load, or transformers 4.57.x, which has no 16-bit fallback. The
FBGEMM rule and the non-CUDA rules are unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 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-21T16:21:32.738975Z 37a426b Manual request
🔒 Security Review ⚠️ Failed 2026-10-21T15:50:50.625271Z
We couldn’t complete this review. Comment @codex security review to try again.
34657ec PR opened
ℹ️ 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

The account paying for this security review has reached its Codex usage limits. The payer can check the Codex usage dashboard. For personal accounts, using credits requires enabling “Use credits for security reviews” in Code review settings. If you do not manage the paying account, contact this repository's admins.

@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: 34657ec0bc

ℹ️ 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 thread unsloth/models/llama.py Outdated
model_max_seq_length = model_config.max_position_embeddings

verify_fp8_support_if_applicable(model_config)
verify_fp8_support_if_applicable(model_config, fast_inference = fast_inference)

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 Gate on the actual vLLM weight-loading path

When fast_inference=True is combined with num_labels, this passes True even though _vllm_will_load_weights(fast_inference, num_labels) returns False and the later AutoModelForSequenceClassification branch loads the weights through Transformers. Consequently, native FP8 classification checkpoints on T4/A100/sm86 still raise the sm89 error instead of using the new 16-bit fallback. Pass the effective weight-loader decision here (and to the rewritten-config check) rather than the raw request flag.

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.

Fixed in 37a426b: both checks in FastLlamaModel.from_pretrained now pass _vllm_will_load_weights(fast_inference, num_labels), so a fast_inference = True load with num_labels (weights through transformers) gets the 16-bit fallback below sm89. Test added (fails before).

@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. Breezy!

Reviewed commit: 37a426b5ff

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 37a426b5ff

ℹ️ 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 b7e66d7 into main Oct 21, 2026
@danielhanchen
danielhanchen deleted the dh/s2-w21-fp8t4 branch October 21, 2026 16:41
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 25, 2026
…antization_config object before stamping (unslothai#14803)

Follow-up to unslothai#14531 and unslothai#14678. Since unslothai#14531, fp8 checkpoints
(`quant_method: fp8`) load on GPUs below sm89 (T4, A100), where
transformers dequantizes them to 16bit by itself. unslothai#14678 drops the
leftover `Fp8Dequantize` conversions after every load, so full finetunes
and 16bit loads now save correct 16bit weights. One gap is left on the
default load path, and a related one on sm89+.

1. **Default load below sm89.** transformers deletes the fp8
`quantization_config` after dequantizing, so the loaded config looks
unquantized. The `load_in_4bit` default then stamps a bitsandbytes 4bit
`quantization_config` onto a model that holds 16bit weights.
`save_pretrained` writes 16bit tensors under that bnb config. A plain
reload builds `Linear4bit` layers that do not hold the saved weights
(max relative error 1.0 against the dequantized reference, on GPU and on
CPU).
2. **Default load on sm89+.** The load keeps `FP8Linear` and its
`FineGrainedFP8Config`. `get_quant_type` reads only dict configs, so the
stamp overwrites the fp8 config with a bitsandbytes one too.
`save_pretrained` then writes fp8 weights and scales under a bnb config,
and a reload is wrong (relative error 3165).

Colab A100 (sm80), torch 2.11.0, transformers 5.17.0, on a tiny
block-fp8 Llama (128 x 128 scales). Each arm loads, runs
`save_pretrained`, then does a plain `AutoModelForCausalLM` reload on
CPU, compared with the dequantized reference:

| loader | arm | main (after unslothai#14678) | this PR |
|---|---|---|---|
| `FastLanguageModel` | default | bf16 weights under a bitsandbytes
config, rel err 1.0 | bf16, no `quantization_config`, bit-identical |
| `FastModel` | default | bf16 weights under a bitsandbytes config, rel
err 1.0 | bf16, no `quantization_config`, bit-identical |
| `FastLanguageModel` / `FastModel` | `full_finetuning = True` | bf16,
bit-identical | same |
| `FastLanguageModel` | `load_in_16bit = True` | bf16, bit-identical |
same |

The saved tensors are byte-identical between main and this PR in every
arm; only the config changes.

B200 (sm100), default load (`FastLanguageModel` and `FastModel`): main
saves fp8 + scales under a bitsandbytes config and reloads with relative
error 3165. This PR saves the same bytes under the checkpoint's fp8
config, and the reload dequantizes to the reference bit for bit. 16bit
and full-finetune saves are unchanged.

## Change
- `_drop_fp8_dequantize_conversions` marks the model
`_unsloth_fp8_dequantized` when it finds an `Fp8Dequantize` conversion
and the loaded config no longer has a `quantization_config`
(transformers removed it, so the weights are 16bit). A load that kept
fp8 weights keeps its config and is not marked.
- Both bitsandbytes stamps after a `load_in_4bit` load
(`FastLanguageModel` and `FastModel`):
  - skip a model marked as dequantized;
- also read a `quantization_config` given as a config object, through
the new `_loaded_quant_method`. A kept `FineGrainedFP8Config` is no
longer overwritten.
- Unchanged: unslothai#14678's converter cleanup, the weights any load puts in
memory, and real bitsandbytes loads, which are still stamped as before.

## Tests
New cases in `tests/test_fp8_16bit_text_loader_dequant.py`:
- a CPU load where transformers dequantizes by itself (the branch it
takes below sm89) is marked, and saves bf16 only;
- both bitsandbytes stamps skip a dequantized model and read config
objects;
- `_loaded_quant_method` reads dict, object and missing configs;
- only a load without an fp8 config is marked.

On current main: 4 failed. This PR: 4 passed.
`tests/test_fp8_dequant_save_policy.py` passes with the GPU hidden.
Together with `test_fp8_device_gate.py`,
`test_fp8_leftover_dequant_16bit.py` and `test_modelopt_fp8_loading.py`
on B200: 118 passed, 1 skipped, 1 failed. The failure is
`test_explicit_and_automatic_dequantize_reload[False]`, which also fails
on main whenever a GPU is visible: it expects a CPU load to dequantize,
but transformers checks the visible GPU's capability, and a B200 keeps
fp8.

Kaggle T4 (sm75, fp16) and Colab L4 (sm89), merge base vs head, both
loaders, default load, then a fresh-process reload:

| GPU | arm | saved config | reload | max rel err |
|---|---|---|---|---|
| T4 | main | bitsandbytes over fp16 weights | `Linear4bit` | 1.0 |
| T4 | this PR | none | `Linear` | 2.4e-4 (fp16 rounding) |
| L4 | main | bitsandbytes over fp8 weights | `Linear4bit` | 7394 |
| L4 | this PR | fp8 | `FP8Linear` | 0.0 |

## Not covered
- Merged saves (`save_pretrained_merged`, GGUF) after a sub-sm89 fp8
load.
- Full finetuning on sm89+ saves bf16 weights under the fp8 config
(reload relative error 0.006 here), the same on main and with this PR.
Not changed here.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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