Upload create_lora.py with huggingface_hub
Browse files- create_lora.py +80 -0
create_lora.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Adapted from
|
| 3 |
+
https://github.com/Stability-AI/stability-ComfyUI-nodes/blob/001154622564b17223ce0191803c5fff7b87146c/control_lora_create.py
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from diffusers import CogVideoXTransformer3DModel
|
| 7 |
+
from tqdm.auto import tqdm
|
| 8 |
+
from safetensors.torch import save_file
|
| 9 |
+
import torch
|
| 10 |
+
|
| 11 |
+
RANK = 64
|
| 12 |
+
CLAMP_QUANTILE = 0.99
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Comes from
|
| 16 |
+
# https://github.com/Stability-AI/stability-ComfyUI-nodes/blob/001154622564b17223ce0191803c5fff7b87146c/control_lora_create.py#L9
|
| 17 |
+
def extract_lora(diff, rank):
|
| 18 |
+
if torch.cuda.is_available():
|
| 19 |
+
diff = diff.to("cuda")
|
| 20 |
+
|
| 21 |
+
is_conv2d = (len(diff.shape) == 4)
|
| 22 |
+
kernel_size = None if not is_conv2d else diff.size()[2:4]
|
| 23 |
+
is_conv2d_3x3 = is_conv2d and kernel_size != (1, 1)
|
| 24 |
+
out_dim, in_dim = diff.size()[0:2]
|
| 25 |
+
rank = min(rank, in_dim, out_dim)
|
| 26 |
+
|
| 27 |
+
if is_conv2d:
|
| 28 |
+
if is_conv2d_3x3:
|
| 29 |
+
diff = diff.flatten(start_dim=1)
|
| 30 |
+
else:
|
| 31 |
+
diff = diff.squeeze()
|
| 32 |
+
|
| 33 |
+
U, S, Vh = torch.linalg.svd(diff.float())
|
| 34 |
+
U = U[:, :rank]
|
| 35 |
+
S = S[:rank]
|
| 36 |
+
U = U @ torch.diag(S)
|
| 37 |
+
Vh = Vh[:rank, :]
|
| 38 |
+
|
| 39 |
+
dist = torch.cat([U.flatten(), Vh.flatten()])
|
| 40 |
+
hi_val = torch.quantile(dist, CLAMP_QUANTILE)
|
| 41 |
+
low_val = -hi_val
|
| 42 |
+
|
| 43 |
+
U = U.clamp(low_val, hi_val)
|
| 44 |
+
Vh = Vh.clamp(low_val, hi_val)
|
| 45 |
+
if is_conv2d:
|
| 46 |
+
U = U.reshape(out_dim, rank, 1, 1)
|
| 47 |
+
Vh = Vh.reshape(rank, in_dim, kernel_size[0], kernel_size[1])
|
| 48 |
+
return (U.cpu(), Vh.cpu())
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
transformer_finetuned = CogVideoXTransformer3DModel.from_pretrained(
|
| 52 |
+
"cogvideox-cakeify", subfolder="transformer", torch_dtype=torch.bfloat16
|
| 53 |
+
)
|
| 54 |
+
state_dict_ft = transformer_finetuned.state_dict()
|
| 55 |
+
|
| 56 |
+
transformer = CogVideoXTransformer3DModel.from_pretrained(
|
| 57 |
+
"THUDM/CogVideoX-5b", subfolder="transformer", torch_dtype=torch.bfloat16
|
| 58 |
+
)
|
| 59 |
+
state_dict = transformer.state_dict()
|
| 60 |
+
output_dict = {}
|
| 61 |
+
|
| 62 |
+
for k in tqdm(state_dict, desc="Extracting LoRA..."):
|
| 63 |
+
original_param = state_dict[k]
|
| 64 |
+
finetuned_param = state_dict_ft[k]
|
| 65 |
+
if len(original_param.shape) >= 2:
|
| 66 |
+
diff = finetuned_param.float() - original_param.float()
|
| 67 |
+
out = extract_lora(diff, RANK)
|
| 68 |
+
name = k
|
| 69 |
+
|
| 70 |
+
if name.endswith(".weight"):
|
| 71 |
+
name = name[:-len(".weight")]
|
| 72 |
+
down_key = "{}.lora_A.weight".format(name)
|
| 73 |
+
up_key = "{}.lora_B.weight".format(name)
|
| 74 |
+
|
| 75 |
+
output_dict[up_key] = out[0].contiguous().to(finetuned_param.dtype)
|
| 76 |
+
output_dict[down_key] = out[1].contiguous().to(finetuned_param.dtype)
|
| 77 |
+
|
| 78 |
+
output_dict = {f"transformer.{k}": v for k, v in output_dict.items()}
|
| 79 |
+
save_file(output_dict, "extracted_cakeify_lora_64.safetensors")
|
| 80 |
+
print(f"LoRA saved and it contains {len(output_dict)} keys.")
|