Skip to content
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 stable diffusion from huggingface #254

Merged
merged 39 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
73931f1
first commit
MarcelWilnicki Jun 7, 2024
4f5dd6e
wip
MarcelWilnicki Jun 10, 2024
22fab55
wip
MarcelWilnicki Jun 10, 2024
ee2779a
wip
MarcelWilnicki Jun 10, 2024
516be16
wip
MarcelWilnicki Jun 10, 2024
d4d63f1
wip
MarcelWilnicki Jun 12, 2024
a448e6e
wip
MarcelWilnicki Jun 13, 2024
0f5e469
wip
MarcelWilnicki Jun 13, 2024
35944e6
wip
MarcelWilnicki Jun 13, 2024
b4dcce5
wip
MarcelWilnicki Jun 13, 2024
c67203e
wip
MarcelWilnicki Jun 13, 2024
32fe752
wip
MarcelWilnicki Jun 17, 2024
83197c2
wip
MarcelWilnicki Jun 17, 2024
60bdb2d
add sapphire rapid version
MarcelWilnicki Jun 20, 2024
bc1d48f
wip
MarcelWilnicki Jun 25, 2024
c24959f
wip
MarcelWilnicki Jun 25, 2024
cf2e2ce
wip
MarcelWilnicki Jun 25, 2024
d6953cf
wip
MarcelWilnicki Jun 25, 2024
731c565
wip
MarcelWilnicki Jun 25, 2024
d261f4b
wip
MarcelWilnicki Jun 25, 2024
62ae9ba
wip
MarcelWilnicki Jun 25, 2024
46cad0c
wip
MarcelWilnicki Jun 25, 2024
a7403da
wip
MarcelWilnicki Jun 25, 2024
625d2f4
wip
MarcelWilnicki Jun 25, 2024
de4edef
wip
MarcelWilnicki Jun 25, 2024
778594a
wip
MarcelWilnicki Jun 25, 2024
45e6f85
wip
MarcelWilnicki Jun 25, 2024
ee86fa2
rename files
MarcelWilnicki Jun 25, 2024
2bab8a7
wipC
MarcelWilnicki Jun 26, 2024
273b7dc
wip
MarcelWilnicki Jun 26, 2024
9038ce6
wip
MarcelWilnicki Jun 26, 2024
061dcf5
wip
MarcelWilnicki Jul 1, 2024
02b1167
wip
MarcelWilnicki Jul 1, 2024
629a088
wip
MarcelWilnicki Jul 1, 2024
135f6d9
wip
MarcelWilnicki Aug 28, 2024
382c1c5
wip
MarcelWilnicki Aug 28, 2024
2d49f62
wip
MarcelWilnicki Aug 28, 2024
4ec2c31
Merge branch 'main' into marcel/stable-diffusionxlbase
MarcelWilnicki Aug 28, 2024
95ef5dd
wip
MarcelWilnicki Aug 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions text_to_image/stable_diffusion/run_hf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2024, Ampere Computing LLC
try:
from utils import misc # noqa
except ModuleNotFoundError:
import os
import sys
filename = "set_env_variables.sh"
directory = os.path.realpath(__file__).split("/")[:-1]
for idx in range(1, len(directory) - 1):
subdir = "/".join(directory[:-idx])
if filename in os.listdir(subdir):
print(f"\nPlease run \033[91m'source {os.path.join(subdir, filename)}'\033[0m first.")
break
else:
print(f"\n\033[91mFAIL: Couldn't find {filename}, are you running this script as part of Ampere Model Library?"
f"\033[0m")
sys.exit(1)


def run_pytorch_fp32(model_name, steps, batch_size, num_runs, timeout, **kwargs):
import os
import torch._dynamo
from diffusers import DiffusionPipeline
torch._dynamo.config.suppress_errors = True

from utils.benchmark import run_model
from utils.pytorch import apply_compile
from utils.pytorch import PyTorchRunnerV2
from utils.text_to_image.stable_diffusion import StableDiffusion

if os.environ.get("ENABLE_BF16_X86") == "1":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ENABLE_BF16_X86 is currently used to enable implicit bf16 in AML. Here you are explicitly loading model in bfloat16 - create run_pytorch_bf16 function instead.

model = DiffusionPipeline.from_pretrained(model_name,
use_safetensors=True,
torch_dtype=torch.bfloat16).to("cpu")
else:
model = DiffusionPipeline.from_pretrained(model_name,
use_safetensors=True).to("cpu")

model.unet = apply_compile(model.unet)

def single_pass_pytorch(_runner, _stablediffusion):
prompts = [_stablediffusion.get_input() for _ in range(batch_size)]
x_samples = _runner.run(batch_size * steps, prompt=prompts, num_inference_steps=steps)
_stablediffusion.submit_count(batch_size, x_samples)

runner = PyTorchRunnerV2(model)
stablediffusion = StableDiffusion()
return run_model(single_pass_pytorch, runner, stablediffusion, batch_size, num_runs, timeout)


if __name__ == "__main__":
from utils.helpers import DefaultArgParser

stablediffusion_variants = ["stabilityai/stable-diffusion-xl-base-1.0"]
parser = DefaultArgParser(["pytorch"])
parser.require_model_name(stablediffusion_variants)
parser.ask_for_batch_size()
parser.add_argument("--steps", type=int, default=25, help="steps through which the model processes the input")

run_pytorch_fp32(**vars(parser.parse()))
3 changes: 1 addition & 2 deletions utils/text_to_image/stable_diffusion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2024, Ampere Computing LLC
from random import randint, seed
from random import randint
from utils.helpers import Dataset


Expand All @@ -14,7 +14,6 @@ def get_input(self):
adjectives = ["big", "small", "thin", "wide", "blonde", "pale"]
nouns = ["dog", "cat", "horse", "astronaut", "human", "robot"]
actions = ["sings", "rides a triceratop", "rides a horse", "eats a burger", "washes clothes", "looks at hands"]
seed(42)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove seed? It's good to have deterministic behavior and repeatable results.


a = adjectives[randint(0, len(adjectives) - 1)] + " "
b = nouns[randint(0, len(nouns) - 1)] + " " + actions[randint(0, len(actions) - 1)]
Expand Down