-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_sft_seq2seq_bart.py
327 lines (230 loc) · 11.6 KB
/
train_sft_seq2seq_bart.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
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import argparse
import os
from accelerate import Accelerator
from datasets import load_dataset
from peft import LoraConfig, prepare_model_for_kbit_training, get_peft_model
from tqdm import tqdm
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, TrainingArguments, logging, set_seed
from transformers import Trainer, BitsAndBytesConfig, TrainerCallback
from trl import SFTTrainer
from trl.trainer import ConstantLengthDataset
import numpy as np
import torch
from transformers import BartForConditionalGeneration
def print_trainable_parameters(model):
"""
Prints the number of trainable parameters in the model.
"""
trainable_params = 0
all_param = 0
for _, param in model.named_parameters():
all_param += param.numel()
if param.requires_grad:
trainable_params += param.numel()
print(
f"trainable params: {trainable_params} || all params: {all_param} || trainable%: {100 * trainable_params / all_param}"
)
def get_prompt(example):
"""Prepare the text from a sample of the dataset."""
input_text = "\n\n".join(
[
"### Citing Paper Title:\n%s"%( example["citing_paper_content"]["title"] ),
"### Citing Paper Abstract:\n%s"%( example["citing_paper_content"]["abstract"] ),
"### Cited Paper Title:\n%s"%( example["cited_paper_content"]["title"] ),
"### Cited Paper Abstract:\n%s"%( example["cited_paper_content"]["abstract"] ),
"### Text Before Citation:\n%s"%( " ".join( example["text_before_citation"] ) )
]
)
output_text = "### Citation Intent:\n%s\n\n### Keywords:\n%s\n\n### Citation:\n%s" % (
example["citation_intent"],
"; ".join( example["keywords"] ),
example["citation"] + " " + " ".join( example["text_after_citation"] )
)
return input_text, output_text
def tokenize_example( example ):
global tokenizer, args
input_text, output_text = get_prompt(example)
input_ids = np.array( tokenizer.encode( input_text, max_length=args.max_length, truncation=True ) )
decoder_input_ids = np.array( tokenizer.encode( output_text, max_length=args.max_gen_length, truncation= True) )
labels = decoder_input_ids[1:]
decoder_input_ids = decoder_input_ids[:-1]
return {
"input_ids":input_ids.tolist() ,
"decoder_input_ids":decoder_input_ids.tolist() ,
"labels":labels.tolist()
}
class DataCollator:
def __init__(self, pad_token_id):
self.pad_token_id = pad_token_id
def __call__(self, examples ):
input_ids_list = [ example["input_ids"] for example in examples ]
decoder_input_ids_list = [ example["decoder_input_ids"] for example in examples ]
labels_list = [ example["labels"] for example in examples ]
padded_input_ids_length = max( [ len(input_ids) for input_ids in input_ids_list] )
padded_decoder_length = max( [ len(input_ids) for input_ids in decoder_input_ids_list] )
padded_input_ids_list = []
attention_mask_list = []
padded_decoder_input_ids_list = []
decoder_attention_mask_list = []
padded_labels_list = []
for input_ids, decoder_input_ids, labels in zip(input_ids_list, decoder_input_ids_list, labels_list):
attention_mask = [ 1 ] * len( input_ids ) + [ 0 ] * ( padded_input_ids_length - len( input_ids ) )
padded_input_ids = input_ids + [ self.pad_token_id ] * ( padded_input_ids_length - len(input_ids) )
decoder_attention_mask = [ 1 ] * len( decoder_input_ids ) + [ 0 ] * ( padded_decoder_length - len( decoder_input_ids ) )
padded_decoder_input_ids = decoder_input_ids + [ self.pad_token_id ] * ( padded_decoder_length - len( decoder_input_ids ) )
padded_labels = labels + [ -100 ] * ( padded_decoder_length - len( labels ) )
padded_input_ids_list.append( padded_input_ids )
attention_mask_list.append( attention_mask )
padded_decoder_input_ids_list.append( padded_decoder_input_ids )
decoder_attention_mask_list.append( decoder_attention_mask )
padded_labels_list.append( padded_labels )
return {
"input_ids": torch.LongTensor( padded_input_ids_list ),
"attention_mask": torch.LongTensor( attention_mask_list ),
"decoder_input_ids": torch.LongTensor( padded_decoder_input_ids_list ),
"decoder_attention_mask": torch.LongTensor( decoder_attention_mask_list ),
"labels": torch.LongTensor( padded_labels_list )
}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_path", type=str, )
parser.add_argument("--output_dir", type=str, )
parser.add_argument("--streaming", type = int, default = 0 )
parser.add_argument("--log_freq", type=int, default=50 )
parser.add_argument("--batch_size", type = int, default = 16)
parser.add_argument("--gradient_accumulation_steps", type = int, default = 1)
parser.add_argument("--max_steps", type = int, default = 5000, help = "if max_steps is set to -1, then num_train_epochs is effective")
parser.add_argument("--eval_freq", type=int, default=1000 )
parser.add_argument("--save_freq", type=int, default=1000 )
parser.add_argument("--train_dataset_name", type=str, default="data/train.jsonl")
parser.add_argument("--val_dataset_name", type=str, default="data/val.jsonl")
parser.add_argument("--shuffle_buffer", type=int, default = 5000)
parser.add_argument("--max_length", type = int, default = 1024)
parser.add_argument("--max_gen_length", type = int, default = 200)
parser.add_argument("--num_train_epochs", type = int, default = 3, help = "To use num_train_epochs, we need to know the total length of the dataset. This is not compatible with the IterableDataset.")
parser.add_argument("--learning_rate", type = float, default = 1e-5)
parser.add_argument("--lr_scheduler_type", type = str, default = "cosine")
parser.add_argument("--num_warmup_steps", type=int, default=500)
parser.add_argument("--weight_decay", type=float, default=0.05)
parser.add_argument("--local_rank", type=int, default=0)
parser.add_argument("--fp16", type = int, default = 1)
parser.add_argument("--bf16", type = int, default = 0)
parser.add_argument("--gradient_checkpointing", type = int, default = 1)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--num_workers", type=int, default=8)
args = parser.parse_args()
# In[3]:
# import re
# para_matcher = re.compile('"--(.*?)"')
# value_matcher = re.compile('default\s*=\s*(.*?)[\s\),]')
# for line in """
# parser = argparse.ArgumentParser()
# parser.add_argument("--model_path", type=str, )
# parser.add_argument("--output_dir", type=str, )
# parser.add_argument("--streaming", type = int, default = 0 )
# parser.add_argument("--log_freq", type=int, default=50 )
# parser.add_argument("--batch_size", type = int, default = 16 )
# parser.add_argument("--gradient_accumulation_steps", type = int, default = 1 )
# parser.add_argument("--max_steps", type = int, default = 5000, help = "if max_steps is set to -1, then num_train_epochs is effective")
# parser.add_argument("--eval_freq", type=int, default=1000 )
# parser.add_argument("--save_freq", type=int, default=1000 )
# parser.add_argument("--train_dataset_name", type=str, default="data/train_sft.jsonl" )
# parser.add_argument("--val_dataset_name", type=str, default="data/val.jsonl" )
# parser.add_argument("--shuffle_buffer", type=int, default = 5000 )
# parser.add_argument("--max_length", type = int, default = 1024 )
# parser.add_argument("--num_train_epochs", type = int, default = 3, help = "To use num_train_epochs, we need to know the total length of the dataset. This is not compatible with the IterableDataset.")
# parser.add_argument("--learning_rate", type = float, default = 1e-4 )
# parser.add_argument("--lr_scheduler_type", type = str, default = "cosine" )
# parser.add_argument("--num_warmup_steps", type=int, default=500 )
# parser.add_argument("--weight_decay", type=float, default=0.05 )
# parser.add_argument("--local_rank", type=int, default=0 )
# parser.add_argument("--fp16", type = int, default = 1 )
# parser.add_argument("--bf16", type = int, default = 0 )
# parser.add_argument("--gradient_checkpointing", type = int, default = 1 )
# parser.add_argument("--seed", type=int, default=0 )
# parser.add_argument("--num_workers", type=int, default=8 )
# """.split("\n"):
# para = (para_matcher.findall(line)+[None])[0]
# if para is not None:
# value = (value_matcher.findall(line) + [""])[0]
# print("args.%s = %s"%( para, value ))
# In[4]:
set_seed(args.seed)
os.makedirs(args.output_dir, exist_ok=True)
logging.set_verbosity_error()
tokenizer = AutoTokenizer.from_pretrained(args.model_path)
train_data = load_dataset('json',
data_files = args.train_dataset_name,
split = 'train',
num_proc = args.num_workers if not args.streaming else None,
streaming = args.streaming
)
# In[5]:
if args.streaming:
train_data = train_data.shuffle(buffer_size=args.shuffle_buffer, seed=args.seed)
else:
train_data = train_data.shuffle( seed=args.seed)
valid_data = load_dataset('json',
data_files = args.val_dataset_name,
split = 'train',
num_proc = args.num_workers if not args.streaming else None,
streaming = args.streaming
)
# In[6]:
for example in train_data:
break
exsiting_columns = list(example.keys())
# In[7]:
train_dataset = train_data.map( tokenize_example, remove_columns = exsiting_columns )
valid_dataset = valid_data.map( tokenize_example, remove_columns = exsiting_columns )
# In[8]:
data_collator = DataCollator(tokenizer.pad_token_id)
# In[9]:
print("Loading the model")
# In[10]:
model = BartForConditionalGeneration.from_pretrained(args.model_path).to( "cuda:%d"%( Accelerator().process_index ) )
# In[11]:
print_trainable_parameters( model)
# In[12]:
training_args = TrainingArguments(
output_dir=args.output_dir,
dataloader_drop_last=True,
evaluation_strategy="no" if args.eval_freq <= 0 else "steps",
max_steps=args.max_steps,
num_train_epochs=args.num_train_epochs,
eval_steps=args.eval_freq,
save_steps=args.save_freq,
logging_steps=args.log_freq,
per_device_train_batch_size=args.batch_size,
per_device_eval_batch_size=args.batch_size,
learning_rate=args.learning_rate,
lr_scheduler_type=args.lr_scheduler_type,
warmup_steps=args.num_warmup_steps,
gradient_accumulation_steps=args.gradient_accumulation_steps,
gradient_checkpointing=args.gradient_checkpointing,
fp16=args.fp16,
bf16=args.bf16,
weight_decay=args.weight_decay,
run_name="sft-citation-generator-seq2seq",
report_to="wandb",
ddp_find_unused_parameters=False,
)
# In[13]:
trainer = Trainer(
model=model,
args=training_args,
tokenizer = tokenizer,
train_dataset=train_dataset,
eval_dataset=valid_dataset,
data_collator = data_collator,
)
# In[ ]:
print("Training...")
trainer.train()
# In[ ]:
print("Saving last checkpoint of the model")
trainer.model.save_pretrained(os.path.join(args.output_dir, "final_checkpoint/"))
# In[ ]: