Skip to content

Add env var SGLANG_DUAL_STREAM_TOKEN_THRESHOLD #9405

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

trevor-m
Copy link
Collaborator

Motivation

The token threshold was hardcoded to 1024, add an env var to make it more flexible.

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

Copy link
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 @trevor-m, 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 enhances the flexibility of the DeepSeek V2 model's Mixture-of-Experts (MoE) layer by making a previously hardcoded token threshold configurable. It introduces a new environment variable, SGLANG_DUAL_STREAM_TOKEN_THRESHOLD, which allows users to dynamically adjust the threshold for parallel execution of shared and routed experts using separate CUDA streams. This change removes a fixed value, providing greater control over performance tuning for small batches.

Highlights

  • New Environment Variable: Introduces SGLANG_DUAL_STREAM_TOKEN_THRESHOLD to allow dynamic configuration of the token threshold for dual-stream processing.
  • DeepSeek V2 MoE Layer Flexibility: Replaces a hardcoded 1024 token threshold in the DeepSeek V2 model's Mixture-of-Experts (MoE) layer with the new SGLANG_DUAL_STREAM_TOKEN_THRESHOLD environment variable, enabling more flexible parallel execution.
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 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.

Copy link
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 introduces a new environment variable SGLANG_DUAL_STREAM_TOKEN_THRESHOLD to make the token threshold for dual-stream execution in DeepSeek-V2 MoE layers configurable, replacing a hardcoded value. The changes include updating the model implementation to use this environment variable and adding corresponding documentation.

My feedback focuses on improving the efficiency of reading this new configuration value. Instead of reading the environment variable in the __init__ of each MoE layer, which can be inefficient, I suggest reading it once at the module level. This aligns better with existing patterns in the codebase for handling global configurations.

Comment on lines +305 to +307
self.dual_stream_token_threshold = get_int_env_var(
"SGLANG_DUAL_STREAM_TOKEN_THRESHOLD", 1024
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better performance and code consistency, it's recommended to read the environment variable only once at the module level instead of in the __init__ of each MoE layer. This avoids repeated os.getenv calls for a value that does not change during runtime.

You could define a module-level constant at the top of the file, similar to how other configurations like _is_hip and _is_cuda are handled. For example:

# At the top of the file
_DUAL_STREAM_TOKEN_THRESHOLD = get_int_env_var("SGLANG_DUAL_STREAM_TOKEN_THRESHOLD", 1024)

# In DeepseekV2MoE.__init__
self.dual_stream_token_threshold = _DUAL_STREAM_TOKEN_THRESHOLD

This approach is more efficient and aligns better with existing patterns in the codebase for handling global settings.

if (
self.alt_stream is not None
and self.num_fused_shared_experts == 0
and hidden_states.shape[0] > 0
and hidden_states.shape[0] <= DUAL_STREAM_TOKEN_THRESHOLD
and hidden_states.shape[0] <= self.dual_stream_token_threshold
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TRT-LLM doesn't have a threshold and instead enables dual stream whenever cuda graph is used. https://github.com/NVIDIA/TensorRT-LLM/blob/e5e417019b3b06d8f4b5e870937e18b77424211d/tensorrt_llm/_torch/modules/multi_stream_utils.py#L60

Should we adopt the same? If the number of tokens is high, the driver should automatically serialize the streams anyway. @Alcanderian

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.

1 participant