Repository navigation
Full finetuning: dequantize block FP8 checkpoints on load - #15367
Conversation
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.
|
@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. |
|
Codex Review: Didn't find any major issues. Swish! 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". |
…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]
|
@codex review |
There was a problem hiding this comment.
💡 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".
| model, _ = FastModel.from_pretrained(src, max_seq_length = 64, dtype = torch.bfloat16, | ||
| full_finetuning = True, load_in_4bit = False, device_map = "cuda:0") |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
[skip ci]
…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.
Problem
FastModel only passes
dequantize = Trueto transformers'FineGrainedFP8Configfor an explicitload_in_16bit = Trueload. Full finetuning does not setload_in_16bit, so transformers keepsFP8Linearlayers. Training preparation then casts the fp8 weight storage to bf16 without applyingweight_scale_inv, and three things go wrong:FP8Linear.forwardtreats a two-byte weight as a plainF.linearweight, so outputs are off by the block scales;save_pretrainedwrites bf16 tensors under aquant_method: fp8, dequantize: falseconfig, so a reload changes every projection again.This predates #14803. The same load with
load_in_16bit = Truewas already correct.Fix
In
FastBaseModel.from_pretrained, full finetuning of aquant_method: fp8checkpoint now requestsdequantize = True, as the explicit 16bit path does. It also runs_dequantize_leftover_fp8_paramsafter load, again as the 16bit path does. A caller's ownquantization_configstill wins.If the installed transformers has no
dequantizeflag onFineGrainedFP8Config, 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:full_finetuning = Truewithload_in_4bit = False, and with the defaultload_in_4bit, requestsdequantize = True. Both fail on main.full_finetuningwithload_in_16bit = Truepasses on main and here.dequantize = False.dequantizeflag refuses full finetuning (fails on main, which loads) and still allows a LoRA load.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:load_in_16bit = Truequantization_configdequantize: falseThe 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
FineGrainedFP8Configthat has nodequantizeflag, not on a real transformers 4.57.x install.