| import os |
| from typing import Any, Dict, Union |
| from PIL import Image |
| import torch |
| from diffusers import FluxPipeline |
| from huggingface_inference_toolkit.logging import logger |
| from para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipe |
| from torchao.quantization import autoquant |
| import time |
| import gc |
|
|
| |
| |
| torch.set_float32_matmul_precision("high") |
|
|
| import torch._dynamo |
| torch._dynamo.config.suppress_errors = False |
|
|
| class EndpointHandler: |
| def __init__(self, path=""): |
| self.pipeline = FluxPipeline.from_pretrained( |
| "NoMoreCopyrightOrg/flux-dev", |
| torch_dtype=torch.bfloat16, |
| ).to("cuda") |
| self.pipeline.enable_vae_slicing() |
| self.pipeline.enable_vae_tiling() |
| self.pipeline.transformer.fuse_qkv_projections() |
| self.pipeline.vae.fuse_qkv_projections() |
| self.pipeline.transformer.to(memory_format=torch.channels_last) |
| self.pipeline.vae.to(memory_format=torch.channels_last) |
| apply_cache_on_pipe(self.pipeline, residual_diff_threshold=0.12) |
| self.pipeline.transformer = torch.compile( |
| self.pipeline.transformer, mode="max-autotune-no-cudagraphs", |
| ) |
| self.pipeline.vae = torch.compile( |
| self.pipeline.vae, mode="max-autotune-no-cudagraphs", |
| ) |
| self.pipeline.transformer = autoquant(self.pipeline.transformer, error_on_unseen=False) |
| self.pipeline.vae = autoquant(self.pipeline.vae, error_on_unseen=False) |
| |
| gc.collect() |
| torch.cuda.empty_cache() |
|
|
| start_time = time.time() |
| print("Start warming-up pipeline") |
| self.pipeline("Hello world!") |
| end_time = time.time() |
| time_taken = end_time - start_time |
| print(f"Time taken: {time_taken:.2f} seconds") |
|
|
| def __call__(self, data: Dict[str, Any]) -> Union[Image.Image, None]: |
| logger.info(f"Received incoming request with {data=}") |
| try: |
| if "inputs" in data and isinstance(data["inputs"], str): |
| prompt = data.pop("inputs") |
| elif "prompt" in data and isinstance(data["prompt"], str): |
| prompt = data.pop("prompt") |
| else: |
| raise ValueError( |
| "Provided input body must contain either the key `inputs` or `prompt` with the" |
| " prompt to use for the image generation, and it needs to be a non-empty string." |
| ) |
|
|
| parameters = data.pop("parameters", {}) |
|
|
| num_inference_steps = parameters.get("num_inference_steps", 28) |
| width = parameters.get("width", 1024) |
| height = parameters.get("height", 1024) |
| |
| guidance_scale = parameters.get("guidance", 3.5) |
|
|
| |
| seed = parameters.get("seed", 0) |
| generator = torch.manual_seed(seed) |
| start_time = time.time() |
| result = self.pipeline( |
| prompt, |
| height=height, |
| width=width, |
| guidance_scale=guidance_scale, |
| num_inference_steps=num_inference_steps, |
| generator=generator, |
| ).images[0] |
| end_time = time.time() |
| time_taken = end_time - start_time |
| print(f"Time taken: {time_taken:.2f} seconds") |
|
|
| return result |
| except Exception as e: |
| print(e) |
| return None |
|
|