Repository navigation
Load FP8 checkpoints dequantized to 16-bit on GPUs below sm89 - #14531
Conversation
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.
|
@codex review |
Codex Review SummaryThis 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. |
|
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. |
There was a problem hiding this comment.
💡 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".
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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).
…ransformers) [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
|
Codex Review: Didn't find any major issues. 🎉 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". |
…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>
FP8 checkpoints (
quant_method: fp8) could not be loaded at all on T4, A100 or other GPUs below sm89, not even withload_in_16bit = True:On transformers 5.x nothing needs FP8 hardware there.
FineGrainedFP8HfQuantizerdequantizes the weights to 16-bit with their scales whenever the GPU is below sm89. Onlyverify_fp8_support_if_applicablestopped the load.Summary
verify_fp8_support_if_applicableno longer refuses below sm89 when:FineGrainedFP8Confighasdequantize, transformers 5.0+);It logs that the checkpoint is dequantized instead.
Unchanged:
fast_inference = True) loads;FastModel and FastLanguageModel pass
fast_inferenceto the check.Tests
tests/test_fp8_device_gate.py: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:
load_in_16bit = TrueNot covered