(model, device_to, transformer_load_device, params_to_keep=None, dtype=None, base_dtype=None, state_dict=None, low_mem_load=False)
| 359 | return (new_modelpatcher, new_clip) |
| 360 | |
| 361 | def apply_lora(model, device_to, transformer_load_device, params_to_keep=None, dtype=None, base_dtype=None, state_dict=None, low_mem_load=False): |
| 362 | to_load = [] |
| 363 | for n, m in model.model.named_modules(): |
| 364 | params = [] |
| 365 | skip = False |
| 366 | for name, param in m.named_parameters(recurse=False): |
| 367 | params.append(name) |
| 368 | for name, param in m.named_parameters(recurse=True): |
| 369 | if name not in params: |
| 370 | skip = True # skip random weights in non leaf modules |
| 371 | break |
| 372 | if not skip and (hasattr(m, "comfy_cast_weights") or len(params) > 0): |
| 373 | to_load.append((n, m, params)) |
| 374 | |
| 375 | to_load.sort(reverse=True) |
| 376 | for x in tqdm(to_load, desc="Loading model and applying LoRA weights:", leave=True): |
| 377 | name = x[0] |
| 378 | m = x[1] |
| 379 | params = x[2] |
| 380 | if hasattr(m, "comfy_patched_weights"): |
| 381 | if m.comfy_patched_weights == True: |
| 382 | continue |
| 383 | for param in params: |
| 384 | name = name.replace("._orig_mod.", ".") # torch compiled modules have this prefix |
| 385 | if low_mem_load: |
| 386 | dtype_to_use = base_dtype if any(keyword in name for keyword in params_to_keep) else dtype |
| 387 | if "patch_embedding" in name: |
| 388 | dtype_to_use = torch.float32 |
| 389 | if name.startswith("diffusion_model."): |
| 390 | name_no_prefix = name[len("diffusion_model."):] |
| 391 | key = "{}.{}".format(name_no_prefix, param) |
| 392 | try: |
| 393 | set_module_tensor_to_device(model.model.diffusion_model, key, device=transformer_load_device, dtype=dtype_to_use, value=state_dict[key]) |
| 394 | except: |
| 395 | continue |
| 396 | model.patch_weight_to_device("{}.{}".format(name, param), device_to=device_to) |
| 397 | if low_mem_load: |
| 398 | try: |
| 399 | set_module_tensor_to_device(model.model.diffusion_model, key, device=transformer_load_device, dtype=dtype_to_use, value=model.model.diffusion_model.state_dict()[key]) |
| 400 | except: |
| 401 | continue |
| 402 | m.comfy_patched_weights = True |
| 403 | |
| 404 | model.current_weight_patches_uuid = model.patches_uuid |
| 405 | if low_mem_load: |
| 406 | for name, param in model.model.diffusion_model.named_parameters(): |
| 407 | if param.device != transformer_load_device: |
| 408 | dtype_to_use = base_dtype if any(keyword in name for keyword in params_to_keep) else dtype |
| 409 | if "patch_embedding" in name: |
| 410 | dtype_to_use = torch.float32 |
| 411 | try: |
| 412 | set_module_tensor_to_device(model.model.diffusion_model, name, device=transformer_load_device, dtype=dtype_to_use, value=state_dict[name]) |
| 413 | except: |
| 414 | continue |
| 415 | return model |
| 416 | |
| 417 | def apply_lora(model, device_to, transformer_load_device, params_to_keep=None, dtype=None, base_dtype=None, state_dict=None, low_mem_load=False): |
| 418 | to_load = [] |
nothing calls this directly
no outgoing calls
no test coverage detected