Spaces:
Runtime error
Runtime error
File size: 20,909 Bytes
0a72c84 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
import gc
import os
from typing import Any, Callable, List, Literal, Union, Dict, Tuple
import logging
from safetensors.torch import load_file
from safetensors import safe_open
import torch
from torch import nn
from diffusers.models.controlnet import ControlNetModel
from diffusers.pipelines.controlnet.multicontrolnet import MultiControlNetModel
from diffusers.pipelines.pipeline_utils import DiffusionPipeline
from .convert_from_ckpt import (
convert_ldm_unet_checkpoint,
convert_ldm_vae_checkpoint,
convert_ldm_clip_checkpoint,
)
from .convert_lora_safetensor_to_diffusers import convert_motion_lora_ckpt_to_diffusers
logger = logging.getLogger(__name__)
def update_pipeline_model_parameters(
pipeline: DiffusionPipeline,
model_path: str = None,
lora_dict: Dict[str, Dict] = None,
text_model_path: str = None,
device="cuda",
need_unload: bool = False,
):
if model_path is not None:
pipeline = update_pipeline_basemodel(
pipeline, model_path, text_sd_model_path=text_model_path, device=device
)
if lora_dict is not None:
pipeline, unload_dict = update_pipeline_lora_models(
pipeline,
lora_dict,
device=device,
need_unload=need_unload,
)
if need_unload:
return pipeline, unload_dict
return pipeline
def update_pipeline_basemodel(
pipeline: DiffusionPipeline,
model_path: str,
text_sd_model_path: str,
device: str = "cuda",
):
"""使用model_path更新pipeline中的基础参数
Args:
pipeline (DiffusionPipeline): _description_
model_path (str): _description_
text_sd_model_path (str): _description_
device (str, optional): _description_. Defaults to "cuda".
Returns:
_type_: _description_
"""
# load base
if model_path.endswith(".ckpt"):
state_dict = torch.load(model_path, map_location=device)
pipeline.unet.load_state_dict(state_dict)
print("update sd_model", model_path)
elif model_path.endswith(".safetensors"):
base_state_dict = {}
with safe_open(model_path, framework="pt", device=device) as f:
for key in f.keys():
base_state_dict[key] = f.get_tensor(key)
is_lora = all("lora" in k for k in base_state_dict.keys())
assert is_lora == False, "Base model cannot be LoRA: {}".format(model_path)
# vae
converted_vae_checkpoint = convert_ldm_vae_checkpoint(
base_state_dict, pipeline.vae.config
)
pipeline.vae.load_state_dict(converted_vae_checkpoint)
# unet
converted_unet_checkpoint = convert_ldm_unet_checkpoint(
base_state_dict, pipeline.unet.config
)
pipeline.unet.load_state_dict(converted_unet_checkpoint, strict=False)
# text_model
pipeline.text_encoder = convert_ldm_clip_checkpoint(
base_state_dict, text_sd_model_path
)
print("update sd_model", model_path)
pipeline.to(device)
return pipeline
# ref https://git.woa.com/innovative_tech/GenerationGroup/VirtualIdol/VidolImageDraw/blob/master/cfg.yaml
LORA_BLOCK_WEIGHT_MAP = {
"FACE": [1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
"DEFACE": [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
"ALL": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
"MIDD": [1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
"OUTALL": [1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
}
# ref https://git.woa.com/innovative_tech/GenerationGroup/VirtualIdol/VidolImageDraw/blob/master/pipeline/draw_pipe.py
def update_pipeline_lora_model(
pipeline: DiffusionPipeline,
lora: Union[str, Dict],
alpha: float = 0.75,
device: str = "cuda",
lora_prefix_unet: str = "lora_unet",
lora_prefix_text_encoder: str = "lora_te",
lora_unet_layers=[
"lora_unet_down_blocks_0_attentions_0",
"lora_unet_down_blocks_0_attentions_1",
"lora_unet_down_blocks_1_attentions_0",
"lora_unet_down_blocks_1_attentions_1",
"lora_unet_down_blocks_2_attentions_0",
"lora_unet_down_blocks_2_attentions_1",
"lora_unet_mid_block_attentions_0",
"lora_unet_up_blocks_1_attentions_0",
"lora_unet_up_blocks_1_attentions_1",
"lora_unet_up_blocks_1_attentions_2",
"lora_unet_up_blocks_2_attentions_0",
"lora_unet_up_blocks_2_attentions_1",
"lora_unet_up_blocks_2_attentions_2",
"lora_unet_up_blocks_3_attentions_0",
"lora_unet_up_blocks_3_attentions_1",
"lora_unet_up_blocks_3_attentions_2",
],
lora_block_weight_str: Literal["FACE", "ALL"] = "ALL",
need_unload: bool = False,
):
"""使用 lora 更新pipeline中的unet相关参数
Args:
pipeline (DiffusionPipeline): _description_
lora (Union[str, Dict]): _description_
alpha (float, optional): _description_. Defaults to 0.75.
device (str, optional): _description_. Defaults to "cuda".
lora_prefix_unet (str, optional): _description_. Defaults to "lora_unet".
lora_prefix_text_encoder (str, optional): _description_. Defaults to "lora_te".
lora_unet_layers (list, optional): _description_. Defaults to [ "lora_unet_down_blocks_0_attentions_0", "lora_unet_down_blocks_0_attentions_1", "lora_unet_down_blocks_1_attentions_0", "lora_unet_down_blocks_1_attentions_1", "lora_unet_down_blocks_2_attentions_0", "lora_unet_down_blocks_2_attentions_1", "lora_unet_mid_block_attentions_0", "lora_unet_up_blocks_1_attentions_0", "lora_unet_up_blocks_1_attentions_1", "lora_unet_up_blocks_1_attentions_2", "lora_unet_up_blocks_2_attentions_0", "lora_unet_up_blocks_2_attentions_1", "lora_unet_up_blocks_2_attentions_2", "lora_unet_up_blocks_3_attentions_0", "lora_unet_up_blocks_3_attentions_1", "lora_unet_up_blocks_3_attentions_2", ].
lora_block_weight_str (Literal["FACE", "ALL"], optional): _description_. Defaults to "ALL".
need_unload (bool, optional): _description_. Defaults to False.
Returns:
_type_: _description_
"""
# ref https://git.woa.com/innovative_tech/GenerationGroup/VirtualIdol/VidolImageDraw/blob/master/pipeline/tool.py#L20
if lora_block_weight_str is not None:
lora_block_weight = LORA_BLOCK_WEIGHT_MAP[lora_block_weight_str.upper()]
if lora_block_weight:
assert len(lora_block_weight) == 17
# load lora weight
if isinstance(lora, str):
state_dict = load_file(lora, device=device)
else:
for k in lora:
lora[k] = lora[k].to(device)
state_dict = lora # state_dict = {}
visited = set()
unload_dict = []
# directly update weight in diffusers model
for key in state_dict:
# it is suggested to print out the key, it usually will be something like below
# "lora_te_text_model_encoder_layers_0_self_attn_k_proj.lora_down.weight"
# as we have set the alpha beforehand, so just skip
if ".alpha" in key or key in visited:
continue
if "text" in key:
layer_infos = (
key.split(".")[0].split(lora_prefix_text_encoder + "_")[-1].split("_")
)
curr_layer = pipeline.text_encoder
else:
layer_infos = key.split(".")[0].split(lora_prefix_unet + "_")[-1].split("_")
curr_layer = pipeline.unet
# find the target layer
temp_name = layer_infos.pop(0)
while len(layer_infos) > -1:
try:
curr_layer = curr_layer.__getattr__(temp_name)
if len(layer_infos) > 0:
temp_name = layer_infos.pop(0)
elif len(layer_infos) == 0:
break
except Exception:
if len(temp_name) > 0:
temp_name += "_" + layer_infos.pop(0)
else:
temp_name = layer_infos.pop(0)
pair_keys = []
if "lora_down" in key:
pair_keys.append(key.replace("lora_down", "lora_up"))
pair_keys.append(key)
alpha_key = key.replace("lora_down.weight", "alpha")
else:
pair_keys.append(key)
pair_keys.append(key.replace("lora_up", "lora_down"))
alpha_key = key.replace("lora_up.weight", "alpha")
# update weight
if len(state_dict[pair_keys[0]].shape) == 4:
weight_up = state_dict[pair_keys[0]].squeeze(3).squeeze(2).to(torch.float32)
weight_down = (
state_dict[pair_keys[1]].squeeze(3).squeeze(2).to(torch.float32)
)
if alpha_key in state_dict:
weight_scale = state_dict[alpha_key].item() / weight_up.shape[1]
else:
weight_scale = 1.0
# adding_weight = alpha * torch.mm(weight_up, weight_down).unsqueeze(2).unsqueeze(3)
if len(weight_up.shape) == len(weight_down.shape):
adding_weight = (
alpha
* weight_scale
* torch.mm(weight_up, weight_down).unsqueeze(2).unsqueeze(3)
)
else:
adding_weight = (
alpha
* weight_scale
* torch.einsum("a b, b c h w -> a c h w", weight_up, weight_down)
)
else:
weight_up = state_dict[pair_keys[0]].to(torch.float32)
weight_down = state_dict[pair_keys[1]].to(torch.float32)
if alpha_key in state_dict:
weight_scale = state_dict[alpha_key].item() / weight_up.shape[1]
else:
weight_scale = 1.0
adding_weight = alpha * weight_scale * torch.mm(weight_up, weight_down)
adding_weight = adding_weight.to(torch.float16)
if lora_block_weight:
if "text" in key:
adding_weight *= lora_block_weight[0]
else:
for idx, layer in enumerate(lora_unet_layers):
if layer in key:
adding_weight *= lora_block_weight[idx + 1]
break
curr_layer_unload_data = {"layer": curr_layer, "added_weight": adding_weight}
curr_layer.weight.data += adding_weight
unload_dict.append(curr_layer_unload_data)
# update visited list
for item in pair_keys:
visited.add(item)
if need_unload:
return pipeline, unload_dict
else:
return pipeline
# ref https://git.woa.com/innovative_tech/GenerationGroup/VirtualIdol/VidolImageDraw/blob/master/pipeline/draw_pipe.py
def update_pipeline_lora_model_old(
pipeline: DiffusionPipeline,
lora: Union[str, Dict],
alpha: float = 0.75,
device: str = "cuda",
lora_prefix_unet: str = "lora_unet",
lora_prefix_text_encoder: str = "lora_te",
lora_unet_layers=[
"lora_unet_down_blocks_0_attentions_0",
"lora_unet_down_blocks_0_attentions_1",
"lora_unet_down_blocks_1_attentions_0",
"lora_unet_down_blocks_1_attentions_1",
"lora_unet_down_blocks_2_attentions_0",
"lora_unet_down_blocks_2_attentions_1",
"lora_unet_mid_block_attentions_0",
"lora_unet_up_blocks_1_attentions_0",
"lora_unet_up_blocks_1_attentions_1",
"lora_unet_up_blocks_1_attentions_2",
"lora_unet_up_blocks_2_attentions_0",
"lora_unet_up_blocks_2_attentions_1",
"lora_unet_up_blocks_2_attentions_2",
"lora_unet_up_blocks_3_attentions_0",
"lora_unet_up_blocks_3_attentions_1",
"lora_unet_up_blocks_3_attentions_2",
],
lora_block_weight_str: Literal["FACE", "ALL"] = "ALL",
need_unload: bool = False,
):
"""使用 lora 更新pipeline中的unet相关参数
Args:
pipeline (DiffusionPipeline): _description_
lora (Union[str, Dict]): _description_
alpha (float, optional): _description_. Defaults to 0.75.
device (str, optional): _description_. Defaults to "cuda".
lora_prefix_unet (str, optional): _description_. Defaults to "lora_unet".
lora_prefix_text_encoder (str, optional): _description_. Defaults to "lora_te".
lora_unet_layers (list, optional): _description_. Defaults to [ "lora_unet_down_blocks_0_attentions_0", "lora_unet_down_blocks_0_attentions_1", "lora_unet_down_blocks_1_attentions_0", "lora_unet_down_blocks_1_attentions_1", "lora_unet_down_blocks_2_attentions_0", "lora_unet_down_blocks_2_attentions_1", "lora_unet_mid_block_attentions_0", "lora_unet_up_blocks_1_attentions_0", "lora_unet_up_blocks_1_attentions_1", "lora_unet_up_blocks_1_attentions_2", "lora_unet_up_blocks_2_attentions_0", "lora_unet_up_blocks_2_attentions_1", "lora_unet_up_blocks_2_attentions_2", "lora_unet_up_blocks_3_attentions_0", "lora_unet_up_blocks_3_attentions_1", "lora_unet_up_blocks_3_attentions_2", ].
lora_block_weight_str (Literal["FACE", "ALL"], optional): _description_. Defaults to "ALL".
need_unload (bool, optional): _description_. Defaults to False.
Returns:
_type_: _description_
"""
# ref https://git.woa.com/innovative_tech/GenerationGroup/VirtualIdol/VidolImageDraw/blob/master/pipeline/tool.py#L20
if lora_block_weight_str is not None:
lora_block_weight = LORA_BLOCK_WEIGHT_MAP[lora_block_weight_str.upper()]
if lora_block_weight:
assert len(lora_block_weight) == 17
# load lora weight
if isinstance(lora, str):
state_dict = load_file(lora, device=device)
else:
for k in lora:
lora[k] = lora[k].to(device)
state_dict = lora # state_dict = {}
visited = set()
unload_dict = []
# directly update weight in diffusers model
for key in state_dict:
# it is suggested to print out the key, it usually will be something like below
# "lora_te_text_model_encoder_layers_0_self_attn_k_proj.lora_down.weight"
# as we have set the alpha beforehand, so just skip
if ".alpha" in key or key in visited:
continue
if "text" in key:
layer_infos = (
key.split(".")[0].split(lora_prefix_text_encoder + "_")[-1].split("_")
)
curr_layer = pipeline.text_encoder
else:
layer_infos = key.split(".")[0].split(lora_prefix_unet + "_")[-1].split("_")
curr_layer = pipeline.unet
# find the target layer
temp_name = layer_infos.pop(0)
while len(layer_infos) > -1:
try:
curr_layer = curr_layer.__getattr__(temp_name)
if len(layer_infos) > 0:
temp_name = layer_infos.pop(0)
elif len(layer_infos) == 0:
break
except Exception:
if len(temp_name) > 0:
temp_name += "_" + layer_infos.pop(0)
else:
temp_name = layer_infos.pop(0)
pair_keys = []
if "lora_down" in key:
pair_keys.append(key.replace("lora_down", "lora_up"))
pair_keys.append(key)
else:
pair_keys.append(key)
pair_keys.append(key.replace("lora_up", "lora_down"))
# update weight
if len(state_dict[pair_keys[0]].shape) == 4:
weight_up = state_dict[pair_keys[0]].squeeze(3).squeeze(2).to(torch.float32)
weight_down = (
state_dict[pair_keys[1]].squeeze(3).squeeze(2).to(torch.float32)
)
adding_weight = alpha * torch.mm(weight_up, weight_down).unsqueeze(
2
).unsqueeze(3)
else:
weight_up = state_dict[pair_keys[0]].to(torch.float32)
weight_down = state_dict[pair_keys[1]].to(torch.float32)
adding_weight = alpha * torch.mm(weight_up, weight_down)
if lora_block_weight:
if "text" in key:
adding_weight *= lora_block_weight[0]
else:
for idx, layer in enumerate(lora_unet_layers):
if layer in key:
adding_weight *= lora_block_weight[idx + 1]
break
curr_layer_unload_data = {"layer": curr_layer, "added_weight": adding_weight}
curr_layer.weight.data += adding_weight
unload_dict.append(curr_layer_unload_data)
# update visited list
for item in pair_keys:
visited.add(item)
if need_unload:
return pipeline, unload_dict
else:
return pipeline
def update_pipeline_lora_models(
pipeline: DiffusionPipeline,
lora_dict: Dict[str, Dict],
device: str = "cuda",
need_unload: bool = True,
lora_prefix_unet: str = "lora_unet",
lora_prefix_text_encoder: str = "lora_te",
lora_unet_layers=[
"lora_unet_down_blocks_0_attentions_0",
"lora_unet_down_blocks_0_attentions_1",
"lora_unet_down_blocks_1_attentions_0",
"lora_unet_down_blocks_1_attentions_1",
"lora_unet_down_blocks_2_attentions_0",
"lora_unet_down_blocks_2_attentions_1",
"lora_unet_mid_block_attentions_0",
"lora_unet_up_blocks_1_attentions_0",
"lora_unet_up_blocks_1_attentions_1",
"lora_unet_up_blocks_1_attentions_2",
"lora_unet_up_blocks_2_attentions_0",
"lora_unet_up_blocks_2_attentions_1",
"lora_unet_up_blocks_2_attentions_2",
"lora_unet_up_blocks_3_attentions_0",
"lora_unet_up_blocks_3_attentions_1",
"lora_unet_up_blocks_3_attentions_2",
],
):
"""使用 lora 更新pipeline中的unet相关参数
Args:
pipeline (DiffusionPipeline): _description_
lora_dict (Dict[str, Dict]): _description_
device (str, optional): _description_. Defaults to "cuda".
lora_prefix_unet (str, optional): _description_. Defaults to "lora_unet".
lora_prefix_text_encoder (str, optional): _description_. Defaults to "lora_te".
lora_unet_layers (list, optional): _description_. Defaults to [ "lora_unet_down_blocks_0_attentions_0", "lora_unet_down_blocks_0_attentions_1", "lora_unet_down_blocks_1_attentions_0", "lora_unet_down_blocks_1_attentions_1", "lora_unet_down_blocks_2_attentions_0", "lora_unet_down_blocks_2_attentions_1", "lora_unet_mid_block_attentions_0", "lora_unet_up_blocks_1_attentions_0", "lora_unet_up_blocks_1_attentions_1", "lora_unet_up_blocks_1_attentions_2", "lora_unet_up_blocks_2_attentions_0", "lora_unet_up_blocks_2_attentions_1", "lora_unet_up_blocks_2_attentions_2", "lora_unet_up_blocks_3_attentions_0", "lora_unet_up_blocks_3_attentions_1", "lora_unet_up_blocks_3_attentions_2", ].
Returns:
_type_: _description_
"""
unload_dicts = []
for lora, value in lora_dict.items():
lora_name = os.path.basename(lora).replace(".safetensors", "")
strength_offset = value.get("strength_offset", 0.0)
alpha = value.get("strength", 1.0)
alpha += strength_offset
lora_weight_str = value.get("lora_block_weight", "ALL")
lora = load_file(lora)
pipeline, unload_dict = update_pipeline_lora_model(
pipeline,
lora=lora,
device=device,
alpha=alpha,
lora_prefix_unet=lora_prefix_unet,
lora_prefix_text_encoder=lora_prefix_text_encoder,
lora_unet_layers=lora_unet_layers,
lora_block_weight_str=lora_weight_str,
need_unload=True,
)
print(
"Update LoRA {} with alpha {} and weight {}".format(
lora_name, alpha, lora_weight_str
)
)
unload_dicts += unload_dict
return pipeline, unload_dicts
def unload_lora(unload_dict: List[Dict[str, nn.Module]]):
for layer_data in unload_dict:
layer = layer_data["layer"]
added_weight = layer_data["added_weight"]
layer.weight.data -= added_weight
gc.collect()
torch.cuda.empty_cache()
def load_motion_lora_weights(
animation_pipeline,
motion_module_lora_configs=[],
):
for motion_module_lora_config in motion_module_lora_configs:
path, alpha = (
motion_module_lora_config["path"],
motion_module_lora_config["alpha"],
)
print(f"load motion LoRA from {path}")
motion_lora_state_dict = torch.load(path, map_location="cpu")
motion_lora_state_dict = (
motion_lora_state_dict["state_dict"]
if "state_dict" in motion_lora_state_dict
else motion_lora_state_dict
)
animation_pipeline = convert_motion_lora_ckpt_to_diffusers(
animation_pipeline, motion_lora_state_dict, alpha
)
return animation_pipeline
|