-
Notifications
You must be signed in to change notification settings - Fork 416
/
train_flash_atten.py
130 lines (106 loc) · 3.48 KB
/
train_flash_atten.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
# Copyright © 2022 BAAI. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License")
import sys
import os
import torch
from torch.utils.data import Dataset
from flagai.auto_model.auto_loader import AutoLoader
from flagai.trainer import Trainer
from flagai.data.collate_utils import seq2seq_collate_fn as title_generation_collate_fn
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
cur_dir = os.path.dirname(os.path.abspath(__file__))
train_path = cur_dir + "/data/news.tsv"
# single gpu
trainer = Trainer(
env_type="pytorch",
experiment_name="bert-title-generation",
batch_size=32,
gradient_accumulation_steps=1,
lr=1e-5,
weight_decay=1e-3,
epochs=10,
log_interval=1,
eval_interval=10,
load_dir=None,
pytorch_device=device,
save_dir="checkpoints-bert-title-generation-en",
checkpoint_activations=False,
save_interval=1000,
fp16 = True)
model_dir = "../state_dict/" # download_path for the model
os.makedirs(model_dir, exist_ok=True)
maxlen = 256
auto_loader = AutoLoader(
"title-generation",
model_name="BERT-base-en",
model_dir=model_dir,
)
model = auto_loader.get_model()
tokenizer = auto_loader.get_tokenizer()
def read_file():
src = []
tgt = []
index = 0
with open(train_path, 'r', encoding='utf-8') as f:
for line in f:
index += 1
if index == 1:
continue
line = line.strip('\n').split('\t')
src_list = line[4].split(" ")
if len(src_list) > 510:
src_list = src_list[:510]
line[4]=" ".join(src_list)
src.append(line[4])
tgt.append(line[3])
if index == 100000:
break
return src, tgt
class BertTitleGenerationDataset(Dataset):
def __init__(self, sents_src, sents_tgt, tokenizer, maxlen=512):
super(BertTitleGenerationDataset, self).__init__()
self.sents_src = sents_src
self.sents_tgt = sents_tgt
self.tokenizer = tokenizer
self.maxlen = maxlen
def __getitem__(self, i):
src = self.sents_src[i]
tgt = self.sents_tgt[i]
data = self.tokenizer.encode_plus(src,
tgt,
max_length=self.maxlen,
truncation=True)
output = {
"input_ids": data["input_ids"],
"segment_ids": data["token_type_ids"],
"flash_atten": model.config["enable_flash_atten"],
}
return output
def __len__(self):
return len(self.sents_src)
sents_src, sents_tgt = read_file()
print(sents_src[0])
print(sents_tgt[0])
print(len(sents_src))
data_len = len(sents_tgt)
train_size = data_len
# train_size = int(data_len * 0.9)
train_src = sents_src[:train_size]
train_tgt = sents_tgt[:train_size]
val_src = sents_src[train_size:]
val_tgt = sents_tgt[train_size:]
train_dataset = BertTitleGenerationDataset(train_src,
train_tgt,
tokenizer=tokenizer,
maxlen=maxlen)
# val_dataset = BertTitleGenerationDataset(val_src,
# val_tgt,
# tokenizer=tokenizer,
# maxlen=maxlen)
trainer.train(
model,
train_dataset=train_dataset,
# valid_dataset=val_dataset,
collate_fn=title_generation_collate_fn,
)