Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
KohakuBlueleaf committed Mar 7, 2024
1 parent 58f7410 commit 12bcacf
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions extensions-builtin/Lora/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def __init__(self, net: Network, weights: NetworkWeights):
self.alpha = weights.w["alpha"].item() if "alpha" in weights.w else None
self.scale = weights.w["scale"].item() if "scale" in weights.w else None

self.dora_scale = weights.w["dora_scale"] if "dora_scale" in weights.w else None
self.dora_mean_dim = tuple(i for i in range(len(self.shape)) if i != 1)

def multiplier(self):
if 'transformer' in self.sd_key[:20]:
return self.network.te_multiplier
Expand All @@ -160,6 +163,15 @@ def calc_scale(self):

return 1.0

def apply_weight_decompose(self, updown, orig_weight):
orig_weight = orig_weight.to(updown)
merged_scale1 = updown + orig_weight
dora_merged = (
merged_scale1 / merged_scale1(dim=self.dora_mean_dim, keepdim=True) * self.dora_scale
)
final_updown = dora_merged - orig_weight
return final_updown

def finalize_updown(self, updown, orig_weight, output_shape, ex_bias=None):
if self.bias is not None:
updown = updown.reshape(self.bias.shape)
Expand All @@ -175,6 +187,9 @@ def finalize_updown(self, updown, orig_weight, output_shape, ex_bias=None):
if ex_bias is not None:
ex_bias = ex_bias * self.multiplier()

if self.dora_scale is not None:
updown = self.apply_weight_decompose(updown, orig_weight)

return updown * self.calc_scale() * self.multiplier(), ex_bias

def calc_updown(self, target):
Expand Down

0 comments on commit 12bcacf

Please sign in to comment.