lycoris-lora 3.0.0.dev1__tar.gz → 3.0.0.dev3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {lycoris_lora-3.0.0.dev1/lycoris_lora.egg-info → lycoris_lora-3.0.0.dev3}/PKG-INFO +1 -4
  2. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/__init__.py +4 -1
  3. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/functional/general.py +2 -0
  4. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/base.py +38 -0
  5. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/boft.py +41 -31
  6. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/diag_oft.py +41 -25
  7. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/full.py +30 -15
  8. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/glora.py +40 -25
  9. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/locon.py +44 -28
  10. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/loha.py +44 -29
  11. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/lokr.py +49 -27
  12. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3/lycoris_lora.egg-info}/PKG-INFO +1 -4
  13. lycoris_lora-3.0.0.dev3/lycoris_lora.egg-info/requires.txt +3 -0
  14. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/setup.py +1 -4
  15. lycoris_lora-3.0.0.dev1/lycoris_lora.egg-info/requires.txt +0 -6
  16. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/LICENSE.md +0 -0
  17. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/README.md +0 -0
  18. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/config.py +0 -0
  19. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/functional/__init__.py +0 -0
  20. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/functional/locon.py +0 -0
  21. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/functional/loha.py +0 -0
  22. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/kohya.py +0 -0
  23. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/logging.py +0 -0
  24. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/__init__.py +0 -0
  25. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/dylora.py +0 -0
  26. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/ia3.py +0 -0
  27. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/modules/norms.py +0 -0
  28. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/utils/__init__.py +0 -0
  29. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/utils/bnb.py +0 -0
  30. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/utils/logger.py +0 -0
  31. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/utils/preset.py +0 -0
  32. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/utils/xformers_utils.py +0 -0
  33. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris/wrapper.py +0 -0
  34. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris_lora.egg-info/SOURCES.txt +0 -0
  35. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris_lora.egg-info/dependency_links.txt +0 -0
  36. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris_lora.egg-info/not-zip-safe +0 -0
  37. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/lycoris_lora.egg-info/top_level.txt +0 -0
  38. {lycoris_lora-3.0.0.dev1 → lycoris_lora-3.0.0.dev3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris_lora
3
- Version: 3.0.0.dev1
3
+ Version: 3.0.0.dev3
4
4
  Summary: Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion
5
5
  Home-page: https://github.com/KohakuBlueleaf/LyCORIS
6
6
  Author: Shih-Ying Yeh(KohakuBlueLeaf), Yu-Guan Hsieh, Zhidong Gao
@@ -8,8 +8,5 @@ Author-email: apolloyeh0123@gmail.com
8
8
  Requires-Python: >=3.10
9
9
  License-File: LICENSE.md
10
10
  Requires-Dist: torch
11
- Requires-Dist: safetensors
12
- Requires-Dist: diffusers
13
- Requires-Dist: transformers
14
11
  Requires-Dist: einops
15
12
  Requires-Dist: toml
@@ -1,5 +1,8 @@
1
+ try:
2
+ from . import kohya
3
+ except:
4
+ pass
1
5
  from . import (
2
- kohya,
3
6
  modules,
4
7
  utils,
5
8
  )
@@ -59,6 +59,8 @@ def power2factorization(dimension: int, factor: int = -1) -> tuple[int, int]:
59
59
  n = 2**p
60
60
  m*n = dim
61
61
  """
62
+ if factor == -1:
63
+ factor = dimension
62
64
 
63
65
  # Find the first solution and check if it is even doable
64
66
  m = n = 0
@@ -3,6 +3,7 @@ from collections import OrderedDict
3
3
  import torch
4
4
  import torch.nn as nn
5
5
  import torch.nn.functional as F
6
+ import torch.nn.utils.parametrize as parametrize
6
7
 
7
8
  from ..utils.bnb import QuantLinears, log_bypass
8
9
 
@@ -181,6 +182,38 @@ class LycorisBaseModule(ModuleCustomSD):
181
182
  self.org_forward = org_module.forward
182
183
  self.org_module = [org_module]
183
184
 
185
+ @classmethod
186
+ def parametrize(cls, org_module, attr, *args, **kwargs):
187
+ target_param = getattr(org_module, attr)
188
+ kwargs["bypass_mode"] = False
189
+ if target_param.dim() == 2:
190
+ proxy_module = nn.Linear(
191
+ target_param.shape[0], target_param.shape[1], bias=False
192
+ )
193
+ proxy_module.weight = target_param
194
+ elif target_param.dim() > 2:
195
+ module_type = [
196
+ None,
197
+ None,
198
+ None,
199
+ nn.Conv1d,
200
+ nn.Conv2d,
201
+ nn.Conv3d,
202
+ None,
203
+ None,
204
+ ][target_param.dim()]
205
+ proxy_module = module_type(
206
+ target_param.shape[0],
207
+ target_param.shape[1],
208
+ *target_param.shape[2:],
209
+ )
210
+ proxy_module.weight = target_param
211
+ module_obj = cls("", proxy_module, *args, **kwargs)
212
+ module_obj.forward = module_obj.parametrize_forward
213
+ module_obj.to(target_param.device)
214
+ parametrize.register_parametrization(org_module, attr, module_obj)
215
+ return module_obj
216
+
184
217
  @property
185
218
  def dtype(self):
186
219
  return self.dtype_tensor.dtype
@@ -231,5 +264,10 @@ class LycorisBaseModule(ModuleCustomSD):
231
264
  def bypass_forward(self, x, scale=1):
232
265
  raise NotImplementedError
233
266
 
267
+ def parametrize_forward(self, x: torch.Tensor, *args, **kwargs):
268
+ return self.get_merged_weight(
269
+ multiplier=self.multiplier, shape=x.shape, device=x.device
270
+ )[0].to(x.dtype)
271
+
234
272
  def forward(self, *args, **kwargs):
235
273
  raise NotImplementedError
@@ -136,7 +136,7 @@ class ButterflyOFTModule(LycorisBaseModule):
136
136
  diff = self.make_weight(scale=multiplier, device=device, diff=True)
137
137
  if shape is not None:
138
138
  diff = diff.view(shape)
139
- return diff
139
+ return diff, None
140
140
 
141
141
  def get_merged_weight(self, multiplier=1, shape=None, device=None):
142
142
  diff = self.make_weight(scale=multiplier, device=device)
@@ -200,33 +200,43 @@ class ButterflyOFTModule(LycorisBaseModule):
200
200
 
201
201
 
202
202
  if __name__ == "__main__":
203
- base = nn.Linear(128, 128).cuda()
204
- boft = ButterflyOFTModule(
205
- "test", base, 1, 4, 1, weight_decompose=True, factor=8
206
- ).cuda()
207
- print(boft)
208
- test_input = torch.randn(1, 77, 128).cuda()
209
- test_output = boft(test_input)
210
- torch.sum(test_output).backward()
211
- print(test_output.shape)
212
-
213
- base_4bit = LinearNF4(128, 128)
214
- base_4bit.load_state_dict(base.state_dict())
215
- base_4bit.cuda()
216
- qboft = ButterflyOFTModule(
217
- "test", base_4bit, 1, 4, 1, weight_decompose=False
218
- ).cuda()
219
- print(qboft)
220
- test_output = qboft(test_input)
221
- torch.sum(test_output).backward()
222
- print(test_output.shape)
223
-
224
- base = nn.Conv2d(128, 128, 3, 1, 1)
225
- boft = ButterflyOFTModule(
226
- "test", base, 1, 4, 1, weight_decompose=True, use_tucker=True
227
- )
228
- print(boft)
229
- test_input = torch.randn(1, 128, 16, 16)
230
- test_output = boft(test_input)
231
- torch.sum(test_output).backward()
232
- print(test_output.shape)
203
+ device = torch.device("cuda")
204
+ module = ButterflyOFTModule
205
+ with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
206
+ base = nn.Linear(128, 128).to(device).half()
207
+ net = module("test", base, 1, 4, 1, weight_decompose=True).to(device)
208
+ print(net)
209
+ test_input = torch.randn(1, 128).to(device).half()
210
+ test_output = net(test_input)
211
+ torch.sum(test_output).backward()
212
+ print(test_output.shape)
213
+
214
+ base_4bit = LinearNF4(128, 128, device="cuda")
215
+ base_4bit.load_state_dict(base.state_dict())
216
+ base_4bit.to(device)
217
+ qnet = module("test", base_4bit, 1, 4, 1, weight_decompose=False).to(device)
218
+ print(qnet)
219
+ test_input = torch.randn(1, 128).to(device).half()
220
+ test_output = qnet(test_input)
221
+ torch.sum(test_output).backward()
222
+ print(test_output.shape)
223
+
224
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
225
+ net = module("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True).to(
226
+ device
227
+ )
228
+ print(net)
229
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
230
+ test_output = net(test_input)
231
+ torch.sum(test_output).backward()
232
+ print(test_output.shape)
233
+
234
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
235
+ net = module.parametrize(
236
+ base, "weight", 1, 4, 1, weight_decompose=True, use_tucker=True
237
+ ).to(device)
238
+ print(base)
239
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
240
+ test_output = base(test_input)
241
+ torch.sum(test_output).backward()
242
+ print(test_output.shape)
@@ -119,7 +119,7 @@ class DiagOFTModule(LycorisBaseModule):
119
119
  diff = self.make_weight(scale=multiplier, device=device, diff=True)
120
120
  if shape is not None:
121
121
  diff = diff.view(shape)
122
- return diff
122
+ return diff, None
123
123
 
124
124
  def get_merged_weight(self, multiplier=1, shape=None, device=None):
125
125
  diff = self.make_weight(scale=multiplier, device=device)
@@ -185,27 +185,43 @@ class DiagOFTModule(LycorisBaseModule):
185
185
 
186
186
 
187
187
  if __name__ == "__main__":
188
- base = nn.Linear(128, 128).cuda()
189
- doft = DiagOFTModule("test", base, 1, 4, 1, weight_decompose=True, factor=8).cuda()
190
- print(doft)
191
- test_input = torch.randn(1, 77, 128).cuda()
192
- test_output = doft(test_input)
193
- torch.sum(test_output).backward()
194
- print(test_output.shape)
195
-
196
- base_4bit = LinearNF4(128, 128)
197
- base_4bit.load_state_dict(base.state_dict())
198
- base_4bit.cuda()
199
- qdoft = DiagOFTModule("test", base_4bit, 1, 4, 1, weight_decompose=False).cuda()
200
- print(qdoft)
201
- test_output = qdoft(test_input)
202
- torch.sum(test_output).backward()
203
- print(test_output.shape)
204
-
205
- base = nn.Conv2d(128, 128, 3, 1, 1)
206
- doft = DiagOFTModule("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True)
207
- print(doft)
208
- test_input = torch.randn(1, 128, 16, 16)
209
- test_output = doft(test_input)
210
- torch.sum(test_output).backward()
211
- print(test_output.shape)
188
+ device = torch.device("cuda")
189
+ module = DiagOFTModule
190
+ with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
191
+ base = nn.Linear(128, 128).to(device).half()
192
+ net = module("test", base, 1, 4, 1, weight_decompose=True).to(device)
193
+ print(net)
194
+ test_input = torch.randn(1, 128).to(device).half()
195
+ test_output = net(test_input)
196
+ torch.sum(test_output).backward()
197
+ print(test_output.shape)
198
+
199
+ base_4bit = LinearNF4(128, 128, device="cuda")
200
+ base_4bit.load_state_dict(base.state_dict())
201
+ base_4bit.to(device)
202
+ qnet = module("test", base_4bit, 1, 4, 1, weight_decompose=False).to(device)
203
+ print(qnet)
204
+ test_input = torch.randn(1, 128).to(device).half()
205
+ test_output = qnet(test_input)
206
+ torch.sum(test_output).backward()
207
+ print(test_output.shape)
208
+
209
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
210
+ net = module("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True).to(
211
+ device
212
+ )
213
+ print(net)
214
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
215
+ test_output = net(test_input)
216
+ torch.sum(test_output).backward()
217
+ print(test_output.shape)
218
+
219
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
220
+ net = module.parametrize(
221
+ base, "weight", 1, 4, 1, weight_decompose=True, use_tucker=True
222
+ ).to(device)
223
+ print(base)
224
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
225
+ test_output = base(test_input)
226
+ torch.sum(test_output).backward()
227
+ print(test_output.shape)
@@ -158,18 +158,33 @@ class FullModule(LycorisBaseModule):
158
158
 
159
159
 
160
160
  if __name__ == "__main__":
161
- base = nn.Linear(128, 128).cuda()
162
- full = FullModule("test", base, 1, 4, 1, weight_decompose=True, factor=8).cuda()
163
- print(full)
164
- test_input = torch.randn(1, 77, 128).cuda()
165
- test_output = full(test_input)
166
- torch.sum(test_output).backward()
167
- print(test_output.shape)
168
-
169
- base = nn.Conv2d(128, 128, 3, 1, 1)
170
- full = FullModule("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True)
171
- print(full)
172
- test_input = torch.randn(1, 128, 16, 16)
173
- test_output = full(test_input)
174
- torch.sum(test_output).backward()
175
- print(test_output.shape)
161
+ device = torch.device("cuda")
162
+ module = FullModule
163
+ with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
164
+ base = nn.Linear(128, 128).to(device).half()
165
+ net = module("test", base, 1, 4, 1, weight_decompose=True).to(device)
166
+ print(net)
167
+ test_input = torch.randn(1, 128).to(device).half()
168
+ test_output = net(test_input)
169
+ torch.sum(test_output).backward()
170
+ print(test_output.shape)
171
+
172
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
173
+ net = module("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True).to(
174
+ device
175
+ )
176
+ print(net)
177
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
178
+ test_output = net(test_input)
179
+ torch.sum(test_output).backward()
180
+ print(test_output.shape)
181
+
182
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
183
+ net = module.parametrize(
184
+ base, "weight", 1, 4, 1, weight_decompose=True, use_tucker=True
185
+ ).to(device)
186
+ print(base)
187
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
188
+ test_output = base(test_input)
189
+ torch.sum(test_output).backward()
190
+ print(test_output.shape)
@@ -169,28 +169,43 @@ class GLoRAModule(LycorisBaseModule):
169
169
 
170
170
 
171
171
  if __name__ == "__main__":
172
- base = nn.Linear(128, 128).cuda()
173
- glora = GLoRAModule("test", base, 1, 4, 1, weight_decompose=True).cuda()
174
- print(glora)
175
- test_input = torch.randn(1, 128).cuda()
176
- test_output = glora(test_input)
177
- torch.sum(test_output).backward()
178
- print(test_output.shape)
179
-
180
- base_4bit = LinearNF4(128, 128)
181
- base_4bit.load_state_dict(base.state_dict())
182
- base_4bit.cuda()
183
- qglora = GLoRAModule("test", base_4bit, 1, 4, 1, weight_decompose=False).cuda()
184
- print(qglora)
185
- test_input = torch.randn(1, 128).cuda()
186
- test_output = qglora(test_input)
187
- torch.sum(test_output).backward()
188
- print(test_output.shape)
189
-
190
- base = nn.Conv2d(128, 128, 3, 1, 1)
191
- glora = GLoRAModule("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True)
192
- print(glora)
193
- test_input = torch.randn(1, 128, 16, 16)
194
- test_output = glora(test_input)
195
- torch.sum(test_output).backward()
196
- print(test_output.shape)
172
+ device = torch.device("cuda")
173
+ module = GLoRAModule
174
+ with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
175
+ base = nn.Linear(128, 128).to(device).half()
176
+ net = module("test", base, 1, 4, 1, weight_decompose=True).to(device)
177
+ print(net)
178
+ test_input = torch.randn(1, 128).to(device).half()
179
+ test_output = net(test_input)
180
+ torch.sum(test_output).backward()
181
+ print(test_output.shape)
182
+
183
+ base_4bit = LinearNF4(128, 128, device="cuda")
184
+ base_4bit.load_state_dict(base.state_dict())
185
+ base_4bit.to(device)
186
+ qnet = module("test", base_4bit, 1, 4, 1, weight_decompose=False).to(device)
187
+ print(qnet)
188
+ test_input = torch.randn(1, 128).to(device).half()
189
+ test_output = qnet(test_input)
190
+ torch.sum(test_output).backward()
191
+ print(test_output.shape)
192
+
193
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
194
+ net = module("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True).to(
195
+ device
196
+ )
197
+ print(net)
198
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
199
+ test_output = net(test_input)
200
+ torch.sum(test_output).backward()
201
+ print(test_output.shape)
202
+
203
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
204
+ net = module.parametrize(
205
+ base, "weight", 1, 4, 1, weight_decompose=True, use_tucker=True
206
+ ).to(device)
207
+ print(base)
208
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
209
+ test_output = base(test_input)
210
+ torch.sum(test_output).backward()
211
+ print(test_output.shape)
@@ -183,11 +183,12 @@ class LoConModule(LycorisBaseModule):
183
183
  diff = diff.view(shape)
184
184
  if device is not None:
185
185
  diff = diff.to(device)
186
- return diff
186
+ return diff, None
187
187
 
188
188
  def get_merged_weight(self, multiplier=1, shape=None, device=None):
189
- merged = self.org_module[0].weight.data + self.get_diff_weight(
190
- multiplier=multiplier, shape=shape, device=device
189
+ merged = (
190
+ self.org_module[0].weight.data
191
+ + self.get_diff_weight(multiplier=multiplier, shape=shape, device=device)[0]
191
192
  )
192
193
  if self.wd:
193
194
  merged = self.apply_weight_decompose(merged)
@@ -277,28 +278,43 @@ class LoConModule(LycorisBaseModule):
277
278
 
278
279
 
279
280
  if __name__ == "__main__":
280
- base = nn.Linear(128, 128).cuda()
281
- locon = LoConModule("test", base, 1, 4, 1, weight_decompose=True).cuda()
282
- print(locon)
283
- test_input = torch.randn(1, 128).cuda()
284
- test_output = locon(test_input)
285
- torch.sum(test_output).backward()
286
- print(test_output.shape)
287
-
288
- base_4bit = LinearNF4(128, 128)
289
- base_4bit.load_state_dict(base.state_dict())
290
- base_4bit.cuda()
291
- qlocon = LoConModule("test", base_4bit, 1, 4, 1, weight_decompose=False).cuda()
292
- print(qlocon)
293
- test_input = torch.randn(1, 128).cuda()
294
- test_output = qlocon(test_input)
295
- torch.sum(test_output).backward()
296
- print(test_output.shape)
297
-
298
- base = nn.Conv2d(128, 128, 3, 1, 1)
299
- locon = LoConModule("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True)
300
- print(locon)
301
- test_input = torch.randn(1, 128, 16, 16)
302
- test_output = locon(test_input)
303
- torch.sum(test_output).backward()
304
- print(test_output.shape)
281
+ device = torch.device("cuda")
282
+ module = LoConModule
283
+ with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
284
+ base = nn.Linear(128, 128).to(device).half()
285
+ net = module("test", base, 1, 4, 1, weight_decompose=True).to(device)
286
+ print(net)
287
+ test_input = torch.randn(1, 128).to(device).half()
288
+ test_output = net(test_input)
289
+ torch.sum(test_output).backward()
290
+ print(test_output.shape)
291
+
292
+ base_4bit = LinearNF4(128, 128, device="cuda")
293
+ base_4bit.load_state_dict(base.state_dict())
294
+ base_4bit.to(device)
295
+ qnet = module("test", base_4bit, 1, 4, 1, weight_decompose=False).to(device)
296
+ print(qnet)
297
+ test_input = torch.randn(1, 128).to(device).half()
298
+ test_output = qnet(test_input)
299
+ torch.sum(test_output).backward()
300
+ print(test_output.shape)
301
+
302
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
303
+ net = module("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True).to(
304
+ device
305
+ )
306
+ print(net)
307
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
308
+ test_output = net(test_input)
309
+ torch.sum(test_output).backward()
310
+ print(test_output.shape)
311
+
312
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
313
+ net = module.parametrize(
314
+ base, "weight", 1, 4, 1, weight_decompose=True, use_tucker=True
315
+ )
316
+ print(base)
317
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
318
+ test_output = base(test_input)
319
+ torch.sum(test_output).backward()
320
+ print(test_output.shape)
@@ -181,15 +181,16 @@ class LohaModule(LycorisBaseModule):
181
181
  diff = self.get_weight(shape) * scale
182
182
  if device is not None:
183
183
  diff = diff.to(device)
184
- return diff
184
+ return diff, None
185
185
 
186
186
  def get_merged_weight(self, multiplier=1, shape=None, device=None):
187
- merged = self.org_module[0].weight.data + self.get_diff_weight(
188
- multiplier=multiplier, shape=shape, device=device
187
+ merged = (
188
+ self.org_module[0].weight.data
189
+ + self.get_diff_weight(multiplier=multiplier, shape=shape, device=device)[0]
189
190
  )
190
191
  if self.wd:
191
192
  merged = self.apply_weight_decompose(merged)
192
- return merged
193
+ return merged, None
193
194
 
194
195
  def apply_weight_decompose(self, weight):
195
196
  weight = weight.to(self.dora_scale.dtype)
@@ -269,28 +270,42 @@ class LohaModule(LycorisBaseModule):
269
270
 
270
271
  if __name__ == "__main__":
271
272
  device = torch.device("cuda")
272
- base = nn.Linear(128, 128).to(device)
273
- loha = LohaModule("test", base, 1, 4, 1, weight_decompose=True).to(device)
274
- print(loha)
275
- test_input = torch.randn(1, 128).to(device)
276
- test_output = loha(test_input)
277
- torch.sum(test_output).backward()
278
- print(test_output.shape)
279
-
280
- base_4bit = LinearNF4(128, 128)
281
- base_4bit.load_state_dict(base.state_dict())
282
- base_4bit.to(device)
283
- qloha = LohaModule("test", base_4bit, 1, 4, 1, weight_decompose=False).to(device)
284
- print(qloha)
285
- test_input = torch.randn(1, 128).to(device)
286
- test_output = qloha(test_input)
287
- torch.sum(test_output).backward()
288
- print(test_output.shape)
289
-
290
- base = nn.Conv2d(128, 128, 3, 1, 1)
291
- loha = LohaModule("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True)
292
- print(loha)
293
- test_input = torch.randn(1, 128, 16, 16)
294
- test_output = loha(test_input)
295
- torch.sum(test_output).backward()
296
- print(test_output.shape)
273
+ module = LohaModule
274
+ with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
275
+ base = nn.Linear(128, 128).to(device).half()
276
+ net = module("test", base, 1, 4, 1, weight_decompose=True).to(device)
277
+ print(net)
278
+ test_input = torch.randn(1, 128).to(device).half()
279
+ test_output = net(test_input)
280
+ torch.sum(test_output).backward()
281
+ print(test_output.shape)
282
+
283
+ base_4bit = LinearNF4(128, 128, device="cuda")
284
+ base_4bit.load_state_dict(base.state_dict())
285
+ base_4bit.to(device)
286
+ qnet = module("test", base_4bit, 1, 4, 1, weight_decompose=False).to(device)
287
+ print(qnet)
288
+ test_input = torch.randn(1, 128).to(device).half()
289
+ test_output = qnet(test_input)
290
+ torch.sum(test_output).backward()
291
+ print(test_output.shape)
292
+
293
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
294
+ net = module("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True).to(
295
+ device
296
+ )
297
+ print(net)
298
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
299
+ test_output = net(test_input)
300
+ torch.sum(test_output).backward()
301
+ print(test_output.shape)
302
+
303
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
304
+ net = module.parametrize(
305
+ base, "weight", 1, 4, 1, weight_decompose=True, use_tucker=True
306
+ )
307
+ print(base)
308
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
309
+ test_output = base(test_input)
310
+ torch.sum(test_output).backward()
311
+ print(test_output.shape)
@@ -262,15 +262,21 @@ class LokrModule(LycorisBaseModule):
262
262
  weight *= drop
263
263
  return weight
264
264
 
265
- def get_merged_weight(self, multiplier=1, shape=None, device=None):
265
+ def get_diff_weight(self, multiplier=1, shape=None, device=None):
266
266
  scale = self.scale * multiplier
267
267
  diff = self.get_weight(shape) * scale
268
268
  if device is not None:
269
269
  diff = diff.to(device)
270
- merged = self.org_module[0].weight.data + diff
270
+ return diff, None
271
+
272
+ def get_merged_weight(self, multiplier=1, shape=None, device=None):
273
+ merged = (
274
+ self.org_module[0].weight.data
275
+ + self.get_diff_weight(multiplier=multiplier, shape=shape, device=device)[0]
276
+ )
271
277
  if self.wd:
272
278
  merged = self.apply_weight_decompose(merged)
273
- return merged
279
+ return merged, None
274
280
 
275
281
  def apply_weight_decompose(self, weight):
276
282
  weight = weight.to(self.dora_scale.dtype)
@@ -394,27 +400,43 @@ class LokrModule(LycorisBaseModule):
394
400
 
395
401
 
396
402
  if __name__ == "__main__":
397
- base = nn.Linear(128, 128).cuda()
398
- lokr = LokrModule("test", base, 1, 4, 1, weight_decompose=True, factor=8).cuda()
399
- print(lokr)
400
- test_input = torch.randn(1, 77, 128).cuda()
401
- test_output = lokr(test_input)
402
- torch.sum(test_output).backward()
403
- print(test_output.shape)
404
-
405
- base_4bit = LinearNF4(128, 128)
406
- base_4bit.load_state_dict(base.state_dict())
407
- base_4bit.cuda()
408
- qlokr = LokrModule("test", base_4bit, 1, 4, 1, weight_decompose=False).cuda()
409
- print(qlokr)
410
- test_output = qlokr(test_input)
411
- torch.sum(test_output).backward()
412
- print(test_output.shape)
413
-
414
- base = nn.Conv2d(128, 128, 3, 1, 1)
415
- lokr = LokrModule("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True)
416
- print(lokr)
417
- test_input = torch.randn(1, 128, 16, 16)
418
- test_output = lokr(test_input)
419
- torch.sum(test_output).backward()
420
- print(test_output.shape)
403
+ device = torch.device("cuda")
404
+ module = LokrModule
405
+ with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
406
+ base = nn.Linear(128, 128).to(device).half()
407
+ net = module("test", base, 1, 4, 1, weight_decompose=True).to(device)
408
+ print(net)
409
+ test_input = torch.randn(1, 128).to(device).half()
410
+ test_output = net(test_input)
411
+ torch.sum(test_output).backward()
412
+ print(test_output.shape)
413
+
414
+ base_4bit = LinearNF4(128, 128, device="cuda")
415
+ base_4bit.load_state_dict(base.state_dict())
416
+ base_4bit.to(device)
417
+ qnet = module("test", base_4bit, 1, 4, 1, weight_decompose=False).to(device)
418
+ print(qnet)
419
+ test_input = torch.randn(1, 128).to(device).half()
420
+ test_output = qnet(test_input)
421
+ torch.sum(test_output).backward()
422
+ print(test_output.shape)
423
+
424
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
425
+ net = module("test", base, 1, 4, 1, weight_decompose=True, use_tucker=True).to(
426
+ device
427
+ )
428
+ print(net)
429
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
430
+ test_output = net(test_input)
431
+ torch.sum(test_output).backward()
432
+ print(test_output.shape)
433
+
434
+ base = nn.Conv2d(128, 128, 3, 1, 1).to(device).half()
435
+ net = module.parametrize(
436
+ base, "weight", 1, 4, 1, weight_decompose=True, use_tucker=True
437
+ )
438
+ print(base)
439
+ test_input = torch.randn(1, 128, 16, 16).to(device).half()
440
+ test_output = base(test_input)
441
+ torch.sum(test_output).backward()
442
+ print(test_output.shape)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris_lora
3
- Version: 3.0.0.dev1
3
+ Version: 3.0.0.dev3
4
4
  Summary: Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion
5
5
  Home-page: https://github.com/KohakuBlueleaf/LyCORIS
6
6
  Author: Shih-Ying Yeh(KohakuBlueLeaf), Yu-Guan Hsieh, Zhidong Gao
@@ -8,8 +8,5 @@ Author-email: apolloyeh0123@gmail.com
8
8
  Requires-Python: >=3.10
9
9
  License-File: LICENSE.md
10
10
  Requires-Dist: torch
11
- Requires-Dist: safetensors
12
- Requires-Dist: diffusers
13
- Requires-Dist: transformers
14
11
  Requires-Dist: einops
15
12
  Requires-Dist: toml
@@ -0,0 +1,3 @@
1
+ torch
2
+ einops
3
+ toml
@@ -4,7 +4,7 @@ from setuptools import setup, find_packages
4
4
  setup(
5
5
  name="lycoris_lora",
6
6
  packages=find_packages(),
7
- version="3.0.0.dev1",
7
+ version="3.0.0.dev3",
8
8
  url="https://github.com/KohakuBlueleaf/LyCORIS",
9
9
  description="Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion",
10
10
  author="Shih-Ying Yeh(KohakuBlueLeaf), Yu-Guan Hsieh, Zhidong Gao",
@@ -12,9 +12,6 @@ setup(
12
12
  zip_safe=False,
13
13
  install_requires=[
14
14
  "torch",
15
- "safetensors",
16
- "diffusers",
17
- "transformers",
18
15
  "einops",
19
16
  "toml",
20
17
  ],
@@ -1,6 +0,0 @@
1
- torch
2
- safetensors
3
- diffusers
4
- transformers
5
- einops
6
- toml