Repository navigation
Context parallel: gather the FSDP2 state dict in save_model on transformers 4.x - #14028
Conversation
…n transformers < 5 transformers 4.57 dispatches save_model on accelerator.parallelism_config before FSDP, so the config Unsloth installs for the (dp_shard, cp) mesh skipped the full state dict gather and PEFT failed on DTensor shards. Save under the user's own config, as FSDP2 without CP does.
|
@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 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e900bbf43b
ℹ️ 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".
| return original_save_model(self, *args, **kwargs) | ||
| # transformers < 5 checks parallelism_config before FSDP and would skip the FSDP2 state dict | ||
| # gather; save under the config the user had, as FSDP2 without CP does. | ||
| state.parallelism_config = _INSTALLED_PARALLELISM_CONFIG[0] |
There was a problem hiding this comment.
Clear even a user-supplied parallelism config while saving
When the user supplied a pre-existing ParallelismConfig—which _check_fsdp2_parallelism_config permits when only dp_shard is active—slot 0 is non-null, so this assignment still selects Transformers 4.57.6's parallelism branch. That branch tests only whether the config is non-null and calls _save without gathering a state dict before reaching the FSDP branch, leaving these users with the same DTensor/PEFT failure for manual and periodic saves; temporarily set the config to None, then restore the installed config afterward. See Transformers 4.57.6 save_model.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Right, transformers 4.57.6 takes the parallelism branch for any non-None config, so restoring a user's dp_shard-only config still skipped the gather. Fixed in the latest commit: under context parallelism save_model now runs with parallelism_config = None and reinstalls Unsloth's config afterwards. The gloo save test is parametrized with a user ParallelismConfig(dp_shard_size = 2); on transformers 4.57.6 / PEFT 0.18.0 that case fails on the previous commit and passes now, and both cases pass on 5.17.
… config also gathers on transformers 4.x [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
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". |
…-GaLore offload, GRPO truncated support, CP save on transformers 4.x (unslothai#14226) ## Summary Fixes for four problems found when re-reviewing recently merged PRs. Each one still reproduced on main, and each has a test that fails on main and passes here. Default configs are unchanged. 1. **Fused LoRA with dropout or a `lora_B` bias (unslothai#13898).** - **Graph breaks:** before torch 2.11 the fused LoRA function runs with Dynamo disabled. unslothai#13898 moved dropout and bias projections from PEFT onto that path, so compiling them hit a graph break each and `fullgraph = True` failed. While compiling on torch < 2.11 those projections now run PEFT again, as they did before unslothai#13898. Plain LoRA, eager fusion and torch 2.11+ are unchanged. - **Memory:** on 4-bit weights the dropout and bias path dequantized into the shared global buffer, so one dense weight stayed resident; PEFT never keeps one. That path now dequantizes into a temporary, including in eval, where dropout is a no-op. `matmul_lora` gets a `use_global_buffer` keyword, default `True`, so every other caller is unchanged. 2. **Q-GaLore optimizer state offload (unslothai#14025).** If a step pre-hook registered after the offloader created a gradient, that parameter's state stayed on CPU and the step crashed with a device mismatch. State is now restored inside the step, after every pre-hook has run. 3. **GRPO truncated-support scoring (unslothai#13900).** This needs the scored policy to be the sampler. That is not the case with a reference model (`beta != 0`), with rollouts reused across optimizer steps, or when vLLM weight sync is skipped (LoRA or quantized without `fast_inference`). In those cases the new policy's implied probabilities did not sum to 1, which made the PPO ratio wrong. Those cases now warn once and score the full vocabulary; on-policy runs with `beta = 0` keep the feature. 4. **Context-parallel save on transformers 4.x (unslothai#14028).** With `SHARDED_STATE_DICT` the sharded save wrote no adapter, and with `FULL_STATE_DICT` it gathered the frozen base weights. The save now gathers only the adapter and rank 0 writes it. Transformers 5 is unchanged. The routing replay part of the same review is in unslothai/unsloth-zoo#1874. ## Tests | Test | main | this PR | |---|---|---| | `tests/test_fused_lora_dropout_bias_compile_memory.py` | 18 failed | 18 passed | | `tests/test_fused_lora_dropout_bias.py` (eval keeps the dropout route) | failed | passed | | `tests/test_optimizer_state_offload.py` | 2 failed | 38 passed | | `tests/test_grpo_truncated_support_policy_gate.py` | 4 failed | passed | | `tests/test_context_parallel_fsdp2_save_gloo.py` (transformers 4.57.6, 2 gloo ranks) | 2 failed | passed | unslothai#13898 base vs head, Llama block at 4096 / 11008, 3 arms. "PEFT" is main with fused dropout and bias turned off, i.e. the routing before unslothai#13898: | torch | Graph breaks on dropout / bias configs (main / this PR) | 4-bit dropout / bias peak MiB (main / PEFT / this PR) | |---|---|---| | 2.10, T4 | 6 / 0, fullgraph fails on main | 293.0 to 294.3 / 209.8 to 211.0 / 207.0 to 208.3 | | 2.13, B200 | 0 / 0 | 340.6 to 341.1 / 257.5 to 257.7 / 254.6 to 255.1 | | 2.14, T4 | 0 / 0 | 292.9 to 293.3 / 209.7 to 210.0 / 206.9 to 207.3 | 16-bit and default configs give the same memory and graph break counts in all three arms. Plain LoRA on 2.10 has 6 graph breaks in every arm; that is older than unslothai#13898 and is left as is. ## Not covered here - Torch 2.11 and 2.12 were not run. Items 2 to 4 ran on torch 2.13 only. - No full-model training or throughput run; the unslothai#13898 memory numbers are per block. - No vLLM end-to-end GRPO run, no NCCL multi-GPU save, no `push_to_hub`. - Off-policy truncated support falls back to full-vocabulary scoring rather than the sampler's kept token set.
Follow-up to #13910.
With FSDP2 and context parallelism enabled,
save_modelfailed on transformers 4.57.6. That version checksaccelerator.parallelism_configbefore FSDP, so with the parallelism config Unsloth installs for CP it skipped the FSDP2 full state dict gather. PEFT then received sharded tensors and every rank raisedRuntimeError: Attempted to access the data pointer on an invalid python storage., and no adapter file was written. Periodic checkpoints go throughsave_modeltoo. Transformers 5.x checks FSDP first, so it was not affected.Change (
unsloth/context_parallel.py): the patched SFTTrainer also wrapssave_model. Only while Unsloth's own parallelism config is the live one, it puts back the config the user had for the duration of the call and reinstalls Unsloth's afterwards, so the save takes the same path as FSDP2 without CP. In every other case, including CP off, it calls the originalsave_modeldirectly.Tests:
tests/test_context_parallel_fsdp2_save_gloo.py: two gloo ranks on CPU through the real transformers, accelerate and PEFT save code. Rank 0 must write all 4 LoRA tensors whole and equal to the unsharded originals, and Unsloth's config must still be installed afterwards. Fails on main and passes here on transformers 4.57.6 with PEFT 0.18.0 and 0.20.0; passes on both on 5.17.tests/test_context_parallel*.py: 96 passed, 1 skipped.Not covered: a real multi-GPU NCCL save and resume, and
SHARDED_STATE_DICTcheckpoints.