Repository navigation
FP8: keep every scoped converter and clean up every dequantizing load before save - #14678
Conversation
… before save _drop_fp8_dequantize_conversions replaced each Fp8Dequantize model converter with the first plain converter of the same type and targets. On a composite model transformers scopes one converter per submodel while the dequantizing copy is unscoped, so only the first submodel kept its expert unmerge and the others saved packed experts that transformers 4.57 drops. An unscoped conversion is now replaced by every matching scoped counterpart. The cleanup also only ran when Unsloth itself injected FineGrainedFP8Config(dequantize = True). A caller's dequantize config, and transformers' own dequantize on pre-sm89 GPUs and CPU, kept the conversions, so save_pretrained wrote fp8 weights under a config with no quantization_config and a reload read them as 16bit. The helper is a no-op without Fp8Dequantize conversions, so it now runs after every FastLanguageModel / FastBaseModel load.
|
@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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6f66093c9c
ℹ️ 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".
| matches = [ | ||
| c | ||
| for c in plain | ||
| if type(c) is type(conversion) | ||
| and _targets(c) == _targets(conversion) | ||
| and (scope is None or getattr(c, "scope_prefix", None) == scope) |
There was a problem hiding this comment.
Avoid re-adding converters already retained
For heterogeneous composite models, an unscoped FP8 converter can share its type and target patterns with a different plain converter whose source patterns do not end in .weight, so that plain converter was never wrapped with Fp8Dequantize and is already present in kept. This broad match nevertheless appends a fresh copy from plain because kept_ids tracks only appended matches. During save_pretrained, the duplicate reverse conversion then runs twice—for example, a transpose can cancel itself or a merge can fail—producing an incorrect checkpoint. Include source/operation identity in the match or deduplicate against the converters already retained.
Useful? React with 👍 / 👎.
…tained converter In a heterogeneous composite model another submodel's converter can share the type and targets of an unscoped fp8 converter while its sources do not end in .weight. The quantizer never wraps it, so it is already kept as loaded, but the broad match appended a fresh copy from the plain mapping and save_pretrained reversed it twice. Counterparts now also match on source patterns (the dequant anchors and scale sources stripped) and operations, and a counterpart whose signature equals an already-retained converter is skipped.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
…tests (unslothai#14741) `tests/test_fp8_dequant_save_policy.py` (unslothai#14678) runs real fp8 loads after `import unsloth`, which patch transformers' `Fp8Dequantize` (and `replace_with_fp8_linear`) for the whole process. Any test file that runs later in the same pytest session and expects the unpatched class then fails. On current main: ``` pytest -o addopts= tests/test_fp8_dequant_save_policy.py tests/test_fp8_ragged_block_dequant.py FAILED tests/test_fp8_ragged_block_dequant.py::test_ragged_rows_match_block_reference[glm5_3] FAILED tests/test_fp8_ragged_block_dequant.py::test_ragged_rows_match_block_reference[ling3] ``` `test_fp8_ragged_block_dequant.py` alone passes, so this is test isolation only, not a product change. ## Change An autouse fixture in `test_fp8_dequant_save_policy.py` snapshots the `Fp8Dequantize` attributes and `replace_with_fp8_linear` through `monkeypatch`, so they are restored after every test in the file. ## Validation (CPU, CUDA hidden) `pytest -o addopts= -m "not server and not native_mxc" tests/test_fp8_dequant_save_policy.py tests/test_fp8_*.py`: | torch | main | this PR | |---|---|---| | 2.14 | 2 failed, 176 passed, 219 skipped | 178 passed, 219 skipped | | 2.10 | not run | 178 passed, 219 skipped |
…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>
Follow-up to #14516. Two gaps remain in the fp8 dequantize-then-save cleanup.
Composite models. _drop_fp8_dequantize_conversions swapped each Fp8Dequantize model converter
for the first plain converter with the same type and targets. Transformers scopes one converter
per submodel (scope_prefix), but the dequantizing copy built by the fp8 quantizer is unscoped. So
only the first submodel got its expert unmerge back. The others saved packed experts, which
transformers 4.57.6 reports as 6 missing and 2 unexpected keys and re-initializes. An unscoped
conversion is now replaced by every matching scoped counterpart, and a scoped one by its own
scope.
Routes that skipped the cleanup. It only ran when Unsloth injected
FineGrainedFP8Config(dequantize = True) itself (FastLanguageModel) or when load_in_16bit was set
(FastBaseModel). A caller's dequantize config, or transformers' own dequantize on CPU and pre-sm89
GPUs, kept the conversions. save_pretrained then wrote fp8 bytes under a config with no
quantization_config, and a reload changed every quantized weight (9 of 9 on a tiny Llama). The
helper is a no-op without Fp8Dequantize conversions, so it now runs after every load.
Repro (tiny block-FP8 checkpoints, CPU):
save matches a plain 16bit save and reloads on 4.57.6 with no missing or unexpected keys
0 and 0 after
Tests: tests/test_fp8_dequant_save_policy.py (CPU, transformers 5.x; the AST checks also run on
4.57.6). 3 of its 5 cases fail before this change. The existing FP8 tests pass on torch 2.13 and
2.14 with transformers 5.17.