Skip to content

diffusion: Add Configurable Generator Device and Seed Support via API#14366

Merged
mickqian merged 5 commits intosgl-project:mainfrom
niehen6174:suppor_generator_device
Dec 4, 2025
Merged

diffusion: Add Configurable Generator Device and Seed Support via API#14366
mickqian merged 5 commits intosgl-project:mainfrom
niehen6174:suppor_generator_device

Conversation

@niehen6174
Copy link
Copy Markdown
Contributor

Motivation

Previously, the random generator in the diffusion pipeline was hardcoded to use CPU device (torch.Generator("cpu")), which limited flexibility and performance. Additionally, there was no way to set the random seed through the server API, making it difficult to reproduce results or control randomness in generation tasks.

This PR addresses these limitations by:

  1. Making generator device configurable: Allow users to choose between CUDA and CPU for the random generator, with CUDA as the default device. (When using CUDA in the pipe, diffusers by default use CUDA as the device for random generator.)
  2. Adding seed parameter support via API: Enable setting random seeds through the OpenAI-compatible API endpoints for both image and video generation

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

@github-actions github-actions Bot added npu diffusion SGLang Diffusion labels Dec 3, 2025
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @niehen6174, 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 significantly improves the control and reproducibility of the diffusion pipeline by introducing the ability to specify the device for the random number generator and to pass a random seed via the API. Previously, the generator was hardcoded to CPU, and seed control was unavailable through the API. These changes provide users with greater flexibility to optimize performance and ensure consistent generation outcomes for both image and video tasks.

Highlights

  • Configurable Generator Device: The random generator in the diffusion pipeline can now be configured to use either CUDA or CPU, with CUDA as the default, enhancing flexibility and performance.
  • API Seed Support: Users can now set random seeds through the OpenAI-compatible API endpoints for both image and video generation, allowing for reproducible results and controlled randomness.
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.

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 introduces support for a configurable generator device and seed via the API, which is a great enhancement for reproducibility and performance tuning. The changes are well-implemented across the different layers, from the API endpoints to the core pipeline logic. I have one suggestion in input_validation.py to simplify the device selection logic and add a warning when falling back to CPU, which will improve code clarity and user experience.

Comment on lines +59 to +66
generator_device = batch.generator_device

# Determine the device for generator
if generator_device == "cpu":
device_str = "cpu"
else:
# Use cuda if available, otherwise fallback to cpu
device_str = "cuda" if torch.cuda.is_available() else "cpu"
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.

medium

The logic to determine the generator device can be simplified for better readability. Additionally, adding a warning when CUDA is requested but unavailable, causing a fallback to CPU, would provide valuable feedback to the user about the execution environment.

Suggested change
generator_device = batch.generator_device
# Determine the device for generator
if generator_device == "cpu":
device_str = "cpu"
else:
# Use cuda if available, otherwise fallback to cpu
device_str = "cuda" if torch.cuda.is_available() else "cpu"
# Determine the device for the generator, with a fallback to CPU if CUDA is not available.
device_str = batch.generator_device
if device_str == "cuda" and not torch.cuda.is_available():
logger.warning(
"CUDA device requested for generator, but it is not available. Falling back to CPU."
)
device_str = "cpu"

@mickqian
Copy link
Copy Markdown
Collaborator

mickqian commented Dec 3, 2025

Some comments could be cleaned

@niehen6174
Copy link
Copy Markdown
Contributor Author

Some comments could be cleaned

Done.

@mickqian
Copy link
Copy Markdown
Collaborator

mickqian commented Dec 3, 2025

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci label Dec 3, 2025
@mickqian mickqian merged commit 788628b into sgl-project:main Dec 4, 2025
47 checks passed
tonyluj pushed a commit to openanolis/sglang that referenced this pull request Dec 5, 2025
…via API (sgl-project#14366)

Co-authored-by: niehen6174 <niehen.6174@gmail.com>
Co-authored-by: Mick <mickjagger19@icloud.com>
tonyluj pushed a commit to openanolis/sglang that referenced this pull request Dec 5, 2025
…via API (sgl-project#14366)

Co-authored-by: niehen6174 <niehen.6174@gmail.com>
Co-authored-by: Mick <mickjagger19@icloud.com>
yuchengz816-bot pushed a commit to yuchengz816-bot/sglang that referenced this pull request Dec 8, 2025
…via API (sgl-project#14366)

Co-authored-by: niehen6174 <niehen.6174@gmail.com>
Co-authored-by: Mick <mickjagger19@icloud.com>
Kevin-XiongC pushed a commit to novitalabs/sglang that referenced this pull request Dec 9, 2025
…via API (sgl-project#14366)

Co-authored-by: niehen6174 <niehen.6174@gmail.com>
Co-authored-by: Mick <mickjagger19@icloud.com>
dcampora pushed a commit to dcampora/sglang that referenced this pull request Dec 15, 2025
…via API (sgl-project#14366)

Co-authored-by: niehen6174 <niehen.6174@gmail.com>
Co-authored-by: Mick <mickjagger19@icloud.com>
YChange01 pushed a commit to YChange01/sglang that referenced this pull request Jan 13, 2026
…via API (sgl-project#14366)

Co-authored-by: niehen6174 <niehen.6174@gmail.com>
Co-authored-by: Mick <mickjagger19@icloud.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants