-
Notifications
You must be signed in to change notification settings - Fork 103
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
sd2 oneflow compile #244
Merged
Merged
sd2 oneflow compile #244
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d81c136
add env var for graph opt
strint 5fcf5b6
test pass of sdxl graph
strint 2909c92
draft graph run
strint 9b0a373
unet sd1.5 test passed
strint 0eaa2c9
part2
strint 4d95606
Merge branch 'refactor-backend' of https://github.com/Oneflow-Inc/dif…
strint a1eaf26
sdxl unet graph test passed
strint da4f474
oneflow unet replace test passed
strint a3adab9
add unet graph to sdxl test passed
strint 97d69cf
sdxl unet graph multi input shape
strint 570d383
sdxl unet graph save load multi input test passed
strint 39263e5
refine oneflow_compile api
strint b62c21f
refine code
strint 83a19f3
format
strint f666512
deal with special case
strint 7e8c0f8
open GROUP_MATMUL
strint 4c7d6da
add (base and refiner) full pipeline
strint 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
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
92 changes: 92 additions & 0 deletions
92
examples/text_to_image_sdxl_fp16_base_and_refiner_with_oneflow_compile.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,92 @@ | ||
import os | ||
import argparse | ||
# cv2 must be imported before diffusers and oneflow to avlid error: AttributeError: module 'cv2.gapi' has no attribute 'wip' | ||
# Maybe bacause oneflow use a lower version of cv2 | ||
import cv2 | ||
import oneflow as flow | ||
import torch | ||
# oneflow_compile should be imported before importing any diffusers | ||
from onediff.infer_compiler import oneflow_compile | ||
from diffusers import DiffusionPipeline | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--base", type=str, default="/share_nfs/hf_models/stable-diffusion-xl-base-1.0" | ||
) | ||
parser.add_argument( | ||
"--refiner", type=str, default="stabilityai/stable-diffusion-xl-refiner-1.0" | ||
) | ||
parser.add_argument("--variant", type=str, default="fp16") | ||
parser.add_argument( | ||
"--prompt", | ||
type=str, | ||
default="street style, detailed, raw photo, woman, face, shot on CineStill 800T", | ||
) | ||
parser.add_argument("--n_steps", type=int, default=30) | ||
parser.add_argument("--saved_image", type=str, required=False, default="xl-refiner-out.png") | ||
parser.add_argument("--seed", type=int, default=1) | ||
parser.add_argument("--compile", action=argparse.BooleanOptionalAction) | ||
parser.add_argument("--save", action=argparse.BooleanOptionalAction) | ||
parser.add_argument("--load", action=argparse.BooleanOptionalAction) | ||
parser.add_argument("--file", type=str, required=False, default="unet_compiled") | ||
cmd_args = parser.parse_args() | ||
|
||
# Normal SDXL | ||
seed = torch.manual_seed(cmd_args.seed) | ||
# SDXL base: StableDiffusionXLPipeline | ||
base = DiffusionPipeline.from_pretrained( | ||
cmd_args.base, | ||
torch_dtype=torch.float16, | ||
variant=cmd_args.variant, | ||
use_safetensors=True, | ||
) | ||
base.to("cuda") | ||
# SDXL refiner: StableDiffusionXLImg2ImgPipeline | ||
refiner = DiffusionPipeline.from_pretrained( | ||
cmd_args.refiner, | ||
text_encoder_2=base.text_encoder_2, | ||
vae=base.vae, | ||
torch_dtype=torch.float16, | ||
use_safetensors=True, | ||
variant=cmd_args.variant, | ||
) | ||
refiner.to("cuda") | ||
|
||
# Compile unet with oneflow | ||
if cmd_args.compile: | ||
base.unet = oneflow_compile(base.unet) | ||
refiner.unet = oneflow_compile(refiner.unet) | ||
print("unet is compiled to oneflow.") | ||
if cmd_args.load: | ||
# Load compiled unet with oneflow | ||
print("loading graphs...") | ||
base.unet._graph_load("base_" + cmd_args.file) | ||
refiner.unet._graph_load("refiner_" + cmd_args.file) | ||
|
||
# Normal SDXL call | ||
sizes = [1024, 896, 768] | ||
#sizes = [1024] | ||
for h in sizes: | ||
for w in sizes: | ||
for i in range(3): | ||
image = base( | ||
prompt=cmd_args.prompt, | ||
height=h, | ||
width=w, | ||
generator=seed, | ||
num_inference_steps=cmd_args.n_steps, | ||
output_type="latent", | ||
).images | ||
image = refiner( | ||
prompt=cmd_args.prompt, | ||
generator=seed, | ||
num_inference_steps=cmd_args.n_steps, | ||
image=image, | ||
).images[0] | ||
image.save(f"h{h}-w{w}-i{i}-{cmd_args.saved_image}") | ||
|
||
# Save compiled unet with oneflow | ||
if cmd_args.compile and cmd_args.save: | ||
print("saving graphs...") | ||
base.unet._graph_save("base_" + cmd_args.file) | ||
refiner.unet._graph_save("refiner_" + cmd_args.file) |
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,58 @@ | ||
import os | ||
import argparse | ||
# cv2 must be imported before diffusers and oneflow to avlid error: AttributeError: module 'cv2.gapi' has no attribute 'wip' | ||
# Maybe bacause oneflow use a lower version of cv2 | ||
import cv2 | ||
import oneflow as flow | ||
import torch | ||
# oneflow_compile should be imported before importing any diffusers | ||
from onediff.infer_compiler import oneflow_compile | ||
from diffusers import StableDiffusionXLPipeline | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--model", type=str, default="/share_nfs/hf_models/stable-diffusion-xl-base-1.0" | ||
) | ||
parser.add_argument("--variant", type=str, default="fp16") | ||
parser.add_argument( | ||
"--prompt", | ||
type=str, | ||
default="street style, detailed, raw photo, woman, face, shot on CineStill 800T", | ||
) | ||
parser.add_argument("--saved_image", type=str, required=False, default="xl-base-out.png") | ||
parser.add_argument("--seed", type=int, default=1) | ||
parser.add_argument("--compile", action=argparse.BooleanOptionalAction) | ||
parser.add_argument("--save", action=argparse.BooleanOptionalAction) | ||
parser.add_argument("--load", action=argparse.BooleanOptionalAction) | ||
parser.add_argument("--file", type=str, required=False, default="unet_compiled") | ||
cmd_args = parser.parse_args() | ||
|
||
# Normal SDXL | ||
torch.manual_seed(cmd_args.seed) | ||
pipe = StableDiffusionXLPipeline.from_pretrained( | ||
cmd_args.model, torch_dtype=torch.float16, variant=cmd_args.variant, use_safetensors=True | ||
) | ||
pipe.to("cuda") | ||
|
||
# Compile unet with oneflow | ||
if cmd_args.compile: | ||
pipe.unet = oneflow_compile(pipe.unet) | ||
print("unet is compiled to oneflow.") | ||
if cmd_args.load: | ||
# Load compiled unet with oneflow | ||
print("loading graphs...") | ||
pipe.unet._graph_load(cmd_args.file) | ||
|
||
# Normal SDXL call | ||
sizes = [1024, 896, 768] | ||
#sizes = [1024] | ||
for h in sizes: | ||
for w in sizes: | ||
for i in range(3): | ||
image = pipe(prompt=cmd_args.prompt, height=h, width=w, num_inference_steps=30).images[0] | ||
image.save(f"h{h}-w{w}-i{i}-{cmd_args.saved_image}") | ||
|
||
# Save compiled unet with oneflow | ||
if cmd_args.compile and cmd_args.save: | ||
print("saving graphs...") | ||
pipe.unet._graph_save(cmd_args.file) |
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
Oops, something went wrong.
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.
和 torch.compile 体验一致的 oneflow_compile 函数。实现了一键替换 pipe 中的 unet 实例的效果(显存共享的)。且该 unet 支持 save/load/dynamic input。
一键替换 pipe 中的 unet 要解决的两个问题:
这个作为 base(验证性能和正确性) 和保底方案。
后面基于这个去做 torch.compile 路线的 save/load/dynamic input 支持。torch.compile 本身对 save/load/dynamic input 这些特性的支持还太完善: