-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_comfy.py
103 lines (90 loc) · 2.77 KB
/
benchmark_comfy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from __future__ import annotations
import fal
from fal.toolkit import clone_repository, download_file
from benchmarks.settings import BenchmarkResults, BenchmarkSettings, InputParameters
@fal.function(
requirements=[
"torch==2.1.0",
"torchsde",
"einops",
"transformers>=4.25.1",
"safetensors>=0.3.0",
"aiohttp",
"accelerate",
"pyyaml",
"Pillow",
"scipy",
"tqdm",
"psutil",
"torchvision",
"huggingface_hub",
"accelerate==0.24.1",
"xformers==0.0.22.post7",
],
machine_type="GPU",
)
def comfy_sdxl(
benchmark_settings: BenchmarkSettings,
parameters: InputParameters,
) -> BenchmarkResults:
import sys
comfy_repo = clone_repository(
"https://github.com/comfyanonymous/ComfyUI.git",
commit_hash="2a23ba0b8c225b59902423ef08db0de39d2ed7e7",
)
sys.path.insert(0, str(comfy_repo))
download_file(
"https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors?download=true",
comfy_repo / "models" / "checkpoints",
file_name="sd_xl_base_1.0.safetensors",
)
import numpy as np
import torch
from nodes import (
CheckpointLoaderSimple,
CLIPTextEncode,
EmptyLatentImage,
KSampler,
VAEDecode,
)
from PIL import Image
checkpoint_loader_simple = CheckpointLoaderSimple()
empty_latent_image = EmptyLatentImage()
clip_text_encode = CLIPTextEncode()
k_sampler = KSampler()
vae_decode = VAEDecode()
(
model,
clip,
vae,
) = checkpoint_loader_simple.load_checkpoint(ckpt_name="sd_xl_base_1.0.safetensors")
@torch.inference_mode
def inference_func():
(latent,) = empty_latent_image.generate(width=1024, height=1024, batch_size=1)
(conditioning,) = clip_text_encode.encode(text="", clip=clip)
(conditioning_2,) = clip_text_encode.encode(text=parameters.prompt, clip=clip)
(latent_2,) = k_sampler.sample(
seed=0,
steps=parameters.steps,
cfg=7.5,
sampler_name="euler",
scheduler="normal",
denoise=1,
model=model,
positive=conditioning_2,
negative=conditioning,
latent_image=latent,
)
(images,) = vae_decode.decode(samples=latent_2, vae=vae)
for image in images:
i = 255.0 * image.cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
return img
return benchmark_settings.apply(inference_func)
LOCAL_BENCHMARKS = [
{
"name": "Comfy (torch 2.1, xformers)",
"category": "SDXL (End-to-end)",
"function": comfy_sdxl,
},
]