|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +import torch |
| 5 | + |
| 6 | +from diffusers import ( |
| 7 | + AutoPipelineForImage2Image, |
| 8 | + AutoPipelineForInpainting, |
| 9 | + AutoPipelineForText2Image, |
| 10 | + ControlNetModel, |
| 11 | + LCMScheduler, |
| 12 | + StableDiffusionAdapterPipeline, |
| 13 | + StableDiffusionControlNetPipeline, |
| 14 | + StableDiffusionXLAdapterPipeline, |
| 15 | + StableDiffusionXLControlNetPipeline, |
| 16 | + T2IAdapter, |
| 17 | + WuerstchenCombinedPipeline, |
| 18 | +) |
| 19 | +from diffusers.utils import load_image |
| 20 | + |
| 21 | + |
| 22 | +sys.path.append(".") |
| 23 | + |
| 24 | +from utils import ( # noqa: E402 |
| 25 | + BASE_PATH, |
| 26 | + PROMPT, |
| 27 | + BenchmarkInfo, |
| 28 | + benchmark_fn, |
| 29 | + bytes_to_giga_bytes, |
| 30 | + flush, |
| 31 | + generate_csv_dict, |
| 32 | + write_to_csv, |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +RESOLUTION_MAPPING = { |
| 37 | + "runwayml/stable-diffusion-v1-5": (512, 512), |
| 38 | + "lllyasviel/sd-controlnet-canny": (512, 512), |
| 39 | + "diffusers/controlnet-canny-sdxl-1.0": (1024, 1024), |
| 40 | + "TencentARC/t2iadapter_canny_sd14v1": (512, 512), |
| 41 | + "TencentARC/t2i-adapter-canny-sdxl-1.0": (1024, 1024), |
| 42 | + "stabilityai/stable-diffusion-2-1": (768, 768), |
| 43 | + "stabilityai/stable-diffusion-xl-base-1.0": (1024, 1024), |
| 44 | + "stabilityai/stable-diffusion-xl-refiner-1.0": (1024, 1024), |
| 45 | + "stabilityai/sdxl-turbo": (512, 512), |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +class BaseBenchmak: |
| 50 | + pipeline_class = None |
| 51 | + |
| 52 | + def __init__(self, args): |
| 53 | + super().__init__() |
| 54 | + |
| 55 | + def run_inference(self, args): |
| 56 | + raise NotImplementedError |
| 57 | + |
| 58 | + def benchmark(self, args): |
| 59 | + raise NotImplementedError |
| 60 | + |
| 61 | + def get_result_filepath(self, args): |
| 62 | + pipeline_class_name = str(self.pipe.__class__.__name__) |
| 63 | + name = ( |
| 64 | + args.ckpt.replace("/", "_") |
| 65 | + + "_" |
| 66 | + + pipeline_class_name |
| 67 | + + f"-bs@{args.batch_size}-steps@{args.num_inference_steps}-mco@{args.model_cpu_offload}-compile@{args.run_compile}.csv" |
| 68 | + ) |
| 69 | + filepath = os.path.join(BASE_PATH, name) |
| 70 | + return filepath |
| 71 | + |
| 72 | + |
| 73 | +class TextToImageBenchmark(BaseBenchmak): |
| 74 | + pipeline_class = AutoPipelineForText2Image |
| 75 | + |
| 76 | + def __init__(self, args): |
| 77 | + pipe = self.pipeline_class.from_pretrained(args.ckpt, torch_dtype=torch.float16) |
| 78 | + pipe = pipe.to("cuda") |
| 79 | + |
| 80 | + if args.run_compile: |
| 81 | + if not isinstance(pipe, WuerstchenCombinedPipeline): |
| 82 | + pipe.unet.to(memory_format=torch.channels_last) |
| 83 | + print("Run torch compile") |
| 84 | + pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True) |
| 85 | + |
| 86 | + if hasattr(pipe, "movq") and getattr(pipe, "movq", None) is not None: |
| 87 | + pipe.movq.to(memory_format=torch.channels_last) |
| 88 | + pipe.movq = torch.compile(pipe.movq, mode="reduce-overhead", fullgraph=True) |
| 89 | + else: |
| 90 | + print("Run torch compile") |
| 91 | + pipe.decoder = torch.compile(pipe.decoder, mode="reduce-overhead", fullgraph=True) |
| 92 | + pipe.vqgan = torch.compile(pipe.vqgan, mode="reduce-overhead", fullgraph=True) |
| 93 | + |
| 94 | + pipe.set_progress_bar_config(disable=True) |
| 95 | + self.pipe = pipe |
| 96 | + |
| 97 | + def run_inference(self, pipe, args): |
| 98 | + _ = pipe( |
| 99 | + prompt=PROMPT, |
| 100 | + num_inference_steps=args.num_inference_steps, |
| 101 | + num_images_per_prompt=args.batch_size, |
| 102 | + ) |
| 103 | + |
| 104 | + def benchmark(self, args): |
| 105 | + flush() |
| 106 | + |
| 107 | + print(f"[INFO] {self.pipe.__class__.__name__}: Running benchmark with: {vars(args)}\n") |
| 108 | + |
| 109 | + time = benchmark_fn(self.run_inference, self.pipe, args) # in seconds. |
| 110 | + memory = bytes_to_giga_bytes(torch.cuda.max_memory_allocated()) # in GBs. |
| 111 | + benchmark_info = BenchmarkInfo(time=time, memory=memory) |
| 112 | + |
| 113 | + pipeline_class_name = str(self.pipe.__class__.__name__) |
| 114 | + flush() |
| 115 | + csv_dict = generate_csv_dict( |
| 116 | + pipeline_cls=pipeline_class_name, ckpt=args.ckpt, args=args, benchmark_info=benchmark_info |
| 117 | + ) |
| 118 | + filepath = self.get_result_filepath(args) |
| 119 | + write_to_csv(filepath, csv_dict) |
| 120 | + print(f"Logs written to: {filepath}") |
| 121 | + flush() |
| 122 | + |
| 123 | + |
| 124 | +class TurboTextToImageBenchmark(TextToImageBenchmark): |
| 125 | + def __init__(self, args): |
| 126 | + super().__init__(args) |
| 127 | + |
| 128 | + def run_inference(self, pipe, args): |
| 129 | + _ = pipe( |
| 130 | + prompt=PROMPT, |
| 131 | + num_inference_steps=args.num_inference_steps, |
| 132 | + num_images_per_prompt=args.batch_size, |
| 133 | + guidance_scale=0.0, |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +class LCMLoRATextToImageBenchmark(TextToImageBenchmark): |
| 138 | + lora_id = "latent-consistency/lcm-lora-sdxl" |
| 139 | + |
| 140 | + def __init__(self, args): |
| 141 | + super().__init__(args) |
| 142 | + self.pipe.load_lora_weights(self.lora_id) |
| 143 | + self.pipe.fuse_lora() |
| 144 | + self.pipe.scheduler = LCMScheduler.from_config(self.pipe.scheduler.config) |
| 145 | + |
| 146 | + def get_result_filepath(self, args): |
| 147 | + pipeline_class_name = str(self.pipe.__class__.__name__) |
| 148 | + name = ( |
| 149 | + self.lora_id.replace("/", "_") |
| 150 | + + "_" |
| 151 | + + pipeline_class_name |
| 152 | + + f"-bs@{args.batch_size}-steps@{args.num_inference_steps}-mco@{args.model_cpu_offload}-compile@{args.run_compile}.csv" |
| 153 | + ) |
| 154 | + filepath = os.path.join(BASE_PATH, name) |
| 155 | + return filepath |
| 156 | + |
| 157 | + def run_inference(self, pipe, args): |
| 158 | + _ = pipe( |
| 159 | + prompt=PROMPT, |
| 160 | + num_inference_steps=args.num_inference_steps, |
| 161 | + num_images_per_prompt=args.batch_size, |
| 162 | + guidance_scale=1.0, |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +class ImageToImageBenchmark(TextToImageBenchmark): |
| 167 | + pipeline_class = AutoPipelineForImage2Image |
| 168 | + url = "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/benchmarking/1665_Girl_with_a_Pearl_Earring.jpg" |
| 169 | + image = load_image(url).convert("RGB") |
| 170 | + |
| 171 | + def __init__(self, args): |
| 172 | + super().__init__(args) |
| 173 | + self.image = self.image.resize(RESOLUTION_MAPPING[args.ckpt]) |
| 174 | + |
| 175 | + def run_inference(self, pipe, args): |
| 176 | + _ = pipe( |
| 177 | + prompt=PROMPT, |
| 178 | + image=self.image, |
| 179 | + num_inference_steps=args.num_inference_steps, |
| 180 | + num_images_per_prompt=args.batch_size, |
| 181 | + ) |
| 182 | + |
| 183 | + |
| 184 | +class TurboImageToImageBenchmark(ImageToImageBenchmark): |
| 185 | + def __init__(self, args): |
| 186 | + super().__init__(args) |
| 187 | + |
| 188 | + def run_inference(self, pipe, args): |
| 189 | + _ = pipe( |
| 190 | + prompt=PROMPT, |
| 191 | + image=self.image, |
| 192 | + num_inference_steps=args.num_inference_steps, |
| 193 | + num_images_per_prompt=args.batch_size, |
| 194 | + guidance_scale=0.0, |
| 195 | + strength=0.5, |
| 196 | + ) |
| 197 | + |
| 198 | + |
| 199 | +class InpaintingBenchmark(ImageToImageBenchmark): |
| 200 | + pipeline_class = AutoPipelineForInpainting |
| 201 | + mask_url = "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/benchmarking/overture-creations-5sI6fQgYIuo_mask.png" |
| 202 | + mask = load_image(mask_url).convert("RGB") |
| 203 | + |
| 204 | + def __init__(self, args): |
| 205 | + super().__init__(args) |
| 206 | + self.image = self.image.resize(RESOLUTION_MAPPING[args.ckpt]) |
| 207 | + self.mask = self.mask.resize(RESOLUTION_MAPPING[args.ckpt]) |
| 208 | + |
| 209 | + def run_inference(self, pipe, args): |
| 210 | + _ = pipe( |
| 211 | + prompt=PROMPT, |
| 212 | + image=self.image, |
| 213 | + mask_image=self.mask, |
| 214 | + num_inference_steps=args.num_inference_steps, |
| 215 | + num_images_per_prompt=args.batch_size, |
| 216 | + ) |
| 217 | + |
| 218 | + |
| 219 | +class ControlNetBenchmark(TextToImageBenchmark): |
| 220 | + pipeline_class = StableDiffusionControlNetPipeline |
| 221 | + aux_network_class = ControlNetModel |
| 222 | + root_ckpt = "runwayml/stable-diffusion-v1-5" |
| 223 | + |
| 224 | + url = "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/benchmarking/canny_image_condition.png" |
| 225 | + image = load_image(url).convert("RGB") |
| 226 | + |
| 227 | + def __init__(self, args): |
| 228 | + aux_network = self.aux_network_class.from_pretrained(args.ckpt, torch_dtype=torch.float16) |
| 229 | + pipe = self.pipeline_class.from_pretrained(self.root_ckpt, controlnet=aux_network, torch_dtype=torch.float16) |
| 230 | + pipe = pipe.to("cuda") |
| 231 | + |
| 232 | + pipe.set_progress_bar_config(disable=True) |
| 233 | + self.pipe = pipe |
| 234 | + |
| 235 | + if args.run_compile: |
| 236 | + pipe.unet.to(memory_format=torch.channels_last) |
| 237 | + pipe.controlnet.to(memory_format=torch.channels_last) |
| 238 | + |
| 239 | + print("Run torch compile") |
| 240 | + pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True) |
| 241 | + pipe.controlnet = torch.compile(pipe.controlnet, mode="reduce-overhead", fullgraph=True) |
| 242 | + |
| 243 | + self.image = self.image.resize(RESOLUTION_MAPPING[args.ckpt]) |
| 244 | + |
| 245 | + def run_inference(self, pipe, args): |
| 246 | + _ = pipe( |
| 247 | + prompt=PROMPT, |
| 248 | + image=self.image, |
| 249 | + num_inference_steps=args.num_inference_steps, |
| 250 | + num_images_per_prompt=args.batch_size, |
| 251 | + ) |
| 252 | + |
| 253 | + |
| 254 | +class ControlNetSDXLBenchmark(ControlNetBenchmark): |
| 255 | + pipeline_class = StableDiffusionXLControlNetPipeline |
| 256 | + root_ckpt = "stabilityai/stable-diffusion-xl-base-1.0" |
| 257 | + |
| 258 | + def __init__(self, args): |
| 259 | + super().__init__(args) |
| 260 | + |
| 261 | + |
| 262 | +class T2IAdapterBenchmark(ControlNetBenchmark): |
| 263 | + pipeline_class = StableDiffusionAdapterPipeline |
| 264 | + aux_network_class = T2IAdapter |
| 265 | + root_ckpt = "CompVis/stable-diffusion-v1-4" |
| 266 | + |
| 267 | + url = "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/benchmarking/canny_for_adapter.png" |
| 268 | + image = load_image(url).convert("L") |
| 269 | + |
| 270 | + def __init__(self, args): |
| 271 | + aux_network = self.aux_network_class.from_pretrained(args.ckpt, torch_dtype=torch.float16) |
| 272 | + pipe = self.pipeline_class.from_pretrained(self.root_ckpt, adapter=aux_network, torch_dtype=torch.float16) |
| 273 | + pipe = pipe.to("cuda") |
| 274 | + |
| 275 | + pipe.set_progress_bar_config(disable=True) |
| 276 | + self.pipe = pipe |
| 277 | + |
| 278 | + if args.run_compile: |
| 279 | + pipe.unet.to(memory_format=torch.channels_last) |
| 280 | + pipe.adapter.to(memory_format=torch.channels_last) |
| 281 | + |
| 282 | + print("Run torch compile") |
| 283 | + pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True) |
| 284 | + pipe.adapter = torch.compile(pipe.adapter, mode="reduce-overhead", fullgraph=True) |
| 285 | + |
| 286 | + self.image = self.image.resize(RESOLUTION_MAPPING[args.ckpt]) |
| 287 | + |
| 288 | + |
| 289 | +class T2IAdapterSDXLBenchmark(T2IAdapterBenchmark): |
| 290 | + pipeline_class = StableDiffusionXLAdapterPipeline |
| 291 | + root_ckpt = "stabilityai/stable-diffusion-xl-base-1.0" |
| 292 | + |
| 293 | + url = "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/benchmarking/canny_for_adapter_sdxl.png" |
| 294 | + image = load_image(url) |
| 295 | + |
| 296 | + def __init__(self, args): |
| 297 | + super().__init__(args) |
0 commit comments