From 6c8038f4a082bdc7884e39e3d70ef34700f9be5a Mon Sep 17 00:00:00 2001 From: Marcel Wilnicki Date: Mon, 2 Sep 2024 11:59:21 +0200 Subject: [PATCH 1/8] first commit --- text_to_image/stable_diffusion/run_hf.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/text_to_image/stable_diffusion/run_hf.py b/text_to_image/stable_diffusion/run_hf.py index be51f051..51f3b804 100644 --- a/text_to_image/stable_diffusion/run_hf.py +++ b/text_to_image/stable_diffusion/run_hf.py @@ -19,6 +19,7 @@ def run_pytorch_bf16(model_name, steps, batch_size, num_runs, timeout, **kwargs): + import torch as th import torch._dynamo from diffusers import DiffusionPipeline torch._dynamo.config.suppress_errors = True @@ -28,17 +29,19 @@ def run_pytorch_bf16(model_name, steps, batch_size, num_runs, timeout, **kwargs) from utils.pytorch import PyTorchRunnerV2 from utils.text_to_image.stable_diffusion import StableDiffusion + print(th.get_num_threads()) model = DiffusionPipeline.from_pretrained(model_name, use_safetensors=True, torch_dtype=torch.bfloat16).to("cpu") - + print(th.get_num_threads()) model.unet = apply_compile(model.unet) - + print(th.get_num_threads()) 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) + print(th.get_num_threads()) runner = PyTorchRunnerV2(model) stablediffusion = StableDiffusion() return run_model(single_pass_pytorch, runner, stablediffusion, batch_size, num_runs, timeout) From 37e5ce5bd67e80885669dbfe3649a61a7a473d74 Mon Sep 17 00:00:00 2001 From: Marcel Wilnicki Date: Mon, 2 Sep 2024 14:08:47 +0200 Subject: [PATCH 2/8] wip --- text_to_image/stable_diffusion/run_hf.py | 37 ++++++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/text_to_image/stable_diffusion/run_hf.py b/text_to_image/stable_diffusion/run_hf.py index 51f3b804..a0c07051 100644 --- a/text_to_image/stable_diffusion/run_hf.py +++ b/text_to_image/stable_diffusion/run_hf.py @@ -19,7 +19,6 @@ def run_pytorch_bf16(model_name, steps, batch_size, num_runs, timeout, **kwargs): - import torch as th import torch._dynamo from diffusers import DiffusionPipeline torch._dynamo.config.suppress_errors = True @@ -29,19 +28,44 @@ def run_pytorch_bf16(model_name, steps, batch_size, num_runs, timeout, **kwargs) from utils.pytorch import PyTorchRunnerV2 from utils.text_to_image.stable_diffusion import StableDiffusion - print(th.get_num_threads()) model = DiffusionPipeline.from_pretrained(model_name, use_safetensors=True, torch_dtype=torch.bfloat16).to("cpu") - print(th.get_num_threads()) + + 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) + + +def run_pytorch_fp16(model_name, steps, batch_size, num_runs, timeout, **kwargs): + 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 + + model = DiffusionPipeline.from_pretrained(model_name, + use_safetensors=True, + torch_dtype=torch.float16, + variant="fp16").to("cpu") + model.unet = apply_compile(model.unet) - print(th.get_num_threads()) + 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) - print(th.get_num_threads()) runner = PyTorchRunnerV2(model) stablediffusion = StableDiffusion() return run_model(single_pass_pytorch, runner, stablediffusion, batch_size, num_runs, timeout) @@ -56,4 +80,5 @@ def single_pass_pytorch(_runner, _stablediffusion): parser.ask_for_batch_size() parser.add_argument("--steps", type=int, default=25, help="steps through which the model processes the input") - run_pytorch_bf16(**vars(parser.parse())) + #run_pytorch_bf16(**vars(parser.parse())) + run_pytorch_fp16(**vars(parser.parse())) From f272fe54b4bd4dd7acc5994df788f1df1b287ecd Mon Sep 17 00:00:00 2001 From: Marcel Wilnicki Date: Tue, 3 Sep 2024 11:27:40 +0200 Subject: [PATCH 3/8] wip --- text_to_image/stable_diffusion/run_hf.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/text_to_image/stable_diffusion/run_hf.py b/text_to_image/stable_diffusion/run_hf.py index a0c07051..f152aad2 100644 --- a/text_to_image/stable_diffusion/run_hf.py +++ b/text_to_image/stable_diffusion/run_hf.py @@ -44,7 +44,7 @@ def single_pass_pytorch(_runner, _stablediffusion): return run_model(single_pass_pytorch, runner, stablediffusion, batch_size, num_runs, timeout) -def run_pytorch_fp16(model_name, steps, batch_size, num_runs, timeout, **kwargs): +def run_pytorch_fp32(model_name, steps, batch_size, num_runs, timeout, **kwargs): import torch._dynamo from diffusers import DiffusionPipeline torch._dynamo.config.suppress_errors = True @@ -55,9 +55,7 @@ def run_pytorch_fp16(model_name, steps, batch_size, num_runs, timeout, **kwargs) from utils.text_to_image.stable_diffusion import StableDiffusion model = DiffusionPipeline.from_pretrained(model_name, - use_safetensors=True, - torch_dtype=torch.float16, - variant="fp16").to("cpu") + use_safetensors=True).to("cpu") model.unet = apply_compile(model.unet) @@ -81,4 +79,4 @@ def single_pass_pytorch(_runner, _stablediffusion): parser.add_argument("--steps", type=int, default=25, help="steps through which the model processes the input") #run_pytorch_bf16(**vars(parser.parse())) - run_pytorch_fp16(**vars(parser.parse())) + run_pytorch_fp32(**vars(parser.parse())) From b68f74e915194e6946360f62f5eb530c41293948 Mon Sep 17 00:00:00 2001 From: Marcel Wilnicki Date: Tue, 3 Sep 2024 13:59:08 +0200 Subject: [PATCH 4/8] wip --- text_to_image/stable_diffusion/run_hf.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/text_to_image/stable_diffusion/run_hf.py b/text_to_image/stable_diffusion/run_hf.py index f152aad2..9f5a4719 100644 --- a/text_to_image/stable_diffusion/run_hf.py +++ b/text_to_image/stable_diffusion/run_hf.py @@ -77,6 +77,10 @@ def single_pass_pytorch(_runner, _stablediffusion): 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") + parser.add_argument("--precision", type=str, choices=["fp32", "int8"], required=True, + help="precision in which to run the model") - #run_pytorch_bf16(**vars(parser.parse())) + print(parser.parse()) + quit() + run_pytorch_bf16(**vars(parser.parse())) run_pytorch_fp32(**vars(parser.parse())) From 2ae42150f46238242b11191a5a79f0372b2d0549 Mon Sep 17 00:00:00 2001 From: Marcel Wilnicki Date: Tue, 3 Sep 2024 14:28:53 +0200 Subject: [PATCH 5/8] wip --- text_to_image/stable_diffusion/run_hf.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/text_to_image/stable_diffusion/run_hf.py b/text_to_image/stable_diffusion/run_hf.py index 9f5a4719..0a9ef03c 100644 --- a/text_to_image/stable_diffusion/run_hf.py +++ b/text_to_image/stable_diffusion/run_hf.py @@ -71,16 +71,22 @@ def single_pass_pytorch(_runner, _stablediffusion): if __name__ == "__main__": from utils.helpers import DefaultArgParser + from utils.misc import print_goodbye_message_and_die 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") - parser.add_argument("--precision", type=str, choices=["fp32", "int8"], required=True, + parser.add_argument("-p", "--precision", type=str, choices=["fp32", "bf16"], required=True, help="precision in which to run the model") - print(parser.parse()) - quit() - run_pytorch_bf16(**vars(parser.parse())) - run_pytorch_fp32(**vars(parser.parse())) + args = parser.parse() + if args.precision == "fp32": + run_pytorch_fp32(**vars(parser.parse())) + elif args.precision == "bf16": + run_pytorch_bf16(**vars(parser.parse())) + else: + print_goodbye_message_and_die( + "this model seems to be unsupported in a specified framework: " + args.framework) + From e82a99f763c328acb8f9224997bd3c6f9e22bb30 Mon Sep 17 00:00:00 2001 From: Marcel Wilnicki Date: Tue, 3 Sep 2024 14:31:30 +0200 Subject: [PATCH 6/8] update requirements --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index d6d51859..d904ae43 100644 --- a/requirements.txt +++ b/requirements.txt @@ -32,3 +32,5 @@ invisible-watermark torchmetrics<1.0.0 kornia open-clip-torch<2.26.1 +diffusers +accelerate From ec01d475ab2f847b5dbb886c935d9b384f34707c Mon Sep 17 00:00:00 2001 From: Marcel Wilnicki Date: Tue, 3 Sep 2024 14:42:09 +0200 Subject: [PATCH 7/8] wip --- text_to_image/stable_diffusion/run_hf.py | 1 - 1 file changed, 1 deletion(-) diff --git a/text_to_image/stable_diffusion/run_hf.py b/text_to_image/stable_diffusion/run_hf.py index 0a9ef03c..abb7a0f8 100644 --- a/text_to_image/stable_diffusion/run_hf.py +++ b/text_to_image/stable_diffusion/run_hf.py @@ -89,4 +89,3 @@ def single_pass_pytorch(_runner, _stablediffusion): else: print_goodbye_message_and_die( "this model seems to be unsupported in a specified framework: " + args.framework) - From 615fab12100381fb3dbef251d770d34c5aa3a886 Mon Sep 17 00:00:00 2001 From: Marcel Wilnicki Date: Wed, 4 Sep 2024 14:16:28 +0200 Subject: [PATCH 8/8] wip --- text_to_image/stable_diffusion/run_hf.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/text_to_image/stable_diffusion/run_hf.py b/text_to_image/stable_diffusion/run_hf.py index abb7a0f8..91a62091 100644 --- a/text_to_image/stable_diffusion/run_hf.py +++ b/text_to_image/stable_diffusion/run_hf.py @@ -40,8 +40,8 @@ def single_pass_pytorch(_runner, _stablediffusion): _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) + stable_diffusion_dataset = StableDiffusion() + return run_model(single_pass_pytorch, runner, stable_diffusion_dataset, batch_size, num_runs, timeout) def run_pytorch_fp32(model_name, steps, batch_size, num_runs, timeout, **kwargs): @@ -65,8 +65,8 @@ def single_pass_pytorch(_runner, _stablediffusion): _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) + stable_diffusion_dataset = StableDiffusion() + return run_model(single_pass_pytorch, runner, stable_diffusion_dataset, batch_size, num_runs, timeout) if __name__ == "__main__": @@ -88,4 +88,4 @@ def single_pass_pytorch(_runner, _stablediffusion): run_pytorch_bf16(**vars(parser.parse())) else: print_goodbye_message_and_die( - "this model seems to be unsupported in a specified framework: " + args.framework) + "this model seems to be unsupported in a specified precision: " + args.precision)