-
Notifications
You must be signed in to change notification settings - Fork 89
feat(diffusers): add server demo for diffusers pipeline inference #1413
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
base: master
Are you sure you want to change the base?
feat(diffusers): add server demo for diffusers pipeline inference #1413
Conversation
Summary of ChangesHello @townwish4git, 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 introduces a robust asynchronous server demo designed to serve 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.
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 valuable example of an asynchronous server for diffusers pipeline inference using FastAPI and MindSpore. The implementation correctly handles thread-safety for concurrent requests by using request-scoped pipeline objects and careful management of mutable state. The code is well-structured and includes useful features like metrics logging and graceful shutdown.
My review includes several suggestions to improve code quality, such as removing unused variables, using more specific exception types, improving error messages, and adhering to Python best practices like avoiding local imports. These changes will enhance the clarity and maintainability of the example code.
| mindspore_dtype=ms.float16, | ||
| ) | ||
| else: | ||
| raise Exception("No Ascend device available") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the generic Exception is generally discouraged. It's better to raise a more specific exception to allow for more granular error handling. RuntimeError would be more appropriate here to indicate that an external condition (the absence of an Ascend device) prevents the program from running.
| raise Exception("No Ascend device available") | |
| raise RuntimeError("No Ascend device available") |
| if self.model in preset_models.SD3: | ||
| self.model_type = "SD3" | ||
| elif self.model in preset_models.SD3_5: | ||
| self.model_type = "SD3_5" | ||
|
|
||
| # Create appropriate pipeline based on model type and type_models | ||
| if self.type_models == "t2im": | ||
| if self.model_type in ["SD3", "SD3_5"]: | ||
| self.pipeline = TextToImagePipelineSD3(self.model) | ||
| else: | ||
| raise ValueError(f"Model type {self.model_type} not supported for text-to-image") | ||
| elif self.type_models == "t2v": | ||
| raise ValueError(f"Unsupported type_models: {self.type_models}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current logic for handling unsupported models can be improved. If an unsupported model name is provided, self.model_type becomes None, leading to a less informative error message: Model type None not supported for text-to-image. It's better to raise an error immediately if the model is not in the preset lists. This refactoring also simplifies the subsequent logic.
| if self.model in preset_models.SD3: | |
| self.model_type = "SD3" | |
| elif self.model in preset_models.SD3_5: | |
| self.model_type = "SD3_5" | |
| # Create appropriate pipeline based on model type and type_models | |
| if self.type_models == "t2im": | |
| if self.model_type in ["SD3", "SD3_5"]: | |
| self.pipeline = TextToImagePipelineSD3(self.model) | |
| else: | |
| raise ValueError(f"Model type {self.model_type} not supported for text-to-image") | |
| elif self.type_models == "t2v": | |
| raise ValueError(f"Unsupported type_models: {self.type_models}") | |
| if self.model in preset_models.SD3: | |
| self.model_type = "SD3" | |
| elif self.model in preset_models.SD3_5: | |
| self.model_type = "SD3_5" | |
| else: | |
| raise ValueError(f"Model '{self.model}' is not a supported preset model.") | |
| # Create appropriate pipeline based on model type and type_models | |
| if self.type_models == "t2im": | |
| self.pipeline = TextToImagePipelineSD3(self.model) | |
| elif self.type_models == "t2v": | |
| raise ValueError(f"Unsupported type_models: {self.type_models}") |
| model_pipeline.start() | ||
|
|
||
| request_pipe = RequestScopedPipeline(model_pipeline.pipeline) | ||
| pipeline_lock = threading.Lock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| num_images_per_prompt = json.num_images_per_prompt | ||
|
|
||
| wrapper = app.state.MODEL_PIPELINE | ||
| initializer = app.state.MODEL_INITIALIZER # noqa: F841 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| app.state.active_inferences += 1 | ||
|
|
||
| # output = await run_in_threadpool(infer) | ||
| loop = asyncio.get_event_loop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function asyncio.get_event_loop() has been deprecated since Python 3.10 and its usage is discouraged. It's recommended to use asyncio.get_running_loop() instead, which is safer as it raises a RuntimeError if no event loop is running.
| loop = asyncio.get_event_loop() | |
| loop = asyncio.get_running_loop() |
| return self._auto_detected_attrs | ||
|
|
||
| candidates: List[str] = [] | ||
| seen = set() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| self.video_dir = os.path.join(tempfile.gettempdir(), "videos") | ||
| if not os.path.exists(self.video_dir): | ||
| os.makedirs(self.video_dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
|
||
| def save_image(self, image): | ||
| if isinstance(image, ms.Tensor): | ||
| from mindspore.dataset.vision import transforms |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this PR do?
Adaptations for mindspore:
examples/serverexamples/server-asyncwhich demonstrate the serving capabilities for inference based on the
mindone.diffusersmodel, supporting concurrent and multi-threaded requests to generate images that may be requested by multiple users at the same time.Note
Before submitting
What's New. Here are thedocumentation guidelines
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@xxx