Sebastian Semeniuc commited on
Commit ·
d4747d7
1
Parent(s): 466101e
fix: commute inference to gpu and touches
Browse files- handler.py +15 -15
handler.py
CHANGED
|
@@ -11,12 +11,12 @@ import cv2
|
|
| 11 |
import controlnet_hinter
|
| 12 |
|
| 13 |
# set device
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
# for the moment, support only canny edge
|
| 22 |
SDXLCONTROLNET_MAPPING = {
|
|
@@ -24,7 +24,7 @@ SDXLCONTROLNET_MAPPING = {
|
|
| 24 |
"model_id": "diffusers/controlnet-canny-sdxl-1.0",
|
| 25 |
"hinter": controlnet_hinter.hint_canny
|
| 26 |
},
|
| 27 |
-
|
| 28 |
"model_id": "lllyasviel/sd-controlnet-openpose",
|
| 29 |
"hinter": controlnet_hinter.hint_openpose
|
| 30 |
},
|
|
@@ -96,7 +96,7 @@ class EndpointHandler():
|
|
| 96 |
# define default controlnet id and load controlnet
|
| 97 |
self.control_type = "canny_edge"
|
| 98 |
self.controlnet = ControlNetModel.from_pretrained(
|
| 99 |
-
SDXLCONTROLNET_MAPPING[self.control_type]["model_id"], torch_dtype=
|
| 100 |
|
| 101 |
# Load StableDiffusionControlNetPipeline
|
| 102 |
self.sdxl_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
|
@@ -104,10 +104,8 @@ class EndpointHandler():
|
|
| 104 |
self.pipe = StableDiffusionXLControlNetPipeline.from_pretrained(self.sdxl_id,
|
| 105 |
controlnet=self.controlnet,
|
| 106 |
# vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16, use_safetensors=True),
|
| 107 |
-
torch_dtype=
|
| 108 |
-
|
| 109 |
-
self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
|
| 110 |
-
self.pipe.enable_model_cpu_offload()
|
| 111 |
self.generator = torch.Generator(device="cpu").manual_seed(3)
|
| 112 |
|
| 113 |
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
|
@@ -127,12 +125,16 @@ class EndpointHandler():
|
|
| 127 |
if num_of_images is None:
|
| 128 |
num_of_images = 1
|
| 129 |
|
|
|
|
|
|
|
|
|
|
| 130 |
# Check if a new controlnet is provided
|
| 131 |
if controlnet_type is not None and controlnet_type != self.control_type:
|
| 132 |
print(
|
| 133 |
f"changing controlnet from {self.control_type} to {controlnet_type} using {SDXLCONTROLNET_MAPPING[controlnet_type]['model_id']} model")
|
| 134 |
self.control_type = controlnet_type
|
| 135 |
-
self.controlnet = ControlNetModel.from_pretrained(
|
|
|
|
| 136 |
self.pipe.controlnet = self.controlnet
|
| 137 |
|
| 138 |
# hyperparamters
|
|
@@ -148,8 +150,6 @@ class EndpointHandler():
|
|
| 148 |
image = self.decode_base64_image(image)
|
| 149 |
control_image = SDXLCONTROLNET_MAPPING[self.control_type]["hinter"](
|
| 150 |
image, width=1024, height=1024)
|
| 151 |
-
|
| 152 |
-
self.generator = torch.manual_seed(1)
|
| 153 |
|
| 154 |
# run inference pipeline
|
| 155 |
images = self.pipe(
|
|
|
|
| 11 |
import controlnet_hinter
|
| 12 |
|
| 13 |
# set device
|
| 14 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 15 |
+
if device.type != 'cuda':
|
| 16 |
+
raise ValueError("need to run on GPU")
|
| 17 |
+
# set mixed precision dtype
|
| 18 |
+
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[
|
| 19 |
+
0] == 8 else torch.float16
|
| 20 |
|
| 21 |
# for the moment, support only canny edge
|
| 22 |
SDXLCONTROLNET_MAPPING = {
|
|
|
|
| 24 |
"model_id": "diffusers/controlnet-canny-sdxl-1.0",
|
| 25 |
"hinter": controlnet_hinter.hint_canny
|
| 26 |
},
|
| 27 |
+
"pose": {
|
| 28 |
"model_id": "lllyasviel/sd-controlnet-openpose",
|
| 29 |
"hinter": controlnet_hinter.hint_openpose
|
| 30 |
},
|
|
|
|
| 96 |
# define default controlnet id and load controlnet
|
| 97 |
self.control_type = "canny_edge"
|
| 98 |
self.controlnet = ControlNetModel.from_pretrained(
|
| 99 |
+
SDXLCONTROLNET_MAPPING[self.control_type]["model_id"], torch_dtype=dtype).to(device)
|
| 100 |
|
| 101 |
# Load StableDiffusionControlNetPipeline
|
| 102 |
self.sdxl_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
|
|
|
| 104 |
self.pipe = StableDiffusionXLControlNetPipeline.from_pretrained(self.sdxl_id,
|
| 105 |
controlnet=self.controlnet,
|
| 106 |
# vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16, use_safetensors=True),
|
| 107 |
+
torch_dtype=dtype,
|
| 108 |
+
safety_checker=None).to(device)
|
|
|
|
|
|
|
| 109 |
self.generator = torch.Generator(device="cpu").manual_seed(3)
|
| 110 |
|
| 111 |
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
|
|
|
| 125 |
if num_of_images is None:
|
| 126 |
num_of_images = 1
|
| 127 |
|
| 128 |
+
if controlnet_type is not "canny_edge" and controlnet_type is not None:
|
| 129 |
+
return {"error": "Please provide a valid controlnet type. Only canny_edge is supported at the moment."}
|
| 130 |
+
|
| 131 |
# Check if a new controlnet is provided
|
| 132 |
if controlnet_type is not None and controlnet_type != self.control_type:
|
| 133 |
print(
|
| 134 |
f"changing controlnet from {self.control_type} to {controlnet_type} using {SDXLCONTROLNET_MAPPING[controlnet_type]['model_id']} model")
|
| 135 |
self.control_type = controlnet_type
|
| 136 |
+
self.controlnet = ControlNetModel.from_pretrained(
|
| 137 |
+
SDXLCONTROLNET_MAPPING[self.control_type]["model_id"], torch_dtype=dtype).to(device)
|
| 138 |
self.pipe.controlnet = self.controlnet
|
| 139 |
|
| 140 |
# hyperparamters
|
|
|
|
| 150 |
image = self.decode_base64_image(image)
|
| 151 |
control_image = SDXLCONTROLNET_MAPPING[self.control_type]["hinter"](
|
| 152 |
image, width=1024, height=1024)
|
|
|
|
|
|
|
| 153 |
|
| 154 |
# run inference pipeline
|
| 155 |
images = self.pipe(
|