diffusion: Add Configurable Generator Device and Seed Support via API#14366
diffusion: Add Configurable Generator Device and Seed Support via API#14366mickqian merged 5 commits intosgl-project:mainfrom
Conversation
Summary of ChangesHello @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
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| 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" |
There was a problem hiding this comment.
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.
| 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" |
|
Some comments could be cleaned |
Done. |
|
/tag-and-rerun-ci |
…via API (sgl-project#14366) Co-authored-by: niehen6174 <niehen.6174@gmail.com> Co-authored-by: Mick <mickjagger19@icloud.com>
…via API (sgl-project#14366) Co-authored-by: niehen6174 <niehen.6174@gmail.com> Co-authored-by: Mick <mickjagger19@icloud.com>
…via API (sgl-project#14366) Co-authored-by: niehen6174 <niehen.6174@gmail.com> Co-authored-by: Mick <mickjagger19@icloud.com>
…via API (sgl-project#14366) Co-authored-by: niehen6174 <niehen.6174@gmail.com> Co-authored-by: Mick <mickjagger19@icloud.com>
…via API (sgl-project#14366) Co-authored-by: niehen6174 <niehen.6174@gmail.com> Co-authored-by: Mick <mickjagger19@icloud.com>
…via API (sgl-project#14366) Co-authored-by: niehen6174 <niehen.6174@gmail.com> Co-authored-by: Mick <mickjagger19@icloud.com>
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:
Modifications
Accuracy Tests
Benchmarking and Profiling
Checklist