mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-01-11 10:25:16 +00:00
SaveLora node can now save "full diff" lora format.
This isn't actually a lora format and is saving the full diff of the weights in a format that can be used in the lora loader nodes.
This commit is contained in:
parent
a09b29ca11
commit
8aabd7c8c0
@ -4,6 +4,7 @@ import comfy.utils
|
|||||||
import folder_paths
|
import folder_paths
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
CLAMP_QUANTILE = 0.99
|
CLAMP_QUANTILE = 0.99
|
||||||
|
|
||||||
@ -38,14 +39,24 @@ def extract_lora(diff, rank):
|
|||||||
Vh = Vh.reshape(rank, in_dim, kernel_size[0], kernel_size[1])
|
Vh = Vh.reshape(rank, in_dim, kernel_size[0], kernel_size[1])
|
||||||
return (U, Vh)
|
return (U, Vh)
|
||||||
|
|
||||||
def calc_lora_model(model_diff, rank, prefix_model, prefix_lora, output_sd, bias_diff=False):
|
class LORAType(Enum):
|
||||||
|
STANDARD = 0
|
||||||
|
FULL_DIFF = 1
|
||||||
|
|
||||||
|
LORA_TYPES = {"standard": LORAType.STANDARD,
|
||||||
|
"full_diff": LORAType.FULL_DIFF}
|
||||||
|
|
||||||
|
def calc_lora_model(model_diff, rank, prefix_model, prefix_lora, output_sd, lora_type, bias_diff=False):
|
||||||
comfy.model_management.load_models_gpu([model_diff], force_patch_weights=True)
|
comfy.model_management.load_models_gpu([model_diff], force_patch_weights=True)
|
||||||
sd = model_diff.model_state_dict(filter_prefix=prefix_model)
|
sd = model_diff.model_state_dict(filter_prefix=prefix_model)
|
||||||
|
|
||||||
for k in sd:
|
for k in sd:
|
||||||
if k.endswith(".weight"):
|
if k.endswith(".weight"):
|
||||||
weight_diff = sd[k]
|
weight_diff = sd[k]
|
||||||
|
if lora_type == LORAType.STANDARD:
|
||||||
if weight_diff.ndim < 2:
|
if weight_diff.ndim < 2:
|
||||||
|
if bias_diff:
|
||||||
|
output_sd["{}{}.diff".format(prefix_lora, k[len(prefix_model):-7])] = weight_diff.contiguous().half().cpu()
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
out = extract_lora(weight_diff, rank)
|
out = extract_lora(weight_diff, rank)
|
||||||
@ -53,6 +64,9 @@ def calc_lora_model(model_diff, rank, prefix_model, prefix_lora, output_sd, bias
|
|||||||
output_sd["{}{}.lora_down.weight".format(prefix_lora, k[len(prefix_model):-7])] = out[1].contiguous().half().cpu()
|
output_sd["{}{}.lora_down.weight".format(prefix_lora, k[len(prefix_model):-7])] = out[1].contiguous().half().cpu()
|
||||||
except:
|
except:
|
||||||
logging.warning("Could not generate lora weights for key {}, is the weight difference a zero?".format(k))
|
logging.warning("Could not generate lora weights for key {}, is the weight difference a zero?".format(k))
|
||||||
|
elif lora_type == LORAType.FULL_DIFF:
|
||||||
|
output_sd["{}{}.diff".format(prefix_lora, k[len(prefix_model):-7])] = weight_diff.contiguous().half().cpu()
|
||||||
|
|
||||||
elif bias_diff and k.endswith(".bias"):
|
elif bias_diff and k.endswith(".bias"):
|
||||||
output_sd["{}{}.diff_b".format(prefix_lora, k[len(prefix_model):-5])] = sd[k].contiguous().half().cpu()
|
output_sd["{}{}.diff_b".format(prefix_lora, k[len(prefix_model):-5])] = sd[k].contiguous().half().cpu()
|
||||||
return output_sd
|
return output_sd
|
||||||
@ -65,7 +79,7 @@ class LoraSave:
|
|||||||
def INPUT_TYPES(s):
|
def INPUT_TYPES(s):
|
||||||
return {"required": {"filename_prefix": ("STRING", {"default": "loras/ComfyUI_extracted_lora"}),
|
return {"required": {"filename_prefix": ("STRING", {"default": "loras/ComfyUI_extracted_lora"}),
|
||||||
"rank": ("INT", {"default": 8, "min": 1, "max": 4096, "step": 1}),
|
"rank": ("INT", {"default": 8, "min": 1, "max": 4096, "step": 1}),
|
||||||
"lora_type": (["standard"],),
|
"lora_type": (tuple(LORA_TYPES.keys()),),
|
||||||
"bias_diff": ("BOOLEAN", {"default": True}),
|
"bias_diff": ("BOOLEAN", {"default": True}),
|
||||||
},
|
},
|
||||||
"optional": {"model_diff": ("MODEL",),
|
"optional": {"model_diff": ("MODEL",),
|
||||||
@ -81,13 +95,14 @@ class LoraSave:
|
|||||||
if model_diff is None and text_encoder_diff is None:
|
if model_diff is None and text_encoder_diff is None:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
lora_type = LORA_TYPES.get(lora_type)
|
||||||
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir)
|
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir)
|
||||||
|
|
||||||
output_sd = {}
|
output_sd = {}
|
||||||
if model_diff is not None:
|
if model_diff is not None:
|
||||||
output_sd = calc_lora_model(model_diff, rank, "diffusion_model.", "diffusion_model.", output_sd, bias_diff=bias_diff)
|
output_sd = calc_lora_model(model_diff, rank, "diffusion_model.", "diffusion_model.", output_sd, lora_type, bias_diff=bias_diff)
|
||||||
if text_encoder_diff is not None:
|
if text_encoder_diff is not None:
|
||||||
output_sd = calc_lora_model(text_encoder_diff.patcher, rank, "", "text_encoders.", output_sd, bias_diff=bias_diff)
|
output_sd = calc_lora_model(text_encoder_diff.patcher, rank, "", "text_encoders.", output_sd, lora_type, bias_diff=bias_diff)
|
||||||
|
|
||||||
output_checkpoint = f"{filename}_{counter:05}_.safetensors"
|
output_checkpoint = f"{filename}_{counter:05}_.safetensors"
|
||||||
output_checkpoint = os.path.join(full_output_folder, output_checkpoint)
|
output_checkpoint = os.path.join(full_output_folder, output_checkpoint)
|
||||||
|
Loading…
Reference in New Issue
Block a user