Skip to content

🍡 Fix using reward model and DeepSpeed ZeRO 3 #3326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2025

Conversation

qgallouedec
Copy link
Member

@qgallouedec qgallouedec commented Apr 18, 2025

Fixes #3202

Before this PR, running this with ZeRO3 would fail:

from datasets import load_dataset
from trl import GRPOConfig, GRPOTrainer

dataset = load_dataset("trl-lib/tldr", split="train")

training_args = GRPOConfig(output_dir="data/Qwen2-0.5B-GRPO", bf16=True)
trainer = GRPOTrainer(
    model="Qwen/Qwen2-0.5B-Instruct",
    reward_funcs="trl-lib/Qwen2-0.5B-Reward",
    args=training_args,
    train_dataset=dataset,
)

trainer.train()

@qgallouedec qgallouedec changed the title reward model name got once and prepare deepspeed for reward models 🍡 Fix using reward model and DeepSpeed ZeRO 3 Apr 18, 2025
Comment on lines +425 to +434
self.reward_func_names = []
for i, reward_func in enumerate(reward_funcs):
if isinstance(reward_func, str):
reward_funcs[i] = AutoModelForSequenceClassification.from_pretrained(
reward_func, num_labels=1, **model_init_kwargs
)
if isinstance(reward_funcs[i], nn.Module): # Use Module over PretrainedModel for compat w/ compiled models
self.reward_func_names.append(reward_funcs[i].config._name_or_path.split("/")[-1])
else:
self.reward_func_names.append(reward_funcs[i].__name__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to get the reward name before it's wrapped with deepspeed.

Comment on lines +658 to +661
if self.is_deepspeed_enabled:
self.reward_funcs[i] = prepare_deepspeed(reward_func, self.accelerator)
else:
self.reward_funcs[i] = self.accelerator.prepare_model(reward_func, evaluation_mode=True)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes this issue

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@qgallouedec qgallouedec merged commit 89556c8 into main Apr 23, 2025
10 checks passed
@qgallouedec qgallouedec deleted the fix-ds3-reward-model branch April 23, 2025 22:09
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.

Issue with GRPO trainer when using a reward_func that is a pretrained model?
3 participants