Repository navigation
FSDP2 QLoRA: keep packed 4-bit bytes out of the loader dtype pass, fix merged_4bit save - #14165
Conversation
Under FSDP2 the 4-bit weights are packed in float16 / bfloat16. The vision loader's patch_model_and_tokenizer then runs module.to(_pre_set_compute_dtype) (gpt-oss bnb-4bit down_projs in float16, forced float32 models), which cast the packed words to float32 values and corrupted the weights. Show them as uint8, the bitsandbytes default, for that pass and restore the float view afterwards.
save_pretrained_merged gathered the shards with offload_to_cpu, which moves everything except the packed 4-bit weights to the CPU. merged_4bit merges with PEFT's Linear4bit.merge, so the dequantized base weight stayed on the GPU while the LoRA delta was on the CPU, and the save raised a device mismatch. Do not offload for the merged_4bit methods.
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. |
…pe-save [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: 59db8f7c6b
ℹ️ 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".
| offload_to_cpu = lambda self, args, kwargs: _merged_save_method(args, kwargs) | ||
| not in ("merged_4bit", "merged_4bit_forced", "forced_merged_4bit"), |
There was a problem hiding this comment.
Keep only merge operands on the GPU
When an FSDP2 CUDA model is saved with a 4-bit merge, this sets offload_to_cpu=False for the entire gather. fsdp2_gathered consequently retains every gathered non-quantized tensor—including full embeddings and unquantized vision-tower weights—on rank 0's GPU until the save finishes, even though only the LoRA tensors need to be colocated with the packed 4-bit weights. Large models that rely on FSDP because these full tensors do not fit on one GPU can therefore OOM before the merge; keep only the tensors participating in the PEFT merge on-device rather than disabling CPU offload globally.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed. Fixed in 167fb79: offload_to_cpu can now be a predicate, and merged 4-bit saves pass offload_all_but_4bit_merge_operands(model), which keeps only the LoRA weights of layers whose base weight is packed 4-bit on the GPU. Embeddings, unquantized towers and LoRA on unquantized layers (with their base) still go to the CPU. Added test_merged_4bit_keeps_only_merge_operands_on_the_gpu.
…n the GPU [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
## Summary The `Core zoo` cells in `consolidated-tests-ci.yml` keep hitting their 25 minute job cap after the tests have already passed. In one cancelled cell: - the zoo `pytest -n 4` step ran from 08:41 to 09:03 and passed (3015 passed, 620 skipped), - the job was then cancelled at 25:17 during `test_apply_fused_lm_head` and the pip cache save. The comment above the cap assumed a ~7.6 min zoo suite. It now takes up to about 23 minutes on a busy runner, so the cap cancels cells that have already passed. Today that cancelled Core zoo cells on #14152, #14165, #14188, #14192, #14199 and #14207, and on staging runs. Each one needs a manual rerun before the PR can merge. This raises the cap to 35 minutes, the same as `consolidated`. Hung tests are still bounded: `PYTEST_TIMEOUT: 300` in the step still names the culprit per test. ## Tests The workflow-parsing tests (`git grep -l consolidated-tests-ci -- tests`): 158 passed, 3 skipped. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…erged (unslothai#14724) ## Summary Under native FSDP2, `push_to_hub_merged(..., save_method="merged_4bit")` (and `merged_4bit_forced`) gathered with the default `offload_to_cpu=True`. The LoRA weights of packed 4-bit layers went to the CPU while the Params4bit base stayed on its GPU, so PEFT's 4-bit merge on rank 0 failed with "Expected all tensors to be on the same device". unslothai#14165 fixed this for `save_pretrained_merged` with `_fsdp2_merged_offload`. This passes the same callback to the Hub decorator, so both exports keep only the adapters of 4-bit layers next to their base and still send everything else to the CPU. ## Changes - `unsloth/save.py`: `unsloth_generic_push_to_hub_merged` uses `offload_to_cpu = _fsdp2_merged_offload`, like `unsloth_generic_save_pretrained_merged`. - `tests/test_fsdp2_save_args.py`: the offload policy test now covers both entry points, `forced_merged_4bit`, and checks the per-parameter decisions on a QLoRA-shaped model. ## Testing - `tests/test_fsdp2_save_args.py`: 14 passed; on main the 3 `push_to_hub_merged` 4-bit cases fail. - Tiny 4-bit Llama with LoRA on one GPU, parameters placed as the FSDP2 gather places them: the old placement raises the device mismatch, the new one merges and the merged weights are bitwise equal to a plain merge. - FSDP2 save, coverage, checkpoint and QLoRA gloo suites pass on CPU. Non-FSDP2 saves are untouched: the callback only runs when the model has FSDP2 units.
Summary
Two fixes for opt-in FSDP2 QLoRA (#13880, #13895). The default single-GPU path is unchanged: both only act under FSDP2.
Silent corruption of 4-bit weights in the loader's dtype pass. Under FSDP2, the 4-bit weights are stored as packed bytes in float16 / bfloat16 so they can be sharded, and that happens before
patch_model_and_tokenizer. Its dtype passes (module.to(module._pre_set_compute_dtype), forced float32) cast every floating parameter of a module, so the packed bytes were converted as if they were weights. Modules with_pre_set_compute_dtype(for example gpt-oss bnb-4bit experts loaded in float16) ended up with wrong weights and no error. On a toy layer through the real loader functions, the weight error was 0.25 before and 0 after.The vision loader now runs that step inside
packed_4bit_as_uint8, which shows the packed bytes as uint8 (what bitsandbytes stores without FSDP2, and which the dtype passes skip) and restores the float view afterwards.save_pretrained_merged(..., "merged_4bit")crashed under FSDP2. The collective save gathers the shards to rank 0 withoffload_to_cpu = True, so the LoRA weights moved to the CPU while the packed 4-bit base weights stayed on the GPU, and PEFT's merge raised a device mismatch. For the merged 4-bit methods the gather now keeps only the merge operands on the GPU: the LoRA weights of layers whose base weight is packed 4-bit (offload_all_but_4bit_merge_operands). Embeddings, unquantized towers and LoRA on unquantized layers still go to the CPU, so rank 0 does not hold full unsharded tensors on its GPU. Every other save method still offloads everything to the CPU.Tests
tests/test_fsdp2_qlora_dtype_pass.py: packed bytes survive the_pre_set_compute_dtypepass in float16 and bfloat16 storage; disabled and uint8 storage are untouched; without the context the pass does cast them; the vision loader wrapspatch_model_and_tokenizer.tests/test_fsdp2_save_args.py: newtest_merged_4bit_keeps_gathered_weights_on_their_deviceandtest_merged_4bit_keeps_only_merge_operands_on_the_gpu.Not run here: real 2-GPU NCCL, a real gpt-oss load, T4 / ROCm, transformers 4.57.6. Not fixed here: the 4-bit MoE forward under FSDP2 still crashes loudly because the all-gathered weight is a plain Parameter (follow-up).