faster (testing)
Browse filesmade faster by
1. Model Loading and Device Placement: The model (pipe) is now loaded only once outside the generate function. This saves significant time on each generation.
2. Resolution Binning: Enabled by default in the options, this technique speeds up generation and reduces VRAM usage, especially for larger images.
3. Torch Compile (Experimental): Added the option to use torch.compile which might further improve performance on compatible hardware. This is highly dependent on your setup.
4. CPU Offloading (Experimental): Allows offloading parts of the model to CPU RAM, potentially enabling larger image generation or batch sizes if your GPU VRAM is limited.
5. Batch Generation: Added the ability to generate multiple images in a single pass (controlled by BATCH_SIZE environment variable or defaulting to 1). This significantly reduces overhead when generating more than one image.
9 unmodified lines10111213141516171314151617181920212223242526272829303132333435363738242526272829303132333940414216 unmodified lines5960615662636465596661676869706465666768697071727371727374757677787975768081828384858687888990919293949596819798991001018710210310410510692107108109110111112113114991001151161171184 unmodified lines12312412511112612712812913013113213313413513613748 unmodified lines18618718816718919019120 unmodified lines2122132142152162172189 unmodified linesimport torchfrom diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
if not torch.cuda.is_available(): DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
MAX_SEED = np.iinfo(np.int32).maxCACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"# Use environment variables for flexibilityMODEL_ID = os.getenv("MODEL_ID", "sd-community/sdxl-flash")MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1")) # Allow generating multiple images at once
# Determine device and load model outside of function for efficiencydevice = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")pipe = StableDiffusionXLPipeline.from_pretrained( MODEL_ID, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, use_safetensors=True, add_watermarker=False, variant="fp16" if torch.cuda.is_available() else None,).to(device)pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
# Torch compile for potential speedup (experimental)if USE_TORCH_COMPILE: pipe.compile()
# CPU offloading for larger RAM capacity (experimental)if ENABLE_CPU_OFFLOAD: pipe.enable_model_cpu_offload()
if torch.cuda.is_available(): pipe = StableDiffusionXLPipeline.from_pretrained( "sd-community/sdxl-flash", torch_dtype=torch.float16, use_safetensors=True, add_watermarker=False ) pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config) pipe.to("cuda")
MAX_SEED = np.iinfo(np.int32).max
def save_image(img): unique_name = str(uuid.uuid4()) + ".png"16 unmodified lines guidance_scale: float = 3, num_inference_steps: int = 30, randomize_seed: bool = False, use_resolution_binning: bool = True, use_resolution_binning: bool = True, num_images: int = 1, # Number of images to generate progress=gr.Progress(track_tqdm=True),): pipe.to(device) seed = int(randomize_seed_fn(seed, randomize_seed)) generator = torch.Generator().manual_seed(seed) generator = torch.Generator(device=device).manual_seed(seed)
# Improved options handling options = { "prompt":prompt, "negative_prompt":negative_prompt, "width":width, "height":height, "guidance_scale":guidance_scale, "num_inference_steps":num_inference_steps, "generator":generator, "use_resolution_binning":use_resolution_binning, "output_type":"pil",
"prompt": [prompt] * num_images, "negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None, "width": width, "height": height, "guidance_scale": guidance_scale, "num_inference_steps": num_inference_steps, "generator": generator, "output_type": "pil", } images = pipe(**options).images
# Use resolution binning for faster generation with less VRAM usage if use_resolution_binning: options["use_resolution_binning"] = True
# Generate images potentially in batches images = [] for i in range(0, num_images, BATCH_SIZE): batch_options = options.copy() batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE] if "negative_prompt" in batch_options: batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE] images.extend(pipe(**batch_options).images)
image_paths = [save_image(img) for img in images] return image_paths, seed
examples = [ "a cat eating a piece of cheese", "a ROBOT riding a BLUE horse on Mars, photorealistic, 4k", "Ironman VS Hulk, ultrarealistic", "Astronaut in a jungle, cold color palette, oil pastel, detailed, 8k", "An alien holding sign board contain word 'Flash', futuristic, neonpunk", "An alien holding a sign board containing the word 'Flash', futuristic, neonpunk", "Kids going to school, Anime style"]
css = '''.gradio-container{max-width: 560px !important}.gradio-container{max-width: 700px !important}h1{text-align:center}footer { visibility: hidden}'''
with gr.Blocks(css=css) as demo: gr.Markdown("""# SDXL Flash ### First Image processing takes time then images generate faster.""") gr.Markdown("""# SDXL Flash""") with gr.Group(): with gr.Row(): prompt = gr.Text(4 unmodified lines container=False, ) run_button = gr.Button("Run", scale=0) result = gr.Gallery(label="Result", columns=1) result = gr.Gallery(label="Result", columns=2, show_label=False) with gr.Accordion("Advanced options", open=False): num_images = gr.Slider( label="Number of Images", minimum=1, maximum=4, step=1, value=1, ) with gr.Row(): use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True) negative_prompt = gr.Text(48 unmodified lines inputs=prompt, outputs=[result, seed], fn=generate, cache_examples=CACHE_EXAMPLES, )
use_negative_prompt.change(20 unmodified lines guidance_scale, num_inference_steps, randomize_seed, num_images ], outputs=[result, seed], api_name="run",