Skip to content

Commit

Permalink
Merge pull request #2 from v0xie/network-oft-change-impl
Browse files Browse the repository at this point in the history
Use same updown implementation for LyCORIS OFT as kohya-ss OFT
  • Loading branch information
v0xie authored Nov 4, 2023
2 parents 1dd25be + bbf00a9 commit 7edd50f
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions extensions-builtin/Lora/network_oft.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ def __init__(self, net: network.Network, weights: network.NetworkWeights):
# kohya-ss
if "oft_blocks" in weights.w.keys():
self.is_kohya = True
self.oft_blocks = weights.w["oft_blocks"]
self.oft_blocks = weights.w["oft_blocks"] # (num_blocks, block_size, block_size)
self.alpha = weights.w["alpha"]
self.dim = self.oft_blocks.shape[0]
self.dim = self.oft_blocks.shape[0] # lora dim
#self.oft_blocks = rearrange(self.oft_blocks, 'k m ... -> (k m) ...')
elif "oft_diag" in weights.w.keys():
self.is_kohya = False
self.oft_blocks = weights.w["oft_diag"]
self.oft_blocks = weights.w["oft_diag"] # (num_blocks, block_size, block_size)

# alpha is rank if alpha is 0 or None
if self.alpha is None:
pass
Expand All @@ -51,12 +53,11 @@ def __init__(self, net: network.Network, weights: network.NetworkWeights):
raise ValueError("sd_module must be Linear or Conv")

if self.is_kohya:
self.num_blocks = self.dim
self.block_size = self.out_dim // self.num_blocks
self.constraint = self.alpha * self.out_dim
self.num_blocks, self.block_size = factorization(self.out_dim, self.dim)
else:
self.block_size, self.num_blocks = factorization(self.out_dim, self.dim)
self.constraint = None
self.block_size, self.num_blocks = factorization(self.out_dim, self.dim)

def merge_weight(self, R_weight, org_weight):
R_weight = R_weight.to(org_weight.device, dtype=org_weight.dtype)
Expand All @@ -77,7 +78,8 @@ def get_weight(self, oft_blocks, multiplier=None):
else:
new_norm_Q = norm_Q
block_Q = block_Q * ((new_norm_Q + 1e-8) / (norm_Q + 1e-8))
m_I = torch.eye(self.block_size, device=oft_blocks.device).unsqueeze(0).repeat(self.num_blocks, 1, 1)
m_I = torch.eye(self.num_blocks, device=oft_blocks.device).unsqueeze(0).repeat(self.block_size, 1, 1)
#m_I = torch.eye(self.block_size, device=oft_blocks.device).unsqueeze(0).repeat(self.num_blocks, 1, 1)
block_R = torch.matmul(m_I + block_Q, (m_I - block_Q).inverse())

block_R_weighted = multiplier * block_R + (1 - multiplier) * m_I
Expand All @@ -97,36 +99,44 @@ def calc_updown_kb(self, orig_weight, multiplier):
is_other_linear = type(self.sd_module) in [torch.nn.MultiheadAttention]

if not is_other_linear:
if is_other_linear and orig_weight.shape[0] != orig_weight.shape[1]:
orig_weight=orig_weight.permute(1, 0)
#if is_other_linear and orig_weight.shape[0] != orig_weight.shape[1]:
# orig_weight=orig_weight.permute(1, 0)

oft_blocks = self.oft_blocks.to(orig_weight.device, dtype=orig_weight.dtype)

# without this line the results are significantly worse / less accurate
oft_blocks = oft_blocks - oft_blocks.transpose(1, 2)

R = oft_blocks.to(orig_weight.device, dtype=orig_weight.dtype)
R = R * multiplier + torch.eye(self.block_size, device=orig_weight.device)

R = self.oft_blocks.to(orig_weight.device, dtype=orig_weight.dtype)
merged_weight = rearrange(orig_weight, '(k n) ... -> k n ...', k=self.num_blocks, n=self.block_size)
merged_weight = torch.einsum(
'k n m, k n ... -> k m ...',
R * multiplier + torch.eye(self.block_size, device=orig_weight.device),
R,
merged_weight
)
merged_weight = rearrange(merged_weight, 'k m ... -> (k m) ...')

if is_other_linear and orig_weight.shape[0] != orig_weight.shape[1]:
orig_weight=orig_weight.permute(1, 0)
#if is_other_linear and orig_weight.shape[0] != orig_weight.shape[1]:
# orig_weight=orig_weight.permute(1, 0)

updown = merged_weight.to(orig_weight.device, dtype=orig_weight.dtype) - orig_weight
output_shape = orig_weight.shape
else:
# FIXME: skip MultiheadAttention for now
#up = self.lin_module.weight.to(orig_weight.device, dtype=orig_weight.dtype)
updown = torch.zeros([orig_weight.shape[1], orig_weight.shape[1]], device=orig_weight.device, dtype=orig_weight.dtype)
output_shape = (orig_weight.shape[1], orig_weight.shape[1])

return self.finalize_updown(updown, orig_weight, output_shape)

def calc_updown(self, orig_weight):
multiplier = self.multiplier() * self.calc_scale()
if self.is_kohya:
return self.calc_updown_kohya(orig_weight, multiplier)
else:
return self.calc_updown_kb(orig_weight, multiplier)
#if self.is_kohya:
# return self.calc_updown_kohya(orig_weight, multiplier)
#else:
return self.calc_updown_kb(orig_weight, multiplier)

# override to remove the multiplier/scale factor; it's already multiplied in get_weight
def finalize_updown(self, updown, orig_weight, output_shape, ex_bias=None):
Expand Down

0 comments on commit 7edd50f

Please sign in to comment.