-
Notifications
You must be signed in to change notification settings - Fork 14
/
run_qlora.py
194 lines (161 loc) · 5.92 KB
/
run_qlora.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
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
from dataclasses import dataclass, field
import os
# upgrade flash attention here
try:
os.system("pip install flash-attn --no-build-isolation --upgrade")
except:
print("flash-attn failed to install")
from typing import Optional
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
set_seed,
default_data_collator,
BitsAndBytesConfig,
Trainer,
TrainingArguments,
HfArgumentParser,
)
from datasets import load_from_disk
import torch
import bitsandbytes as bnb
from huggingface_hub import login
# COPIED FROM https://github.com/artidoro/qlora/blob/main/qlora.py
def find_all_linear_names(model):
lora_module_names = set()
for name, module in model.named_modules():
if isinstance(module, bnb.nn.Linear4bit):
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 list(lora_module_names)
def create_peft_model(model, gradient_checkpointing=True, bf16=True):
from peft import (
get_peft_model,
LoraConfig,
TaskType,
prepare_model_for_kbit_training,
)
from peft.tuners.lora import LoraLayer
# prepare int-4 model for training
model = prepare_model_for_kbit_training(model, use_gradient_checkpointing=gradient_checkpointing)
if gradient_checkpointing:
model.gradient_checkpointing_enable()
# get lora target modules
modules = find_all_linear_names(model)
print(f"Found {len(modules)} modules to quantize: {modules}")
peft_config = LoraConfig(
r=64,
lora_alpha=16,
target_modules=modules,
lora_dropout=0.1,
bias="none",
task_type=TaskType.CAUSAL_LM,
)
model = get_peft_model(model, peft_config)
# pre-process the model by upcasting the layer norms in float 32 for
for name, module in model.named_modules():
if isinstance(module, LoraLayer):
if 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 bf16 and module.weight.dtype == torch.float32:
module = module.to(torch.bfloat16)
model.print_trainable_parameters()
return model
def training_function(script_args, training_args):
# load dataset
dataset = load_from_disk(script_args.dataset_path)
# load model from the hub with a bnb config
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
model = AutoModelForCausalLM.from_pretrained(
script_args.model_id,
use_cache=False if training_args.gradient_checkpointing else True, # this is needed for gradient checkpointing
device_map="auto",
use_flash_attention_2=script_args.use_flash_attn,
quantization_config=bnb_config,
)
# create peft config
model = create_peft_model(
model, gradient_checkpointing=training_args.gradient_checkpointing, bf16=training_args.bf16
)
# Create Trainer instance
trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset,
data_collator=default_data_collator, # no special collator needed since we stacked the dataset
)
# Start training
trainer.train()
sagemaker_save_dir = "/opt/ml/model/"
if script_args.merge_adapters:
# merge adapter weights with base model and save
# save int 4 model
trainer.model.save_pretrained(training_args.output_dir, safe_serialization=False)
# clear memory
del model
del trainer
torch.cuda.empty_cache()
from peft import AutoPeftModelForCausalLM
# load PEFT model in fp16
model = AutoPeftModelForCausalLM.from_pretrained(
training_args.output_dir,
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
)
# Merge LoRA and base model and save
model = model.merge_and_unload()
model.save_pretrained(sagemaker_save_dir, safe_serialization=True, max_shard_size="2GB")
else:
trainer.model.save_pretrained(sagemaker_save_dir, safe_serialization=True)
# save tokenizer for easy inference
tokenizer = AutoTokenizer.from_pretrained(script_args.model_id, padding_side="left")
tokenizer.save_pretrained(sagemaker_save_dir)
@dataclass
class ScriptArguments:
model_id: str = field(
metadata={
"help": "The model that you want to train from the Hugging Face hub. E.g. gpt2, gpt2-xl, bert, etc."
},
)
dataset_path: str = field(
metadata={"help": "Path to the preprocessed and tokenized dataset."},
default=None,
)
hf_token: Optional[str] = field(default=None, metadata={"help": "Hugging Face token for authentication"})
trust_remote_code: bool = field(
metadata={"help": "Whether to trust remote code."},
default=False,
)
use_flash_attn: bool = field(
metadata={"help": "Whether to use Flash Attention."},
default=False,
)
merge_adapters: bool = field(
metadata={"help": "Wether to merge weights for LoRA."},
default=False,
)
def main():
parser = HfArgumentParser([ScriptArguments, TrainingArguments])
script_args, training_args = parser.parse_args_into_dataclasses()
# set seed
set_seed(training_args.seed)
# login to hub
token = script_args.hf_token if script_args.hf_token else os.getenv("HF_TOKEN", None)
if token:
print(f"Logging into the Hugging Face Hub with token {token[:10]}...")
login(token=token)
# run training function
training_function(script_args, training_args)
if __name__ == "__main__":
main()