Last active
July 29, 2023 20:10
-
-
Save erikbern/03a056b79e18692eb9b848f84eefc57b to your computer and use it in GitHub Desktop.
Run Stable Diffusion 2.0 on Modal
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
import io | |
import sys | |
import modal | |
stub = modal.Stub( | |
image=modal.Image.debian_slim() | |
.apt_install(["git"]) | |
.pip_install( | |
[ | |
"git+https://github.com/huggingface/diffusers.git", | |
"transformers", | |
"accelerate", | |
"scipy", | |
] | |
) | |
) | |
@stub.function(gpu=True) | |
def run(prompt): | |
import torch | |
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler | |
model_id = "stabilityai/stable-diffusion-2" | |
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler") | |
pipe = StableDiffusionPipeline.from_pretrained( | |
model_id, scheduler=scheduler, revision="fp16", torch_dtype=torch.float16 | |
) | |
pipe = pipe.to("cuda") | |
image = pipe(prompt, height=768, width=768).images[0] | |
buf = io.BytesIO() | |
image.save(buf, format="PNG") | |
return buf.getvalue() | |
if __name__ == "__main__": | |
with stub.run(): | |
png_data = run(sys.argv[1]) | |
with open("output.png", "wb") as f: | |
f.write(png_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note – this code is not very optimized. There are a few simple optimizations you can do though:
gpu=modal.gpu.A100()
__enter__
method similar to this example: https://github.com/modal-labs/modal-examples/blob/main/misc/batch_inference_using_huggingface.pyThere are a few other optimizations too. We will publish some more info about those hopefully soon!