new handler
Browse files- handler.py +31 -39
handler.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
-
from typing import
|
|
|
|
|
2 |
import torch
|
3 |
-
|
4 |
from diffusers import AutoencoderKL
|
5 |
from diffusers.image_processor import VaeImageProcessor
|
6 |
|
@@ -9,11 +11,7 @@ class EndpointHandler:
|
|
9 |
def __init__(self, path=""):
|
10 |
self.device = "cuda"
|
11 |
self.dtype = torch.bfloat16
|
12 |
-
self.vae = (
|
13 |
-
AutoencoderKL.from_pretrained(path, torch_dtype=self.dtype)
|
14 |
-
.to(self.device, self.dtype)
|
15 |
-
.eval()
|
16 |
-
)
|
17 |
|
18 |
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
|
19 |
self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor)
|
@@ -35,47 +33,41 @@ class EndpointHandler:
|
|
35 |
return latents
|
36 |
|
37 |
@torch.no_grad()
|
38 |
-
def __call__(self, data
|
39 |
"""
|
40 |
Args:
|
41 |
data (:obj:):
|
42 |
includes the input data and the parameters for the inference.
|
43 |
"""
|
44 |
-
tensor = data["inputs"]
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
if
|
54 |
-
|
55 |
-
|
56 |
-
DTYPE_MAP = {
|
57 |
-
"float16": torch.float16,
|
58 |
-
"float32": torch.float32,
|
59 |
-
"bfloat16": torch.bfloat16,
|
60 |
-
}
|
61 |
-
|
62 |
-
shape = parameters.get("shape")
|
63 |
-
dtype = DTYPE_MAP.get(parameters.get("dtype"))
|
64 |
-
height = parameters.get("height")
|
65 |
-
width = parameters.get("width")
|
66 |
-
|
67 |
-
tensor = torch.frombuffer(bytearray(tensor), dtype=dtype).reshape(shape)
|
68 |
|
69 |
tensor = tensor.to(self.device, self.dtype)
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
|
76 |
with torch.no_grad():
|
77 |
-
image = self.vae.decode(tensor, return_dict=False)[0]
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
return image
|
|
|
1 |
+
from typing import cast, Union
|
2 |
+
|
3 |
+
import PIL.Image
|
4 |
import torch
|
5 |
+
|
6 |
from diffusers import AutoencoderKL
|
7 |
from diffusers.image_processor import VaeImageProcessor
|
8 |
|
|
|
11 |
def __init__(self, path=""):
|
12 |
self.device = "cuda"
|
13 |
self.dtype = torch.bfloat16
|
14 |
+
self.vae = cast(AutoencoderKL, AutoencoderKL.from_pretrained(path, torch_dtype=self.dtype).to(self.device, self.dtype).eval())
|
|
|
|
|
|
|
|
|
15 |
|
16 |
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
|
17 |
self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor)
|
|
|
33 |
return latents
|
34 |
|
35 |
@torch.no_grad()
|
36 |
+
def __call__(self, data) -> Union[torch.Tensor, PIL.Image.Image]:
|
37 |
"""
|
38 |
Args:
|
39 |
data (:obj:):
|
40 |
includes the input data and the parameters for the inference.
|
41 |
"""
|
42 |
+
tensor = cast(torch.Tensor, data["inputs"])
|
43 |
+
parameters = cast(dict, data.get("parameters", {}))
|
44 |
+
if tensor.ndim == 3 and ("height" not in parameters or "width" not in parameters):
|
45 |
+
raise ValueError("Expected `height` and `width` in parameters.")
|
46 |
+
height = cast(int, parameters.get("height", 0))
|
47 |
+
width = cast(int, parameters.get("width", 0))
|
48 |
+
do_scaling = cast(bool, parameters.get("do_scaling", True))
|
49 |
+
output_type = cast(str, parameters.get("output_type", "pil"))
|
50 |
+
partial_postprocess = cast(bool, parameters.get("partial_postprocess", False))
|
51 |
+
if partial_postprocess and output_type != "pt":
|
52 |
+
output_type = "pt"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
tensor = tensor.to(self.device, self.dtype)
|
55 |
+
if tensor.ndim == 3:
|
56 |
+
tensor = self._unpack_latents(tensor, height, width, self.vae_scale_factor)
|
57 |
|
58 |
+
if do_scaling:
|
59 |
+
tensor = (
|
60 |
+
tensor / self.vae.config.scaling_factor
|
61 |
+
) + self.vae.config.shift_factor
|
62 |
|
63 |
with torch.no_grad():
|
64 |
+
image = cast(torch.Tensor, self.vae.decode(tensor, return_dict=False)[0])
|
65 |
|
66 |
+
if partial_postprocess:
|
67 |
+
image = (image * 0.5 + 0.5).clamp(0, 1)
|
68 |
+
image = image.permute(0, 2, 3, 1).contiguous().float()
|
69 |
+
image = (image * 255).round().to(torch.uint8)
|
70 |
+
elif output_type == "pil":
|
71 |
+
image = cast(PIL.Image.Image, self.image_processor.postprocess(image, output_type="pil")[0])
|
72 |
|
73 |
+
return image
|