Skip to content

[trl] vllm trl topk fixup#3935

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
Datta0:vllm_trl_topk
Jan 31, 2026
Merged

[trl] vllm trl topk fixup#3935
danielhanchen merged 4 commits into
unslothai:mainfrom
Datta0:vllm_trl_topk

Conversation

@Datta0

@Datta0 Datta0 commented Jan 26, 2026

Copy link
Copy Markdown
Collaborator

Fix top_k for GRPO vLLM.
huggingface/trl#4695 with this change trl added top_k in GRPOConfig and defaults to 0
We don't want that since vllm's all include top_k is -1 and 0 returns an error on SamplingParams creation. (Comparison between None and 0 or something error)

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @Datta0, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical compatibility issue between trl's GRPOConfig and vLLM when handling the top_k sampling parameter. By dynamically adjusting top_k to -1 (which signifies 'all' in vLLM) when it's otherwise set to 0 or None, the change prevents errors during the creation of vLLM's SamplingParams, ensuring smooth operation of GRPO with vLLM.

Highlights

  • GRPO vLLM top_k Fix: Implemented a specific fix for the top_k parameter within the GRPO vLLM configuration to prevent errors.
  • Compatibility with trl: Addresses an incompatibility where trl's GRPOConfig defaults top_k to 0, which is problematic for vLLM's SamplingParams.
  • vLLM SamplingParams Error Prevention: Ensures that vLLM's SamplingParams creation does not error out due to an invalid top_k value (0 or None) by dynamically setting it to -1 when use_vllm is active.
  • Minor Whitespace Adjustments: Corrected minor whitespace inconsistencies in the grpo_trainer__generate_and_score_completions function's string replacements.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a targeted fix for the top_k parameter within GRPOConfig when using vLLM. By setting top_k to -1 if it's None or 0, it correctly resolves a compatibility issue that caused errors during SamplingParams creation in vLLM. The PR also includes several minor whitespace cleanups, which improve code style and readability. The changes are well-implemented and address the issue effectively.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d72e3fe403

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +81 to +82
def grpo_config_fix_vllm_top_k(old_RLTrainer_source, old_RLConfig_source):
return "if use_vllm and (top_k is None or top_k == 0): top_k = -1\n"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Guard top_k fix for older TRL configs

The injected snippet always references top_k (and use_vllm) without checking whether the GRPOConfig signature actually includes it. In TRL versions prior to the linked PR, top_k isn’t defined in the config __init__, so when use_vllm is True this line will raise a NameError during config construction. Other config patches in this file guard on old_RLConfig_source (e.g., num_generations), so this should similarly check that top_k exists before injecting the line to avoid breaking vLLM users on older TRL versions.

Useful? React with 👍 / 👎.

@danielhanchen danielhanchen changed the base branch from nightly to main January 31, 2026 14:33
@danielhanchen danielhanchen merged commit 07a7ff4 into unslothai:main Jan 31, 2026
1 check passed
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
* [transformers] [v5] remove unused hybridcache (unslothai#3910)

* remote unused hybridcache

* cleanup

* Fix top_k on trl GRPO

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Daniel Han <danielhanchen@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

2 participants