-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimization_methods.py
More file actions
698 lines (565 loc) · 28 KB
/
Copy pathoptimization_methods.py
File metadata and controls
698 lines (565 loc) · 28 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
"""
Optimization Methods Configuration
This module defines the available optimization methods for fine-tuning.
To add a new method, simply add it to the METHODS dictionary and implement
the required configuration functions.
"""
from typing import Dict, Any, Tuple
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig
import torch
class OptimizationMethod:
"""Base class for optimization methods."""
def __init__(self, name: str, description: str):
self.name = name
self.description = description
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
"""Load model with this optimization method."""
raise NotImplementedError
def setup_lora_config(self, model_config: dict) -> LoraConfig:
"""Set up LoRA configuration for this method."""
raise NotImplementedError
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
"""Get training configuration specific to this method."""
raise NotImplementedError
class StandardLoRA(OptimizationMethod):
"""Standard LoRA fine-tuning method with low-rank matrix decomposition W = W₀ + BA."""
def __init__(self):
super().__init__("lora", "Standard LoRA fine-tuning with mixed precision")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "lora"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM"
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda
}
class QLoRA(OptimizationMethod):
"""QLoRA fine-tuning method with 4-bit quantization for memory efficiency."""
def __init__(self):
super().__init__("qlora", "QLoRA with 4-bit quantization and double quantization efficiency")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
from transformers import BitsAndBytesConfig
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
if use_cuda:
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True
)
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
quantization_config=quantization_config,
torch_dtype=torch.float16,
device_map="auto",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "qlora"
else:
# Fallback to standard LoRA if CUDA not available
return StandardLoRA().load_model(pretrained_model, trust_remote_code)
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM",
use_rslora=True
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": False, # Don't use FP16 with QLoRA
"bf16": use_cuda, # Use BF16 if available
"dataloader_pin_memory": use_cuda
}
class DoRA(OptimizationMethod):
"""DoRA - Weight magnitude-direction decomposition for better performance."""
def __init__(self):
super().__init__("dora", "DoRA with weight magnitude-direction decomposition")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "dora"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
task_type="CAUSAL_LM",
use_dora=True # Enable DoRA
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"gradient_checkpointing": True
}
class AdaLoRA(OptimizationMethod):
"""AdaLoRA with adaptive rank allocation using SVD-based dynamic rank adjustment."""
def __init__(self):
super().__init__("adalora", "AdaLoRA with adaptive rank allocation and SVD-based optimization")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "adalora"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
task_type="CAUSAL_LM",
target_modules_rank=model_config.get('target_modules_rank', model_config['lora_rank']),
init_lora_weights=True
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"gradient_checkpointing": True,
"max_grad_norm": 1.0
}
class LoRAPlus(OptimizationMethod):
"""LoRA+ with differential learning rates for A/B matrices."""
def __init__(self):
super().__init__("lora_plus", "LoRA+ with differential learning rates λₐ ≠ λᵦ")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "lora_plus"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM",
use_rslora=True
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"optim": "adamw_torch",
"learning_rate": 2e-4,
"weight_decay": 0.01
}
class DeltaLoRA(OptimizationMethod):
"""Delta-LoRA for continual learning optimization with parameter difference storage."""
def __init__(self):
super().__init__("delta_lora", "Delta-LoRA with continual learning and parameter difference storage")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "delta_lora"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
task_type="CAUSAL_LM",
inference_mode=False
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"save_strategy": "steps",
"save_steps": 100,
"save_total_limit": 5
}
class MultiLoRA(OptimizationMethod):
"""Multi-LoRA for multi-task adapter system with parallel LoRA modules."""
def __init__(self):
super().__init__("multi_lora", "Multi-LoRA with parallel adapter modules for different domains")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "multi_lora"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM",
modules_to_save=["embed_tokens", "lm_head"]
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"gradient_checkpointing": True
}
class OLoRA(OptimizationMethod):
"""OLoRA with orthogonal constraint adaptation using Gram-Schmidt optimization."""
def __init__(self):
super().__init__("olora", "OLoRA with orthogonal constraints and Gram-Schmidt optimization")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "olora"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM",
use_rslora=True
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"optim": "adamw_torch",
"learning_rate": 1e-4
}
class VeRA(OptimizationMethod):
"""VeRA with vector-based random adaptation and shared random matrices."""
def __init__(self):
super().__init__("vera", "VeRA with vector-based random adaptation and parameter efficiency")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "vera"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM",
use_rslora=True
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"learning_rate": 3e-4
}
class LoRAFA(OptimizationMethod):
"""LoRA-FA with frozen-A initialization strategy and fixed A matrix."""
def __init__(self):
super().__init__("lora_fa", "LoRA-FA with frozen A matrix and trainable B optimization")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "lora_fa"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM",
inference_mode=False
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"learning_rate": 1e-4
}
class LoRADrop(OptimizationMethod):
"""LoRA-drop with structured dropout patterns for regularization."""
def __init__(self):
super().__init__("lora_drop", "LoRA-drop with structured dropout and selective adapter deactivation")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "lora_drop"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config.get('lora_dropout', 0.3), # Higher dropout for LoRA-drop
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM"
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"gradient_checkpointing": True
}
class TiedLoRA(OptimizationMethod):
"""Tied-LoRA with parameter sharing across layers for memory efficiency."""
def __init__(self):
super().__init__("tied_lora", "Tied-LoRA with parameter sharing and weight reuse")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "tied_lora"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM",
inference_mode=False
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"gradient_checkpointing": True
}
class LoRAXS(OptimizationMethod):
"""LoRA-XS with extreme parameter compression maintaining performance."""
def __init__(self):
super().__init__("lora_xs", "LoRA-XS with <0.01% parameter ratio and performance maintenance")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "lora_xs"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
# Use very low rank for extreme compression
rank = min(model_config.get('lora_rank', 4), 4)
return LoraConfig(
r=rank,
lora_alpha=rank * 2, # Smaller alpha for XS
lora_dropout=model_config.get('lora_dropout', 0.1),
target_modules=["q_proj", "v_proj"],
task_type="CAUSAL_LM"
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"learning_rate": 5e-4, # Higher LR for XS
"gradient_accumulation_steps": 8
}
class HLoRA(OptimizationMethod):
"""HLoRA with hierarchical low-rank structures for complex tasks."""
def __init__(self):
super().__init__("hlora", "HLoRA with hierarchical low-rank structures and multi-level adaptation")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "hlora"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj", "k_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
task_type="CAUSAL_LM",
inference_mode=False
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"gradient_checkpointing": True,
"max_grad_norm": 0.5
}
class GLoRA(OptimizationMethod):
"""GLoRA with group-wise low-rank adaptation and clustered parameter updates."""
def __init__(self):
super().__init__("glora", "GLoRA with group-wise adaptation and clustered parameter updates")
def load_model(self, pretrained_model: str, trust_remote_code: bool = True) -> Tuple[AutoModelForCausalLM, AutoTokenizer, str]:
use_cuda = torch.cuda.is_available()
tokenizer = AutoTokenizer.from_pretrained(pretrained_model, trust_remote_code=trust_remote_code)
tokenizer.pad_token = tokenizer.eos_token or tokenizer.pad_token or "[PAD]"
model = AutoModelForCausalLM.from_pretrained(
pretrained_model,
torch_dtype=torch.float16 if use_cuda else torch.float32,
device_map="auto" if use_cuda else "cpu",
trust_remote_code=trust_remote_code
)
return model, tokenizer, "glora"
def setup_lora_config(self, model_config: dict) -> LoraConfig:
return LoraConfig(
r=model_config['lora_rank'],
lora_alpha=model_config['lora_alpha'],
lora_dropout=model_config['lora_dropout'],
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
task_type="CAUSAL_LM",
inference_mode=False
)
def get_training_config(self, use_cuda: bool) -> Dict[str, Any]:
return {
"fp16": use_cuda,
"bf16": False,
"dataloader_pin_memory": use_cuda,
"gradient_checkpointing": True,
"learning_rate": 2e-4
}
# Registry of available optimization methods
METHODS = {
"lora": StandardLoRA(),
"qlora": QLoRA(),
"dora": DoRA(),
"adalora": AdaLoRA(),
"lora_plus": LoRAPlus(),
"delta_lora": DeltaLoRA(),
"multi_lora": MultiLoRA(),
"olora": OLoRA(),
"vera": VeRA(),
"lora_fa": LoRAFA(),
"lora_drop": LoRADrop(),
"tied_lora": TiedLoRA(),
"lora_xs": LoRAXS(),
"hlora": HLoRA(),
"glora": GLoRA()
}
def get_available_methods() -> list:
"""Get list of available optimization method names."""
return list(METHODS.keys())
def get_method(method_name: str) -> OptimizationMethod:
"""Get optimization method by name."""
if method_name.lower() not in METHODS:
raise ValueError(f"Unknown optimization method: {method_name}. Available: {', '.join(get_available_methods())}")
return METHODS[method_name.lower()]
def add_method(method_name: str, method: OptimizationMethod):
"""Add a new optimization method to the registry."""
METHODS[method_name.lower()] = method
def get_method_info(method_name: str) -> Dict[str, str]:
"""Get detailed information about a specific method."""
method = get_method(method_name)
return {
"name": method.name,
"description": method.description,
"key_features": get_method_key_features(method_name)
}
def get_method_key_features(method_name: str) -> list:
"""Get key features and characteristics of each method."""
features = {
"lora": ["Low-rank decomposition", "Mixed precision", "Standard approach"],
"qlora": ["4-bit quantization", "Memory efficient", "CUDA optimized"],
"dora": ["Magnitude-direction decomposition", "Better performance", "Enhanced adaptation"],
"adalora": ["Adaptive rank allocation", "SVD-based optimization", "Dynamic adjustment"],
"lora_plus": ["Differential learning rates", "A/B matrix optimization", "Enhanced training"],
"delta_lora": ["Continual learning", "Parameter difference storage", "Task sequences"],
"multi_lora": ["Multi-task adapters", "Parallel modules", "Domain specialization"],
"olora": ["Orthogonal constraints", "Gram-Schmidt optimization", "Reduced interference"],
"vera": ["Vector-based adaptation", "Shared random matrices", "Parameter efficiency"],
"lora_fa": ["Frozen-A strategy", "Fixed A matrix", "B optimization only"],
"lora_drop": ["Structured dropout", "Selective deactivation", "Regularization"],
"tied_lora": ["Parameter sharing", "Weight reuse", "Memory efficiency"],
"lora_xs": ["Extreme compression", "<0.01% parameters", "Performance maintenance"],
"hlora": ["Hierarchical structures", "Multi-level adaptation", "Complex tasks"],
"glora": ["Group-wise adaptation", "Clustered updates", "Efficient training"]
}
return features.get(method_name.lower(), ["No specific features documented"])