Update handler.py
Browse files- handler.py +55 -7
handler.py
CHANGED
|
@@ -4,20 +4,45 @@ import os
|
|
| 4 |
from typing import Any, Dict
|
| 5 |
from PIL import Image
|
| 6 |
import torch
|
|
|
|
| 7 |
import torch.distributed as dist
|
| 8 |
from huggingface_inference_toolkit.logging import logger
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
dist.init_process_group()
|
| 11 |
-
torch.cuda.set_device(dist.get_rank())
|
| 12 |
|
| 13 |
-
|
| 14 |
-
from para_attn.context_parallel.diffusers_adapters import parallelize_pipe
|
| 15 |
-
from para_attn.parallel_vae.diffusers_adapters import parallelize_vae
|
| 16 |
|
| 17 |
class EndpointHandler:
|
| 18 |
def __init__(self,path=""):
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def __call__(self, data: Dict[str, Any]) -> Image.Image:
|
| 23 |
logger.info(f"Received incoming request with {data=}")
|
|
@@ -32,4 +57,27 @@ class EndpointHandler:
|
|
| 32 |
" prompt to use for the image generation, and it needs to be a non-empty string."
|
| 33 |
)
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from typing import Any, Dict
|
| 5 |
from PIL import Image
|
| 6 |
import torch
|
| 7 |
+
from diffusers import FluxPipeline
|
| 8 |
import torch.distributed as dist
|
| 9 |
from huggingface_inference_toolkit.logging import logger
|
| 10 |
+
from para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipe
|
| 11 |
+
# from torchao.quantization import quantize_, float8_dynamic_activation_float8_weight, float8_weight_only
|
| 12 |
+
import time
|
| 13 |
dist.init_process_group()
|
|
|
|
| 14 |
|
| 15 |
+
torch.cuda.set_device(dist.get_rank())
|
|
|
|
|
|
|
| 16 |
|
| 17 |
class EndpointHandler:
|
| 18 |
def __init__(self,path=""):
|
| 19 |
|
| 20 |
+
self.pipe = FluxPipeline.from_pretrained(
|
| 21 |
+
"NoMoreCopyrightOrg/flux-dev",
|
| 22 |
+
torch_dtype=torch.bfloat16,
|
| 23 |
+
).to("cuda")
|
| 24 |
+
from para_attn.context_parallel import init_context_parallel_mesh
|
| 25 |
+
from para_attn.context_parallel.diffusers_adapters import parallelize_pipe
|
| 26 |
+
from para_attn.parallel_vae.diffusers_adapters import parallelize_vae
|
| 27 |
+
mesh = init_context_parallel_mesh(
|
| 28 |
+
self.pipe.device.type,
|
| 29 |
+
max_ring_dim_size=2,
|
| 30 |
+
)
|
| 31 |
+
parallelize_pipe(
|
| 32 |
+
self.pipe,
|
| 33 |
+
mesh=mesh,
|
| 34 |
+
)
|
| 35 |
+
parallelize_vae(self.pipe.vae, mesh=mesh._flatten())
|
| 36 |
+
apply_cache_on_pipe(self.pipe, residual_diff_threshold=0.12)
|
| 37 |
+
# quantize_(self.pipe.text_encoder, float8_weight_only())
|
| 38 |
+
# quantize_(self.pipe.transformer, float8_dynamic_activation_float8_weight())
|
| 39 |
+
torch._inductor.config.reorder_for_compute_comm_overlap = True
|
| 40 |
+
self.pipe.transformer = torch.compile(
|
| 41 |
+
self.pipe.transformer, mode="max-autotune-no-cudagraphs",
|
| 42 |
+
)
|
| 43 |
+
self.pipe.vae = torch.compile(
|
| 44 |
+
self.pipe.vae, mode="max-autotune-no-cudagraphs",
|
| 45 |
+
)
|
| 46 |
|
| 47 |
def __call__(self, data: Dict[str, Any]) -> Image.Image:
|
| 48 |
logger.info(f"Received incoming request with {data=}")
|
|
|
|
| 57 |
" prompt to use for the image generation, and it needs to be a non-empty string."
|
| 58 |
)
|
| 59 |
|
| 60 |
+
parameters = data.pop("parameters", {})
|
| 61 |
+
|
| 62 |
+
num_inference_steps = parameters.get("num_inference_steps", 28)
|
| 63 |
+
width = parameters.get("width", 1024)
|
| 64 |
+
height = parameters.get("height", 1024)
|
| 65 |
+
guidance_scale = parameters.get("guidance_scale", 3.5)
|
| 66 |
+
|
| 67 |
+
# seed generator (seed cannot be provided as is but via a generator)
|
| 68 |
+
seed = parameters.get("seed", 0)
|
| 69 |
+
generator = torch.manual_seed(seed)
|
| 70 |
+
start_time = time.time()
|
| 71 |
+
result = self.pipe( # type: ignore
|
| 72 |
+
prompt,
|
| 73 |
+
height=height,
|
| 74 |
+
width=width,
|
| 75 |
+
guidance_scale=guidance_scale,
|
| 76 |
+
num_inference_steps=num_inference_steps,
|
| 77 |
+
generator=generator,
|
| 78 |
+
# output_type="pil" if dist.get_rank() == 0 else "pt",
|
| 79 |
+
).images[0]
|
| 80 |
+
end_time = time.time()
|
| 81 |
+
time_taken = end_time - start_time
|
| 82 |
+
print(f"Time taken: {time_taken:.2f} seconds")
|
| 83 |
+
return result
|