-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformal_finetune_llama2chat7b.py
258 lines (217 loc) · 11.9 KB
/
formal_finetune_llama2chat7b.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
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
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2" #"1" or "0,1" for multiple GPUs
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoTokenizer, DataCollatorWithPadding, GenerationConfig, TrainingArguments, Trainer
import torch
import time
#import evaluate
import pandas as pd
import numpy as np
from peft import LoraConfig, get_peft_model, TaskType, PeftModel, PeftConfig, get_peft_model
from trl import SFTTrainer
from timeit import default_timer
##########################################
# load the model and tokenizer
model_name = "NousResearch/Llama-2-7b-chat-hf"
new_model_path = "./finetuned_models/finetuned_llama-2-7b-chat_fabner_full_training_16bit"
original_model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
device_map="auto"
)
#original_model.config.use_cache = False
#original_model.config.pretraining_tp = 1
tokenizer = AutoTokenizer.from_pretrained(model_name, device_map="auto")
####################################
#load dataset
train_file = "./data/immutable_data_formal/llama2chat7b_fabner_train.jsonl"
raw_dataset = load_dataset(
"json",
data_files={
"train": train_file,
},
)
print( raw_dataset )
# def tokenize_function(example):
# start_prompt = "You are an excellent linguist in the domain of thin film head technology. The task is to label the entities in the given sentence. " \
# "The optional entities include Component, Location, Function, EnergyFlow (Energy Flow), Attribution, Material, Effect, System, ScientificConcept (Scientific Concept), "\
# "Shape, Value, InfoFlow (Information Flow), Consequence, PhysicsFlow (Physics Flow), Measure, State, AnnotatorNotes.\n"\
# "Input sentence: "
# end_prompt = '\nExtracted entities of the above sentence: \n'
# prompt = [start_prompt + input_sentence + end_prompt for input_sentence in example["input"]]
# prompt_output = [prompt[i] + example["input"][i] for i in range(len(prompt))]
# example['input_ids'] = tokenizer(prompt, padding="max_length", max_length = 512, truncation=True, return_tensors="pt").input_ids
# example['labels'] = tokenizer(example["output"], padding="max_length", max_length = 512, truncation=True, return_tensors="pt").input_ids
# return example
# dataset = dataset.map(tokenize_function, batched=True )
# tokenized_datasets = dataset.remove_columns(['instruction', 'input', 'output'])
# data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
#print(f"Shapes of the datasets:")
#print(f"Training: {tokenized_datasets['train'].shape}, \n{tokenized_datasets['train']}")
dataset = raw_dataset['train']
combined_columns = []
# if train_file == "./data/immutable_data_formal/llama2chat7b_thf_train.jsonl":
# start_prompt = """### instruction:
# You are an excellent linguist in the domain of thin film head technology. The task is to label the entities in the given sentence.
# The optional entities include Component, Location, Function, EnergyFlow (Energy Flow), Attribution, Material, Effect, System, ScientificConcept (Scientific Concept),
# Shape, Value, InfoFlow (Information Flow), Consequence, PhysicsFlow (Physics Flow), Measure, State, AnnotatorNotes.\n"""\
# """### Input:
# Input sentence: """
# elif train_file == "./data/immutable_data_formal/llama2chat7b_assembly_train.jsonl":
# start_prompt = """### instruction:
# You are an excellent linguist in assembly instructions. The task is to label the entities in the given sentence.
# The optional entities include PART (parts), RPOS (relative positions), OPER (operations), TOOL (tools), ID (identification numbers), QTY (quantity),
# DIM (dimensions), WGT (weights), PROP (general properties). """\
# """### Input:
# Input sentence: """
# elif train_file == "./data/immutable_data_formal/llama2chat7b_fabner_train.jsonl":
# start_prompt = """### instruction:
# You are an excellent linguist in manufacturing domain. The task is to label the entities in the given sentence.
# The optional entities include CONPRI (Concept/principles), MATE (Material), MANP (Manufacturing process), PRO (Mechanical properties), PARA (Process parameters), CHAR (Process characterization),
# MACEQ (Machine/equipment), APPL (Application), FEAT (Engineering features), ENAT (Enabling technology), MANS (Manufacturing standards), BIOP (Biomedical). """\
# """### Input:
# Input sentence: """
end_prompt = "\nExtracted entities of the input sentence: \n"\
#"Output: \n"""
print(len(dataset["instruction"]))
for i in range(len(dataset["instruction"])):
row = "Input sentence: \n" + dataset["input"][i] + end_prompt + dataset["output"][i]
combined_columns.append(row)
dataset = dataset.add_column("text", combined_columns)
tokenized_datasets = dataset.remove_columns(['instruction', 'input', 'output'])
print(tokenized_datasets)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
# raw_datasets_train = raw_datasets["train"]
# combined_columns = []
# print(len(raw_datasets_train["instruction"]))
# for i in range(len(raw_datasets_train["instruction"])):
# row = "### Instruction: " + raw_datasets_train["instruction"][i] + "### Input: " + raw_datasets_train["input"][i] + "### Output: " + raw_datasets_train["output"][i]
# combined_columns.append(row)
# dataset = raw_datasets_train.add_column("text", combined_columns)
# dataset = raw_datasets_train.remove_columns(['instruction', 'input', 'output'])
###############################
#### finetuning process
#### peft model
lora_config = LoraConfig(
r=64, # Rank
lora_alpha=32,
#target_modules=["q", "v"],
lora_dropout=0.1,
bias="none",
#task_type=TaskType.SEQ_2_SEQ_LM # FLAN-T5
task_type="CAUSAL_LM",
)
def print_number_of_trainable_model_parameters(model):
trainable_model_params = 0
all_model_params = 0
for _, param in model.named_parameters():
all_model_params += param.numel()
if param.requires_grad:
trainable_model_params += param.numel()
return f"trainable model parameters: {trainable_model_params}\nall model parameters: {all_model_params}\npercentage of trainable model parameters: {100 * trainable_model_params / all_model_params:.2f}%"
print(print_number_of_trainable_model_parameters(original_model))
# training_args = TrainingArguments(
# output_dir=new_model,
# learning_rate=1e-5,
# num_train_epochs=3,
# weight_decay=0.01,
# logging_steps=1,
# max_steps=1
# )
peft_training_args = TrainingArguments(
output_dir= "./results" , #f'./peft-thin-film-training-{str(int(time.time()))}',
auto_find_batch_size=True,
#per_device_train_batch_size=8,
learning_rate=1e-4, # Higher learning rate than full fine-tuning.
weight_decay=0.001,
num_train_epochs=1,
logging_steps=1,
max_steps=-1, # Number of training steps (overrides num_train_epochs)
gradient_accumulation_steps=1,
optim="paged_adamw_32bit",
#save_steps=save_steps,
#logging_steps=logging_steps,
#fp16=False,
#bf16=False,
max_grad_norm=0.3, # Maximum gradient normal (gradient clipping)
warmup_ratio=0.03, # Ratio of steps for a linear warmup (from 0 to learning rate)
group_by_length=True, # Group sequences into batches with same length # Saves memory and speeds up training considerably
lr_scheduler_type="cosine", # Learning rate schedule
report_to="tensorboard",
)
### start training
# trainer = Trainer(
# model=original_model,
# args=training_args,
# train_dataset=tokenized_datasets['train'],
# #eval_dataset=tokenized_datasets['validation']
# )
peft_trainer = SFTTrainer(
model=original_model,
args=peft_training_args,
train_dataset=tokenized_datasets,
#data_collator=data_collator,
tokenizer=tokenizer,
dataset_text_field="text",
)
start = default_timer()
peft_trainer.train()
peft_trainer.model.save_pretrained(new_model_path)
tokenizer.save_pretrained(new_model_path)
print("training time: ", default_timer() - start)
# ############################### Short evaluation
# ## load the pretrained model
# # below is the original_model and tokenizer
# # peft_model_base = AutoModelForCausalLM.from_pretrained(
# # model_name,
# # torch_dtype=torch.bfloat16,
# # device_map="auto"
# # )
# # tokenizer = AutoTokenizer.from_pretrained(model_name)
# # peft_model = PeftModel.from_pretrained(peft_model_base,
# # new_model_path,
# # torch_dtype=torch.bfloat16,
# # is_trainable=False)
# peft_model = AutoModelForCausalLM.from_pretrained (new_model_path, is_trainable=False)
# #lora_config = LoraConfig.from_pretrained(new_model_path)
# #peft_model = get_peft_model(original_model, lora_config)
# print(print_number_of_trainable_model_parameters(peft_model))
# ## show the results:
# index = 88
# input = raw_dataset['train'][index]['input']
# baseline_extracted_entities = raw_dataset['train'][index]['output']
# # if train_file == "./data/immutable_data_formal/llama2chat7b_thf_train.jsonl":
# # prompt = f"""
# # You are an excellent linguist in the domain of thin film head technology. The task is to label the entities in the given sentence.
# # The optional entities include Component, Location, Function, EnergyFlow (Energy Flow), Attribution, Material, Effect, System, ScientificConcept (Scientific Concept),
# # Shape, Value, InfoFlow (Information Flow), Consequence, PhysicsFlow (Physics Flow), Measure, State, AnnotatorNotes.
# # Input sentence: {input}
# # Extracted entities of the above sentence: \n """
# # elif train_file == "./data/immutable_data_formal/llama2chat7b_assembly_train.jsonl":
# # prompt = f"""
# # You are an excellent linguist in assembly instructions. The task is to label the entities in the given sentence.
# # The optional entities include PART (parts), RPOS (relative positions), OPER (operations), TOOL (tools), ID (identification numbers), QTY (quantity),
# # DIM (dimensions), WGT (weights), PROP (general properties).
# # Input sentence: {input}
# # Extracted entities of the above sentence: \n """\
# # elif train_file == "./data/immutable_data_formal/llama2chat7b_fabner_train.jsonl":
# # prompt = """### instruction:
# # You are an excellent linguist in manufacturing domain. The task is to label the entities in the given sentence.
# # The optional entities include CONPRI (Concept/principles), MATE (Material), MANP (Manufacturing process), PRO (Mechanical properties), PARA (Process parameters), CHAR (Process characterization),
# # MACEQ (Machine/equipment), APPL (Application), FEAT (Engineering features), ENAT (Enabling technology), MANS (Manufacturing standards), BIOP (Biomedical).
# # Input sentence: {input}
# # Extracted entities of the above sentence: \n """\
# prompt = f"""Input sentence: {input}
# Extracted entities of the above sentence: \n """
# input_ids = tokenizer(prompt, return_tensors="pt").input_ids
# # original_model_outputs = original_model.generate(input_ids=input_ids, generation_config=GenerationConfig(max_new_tokens=300, num_beams=1))
# # original_model_text_output = tokenizer.decode(original_model_outputs[0], skip_special_tokens=True)
# peft_model_outputs = peft_model.generate(input_ids=input_ids, generation_config=GenerationConfig(max_new_tokens=100, num_beams=1))
# peft_model_text_output = tokenizer.decode(peft_model_outputs[0], skip_special_tokens=True)
# dash_line = '-'.join('' for x in range(100))
# print(dash_line)
# print(f'BASELINE HUMAN SUMMARY:\n{baseline_extracted_entities}')
# # print(dash_line)
# # print(f'ORIGINAL MODEL:\n{original_model_text_output}')
# print(dash_line)
# print(f'PEFT MODEL: {peft_model_text_output}')