forked from microsoft/Megatron-DeepSpeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DeepSpeed Stable Diffusion Example (microsoft#233)
This PR adds a DeepSpeed Stable Diffusion example using the prompthero/midjourney-v4-diffusion model.
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
# DeepSpeed Stable Diffusion Example | ||
|
||
# Setup | ||
Python dependencies: | ||
<pre> | ||
pip install -r requirements.txt | ||
</pre> | ||
|
||
# Usage | ||
Examples can be run as follows: | ||
<pre>deepspeed --num_gpus [number of GPUs] test-[model].py</pre> | ||
|
||
# Example Output | ||
Command: | ||
<pre> | ||
deepspeed --num_gpus 1 test-stable-diffusion.py | ||
</pre> | ||
|
||
Output: | ||
<pre> | ||
./baseline.png | ||
./deepspeed.png | ||
</pre> |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
deepspeed | ||
torch | ||
diffusers |
32 changes: 32 additions & 0 deletions
32
inference/huggingface/stable-diffusion/test-stable-diffusion.py
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import deepspeed | ||
import torch | ||
import os | ||
|
||
from diffusers import DiffusionPipeline | ||
|
||
prompt = "a dog on a rocket" | ||
|
||
model = "prompthero/midjourney-v4-diffusion" | ||
local_rank = int(os.getenv("LOCAL_RANK", "0")) | ||
device = torch.device(f"cuda:{local_rank}") | ||
world_size = int(os.getenv('WORLD_SIZE', '4')) | ||
generator = torch.Generator(device=torch.cuda.current_device()) | ||
|
||
pipe = DiffusionPipeline.from_pretrained(model, torch_dtype=torch.half) | ||
pipe = pipe.to(device) | ||
|
||
generator.manual_seed(0xABEDABE7) | ||
baseline_image = pipe(prompt, guidance_scale=7.5, generator=generator).images[0] | ||
baseline_image.save(f"baseline.png") | ||
|
||
# NOTE: DeepSpeed inference supports local CUDA graphs for replaced SD modules | ||
pipe = deepspeed.init_inference( | ||
pipe, | ||
dtype=torch.half, | ||
replace_with_kernel_inject=True, | ||
enable_cuda_graph=True, | ||
) | ||
|
||
generator.manual_seed(0xABEDABE7) | ||
deepspeed_image = pipe(prompt, guidance_scale=7.5, generator=generator).images[0] | ||
deepspeed_image.save(f"deepspeed.png") |