aboutsummaryrefslogtreecommitdiff
path: root/extensions-builtin/Lora/network_glora.py
diff options
context:
space:
mode:
authorAUTOMATIC1111 <16777216c@gmail.com>2023-10-14 07:52:52 +0300
committerGitHub <noreply@github.com>2023-10-14 07:52:52 +0300
commit26500b8c1bd0986882afb0f7e5cbc556b87a782d (patch)
treef74622f0017c1f7719285e4bcaa5886aaad7ccf8 /extensions-builtin/Lora/network_glora.py
parenta109c7aeb8871fe0ae201794f140f8f2e9b5c3ac (diff)
parent906d1179e9a333eeb0f12a95b592dd5b44eb0aaa (diff)
Merge pull request #13610 from v0xie/network-glora
Support inference with LyCORIS GLora networks
Diffstat (limited to 'extensions-builtin/Lora/network_glora.py')
-rw-r--r--extensions-builtin/Lora/network_glora.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/extensions-builtin/Lora/network_glora.py b/extensions-builtin/Lora/network_glora.py
new file mode 100644
index 00000000..492d4870
--- /dev/null
+++ b/extensions-builtin/Lora/network_glora.py
@@ -0,0 +1,33 @@
+
+import network
+
+class ModuleTypeGLora(network.ModuleType):
+ def create_module(self, net: network.Network, weights: network.NetworkWeights):
+ if all(x in weights.w for x in ["a1.weight", "a2.weight", "alpha", "b1.weight", "b2.weight"]):
+ return NetworkModuleGLora(net, weights)
+
+ return None
+
+# adapted from https://github.com/KohakuBlueleaf/LyCORIS
+class NetworkModuleGLora(network.NetworkModule):
+ def __init__(self, net: network.Network, weights: network.NetworkWeights):
+ super().__init__(net, weights)
+
+ if hasattr(self.sd_module, 'weight'):
+ self.shape = self.sd_module.weight.shape
+
+ self.w1a = weights.w["a1.weight"]
+ self.w1b = weights.w["b1.weight"]
+ self.w2a = weights.w["a2.weight"]
+ self.w2b = weights.w["b2.weight"]
+
+ def calc_updown(self, orig_weight):
+ w1a = self.w1a.to(orig_weight.device, dtype=orig_weight.dtype)
+ w1b = self.w1b.to(orig_weight.device, dtype=orig_weight.dtype)
+ w2a = self.w2a.to(orig_weight.device, dtype=orig_weight.dtype)
+ w2b = self.w2b.to(orig_weight.device, dtype=orig_weight.dtype)
+
+ output_shape = [w1a.size(0), w1b.size(1)]
+ updown = ((w2b @ w1b) + ((orig_weight @ w2a) @ w1a))
+
+ return self.finalize_updown(updown, orig_weight, output_shape)