|
| 1 | +# Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Control harness for the ChebBooster (Chebyshev cache) PR. |
| 16 | +
|
| 17 | +Three-arm comparison on FLUX.1-dev with identical seeds/prompts and matched |
| 18 | +`cache_interval` + warmup: |
| 19 | +
|
| 20 | + (a) no-cache reference |
| 21 | + (b) in-repo TaylorSeer cache (the control arm) |
| 22 | + (c) Chebyshev cache (ChebBooster port) |
| 23 | +
|
| 24 | +Reports wall-clock latency per arm and quality of (b) and (c) against the |
| 25 | +no-cache reference (PSNR always; LPIPS and CLIP score when `lpips` / |
| 26 | +`open_clip_torch` are installed). The claim this harness is meant to check: |
| 27 | +at matched latency, Chebyshev cache >= TaylorSeer cache on quality. If the |
| 28 | +delta is within noise, the cache variant is redundant and the PR should not |
| 29 | +be opened upstream. |
| 30 | +
|
| 31 | +Usage: |
| 32 | + python benchmarking_chebyshev_cache.py --num-prompts 8 --num-inference-steps 28 |
| 33 | +""" |
| 34 | + |
| 35 | +import argparse |
| 36 | +import csv |
| 37 | +import time |
| 38 | + |
| 39 | +import torch |
| 40 | + |
| 41 | +from diffusers import ChebyshevCacheConfig, FluxPipeline, TaylorSeerCacheConfig |
| 42 | + |
| 43 | + |
| 44 | +CKPT_ID = "black-forest-labs/FLUX.1-dev" |
| 45 | +RESULT_FILENAME = "chebyshev_cache_control.csv" |
| 46 | + |
| 47 | +# Placeholder prompt set — replace with the DrawBench prompts shipped in the |
| 48 | +# reference repo (ChebBooster-FLUX/DrawBench.jsonl) for the full control run. |
| 49 | +PROMPTS = [ |
| 50 | + "A photograph of an astronaut riding a horse on Mars.", |
| 51 | + "A bowl of fruit on a wooden table, studio lighting.", |
| 52 | + "A cyberpunk city street at night in the rain.", |
| 53 | + "An oil painting of a lighthouse during a storm.", |
| 54 | +] |
| 55 | + |
| 56 | + |
| 57 | +def parse_args(): |
| 58 | + parser = argparse.ArgumentParser() |
| 59 | + parser.add_argument("--num-prompts", type=int, default=len(PROMPTS)) |
| 60 | + parser.add_argument("--num-inference-steps", type=int, default=28) |
| 61 | + parser.add_argument("--cache-interval", type=int, default=5) |
| 62 | + parser.add_argument("--disable-cache-before-step", type=int, default=3) |
| 63 | + parser.add_argument("--cheb-order", type=int, default=6) |
| 64 | + parser.add_argument("--seed", type=int, default=0) |
| 65 | + return parser.parse_args() |
| 66 | + |
| 67 | + |
| 68 | +def load_pipeline(): |
| 69 | + pipe = FluxPipeline.from_pretrained(CKPT_ID, torch_dtype=torch.bfloat16) |
| 70 | + pipe.to("cuda") |
| 71 | + pipe.set_progress_bar_config(disable=True) |
| 72 | + return pipe |
| 73 | + |
| 74 | + |
| 75 | +def run_arm(pipe, prompts, num_inference_steps, seed, cache_config=None): |
| 76 | + if cache_config is not None: |
| 77 | + pipe.transformer.enable_cache(cache_config) |
| 78 | + |
| 79 | + images = [] |
| 80 | + start = time.perf_counter() |
| 81 | + for i, prompt in enumerate(prompts): |
| 82 | + generator = torch.Generator(device="cuda").manual_seed(seed + i) |
| 83 | + image = pipe(prompt, num_inference_steps=num_inference_steps, generator=generator).images[0] |
| 84 | + images.append(image) |
| 85 | + torch.cuda.synchronize() |
| 86 | + latency = (time.perf_counter() - start) / len(prompts) |
| 87 | + |
| 88 | + if cache_config is not None: |
| 89 | + pipe.transformer.disable_cache() |
| 90 | + return images, latency |
| 91 | + |
| 92 | + |
| 93 | +def psnr_vs_reference(images, reference_images): |
| 94 | + values = [] |
| 95 | + for image, reference in zip(images, reference_images): |
| 96 | + x = torch.tensor(list(image.getdata()), dtype=torch.float32) / 255.0 |
| 97 | + y = torch.tensor(list(reference.getdata()), dtype=torch.float32) / 255.0 |
| 98 | + mse = torch.mean((x - y) ** 2).item() |
| 99 | + values.append(float("inf") if mse == 0 else 10.0 * torch.log10(torch.tensor(1.0 / mse)).item()) |
| 100 | + return sum(values) / len(values) |
| 101 | + |
| 102 | + |
| 103 | +def lpips_vs_reference(images, reference_images): |
| 104 | + try: |
| 105 | + import lpips |
| 106 | + import numpy as np |
| 107 | + except ImportError: |
| 108 | + return None |
| 109 | + |
| 110 | + loss_fn = lpips.LPIPS(net="vgg").to("cuda") |
| 111 | + values = [] |
| 112 | + for image, reference in zip(images, reference_images): |
| 113 | + x = torch.from_numpy(np.array(image)).permute(2, 0, 1)[None].float().to("cuda") / 127.5 - 1.0 |
| 114 | + y = torch.from_numpy(np.array(reference)).permute(2, 0, 1)[None].float().to("cuda") / 127.5 - 1.0 |
| 115 | + with torch.no_grad(): |
| 116 | + values.append(loss_fn(x, y).item()) |
| 117 | + return sum(values) / len(values) |
| 118 | + |
| 119 | + |
| 120 | +def main(): |
| 121 | + args = parse_args() |
| 122 | + prompts = PROMPTS[: args.num_prompts] |
| 123 | + shared_kwargs = { |
| 124 | + "cache_interval": args.cache_interval, |
| 125 | + "disable_cache_before_step": args.disable_cache_before_step, |
| 126 | + } |
| 127 | + |
| 128 | + pipe = load_pipeline() |
| 129 | + |
| 130 | + reference_images, reference_latency = run_arm(pipe, prompts, args.num_inference_steps, args.seed) |
| 131 | + |
| 132 | + taylorseer_images, taylorseer_latency = run_arm( |
| 133 | + pipe, |
| 134 | + prompts, |
| 135 | + args.num_inference_steps, |
| 136 | + args.seed, |
| 137 | + cache_config=TaylorSeerCacheConfig(max_order=1, **shared_kwargs), |
| 138 | + ) |
| 139 | + |
| 140 | + chebyshev_images, chebyshev_latency = run_arm( |
| 141 | + pipe, |
| 142 | + prompts, |
| 143 | + args.num_inference_steps, |
| 144 | + args.seed, |
| 145 | + cache_config=ChebyshevCacheConfig(cheb_order=args.cheb_order, **shared_kwargs), |
| 146 | + ) |
| 147 | + |
| 148 | + rows = [ |
| 149 | + { |
| 150 | + "arm": "no-cache", |
| 151 | + "latency_s_per_image": reference_latency, |
| 152 | + "speedup": 1.0, |
| 153 | + "psnr_vs_reference": float("inf"), |
| 154 | + "lpips_vs_reference": 0.0, |
| 155 | + }, |
| 156 | + { |
| 157 | + "arm": "taylorseer", |
| 158 | + "latency_s_per_image": taylorseer_latency, |
| 159 | + "speedup": reference_latency / taylorseer_latency, |
| 160 | + "psnr_vs_reference": psnr_vs_reference(taylorseer_images, reference_images), |
| 161 | + "lpips_vs_reference": lpips_vs_reference(taylorseer_images, reference_images), |
| 162 | + }, |
| 163 | + { |
| 164 | + "arm": "chebyshev", |
| 165 | + "latency_s_per_image": chebyshev_latency, |
| 166 | + "speedup": reference_latency / chebyshev_latency, |
| 167 | + "psnr_vs_reference": psnr_vs_reference(chebyshev_images, reference_images), |
| 168 | + "lpips_vs_reference": lpips_vs_reference(chebyshev_images, reference_images), |
| 169 | + }, |
| 170 | + ] |
| 171 | + |
| 172 | + with open(RESULT_FILENAME, "w", newline="") as f: |
| 173 | + writer = csv.DictWriter(f, fieldnames=list(rows[0].keys())) |
| 174 | + writer.writeheader() |
| 175 | + writer.writerows(rows) |
| 176 | + |
| 177 | + for row in rows: |
| 178 | + print(row) |
| 179 | + print(f"\nResults written to {RESULT_FILENAME}") |
| 180 | + |
| 181 | + |
| 182 | +if __name__ == "__main__": |
| 183 | + main() |
0 commit comments