Repository navigation
Generation keeps inference_mode on DeepSpeed ZeRO-1/2; only ZeRO-3 needs no_grad - #14673
Conversation
…r an unknown stage) needs no_grad
|
@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: 183be52060
ℹ️ 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".
| "yes", | ||
| "on", | ||
| ) or (isinstance(shared_state, dict) and "DEEPSPEED" in str(shared_state.get("distributed_type", ""))) | ||
| if deepspeed and _deepspeed_zero_stage(shared_state) in (None, 3): |
There was a problem hiding this comment.
Include the stage helper in generated GRPO modules
When a generated GRPO trainer runs under any DeepSpeed launch, deepspeed is true and this call raises NameError: name '_deepspeed_zero_stage' is not defined. unsloth/models/rl_replacements.py only appends inspect.getsource(_get_inference_mode_context_manager) to RL_PRE_ITEMS["grpo_trainer"], so the newly extracted helper is absent from the generated module even though the copied function references it. Include the helper in the generated preamble (or keep this logic self-contained) so DeepSpeed GRPO generation and log-prob scoring can run.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 512ca4b. The stage read now lives inside _get_inference_mode_context_manager instead of a separate helper, so the source rl_replacements copies into the generated GRPO trainer is self-contained. A test parses the function's own source and checks it calls no module-level helper.
There was a problem hiding this comment.
Fixed in 512ca4b: the stage read is inlined into _get_inference_mode_context_manager, so the source copied into the generated GRPO trainer is self-contained (test_the_grpo_preamble_copy_is_self_contained).
| plugins = shared_state.get("deepspeed_plugins") if isinstance(shared_state, dict) else None | ||
| if plugins is not None and not isinstance(plugins, dict): | ||
| plugins = {"default": plugins} | ||
| for plugin in (plugins or {}).values(): |
There was a problem hiding this comment.
Read only the selected DeepSpeed plugin
When Accelerator is configured with multiple DeepSpeed plugins and the selected plugin uses ZeRO-2 while an inactive plugin uses ZeRO-3, iterating over every plugin adds stage 3 and forces no_grad, so the optimization introduced by this change silently disappears. Accelerate marks exactly one plugin as selected, and the existing deepspeed_zero_stage() implementation in unsloth/models/loader_utils.py already filters on plugin.selected; apply the same selection here instead of combining active and inactive configurations.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 512ca4b. A plugin with selected=False is skipped, matching deepspeed_zero_stage() in loader_utils.py, so an inactive ZeRO-3 plugin no longer forces no_grad on a ZeRO-2 run. Test: an active stage-2 plugin beside an inactive stage-3 one keeps inference mode.
There was a problem hiding this comment.
Fixed in 512ca4b: plugins whose selected flag is False are skipped (test_only_the_selected_deepspeed_plugin_counts).
… the selected DeepSpeed plugin counts [skip ci]
|
@codex review |
|
@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
#14395 switched generation and logprob scoring from
torch.inference_mode()totorch.no_grad()on any DeepSpeed launch (ACCELERATE_USE_DEEPSPEEDor a DEEPSPEED accelerator state). The failure it fixed, "Inference tensors cannot be saved for backward", comes from ZeRO-3 keeping gathered persistent parameters. ZeRO-1 and ZeRO-2 never partition parameters, so they lost inference mode for no reason (a small decode overhead; TRL itself generates underno_grad, so results are the same either way)._get_inference_mode_context_managernow asks for the ZeRO stage before falling back tono_grad:is_deepspeed_zero3_enabled()(the Trainer's DeepSpeed config) returning True means stage 3;zero_stagefield;ACCELERATE_DEEPSPEED_ZERO_STAGEfromaccelerate launch.Any source saying 3, or no source at all, keeps
no_gradexactly as today. A known stage below 3 keepsinference_mode. Theds_idcheck on partitioned parameters is unchanged and still wins.Tests
tests/test_fsdp_generation_grad_mode.py:ds_id) parameters still gets no_grad;5 of the new cases fail on main; 24 pass here.
test_deepspeed_zero3.py,test_grpo_hidden_states_signal.py,test_fsdp2_native_backend_table.py,test_fa2_fast_generate_bypass.pyandtest_block_swap_auto_ddp.py: 107 passed.Not covered
No real multi-GPU DeepSpeed run here; the decode speed difference on ZeRO-2 was not measured.