|
17 | 17 | """ |
18 | 18 |
|
19 | 19 | import comfy.utils |
| 20 | +import comfy.model_management |
| 21 | +import comfy.model_base |
20 | 22 | import logging |
| 23 | +import torch |
21 | 24 |
|
22 | 25 | LORA_CLIP_MAP = { |
23 | 26 | "mlp.fc1": "mlp_fc1", |
@@ -322,3 +325,192 @@ def model_lora_keys_unet(model, key_map={}): |
322 | 325 | key_map["lycoris_{}".format(k[:-len(".weight")].replace(".", "_"))] = to #simpletrainer lycoris |
323 | 326 |
|
324 | 327 | return key_map |
| 328 | + |
| 329 | + |
| 330 | +def weight_decompose(dora_scale, weight, lora_diff, alpha, strength, intermediate_dtype): |
| 331 | + dora_scale = comfy.model_management.cast_to_device(dora_scale, weight.device, intermediate_dtype) |
| 332 | + lora_diff *= alpha |
| 333 | + weight_calc = weight + lora_diff.type(weight.dtype) |
| 334 | + weight_norm = ( |
| 335 | + weight_calc.transpose(0, 1) |
| 336 | + .reshape(weight_calc.shape[1], -1) |
| 337 | + .norm(dim=1, keepdim=True) |
| 338 | + .reshape(weight_calc.shape[1], *[1] * (weight_calc.dim() - 1)) |
| 339 | + .transpose(0, 1) |
| 340 | + ) |
| 341 | + |
| 342 | + weight_calc *= (dora_scale / weight_norm).type(weight.dtype) |
| 343 | + if strength != 1.0: |
| 344 | + weight_calc -= weight |
| 345 | + weight += strength * (weight_calc) |
| 346 | + else: |
| 347 | + weight[:] = weight_calc |
| 348 | + return weight |
| 349 | + |
| 350 | +def calculate_weight(patches, weight, key, intermediate_dtype=torch.float32): |
| 351 | + for p in patches: |
| 352 | + strength = p[0] |
| 353 | + v = p[1] |
| 354 | + strength_model = p[2] |
| 355 | + offset = p[3] |
| 356 | + function = p[4] |
| 357 | + if function is None: |
| 358 | + function = lambda a: a |
| 359 | + |
| 360 | + old_weight = None |
| 361 | + if offset is not None: |
| 362 | + old_weight = weight |
| 363 | + weight = weight.narrow(offset[0], offset[1], offset[2]) |
| 364 | + |
| 365 | + if strength_model != 1.0: |
| 366 | + weight *= strength_model |
| 367 | + |
| 368 | + if isinstance(v, list): |
| 369 | + v = (calculate_weight(v[1:], v[0].clone(), key, intermediate_dtype=intermediate_dtype), ) |
| 370 | + |
| 371 | + if len(v) == 1: |
| 372 | + patch_type = "diff" |
| 373 | + elif len(v) == 2: |
| 374 | + patch_type = v[0] |
| 375 | + v = v[1] |
| 376 | + |
| 377 | + if patch_type == "diff": |
| 378 | + w1 = v[0] |
| 379 | + if strength != 0.0: |
| 380 | + if w1.shape != weight.shape: |
| 381 | + logging.warning("WARNING SHAPE MISMATCH {} WEIGHT NOT MERGED {} != {}".format(key, w1.shape, weight.shape)) |
| 382 | + else: |
| 383 | + weight += function(strength * comfy.model_management.cast_to_device(w1, weight.device, weight.dtype)) |
| 384 | + elif patch_type == "lora": #lora/locon |
| 385 | + mat1 = comfy.model_management.cast_to_device(v[0], weight.device, intermediate_dtype) |
| 386 | + mat2 = comfy.model_management.cast_to_device(v[1], weight.device, intermediate_dtype) |
| 387 | + dora_scale = v[4] |
| 388 | + if v[2] is not None: |
| 389 | + alpha = v[2] / mat2.shape[0] |
| 390 | + else: |
| 391 | + alpha = 1.0 |
| 392 | + |
| 393 | + if v[3] is not None: |
| 394 | + #locon mid weights, hopefully the math is fine because I didn't properly test it |
| 395 | + mat3 = comfy.model_management.cast_to_device(v[3], weight.device, intermediate_dtype) |
| 396 | + final_shape = [mat2.shape[1], mat2.shape[0], mat3.shape[2], mat3.shape[3]] |
| 397 | + mat2 = torch.mm(mat2.transpose(0, 1).flatten(start_dim=1), mat3.transpose(0, 1).flatten(start_dim=1)).reshape(final_shape).transpose(0, 1) |
| 398 | + try: |
| 399 | + lora_diff = torch.mm(mat1.flatten(start_dim=1), mat2.flatten(start_dim=1)).reshape(weight.shape) |
| 400 | + if dora_scale is not None: |
| 401 | + weight = function(weight_decompose(dora_scale, weight, lora_diff, alpha, strength, intermediate_dtype)) |
| 402 | + else: |
| 403 | + weight += function(((strength * alpha) * lora_diff).type(weight.dtype)) |
| 404 | + except Exception as e: |
| 405 | + logging.error("ERROR {} {} {}".format(patch_type, key, e)) |
| 406 | + elif patch_type == "lokr": |
| 407 | + w1 = v[0] |
| 408 | + w2 = v[1] |
| 409 | + w1_a = v[3] |
| 410 | + w1_b = v[4] |
| 411 | + w2_a = v[5] |
| 412 | + w2_b = v[6] |
| 413 | + t2 = v[7] |
| 414 | + dora_scale = v[8] |
| 415 | + dim = None |
| 416 | + |
| 417 | + if w1 is None: |
| 418 | + dim = w1_b.shape[0] |
| 419 | + w1 = torch.mm(comfy.model_management.cast_to_device(w1_a, weight.device, intermediate_dtype), |
| 420 | + comfy.model_management.cast_to_device(w1_b, weight.device, intermediate_dtype)) |
| 421 | + else: |
| 422 | + w1 = comfy.model_management.cast_to_device(w1, weight.device, intermediate_dtype) |
| 423 | + |
| 424 | + if w2 is None: |
| 425 | + dim = w2_b.shape[0] |
| 426 | + if t2 is None: |
| 427 | + w2 = torch.mm(comfy.model_management.cast_to_device(w2_a, weight.device, intermediate_dtype), |
| 428 | + comfy.model_management.cast_to_device(w2_b, weight.device, intermediate_dtype)) |
| 429 | + else: |
| 430 | + w2 = torch.einsum('i j k l, j r, i p -> p r k l', |
| 431 | + comfy.model_management.cast_to_device(t2, weight.device, intermediate_dtype), |
| 432 | + comfy.model_management.cast_to_device(w2_b, weight.device, intermediate_dtype), |
| 433 | + comfy.model_management.cast_to_device(w2_a, weight.device, intermediate_dtype)) |
| 434 | + else: |
| 435 | + w2 = comfy.model_management.cast_to_device(w2, weight.device, intermediate_dtype) |
| 436 | + |
| 437 | + if len(w2.shape) == 4: |
| 438 | + w1 = w1.unsqueeze(2).unsqueeze(2) |
| 439 | + if v[2] is not None and dim is not None: |
| 440 | + alpha = v[2] / dim |
| 441 | + else: |
| 442 | + alpha = 1.0 |
| 443 | + |
| 444 | + try: |
| 445 | + lora_diff = torch.kron(w1, w2).reshape(weight.shape) |
| 446 | + if dora_scale is not None: |
| 447 | + weight = function(weight_decompose(dora_scale, weight, lora_diff, alpha, strength, intermediate_dtype)) |
| 448 | + else: |
| 449 | + weight += function(((strength * alpha) * lora_diff).type(weight.dtype)) |
| 450 | + except Exception as e: |
| 451 | + logging.error("ERROR {} {} {}".format(patch_type, key, e)) |
| 452 | + elif patch_type == "loha": |
| 453 | + w1a = v[0] |
| 454 | + w1b = v[1] |
| 455 | + if v[2] is not None: |
| 456 | + alpha = v[2] / w1b.shape[0] |
| 457 | + else: |
| 458 | + alpha = 1.0 |
| 459 | + |
| 460 | + w2a = v[3] |
| 461 | + w2b = v[4] |
| 462 | + dora_scale = v[7] |
| 463 | + if v[5] is not None: #cp decomposition |
| 464 | + t1 = v[5] |
| 465 | + t2 = v[6] |
| 466 | + m1 = torch.einsum('i j k l, j r, i p -> p r k l', |
| 467 | + comfy.model_management.cast_to_device(t1, weight.device, intermediate_dtype), |
| 468 | + comfy.model_management.cast_to_device(w1b, weight.device, intermediate_dtype), |
| 469 | + comfy.model_management.cast_to_device(w1a, weight.device, intermediate_dtype)) |
| 470 | + |
| 471 | + m2 = torch.einsum('i j k l, j r, i p -> p r k l', |
| 472 | + comfy.model_management.cast_to_device(t2, weight.device, intermediate_dtype), |
| 473 | + comfy.model_management.cast_to_device(w2b, weight.device, intermediate_dtype), |
| 474 | + comfy.model_management.cast_to_device(w2a, weight.device, intermediate_dtype)) |
| 475 | + else: |
| 476 | + m1 = torch.mm(comfy.model_management.cast_to_device(w1a, weight.device, intermediate_dtype), |
| 477 | + comfy.model_management.cast_to_device(w1b, weight.device, intermediate_dtype)) |
| 478 | + m2 = torch.mm(comfy.model_management.cast_to_device(w2a, weight.device, intermediate_dtype), |
| 479 | + comfy.model_management.cast_to_device(w2b, weight.device, intermediate_dtype)) |
| 480 | + |
| 481 | + try: |
| 482 | + lora_diff = (m1 * m2).reshape(weight.shape) |
| 483 | + if dora_scale is not None: |
| 484 | + weight = function(weight_decompose(dora_scale, weight, lora_diff, alpha, strength, intermediate_dtype)) |
| 485 | + else: |
| 486 | + weight += function(((strength * alpha) * lora_diff).type(weight.dtype)) |
| 487 | + except Exception as e: |
| 488 | + logging.error("ERROR {} {} {}".format(patch_type, key, e)) |
| 489 | + elif patch_type == "glora": |
| 490 | + if v[4] is not None: |
| 491 | + alpha = v[4] / v[0].shape[0] |
| 492 | + else: |
| 493 | + alpha = 1.0 |
| 494 | + |
| 495 | + dora_scale = v[5] |
| 496 | + |
| 497 | + a1 = comfy.model_management.cast_to_device(v[0].flatten(start_dim=1), weight.device, intermediate_dtype) |
| 498 | + a2 = comfy.model_management.cast_to_device(v[1].flatten(start_dim=1), weight.device, intermediate_dtype) |
| 499 | + b1 = comfy.model_management.cast_to_device(v[2].flatten(start_dim=1), weight.device, intermediate_dtype) |
| 500 | + b2 = comfy.model_management.cast_to_device(v[3].flatten(start_dim=1), weight.device, intermediate_dtype) |
| 501 | + |
| 502 | + try: |
| 503 | + lora_diff = (torch.mm(b2, b1) + torch.mm(torch.mm(weight.flatten(start_dim=1), a2), a1)).reshape(weight.shape) |
| 504 | + if dora_scale is not None: |
| 505 | + weight = function(weight_decompose(dora_scale, weight, lora_diff, alpha, strength, intermediate_dtype)) |
| 506 | + else: |
| 507 | + weight += function(((strength * alpha) * lora_diff).type(weight.dtype)) |
| 508 | + except Exception as e: |
| 509 | + logging.error("ERROR {} {} {}".format(patch_type, key, e)) |
| 510 | + else: |
| 511 | + logging.warning("patch type not recognized {} {}".format(patch_type, key)) |
| 512 | + |
| 513 | + if old_weight is not None: |
| 514 | + weight = old_weight |
| 515 | + |
| 516 | + return weight |
0 commit comments