-
Notifications
You must be signed in to change notification settings - Fork 184
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
Add comments to SDXL Turbo example #512
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,39 @@ | ||
# --- | ||
# output-directory: "/tmp/stable-diffusion-xl-turbo" | ||
# args: [] | ||
# runtimes: ["runc", "gvisor"] | ||
# --- | ||
# # Stable Diffusion XL Turbo Image-to-image | ||
# | ||
# This example is similar to the [Stable Diffusion XL](/docs/examples/stable_diffusion_xl) | ||
# example, but it's a distilled model trained for real-time synthesis and is image-to-image. Learn more about it [here](https://stability.ai/news/stability-ai-sdxl-turbo). | ||
# | ||
# Input prompt: | ||
# `dog wizard, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney, 8k` | ||
# | ||
# Input | Output | ||
# :-------------------------:|:-------------------------: | ||
# ![](./stable_diffusion_turbo_input.png) | ![](./stable_diffusion_turbo_output.png) | ||
|
||
# ## Basic setup | ||
|
||
from pathlib import Path | ||
|
||
from modal import Image, Stub, gpu, method | ||
|
||
# ## Define a container image | ||
|
||
|
||
def download_models(): | ||
from huggingface_hub import snapshot_download | ||
|
||
ignore = ["*.bin", "*.onnx_data", "*/diffusion_pytorch_model.safetensors"] | ||
# Ignore files that we don't need to speed up download time. | ||
ignore = [ | ||
"*.bin", | ||
"*.onnx_data", | ||
"*/diffusion_pytorch_model.safetensors", | ||
] | ||
|
||
snapshot_download("stabilityai/sdxl-turbo", ignore_patterns=ignore) | ||
|
||
|
||
|
@@ -15,15 +42,23 @@ def download_models(): | |
.pip_install( | ||
"Pillow~=10.1.0", | ||
"diffusers~=0.24", | ||
"transformers~=4.35", | ||
"accelerate~=0.25", | ||
"safetensors~=0.4", | ||
"transformers~=4.35", # This is needed for `import torch` | ||
"accelerate~=0.25", # Allows `device_map="auto"``, which allows computation of optimized device_map | ||
"safetensors~=0.4", # Enables safetensor format as opposed to using unsafe pickle format | ||
) | ||
.run_function(download_models) | ||
) | ||
|
||
stub = Stub("stable-diffusion-xl-turbo", image=image) | ||
|
||
# ## Load model and run inference | ||
# | ||
# The container lifecycle [`__enter__` function](https://modal.com/docs/guide/lifecycle-functions#container-lifecycle-beta) | ||
# loads the model at startup. Then, we evaluate it in the `inference` function. | ||
# | ||
# To avoid excessive cold-starts, we set the idle timeout to 240 seconds, meaning once a GPU has loaded the model it will stay | ||
# online for 4 minutes before spinning down. This can be adjusted for cost/experience trade-offs. | ||
|
||
|
||
@stub.cls(gpu=gpu.A10G(), container_idle_timeout=240) | ||
class Model: | ||
|
@@ -48,11 +83,17 @@ def inference(self, image_bytes, prompt): | |
init_image = load_image(Image.open(BytesIO(image_bytes))).resize( | ||
(512, 512) | ||
) | ||
num_inference_steps = 4 | ||
strength = 0.9 | ||
# "When using SDXL-Turbo for image-to-image generation, make sure that num_inference_steps * strength is larger or equal to 1" | ||
# See: https://huggingface.co/stabilityai/sdxl-turbo | ||
assert num_inference_steps * strength >= 1 | ||
|
||
image = self.pipe( | ||
prompt, | ||
image=init_image, | ||
num_inference_steps=4, | ||
strength=0.9, | ||
num_inference_steps=num_inference_steps, | ||
strength=strength, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be useful to add something at the bottom for how to run this. I.e. what command to type |
||
guidance_scale=0.0, | ||
).images[0] | ||
|
||
|
@@ -80,3 +121,11 @@ def main( | |
print(f"Saving it to {output_path}") | ||
with open(output_path, "wb") as f: | ||
f.write(output_image_bytes) | ||
|
||
|
||
# ## Running the model | ||
# | ||
# We can run the model with different parameters using the following command, | ||
# ``` | ||
# modal run stable_diffusion_xl_turbo.py --prompt="harry potter, glasses, wizard" --image-path="dog.png" | ||
# ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thanks for annotating these!