Skip to content

fix grpo generation_kwargs #3634

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
Jun 24, 2025
Merged

fix grpo generation_kwargs #3634

merged 2 commits into from
Jun 24, 2025

Conversation

ahatamiz
Copy link
Contributor

What does this PR do?

Currently, If the user does not pass generation_kwargs, then you should not face any issues (i.e. default values). Yet, this is what you get if you specify vllm_mode=colocate:

generatigeneration_kwargson_kwargs.update(self.args.)
TypeError: 'NoneType' object is not iterable

This PR simply fixes that !

Fixes #3633

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a GitHub issue? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

Who can review?

@qgallouedec

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
@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.

@kashif kashif requested a review from Copilot June 24, 2025 09:41
@shirinyamani shirinyamani self-requested a review June 24, 2025 09:42
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR prevents a NoneType error by only updating the default generation_kwargs when user overrides are provided.

  • Adds a guard to skip updating when self.args.generation_kwargs is None.
Comments suppressed due to low confidence (2)

trl/trainer/grpo_trainer.py:1127

  • Consider adding a test case to verify that omitting generation_kwargs (i.e., leaving it as None) does not raise an error and correctly falls back to default values.
                if self.args.generation_kwargs is not None:

trl/trainer/grpo_trainer.py:1117

  • [nitpick] It might help to update the method docstring to note that generation_kwargs can be None and describe the default behavior when no overrides are provided.
                generation_kwargs = {

@kashif kashif added the 🐛 bug Something isn't working label Jun 24, 2025
Copy link
Member

@shirinyamani shirinyamani left a comment

Choose a reason for hiding this comment

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

Hi @ahatamiz Ali jan, Thanks for your contribution;
I've tested your PR;

For future reference I tested with;

from datasets import load_dataset
from trl import GRPOConfig, GRPOTrainer
import os

# Set required environment variables for VLLM
os.environ["RANK"] = "0"
os.environ["LOCAL_RANK"] = "0"
os.environ["WORLD_SIZE"] = "1"
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "12355"

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


def reward_len(completions, **kwargs):
    return [-abs(20 - len(completion)) for completion in completions]


training_args = GRPOConfig(
    output_dir="pr3634",
    use_vllm=True,
    vllm_mode="colocate",
    gradient_checkpointing=True,
    num_generations=4,
    per_device_train_batch_size=4,
    vllm_gpu_memory_utilization=0.10,
    vllm_tensor_parallel_size=1,
    max_prompt_length=512,
    max_completion_length=1024,
    max_steps=2
)

trainer = GRPOTrainer(
    model="Qwen/Qwen2-0.5B-Instruct",
    reward_funcs=reward_len,
    args=training_args,
    train_dataset=dataset,
)

trainer.train()

then ran with;

accelerate launch --config_file examples/accelerate_configs/single_gpu.yaml PR_3634.py

@shirinyamani shirinyamani merged commit 34fa6b9 into huggingface:main Jun 24, 2025
10 checks passed
@ahatamiz
Copy link
Contributor Author

Hi @ahatamiz Ali jan, Thanks for your contribution; I've tested your PR;

For future reference I tested with;

from datasets import load_dataset
from trl import GRPOConfig, GRPOTrainer
import os

# Set required environment variables for VLLM
os.environ["RANK"] = "0"
os.environ["LOCAL_RANK"] = "0"
os.environ["WORLD_SIZE"] = "1"
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "12355"

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


def reward_len(completions, **kwargs):
    return [-abs(20 - len(completion)) for completion in completions]


training_args = GRPOConfig(
    output_dir="pr3634",
    use_vllm=True,
    vllm_mode="colocate",
    gradient_checkpointing=True,
    num_generations=4,
    per_device_train_batch_size=4,
    vllm_gpu_memory_utilization=0.10,
    vllm_tensor_parallel_size=1,
    max_prompt_length=512,
    max_completion_length=1024,
    max_steps=2
)

trainer = GRPOTrainer(
    model="Qwen/Qwen2-0.5B-Instruct",
    reward_funcs=reward_len,
    args=training_args,
    train_dataset=dataset,
)

trainer.train()

then ran with;

accelerate launch --config_file examples/accelerate_configs/single_gpu.yaml PR_3634.py

Thank you @shirinyamani for the review and also this pointer. Sure, I will keep this in mind.

Kind Regards,
Ali

@ahatamiz ahatamiz deleted the dev-1-ah branch June 24, 2025 17:01
qgallouedec pushed a commit that referenced this pull request Jul 8, 2025
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Co-authored-by: Kashif Rasul <kashif.rasul@gmail.com>
marcandrelarochelle pushed a commit to marcandrelarochelle/trl that referenced this pull request Jul 29, 2025
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Co-authored-by: Kashif Rasul <kashif.rasul@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GRPO Colocate mode crashes if generation_kwargs is undefined
4 participants