forked from KohakuBlueleaf/LyCORIS
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_compile_optimizations.py
More file actions
383 lines (304 loc) · 14.6 KB
/
Copy pathtest_compile_optimizations.py
File metadata and controls
383 lines (304 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""Tests for torch.compile optimizations in base.py and locon.py.
Verifies:
- **kw_dict fallback removed from _call_op and _call_op_1x1 (raises NotImplementedError)
- _scale_in_diff cached in __init__ and used correctly in _forward_rebuild_core
- Compiled forward equivalence with all code paths still holds
"""
import unittest
import torch
import torch.nn as nn
import torch.nn.functional as F
from lycoris.modules.locon import LoConModule
from lycoris.modules.loha import LohaModule
CUDA_AVAILABLE = torch.cuda.is_available()
def _device():
return torch.device("cuda") if CUDA_AVAILABLE else torch.device("cpu")
def _compile_kwargs():
if CUDA_AVAILABLE:
return dict(mode="default", dynamic=True, fullgraph=False)
return dict(backend="eager", fullgraph=False)
# ===========================================================================
# 1. _call_op / _call_op_1x1 — **kw_dict fallback removed
# ===========================================================================
class CallOpFallbackTests(unittest.TestCase):
"""Verify that _call_op and _call_op_1x1 raise NotImplementedError
for unsupported module types instead of using **kw_dict fallback."""
def test_call_op_raises_for_unsupported_type(self):
"""_call_op should raise NotImplementedError for unknown module_type."""
device = _device()
base = nn.Linear(16, 16).to(device)
net = LoConModule("test", base, lora_dim=4, alpha=1).to(device)
# Override module_type to an unsupported value
net.module_type = "unsupported_type"
x = torch.randn(2, 16, device=device)
weight = torch.randn(16, 16, device=device)
with self.assertRaises(NotImplementedError) as ctx:
net._call_op(x, weight)
self.assertIn("unsupported_type", str(ctx.exception))
self.assertIn("_call_op", str(ctx.exception))
def test_call_op_raises_for_unsupported_type_with_bias(self):
"""_call_op should raise NotImplementedError even when bias is provided."""
device = _device()
base = nn.Linear(16, 16).to(device)
net = LoConModule("test", base, lora_dim=4, alpha=1).to(device)
net.module_type = "unsupported_type"
x = torch.randn(2, 16, device=device)
weight = torch.randn(16, 16, device=device)
bias = torch.randn(16, device=device)
with self.assertRaises(NotImplementedError):
net._call_op(x, weight, bias)
def test_call_op_1x1_raises_for_unsupported_type(self):
"""_call_op_1x1 should raise NotImplementedError for unknown module_type."""
device = _device()
base = nn.Linear(16, 16).to(device)
net = LoConModule("test", base, lora_dim=4, alpha=1).to(device)
net.module_type = "unsupported_type"
x = torch.randn(2, 16, device=device)
weight = torch.randn(16, 16, device=device)
with self.assertRaises(NotImplementedError) as ctx:
net._call_op_1x1(x, weight)
self.assertIn("unsupported_type", str(ctx.exception))
self.assertIn("_call_op_1x1", str(ctx.exception))
def test_call_op_linear_still_works(self):
"""_call_op should still produce correct results for linear."""
device = _device()
dtype = torch.float32
base = nn.Linear(16, 16).to(device, dtype)
net = LoConModule("test", base, lora_dim=4, alpha=1).to(device, dtype)
x = torch.randn(2, 16, device=device, dtype=dtype)
weight = torch.randn(16, 16, device=device, dtype=dtype)
bias = torch.randn(16, device=device, dtype=dtype)
out = net._call_op(x, weight, bias)
expected = F.linear(x, weight, bias)
torch.testing.assert_close(out, expected)
def test_call_op_conv2d_still_works(self):
"""_call_op should still produce correct results for conv2d."""
device = _device()
dtype = torch.float32
base = nn.Conv2d(16, 16, 3, 1, 1).to(device, dtype)
net = LoConModule("test", base, lora_dim=4, alpha=1).to(device, dtype)
x = torch.randn(1, 16, 8, 8, device=device, dtype=dtype)
weight = torch.randn(16, 16, 3, 3, device=device, dtype=dtype)
out = net._call_op(x, weight)
expected = F.conv2d(x, weight, stride=net._conv_stride,
padding=net._conv_padding,
dilation=net._conv_dilation,
groups=net._conv_groups)
torch.testing.assert_close(out, expected)
def test_call_op_1x1_linear_still_works(self):
"""_call_op_1x1 should still produce correct results for linear."""
device = _device()
dtype = torch.float32
base = nn.Linear(16, 16).to(device, dtype)
net = LoConModule("test", base, lora_dim=4, alpha=1).to(device, dtype)
x = torch.randn(2, 16, device=device, dtype=dtype)
weight = torch.randn(16, 16, device=device, dtype=dtype)
out = net._call_op_1x1(x, weight)
expected = F.linear(x, weight)
torch.testing.assert_close(out, expected)
def test_call_op_1x1_conv2d_still_works(self):
"""_call_op_1x1 should still produce correct results for conv2d (1x1)."""
device = _device()
dtype = torch.float32
base = nn.Conv2d(16, 16, 3, 1, 1).to(device, dtype)
net = LoConModule("test", base, lora_dim=4, alpha=1).to(device, dtype)
x = torch.randn(1, 16, 8, 8, device=device, dtype=dtype)
weight = torch.randn(16, 16, 1, 1, device=device, dtype=dtype)
out = net._call_op_1x1(x, weight)
expected = F.conv2d(x, weight)
torch.testing.assert_close(out, expected)
# ===========================================================================
# 2. _scale_in_diff cached attribute
# ===========================================================================
class ScaleInDiffCacheTests(unittest.TestCase):
"""Verify _scale_in_diff is cached correctly at init time."""
def test_scale_in_diff_false_for_standard_lora(self):
"""Standard LoRA (no tucker, no rank_dropout, no wd): _scale_in_diff=False."""
device = _device()
base = nn.Linear(16, 16).to(device)
net = LoConModule("test", base, lora_dim=4, alpha=1).to(device)
self.assertFalse(net._scale_in_diff)
def test_scale_in_diff_true_with_tucker(self):
"""Tucker mode: _scale_in_diff=True (tucker=True, wd=False)."""
device = _device()
base = nn.Conv2d(16, 16, 3, 1, 1).to(device)
net = LoConModule("test", base, lora_dim=4, alpha=1,
use_tucker=True).to(device)
self.assertTrue(net._scale_in_diff)
def test_scale_in_diff_true_with_rank_dropout(self):
"""Rank dropout: _scale_in_diff=True."""
device = _device()
base = nn.Linear(16, 16).to(device)
net = LoConModule("test", base, lora_dim=4, alpha=1,
rank_dropout=0.1).to(device)
self.assertTrue(net._scale_in_diff)
def test_scale_in_diff_false_with_wd(self):
"""Weight decompose (DoRA): _scale_in_diff=False even with rank_dropout."""
device = _device()
base = nn.Linear(16, 16).to(device)
net = LoConModule("test", base, lora_dim=4, alpha=1,
weight_decompose=True, rank_dropout=0.1).to(device)
self.assertFalse(net._scale_in_diff)
def test_scale_in_diff_false_with_wd_and_tucker(self):
"""Weight decompose + tucker: _scale_in_diff=False (wd takes priority)."""
device = _device()
base = nn.Conv2d(16, 16, 3, 1, 1).to(device)
net = LoConModule("test", base, lora_dim=4, alpha=1,
weight_decompose=True, use_tucker=True).to(device)
self.assertFalse(net._scale_in_diff)
def test_scale_in_diff_matches_manual_computation(self):
"""_scale_in_diff should equal the manual expression for all combos."""
device = _device()
for wd in [False, True]:
for tucker in [False, True]:
for rd in [0.0, 0.1]:
if tucker:
base = nn.Conv2d(16, 16, 3, 1, 1).to(device)
else:
base = nn.Linear(16, 16).to(device)
net = LoConModule(
"test", base, lora_dim=4, alpha=1,
weight_decompose=wd, use_tucker=tucker,
rank_dropout=rd,
).to(device)
expected = (not wd) and (tucker or rd)
self.assertEqual(
net._scale_in_diff, expected,
f"Failed for wd={wd}, tucker={tucker}, rd={rd}: "
f"expected {expected}, got {net._scale_in_diff}"
)
# ===========================================================================
# 3. Compiled forward equivalence with optimizations
# ===========================================================================
class CompiledEquivalenceTests(unittest.TestCase):
"""Verify compiled forward produces identical results after optimizations."""
def test_compile_linear_standard(self):
"""Standard LoRA linear: compiled matches non-compiled."""
device = _device()
dtype = torch.float32
dim = 32
torch.manual_seed(42)
base_a = nn.Linear(dim, dim).to(device, dtype)
net_a = LoConModule("test", base_a, lora_dim=4, alpha=1,
use_scalar=True).to(device, dtype)
net_a.apply_to()
torch.manual_seed(42)
base_b = nn.Linear(dim, dim).to(device, dtype)
base_b.load_state_dict(base_a.state_dict())
net_b = LoConModule("test", base_b, lora_dim=4, alpha=1,
use_scalar=True).to(device, dtype)
net_b.apply_to()
net_b.compile_forward(**_compile_kwargs())
x = torch.randn(3, dim, device=device, dtype=dtype)
with torch.no_grad():
out_a = base_a(x)
out_b = base_b(x)
torch.testing.assert_close(out_a, out_b)
def test_compile_bypass_mode(self):
"""Bypass mode: compiled matches non-compiled."""
device = _device()
dtype = torch.float32
dim = 32
torch.manual_seed(42)
base_a = nn.Linear(dim, dim).to(device, dtype)
net_a = LoConModule("test", base_a, lora_dim=4, alpha=1,
use_scalar=True, bypass_mode=True).to(device, dtype)
net_a.apply_to()
torch.manual_seed(42)
base_b = nn.Linear(dim, dim).to(device, dtype)
base_b.load_state_dict(base_a.state_dict())
net_b = LoConModule("test", base_b, lora_dim=4, alpha=1,
use_scalar=True, bypass_mode=True).to(device, dtype)
net_b.apply_to()
net_b.compile_forward(**_compile_kwargs())
x = torch.randn(3, dim, device=device, dtype=dtype)
with torch.no_grad():
out_a = base_a(x)
out_b = base_b(x)
torch.testing.assert_close(out_a, out_b)
def test_compile_dora(self):
"""DoRA (weight_decompose): compiled matches non-compiled."""
device = _device()
dtype = torch.float32
dim = 32
torch.manual_seed(42)
base_a = nn.Linear(dim, dim, bias=True).to(device, dtype)
net_a = LoConModule("test", base_a, lora_dim=4, alpha=1,
weight_decompose=True).to(device, dtype)
net_a.apply_to()
torch.manual_seed(42)
base_b = nn.Linear(dim, dim, bias=True).to(device, dtype)
base_b.load_state_dict(base_a.state_dict())
net_b = LoConModule("test", base_b, lora_dim=4, alpha=1,
weight_decompose=True).to(device, dtype)
net_b.apply_to()
net_b.compile_forward(**_compile_kwargs())
x = torch.randn(3, dim, device=device, dtype=dtype)
with torch.no_grad():
out_a = base_a(x)
out_b = base_b(x)
torch.testing.assert_close(out_a, out_b)
def test_compile_tucker(self):
"""Tucker mode: compiled matches non-compiled."""
device = _device()
dtype = torch.float32
dim = 16
torch.manual_seed(42)
base_a = nn.Conv2d(dim, dim, 3, 1, 1).to(device, dtype)
net_a = LoConModule("test", base_a, lora_dim=4, alpha=1,
use_tucker=True, use_scalar=True).to(device, dtype)
net_a.apply_to()
torch.manual_seed(42)
base_b = nn.Conv2d(dim, dim, 3, 1, 1).to(device, dtype)
base_b.load_state_dict(base_a.state_dict())
net_b = LoConModule("test", base_b, lora_dim=4, alpha=1,
use_tucker=True, use_scalar=True).to(device, dtype)
net_b.apply_to()
net_b.compile_forward(**_compile_kwargs())
x = torch.randn(1, dim, 8, 8, device=device, dtype=dtype)
with torch.no_grad():
out_a = base_a(x)
out_b = base_b(x)
torch.testing.assert_close(out_a, out_b)
def test_compile_rank_dropout_eval(self):
"""Rank dropout in eval mode: compiled matches non-compiled."""
device = _device()
dtype = torch.float32
dim = 32
torch.manual_seed(42)
base_a = nn.Linear(dim, dim).to(device, dtype)
net_a = LoConModule("test", base_a, lora_dim=4, alpha=1,
rank_dropout=0.1, use_scalar=True).to(device, dtype)
net_a.apply_to()
net_a.eval()
torch.manual_seed(42)
base_b = nn.Linear(dim, dim).to(device, dtype)
base_b.load_state_dict(base_a.state_dict())
net_b = LoConModule("test", base_b, lora_dim=4, alpha=1,
rank_dropout=0.1, use_scalar=True).to(device, dtype)
net_b.apply_to()
net_b.eval()
net_b.compile_forward(**_compile_kwargs())
x = torch.randn(3, dim, device=device, dtype=dtype)
with torch.no_grad():
out_a = base_a(x)
out_b = base_b(x)
torch.testing.assert_close(out_a, out_b)
def test_training_with_compiled_backward(self):
"""Verify gradients flow correctly through compiled forward."""
device = _device()
dtype = torch.float32
base = nn.Linear(16, 16).to(device, dtype)
net = LoConModule("test", base, lora_dim=4, alpha=1,
use_scalar=True).to(device, dtype)
net.compile_forward(**_compile_kwargs())
net.apply_to()
x = torch.randn(2, 16, device=device, dtype=dtype)
out = base(x)
loss = out.sum()
loss.backward()
for name, p in net.named_parameters():
if p.requires_grad:
self.assertIsNotNone(p.grad, f"{name} has no grad")
if __name__ == "__main__":
unittest.main()