Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(patches): support conv-shaped LoHa factors (#10048)

Guishi Wangbe25a5bb67f7
diff --git a/invokeai/backend/patches/layers/loha_layer.py b/invokeai/backend/patches/layers/loha_layer.py
index d337a31883..df4a70adb7 100644
--- a/invokeai/backend/patches/layers/loha_layer.py
+++ b/invokeai/backend/patches/layers/loha_layer.py
@@ -77,7 +77,9 @@ class LoHALayer(LoRALayerBase):
def get_weight(self, orig_weight: torch.Tensor) -> torch.Tensor:
if self.t1 is None:
- weight: torch.Tensor = (self.w1_a @ self.w1_b) * (self.w2_a @ self.w2_b)
+ w1 = self.w1_a.reshape(self.w1_a.shape[0], -1) @ self.w1_b.reshape(self.w1_b.shape[0], -1)
+ w2 = self.w2_a.reshape(self.w2_a.shape[0], -1) @ self.w2_b.reshape(self.w2_b.shape[0], -1)
+ weight: torch.Tensor = w1 * w2
else:
rebuild1 = torch.einsum("i j k l, j r, i p -> p r k l", self.t1, self.w1_b, self.w1_a)
rebuild2 = torch.einsum("i j k l, j r, i p -> p r k l", self.t2, self.w2_b, self.w2_a)
diff --git a/tests/backend/patches/layers/test_loha_layer.py b/tests/backend/patches/layers/test_loha_layer.py
new file mode 100644
index 0000000000..623131533d
--- /dev/null
+++ b/tests/backend/patches/layers/test_loha_layer.py
@@ -0,0 +1,28 @@
+import torch
+
+from invokeai.backend.patches.layers.loha_layer import LoHALayer
+
+
+def test_conv_factors_are_flattened_before_multiplication() -> None:
+ torch.manual_seed(0)
+ w1_a = torch.randn(8, 2, 1, 1)
+ w1_b = torch.randn(2, 4, 3, 3)
+ w2_a = torch.randn(8, 2, 1, 1)
+ w2_b = torch.randn(2, 4, 3, 3)
+ layer = LoHALayer(
+ w1_a=w1_a,
+ w1_b=w1_b,
+ w2_a=w2_a,
+ w2_b=w2_b,
+ t1=None,
+ t2=None,
+ alpha=2.0,
+ bias=None,
+ )
+
+ parameters = layer.get_parameters({"weight": torch.zeros(8, 4, 3, 3)}, weight=1.0)
+
+ expected = ((w1_a.reshape(8, 2) @ w1_b.reshape(2, 36)) * (w2_a.reshape(8, 2) @ w2_b.reshape(2, 36))).reshape(
+ 8, 4, 3, 3
+ )
+ torch.testing.assert_close(parameters["weight"], expected)