Skip to content

Commit 7fcce6f

Browse files
authored
feat: add paddings if LoRA ranks for q, k, v are different (#603)
* Add paddings if LoRA ranks for q, k, v are different * No need to create a list
1 parent f0c8391 commit 7fcce6f

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

nunchaku/lora/flux/compose.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import os
3030

3131
import torch
32+
import torch.nn.functional as F
3233
from safetensors.torch import save_file
3334

3435
from .diffusers_converter import to_diffusers
@@ -120,8 +121,14 @@ def compose_lora(
120121
.replace(".add_q_proj.", ".add_v_proj.")
121122
]
122123

123-
assert q_a.shape[0] == k_a.shape[0] == v_a.shape[0]
124-
assert q_b.shape[1] == k_b.shape[1] == v_b.shape[1]
124+
# Add paddings if their ranks are different
125+
max_rank = max(q_a.shape[0], k_a.shape[0], v_a.shape[0])
126+
q_a = F.pad(q_a, (0, 0, 0, max_rank - q_a.shape[0]))
127+
k_a = F.pad(k_a, (0, 0, 0, max_rank - k_a.shape[0]))
128+
v_a = F.pad(v_a, (0, 0, 0, max_rank - v_a.shape[0]))
129+
q_b = F.pad(q_b, (0, max_rank - q_b.shape[1]))
130+
k_b = F.pad(k_b, (0, max_rank - k_b.shape[1]))
131+
v_b = F.pad(v_b, (0, max_rank - v_b.shape[1]))
125132

126133
if torch.isclose(q_a, k_a).all() and torch.isclose(q_a, v_a).all():
127134
lora_a = q_a

0 commit comments

Comments
 (0)