-
Notifications
You must be signed in to change notification settings - Fork 0
/
online_train_faststream.py
192 lines (159 loc) · 6.44 KB
/
online_train_faststream.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
# pylint: disable=import-error
import os
import csv
import shutil
import pytorch_lightning as pl
from transformers import T5Tokenizer
from online_evaluation import evaluate
from utils import load_dataset
import numpy as np
from dataset import CKLDataset as Dataset
from kilm_dataset import CKLDataset as KILM_Dataset
import time
def find_next_entry(start_idx, train_stream_df):
next_entry = None
for idx, row in train_stream_df.iterrows():
if idx >= start_idx:
if next_entry and next_entry != row['date'] or idx == len(train_stream_df) - 1:
return next_entry
next_entry = row['date']
return None
def train(args, Model):
method_times = {
"kadapter2": 9.09,
"lora": 9.68,
"modular": 10.98,
"recadam": 13.37,
"kd": 85.91,
"baseline": 10.03,
"mixreview": 10.82,
'kilm': 9.27
}
output_folder = ("/".join((args.output_log.split('/'))[:-1]))
if not os.path.isdir(output_folder):
os.makedirs(output_folder)
if os.path.exists(args.output_dir):
shutil.rmtree(args.output_dir)
train_stream_df = load_dataset('train', args)
test_stream_df = load_dataset('test', args)
model = Model(args)
if 't5' in args.model_name_or_path:
tokenizer = T5Tokenizer.from_pretrained(args.model_name_or_path)
else:
tokenizer = model.tokenizer
start_time = time.time()
collector = []
trainer = pl.Trainer(
logger=False,
accumulate_grad_batches=args.gradient_accumulation_steps,
accelerator='gpu',
enable_progress_bar=False,
max_epochs=args.num_train_epochs,
precision=16 if args.use_deepspeed else 32,
devices=args.n_gpu,
gradient_clip_val=args.max_grad_norm,
val_check_interval=args.val_check_interval,
callbacks=[],
enable_checkpointing=False,
strategy='ddp'
)
last_entry = None
bwt = []
fwt = []
acc = []
pre_metric = []
train_time = []
periods = []
first_time = True
writefile = open(f'{args.output_log}results.csv', 'a', newline='', encoding='utf-8')
acc_writefile = open(f'{args.output_log}acc.csv', 'a', newline='', encoding='utf-8')
writer = csv.writer(writefile)
acc_writer = csv.writer(acc_writefile)
writer.writerow(["Date", "EM", "BWT", "FWT", "Time"])
acc_writer.writerow(["Date", "EM"])
writefile.flush()
acc_writefile.flush()
flag = True
if args.method == 'kilm':
CKLDataset = KILM_Dataset
else:
CKLDataset = Dataset
for idx, row in train_stream_df.iterrows():
if last_entry and last_entry != row['date'] or idx == len(train_stream_df) - 1:
repeat_num = args.repeat_num
if args.model_name_or_path != 'initial':
data_count = len(collector)
using_count = int(data_count * (8 / method_times[args.method]))
collector = collector[:using_count]
model.set_dataset(CKLDataset(collector, 'train', tokenizer, args))
if trainer.global_rank == 0:
print('=' * 50)
print('=' * 50)
print('Training -', last_entry)
print(f"Repeating number: {repeat_num}")
print(f"Coreset method: {args.coreset}")
print(f"Coreset ratio: {args.coreset_ratio}")
start_train = time.time()
if args.method != 'initial':
trainer.fit(model)
trainer.fit_loop.max_epochs += args.num_train_epochs
if trainer.global_rank == 0:
train_time.append(time.time() - start_train)
print(f'TRAIN TIME:{train_time[-1]}')
collector = []
if first_time:
periods.append(last_entry)
next_entry = find_next_entry(idx, train_stream_df)
periods.append(next_entry)
if trainer.global_rank == 0:
if args.method != 'initial':
print(f"For periods: {periods}")
metrics, e_time = evaluate(args, model, test_stream_df[test_stream_df['date'].isin(periods)],
tokenizer, trainer.global_rank)
metrics = np.array(metrics)
if len(periods) > 2:
# metric, knowledge:
# 1-1 1-2
# 2-1 2-2 2-3
# 3-1 3-2 3-3 3-4
# 4-1 4-2 4-3 4-4 None
diff = metrics[:len(pre_metric)][-2:] - pre_metric[-2:]
bwt_res = diff[0]
fwt_res = diff[1]
bwt.append(bwt_res)
fwt.append(fwt_res)
print('BWT:', bwt_res)
print('FWT:', fwt_res)
pre_metric = metrics
acc.append(metrics[-2])
print('ACC:', acc[-1])
writer = csv.writer(writefile)
acc_writer = csv.writer(acc_writefile)
if first_time:
writer.writerow([periods[-2], acc[-1], None, None, train_time[-1]])
first_time = False
writefile.flush()
acc_writer.writerow(metrics[:-1])
acc_writefile.flush()
else:
writer.writerow([periods[-2], acc[-1], bwt[-1], fwt[-1], train_time[-1]])
writefile.flush()
acc_writer.writerow(metrics[:-1])
acc_writefile.flush()
else:
if idx == len(train_stream_df) - 1:
print(f"For periods: {periods}")
metrics, e_time = evaluate(args, model, test_stream_df[test_stream_df['date'].isin(periods)],
tokenizer, trainer.global_rank)
metrics = np.array(metrics)
acc_writer.writerow(metrics[:-1])
acc_writefile.flush()
trainer.strategy.barrier()
collector.append(row.to_dict())
last_entry = row['date']
trainer.strategy.barrier()
if trainer.global_rank == 0:
total_time = time.time() - start_time
print('Total time:', total_time)
writefile.close()
acc_writefile.close()