Files
city96-ComfyUI-GGUF/lora.py
T
City fed8ff1e70 Add LoRA support
Initial, will need to change weight function
2024-08-17 05:28:46 +02:00

38 lines
1.3 KiB
Python

# (c) City96 || Apache-2.0 (apache.org/licenses/LICENSE-2.0)
import gguf
import torch
import types
import logging
import comfy.lora
from .ops import GGMLTensor
def load_lora_gguf(model, lora, strength):
key_map = comfy.lora.model_lora_keys_unet(model.model)
loaded = comfy.lora.load_lora(lora, key_map)
new = model.clone()
k = new.add_patches(loaded, strength)
if not getattr(new, "custom_calc_weight", False):
new.calculate_weight_orig = new.calculate_weight
new.calculate_weight = types.MethodType(calculate_weight, new)
new.custom_calc_weight = True
for key in [x for x in loaded if x not in k]:
logging.warning("NOT LOADED {}".format(key))
return (new)
def calculate_weight(self, patches, weight, key):
if isinstance(weight, GGMLTensor):
qtype = weight.tensor_type
# TODO: don't even store these in a custom format
if qtype in [gguf.GGMLQuantizationType.F32, gguf.GGMLQuantizationType.F16]:
return self.calculate_weight_orig(patches, weight, key)
else:
weight.patches.append((self.calculate_weight_orig, patches, key))
return weight
else:
return self.calculate_weight_orig(patches, weight, key)