Skip to content

FSDP2 QLoRA: keep 4-bit merge operands on one device in push_to_hub_merged - #14724

Merged
danielhanchen merged 2 commits into
mainfrom
dh/s3-fsdp2-qlora-merge-device
Oct 22, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/s3-fsdp2-qlora-merge-device

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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

#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.

push_to_hub_merged gathered with the default offload_to_cpu=True, so for
merged_4bit / merged_4bit_forced the LoRA weights of packed 4-bit layers
went to the CPU while the Params4bit base stayed on its GPU, and PEFT's
4-bit merge failed with a device mismatch. Use the same
_fsdp2_merged_offload policy save_pretrained_merged already uses (#14165).
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-22T14:39:40.607512Z d050af0 Manual request
ℹ️ 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

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: d050af097b

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

[skip ci]
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