-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathStep 2 Fine tuning using LoRA.py
216 lines (152 loc) · 5.29 KB
/
Step 2 Fine tuning using 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
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
# Databricks notebook source
# MAGIC %pip install transformers==4.31.0 datasets==2.13.0 peft==0.4.0 accelerate==0.21.0 bitsandbytes==0.40.2 trl==0.4.7
# COMMAND ----------
from peft import get_peft_config, PeftModel, PeftConfig, get_peft_model, LoraConfig, TaskType
from transformers import AutoModelForCausalLM
from transformers import LlamaTokenizer, LlamaForCausalLM
import torch
from transformers.trainer_callback import TrainerCallback
import os
from transformers import BitsAndBytesConfig
from trl import SFTTrainer
import mlflow
# COMMAND ----------
# MAGIC %sql
# MAGIC USE description_generator;
# COMMAND ----------
df = spark.sql("SELECT * FROM product_name_to_description").toPandas()
df['text'] = df["prompt"]+df["response"]
df.drop(columns=['prompt', 'response'], inplace=True)
display(df), df.shape
# COMMAND ----------
from datasets import load_dataset
from datasets import Dataset
dataset = Dataset.from_pandas(df).train_test_split(test_size=0.05, seed=42)
# COMMAND ----------
target_modules = ['q_proj','k_proj','v_proj','o_proj','gate_proj','down_proj','up_proj','lm_head']
#or
# target_modules = ['q_proj','v_proj']
lora_config = LoraConfig(
r=8,#or r=16
lora_alpha=8,
lora_dropout=0.05,
bias="none",
target_modules = target_modules,
task_type="CAUSAL_LM",
)
base_dir = "<base_dir_location>"
per_device_train_batch_size = 4
gradient_accumulation_steps = 4
optim = 'adamw_hf'
learning_rate = 1e-5
max_grad_norm = 0.3
warmup_ratio = 0.03
lr_scheduler_type = "linear"
# COMMAND ----------
from transformers import TrainingArguments
training_args = TrainingArguments(
output_dir=base_dir,
save_strategy="epoch",
evaluation_strategy="epoch",
num_train_epochs = 3.0,
per_device_train_batch_size=per_device_train_batch_size,
gradient_accumulation_steps=gradient_accumulation_steps,
optim=optim,
learning_rate=learning_rate,
fp16=True,
max_grad_norm=max_grad_norm,
warmup_ratio=warmup_ratio,
group_by_length=True,
lr_scheduler_type=lr_scheduler_type,
)
# COMMAND ----------
model_path = 'openlm-research/open_llama_3b_v2'
# COMMAND ----------
tokenizer = LlamaTokenizer.from_pretrained(model_path)
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
# COMMAND ----------
model = LlamaForCausalLM.from_pretrained(
model_path, device_map='auto', load_in_8bit=True,
)
# COMMAND ----------
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
# COMMAND ----------
trainer = SFTTrainer(
model,
train_dataset=dataset['train'],
eval_dataset = dataset['test'],
dataset_text_field="text",
max_seq_length=256,
args=training_args,
)
#Upcast layer norms to float 32 for stability
for name, module in trainer.model.named_modules():
if "norm" in name:
module = module.to(torch.float32)
# COMMAND ----------
# Initiate the training process
with mlflow.start_run(run_name='run_name_of_choice'):
trainer.train()
# COMMAND ----------
# #https://github.com/NVIDIA/apex/issues/965
# for param in model.parameters():
# # Check if parameter dtype is Half (float16)
# if param.dtype == torch.float16:
# param.data = param.data.to(torch.float32)
# COMMAND ----------
# MAGIC %md
# MAGIC ### If loading from saved adapter
# COMMAND ----------
dbutils.fs.ls('<base_dir_location>')
# COMMAND ----------
model_path = 'openlm-research/open_llama_3b_v2'
# COMMAND ----------
tokenizer = LlamaTokenizer.from_pretrained(model_path)
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
# COMMAND ----------
model = LlamaForCausalLM.from_pretrained(
model_path, load_in_8bit=True, device_map='auto',
)
# COMMAND ----------
peft_model_id = '<adapter_final_checkpoint_location>'
# COMMAND ----------
peft_model = PeftModel.from_pretrained(model, peft_model_id)
# COMMAND ----------
test_strings = ["Create a detailed description for the following product: Corelogic Smooth Mouse, belonging to category: Optical Mouse",
"Create a detailed description for the following product: Hoover Lightspeed, belonging to category: Cordless Vacuum Cleaner",
"Create a detailed description for the following product: Flattronic Cinematron, belonging to category: High Definition Flatscreen TV"]
# COMMAND ----------
predictions = []
for test in test_strings:
prompt = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{}
### Response:""".format(test)
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to('cuda')
generation_output = model.generate(
input_ids=input_ids, max_new_tokens=156
)
predictions.append(tokenizer.decode(generation_output[0]))
# COMMAND ----------
def extract_response_text(input_string):
start_marker = '### Response:'
end_marker = '###'
start_index = input_string.find(start_marker)
if start_index == -1:
return None
start_index += len(start_marker)
end_index = input_string.find(end_marker, start_index)
if end_index == -1:
return input_string[start_index:]
return input_string[start_index:end_index].strip()
# COMMAND ----------
# predictions[2]
# COMMAND ----------
for i in range(3):
pred = predictions[i]
text = test_strings[i]
print(text+'\n')
print(extract_response_text(pred))
print('--------')
# COMMAND ----------