-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlora.py
60 lines (50 loc) · 1.76 KB
/
lora.py
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
"""
This file provides some components for the LoRA support of the trainer.
"""
import peft
import os
import torch
from transformers import (
TrainerState,
TrainerControl,
TrainerCallback,
TrainingArguments,
)
from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR
class SavePeftModelCallback(TrainerCallback):
def on_save(
self,
args: TrainingArguments,
state: TrainerState,
control: TrainerControl,
**kwargs,
):
checkpoint_folder = os.path.join(
args.output_dir, f"{PREFIX_CHECKPOINT_DIR}-{state.global_step}")
kwargs["model"].save_pretrained(checkpoint_folder)
pytorch_model_path = os.path.join(
checkpoint_folder, "pytorch_model.bin")
torch.save({}, pytorch_model_path)
return control
def hacky_model_convert(args, model):
for name, module in model.named_modules():
if isinstance(module, peft.tuners.lora.LoraLayer):
if args.bf16:
module = module.to(torch.bfloat16)
if 'norm' in name:
module = module.to(torch.float32)
if 'lm_head' in name or 'embed_tokens' in name:
if hasattr(module, 'weight'):
if args.bf16 and module.weight.dtype == torch.float32:
module = module.to(torch.bfloat16)
def find_all_linear_names(model):
import bitsandbytes as bnb
cls = bnb.nn.Linear8bitLt
lora_module_names = set()
for name, module in model.named_modules():
if isinstance(module, cls):
names = name.split('.')
lora_module_names.add(names[0] if len(names) == 1 else names[-1])
if 'lm_head' in lora_module_names: # needed for 16-bit
lora_module_names.remove('lm_head')
return lora_module_names