Skip to content

[PD] Respect sampling_params.max_new_tokens when PD disaggregation is activated#7147

Closed
ShangmingCai wants to merge 1 commit intosgl-project:mainfrom
kvcache-ai:fix_pd_max_new_tokens
Closed

[PD] Respect sampling_params.max_new_tokens when PD disaggregation is activated#7147
ShangmingCai wants to merge 1 commit intosgl-project:mainfrom
kvcache-ai:fix_pd_max_new_tokens

Conversation

@ShangmingCai
Copy link
Copy Markdown
Collaborator

Motivation

This fixes a bug when PD disaggregation is activated and sampling_params.max_new_tokens <= 1

Before:

(base) root@iZ2zed0tiwrdk9tucyw2p9Z:~# curl -X POST http://127.0.0.1:8000/generate -H "Content-Type: application/json" -d '{
  "text": "You are a helpful assistant.\n",
  "sampling_params": {
    "temperature": 0,
    "max_new_tokens": 1
  }, "stream": true
}'
data: {"text":"Okay","meta_info":{"id":"05a3d0597bd642c991a293960228482d","finish_reason":null,"prompt_tokens":6,"completion_tokens":1,"cached_tokens":0}}

data: {"text":"Okay,","meta_info":{"id":"05a3d0597bd642c991a293960228482d","finish_reason":{"type":"length","length":1},"prompt_tokens":6,"completion_tokens":2,"cached_tokens":0,"e2e_latency":0.33734869956970215}}

data: [DONE]

(base) root@iZ2zed0tiwrdk9tucyw2p9Z:~# curl -X POST http://127.0.0.1:8000/generate -H "Content-Type: application/json" -d '{
  "text": "You are a helpful assistant.\n",
  "sampling_params": {
    "temperature": 0,
    "max_new_tokens": 1
  }, "stream": false
}'
{"text":"Okay,","meta_info":{"id":"87603e4404c446dc8bc4e3f69a1e6063","finish_reason":{"type":"length","length":1},"prompt_tokens":6,"completion_tokens":2,"cached_tokens":0,"e2e_latency":0.10740494728088379}}

The completion_tokens don't match the length required in the sampling params or in the finish reason.

After:

(base) root@iZ2zed0tiwrdk9tucyw2p9Z:~# curl -X POST http://127.0.0.1:8000/generate -H "Content-Type: application/json" -d '{
  "text": "You are a helpful assistant.\n",
  "sampling_params": {
    "temperature": 0,
    "max_new_tokens": 1
  }, "stream": true 
}'
data: {"text":"Okay","meta_info":{"id":"13d7bc930ff74b65b8048e52527e8aea","finish_reason":{"type":"length","length":1},"prompt_tokens":6,"completion_tokens":1,"cached_tokens":0,"e2e_latency":0.3873429298400879}}

data: [DONE]

(base) root@iZ2zed0tiwrdk9tucyw2p9Z:~# curl -X POST http://127.0.0.1:8000/generate -H "Content-Type: application/json" -d '{
  "text": "You are a helpful assistant.\n",
  "sampling_params": {
    "temperature": 0,
    "max_new_tokens": 1
  }, "stream": false
}'
{"text":"Okay","meta_info":{"id":"c8bbea881e054239b75b9c6431e3e2a4","finish_reason":{"type":"length","length":1},"prompt_tokens":6,"completion_tokens":1,"cached_tokens":0,"e2e_latency":0.18650269508361816}}

Modifications

The reason behind this is that we get the first token ids from the prefill node and return the first two tokens when the decode instance finishes the first forward pass. We didn't want to cause a weird waiting time and introduce a concept of Time to Second Token, but we still need to respect the sampling_params.max_new_tokens. This fix is simple by skipping the second token in the streaming output. We should not use req.check_finished() since it will cause a memory leak when disaggregation PD is on.

CC: @ByronHsu @acelyc111 @fzyzcjy @whybeyoung

Checklist

…tivated

Signed-off-by: Shangming Cai <caishangming@linux.alibaba.com>
Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

Summary of Changes

Hello @ShangmingCai, 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 fixes a specific issue in the system's handling of max_new_tokens when Prefill/Decode disaggregation is enabled, particularly for requests with a maximum token limit of 1 or less. The core change ensures the system correctly respects the requested output length and reports the accurate finish reason under these conditions.

Highlights

  • Bug Fix for max_new_tokens: I've addressed a bug where the reported completion_tokens and finish_reason were incorrect when Prefill/Decode (PD) disaggregation was active and sampling_params.max_new_tokens was set to 1 or less. The system was incorrectly generating and reporting two tokens instead of respecting the requested limit.
  • Respecting Length Constraint: The changes ensure that when in decode disaggregation mode and max_new_tokens <= 1, the system now correctly stops after the specified number of tokens (usually 1) and sets the finish_reason to 'length'. This prevents streaming of extra tokens and ensures accurate output length.
  • Tokenizer Manager Handling: I've added logic in the tokenizer manager to handle requests that are finished early due to this length constraint in decode disaggregation mode. This prevents unnecessary error logging related to missing request states when the request is intentionally completed.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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 configureGemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

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

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 aims to fix a bug where sampling_params.max_new_tokens was not correctly respected when PD disaggregation is active, especially for small values of max_new_tokens. The changes involve adjusting the logic in scheduler_output_processor_mixin.py to control token output and correctly set the finish reason, and modifying tokenizer_manager.py to prevent erroneous error logging in early finish scenarios.

My review focuses on ensuring the conditional logic precisely matches the intended scope described in the comments and PR description, clarifying potential discrepancies, suggesting a minor code simplification, and enhancing robustness against a potential runtime error. The changes appear to correctly address the core issue demonstrated in the examples.

Copy link
Copy Markdown
Collaborator

@ByronHsu ByronHsu left a comment

Choose a reason for hiding this comment

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

looks a bit hacky. i wonder if we can just skip the first decode step?

@ShangmingCai
Copy link
Copy Markdown
Collaborator Author

looks a bit hacky. i wonder if we can just skip the first decode step?

Sure. Let's fix this at the source.

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