forked from emorynlp/coref-hoi
-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.py
362 lines (311 loc) · 16.7 KB
/
run.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import logging
import random
import subprocess
import tempfile
import numpy as np
import torch
from torch.utils.tensorboard import SummaryWriter
from transformers import AdamW
from torch.optim import Adam, SGD
import udapi_io
from tensorize import CorefDataProcessor, Tensorizer
import util
import time
from os.path import join
from metrics import CorefEvaluator
from datetime import datetime
from torch.optim.lr_scheduler import LambdaLR
from model import CorefModel
import conll
import sys
import tensorflow as tf
tf.config.set_visible_devices([], 'GPU')
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO)
logger = logging.getLogger()
def evaluate_coreud(gold_path, pred_path):
cmd = ["python", "corefud-scorer/corefud-scorer.py", gold_path, pred_path]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
process.wait()
stdout = stdout.decode("utf-8")
if stderr is not None:
logger.error(stderr)
logger.info("Official result for {}".format(pred_path))
logger.info(stdout)
cmd = ["python", "corefud-scorer/corefud-scorer.py", gold_path, pred_path, "-s"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
process.wait()
stdout = stdout.decode("utf-8")
if stderr is not None:
logger.error(stderr)
logger.info("Official result with singletons for {}".format(pred_path))
logger.info(stdout)
class Runner:
def __init__(self, config_name, gpu_id=0, seed=None):
self.name = config_name
self.name_suffix = datetime.now().strftime('%b%d_%H-%M-%S')
self.gpu_id = gpu_id
self.seed = seed
# Set up config
self.config = util.initialize_config(config_name)
# Set up logger
log_path = join(self.config['log_dir'], 'log_' + self.name_suffix + '.txt')
logger.addHandler(logging.FileHandler(log_path, 'a'))
logger.info('Log file path: %s' % log_path)
# Set up seed
if seed:
util.set_seed(seed)
# Set up device
self.device = torch.device('cpu' if gpu_id is None else f'cuda:{gpu_id}')
# Set up data
self.data = CorefDataProcessor(self.config, language=self.config.language)
def initialize_model(self, saved_suffix=None):
model = CorefModel(self.config, self.device)
if saved_suffix:
self.load_model_checkpoint(model, saved_suffix)
return model
def train(self, model):
best_model_path = None
conf = self.config
logger.info(conf)
epochs, grad_accum = conf['num_epochs'], conf['gradient_accumulation_steps']
model.to(self.device)
logger.info('Model parameters:')
for name, param in model.named_parameters():
logger.info('%s: %s' % (name, tuple(param.shape)))
# Set up tensorboard
tb_path = join(conf['tb_dir'], self.name + '_' + self.name_suffix)
tb_writer = SummaryWriter(tb_path, flush_secs=30)
logger.info('Tensorboard summary path: %s' % tb_path)
# Set up data
examples_train, examples_dev = self.data.get_tensor_examples()
stored_info = self.data.get_stored_info()
# Set up optimizer and scheduler
total_update_steps = len(examples_train) * epochs // grad_accum
optimizers = self.get_optimizer(model)
schedulers = self.get_scheduler(optimizers, total_update_steps)
# Get model parameters for grad clipping
bert_param, task_param = model.get_params()
# Start training
logger.info('*******************Training*******************')
logger.info('Num samples: %d' % len(examples_train))
logger.info('Num epochs: %d' % epochs)
logger.info('Gradient accumulation steps: %d' % grad_accum)
logger.info('Total update steps: %d' % total_update_steps)
loss_during_accum = [] # To compute effective loss at each update
loss_during_report = 0.0 # Effective loss during logging step
loss_history = [] # Full history of effective loss; length equals total update steps
max_f1 = 0
start_time = time.time()
model.zero_grad()
for epo in range(epochs):
random.shuffle(examples_train) # Shuffle training set
for doc_key, example in examples_train:
# Forward pass
model.train()
example_gpu = [d.to(self.device) for d in example]
torch.cuda.empty_cache()
_, loss = model(*example_gpu)
# Backward; accumulate gradients and clip by grad norm
if grad_accum > 1:
loss /= grad_accum
loss.backward()
if conf['max_grad_norm']:
torch.nn.utils.clip_grad_norm_(bert_param, conf['max_grad_norm'])
torch.nn.utils.clip_grad_norm_(task_param, conf['max_grad_norm'])
loss_during_accum.append(loss.item())
# Update
if len(loss_during_accum) % grad_accum == 0:
for optimizer in optimizers:
optimizer.step()
model.zero_grad()
for scheduler in schedulers:
scheduler.step()
# Compute effective loss
effective_loss = np.sum(loss_during_accum).item()
loss_during_accum = []
loss_during_report += effective_loss
loss_history.append(effective_loss)
# Report
if len(loss_history) % conf['report_frequency'] == 0:
# Show avg loss during last report interval
avg_loss = loss_during_report / conf['report_frequency']
loss_during_report = 0.0
end_time = time.time()
logger.info('Step %d: avg loss %.2f; steps/sec %.2f' %
(len(loss_history), avg_loss, conf['report_frequency'] / (end_time - start_time)))
start_time = end_time
tb_writer.add_scalar('Training_Loss', avg_loss, len(loss_history))
tb_writer.add_scalar('Learning_Rate_Bert', schedulers[0].get_last_lr()[0], len(loss_history))
tb_writer.add_scalar('Learning_Rate_Task', schedulers[1].get_last_lr()[-1], len(loss_history))
example_gpu = [e.detach().cpu() for e in example_gpu]
# Evaluate
if len(loss_history) > 0 and len(loss_history) % conf['eval_frequency'] == 0:
f1, _ = self.evaluate(model, examples_dev, stored_info, len(loss_history), official=False, conll_path=self.config['conll_eval_path'], tb_writer=tb_writer)
if f1 > max_f1:
max_f1 = f1
best_model_path = self.save_model_checkpoint(model, len(loss_history))
logger.info('Eval max f1: %.2f' % max_f1)
start_time = time.time()
logger.info('**********Finished training**********')
logger.info('Actual update steps: %d' % len(loss_history))
logger.info('**********Dev eval**********')
f1, _ = self.evaluate(model, examples_dev, stored_info, len(loss_history), official=False, conll_path=self.config['conll_eval_path'], tb_writer=tb_writer)
logger.info('**********Test eval**********')
f1, _ = self.evaluate(model, examples_dev, stored_info, len(loss_history), official=True, conll_path=self.config['conll_test_path'], tb_writer=tb_writer, save_predictions=join(self.config['log_dir'], self.name_suffix + "_predictions.conllu"))
if best_model_path is not None:
logger.info('**********Best model evaluation**********')
self.load_model_checkpoint(model, best_model_path[best_model_path.rindex("model_") + 6: best_model_path.rindex(".bin")])
self.evaluate(model, examples_dev, stored_info, 0, official=True, conll_path=self.config['conll_test_path'], save_predictions=join(self.config['log_dir'], self.name_suffix + "_predictions-best.conllu"))
# Wrap up
tb_writer.close()
return loss_history
def evaluate(self, model, tensor_examples, stored_info, step, official=False, conll_path=None, tb_writer=None, save_predictions=None):
model.to(self.device)
evaluator = CorefEvaluator()
doc_to_prediction = {}
model.eval()
max_sentences = self.config["max_training_sentences"] if "max_pred_sentences" not in self.config else self.config["max_pred_sentences"]
for i, (doc_key, tensor_example) in enumerate(tensor_examples):
gold_clusters = stored_info['gold'][doc_key]
tensor_example = tensor_example[:7] # Strip out gold
num_sentences = tensor_example[0].shape[0]
if num_sentences <= max_sentences:
batch_examples = [tensor_example]
else:
batch_examples = Tensorizer(self.config).split_example(*tensor_example)
predicted_clusters = []
mention_to_cluster_id = {}
for j, example in enumerate(batch_examples):
example_gpu = [d.to(self.device) for d in example]
with torch.no_grad():
_, _, _, span_starts, span_ends, antecedent_idx, antecedent_scores = model(*example_gpu)
sentence_len = tensor_example[3]
offset = j * max_sentences
word_offset = sentence_len[:offset].sum()
span_starts = span_starts + word_offset
span_ends = span_ends + word_offset
example_gpu = [e.detach().cpu() for e in example_gpu]
span_starts, span_ends = span_starts.tolist(), span_ends.tolist()
antecedent_idx, antecedent_scores = antecedent_idx.tolist(), antecedent_scores.tolist()
tmp_predicted_clusters, tmp_mention_to_cluster_id, _ = model.get_predicted_clusters(span_starts, span_ends, antecedent_idx, antecedent_scores)
predicted_clusters.extend(tmp_predicted_clusters)
mention_to_cluster_id = {**tmp_mention_to_cluster_id, **mention_to_cluster_id}
predicted_clusters = model.update_evaluator_from_clusters(predicted_clusters, mention_to_cluster_id, gold_clusters, evaluator)
if self.config["filter_singletons"]:
predicted_clusters = util.discard_singletons(predicted_clusters)
doc_to_prediction[doc_key] = predicted_clusters
p, r, f = evaluator.get_prf()
metrics = {'Eval_Avg_Precision': p * 100, 'Eval_Avg_Recall': r * 100, 'Eval_Avg_F1': f * 100}
for name, score in metrics.items():
logger.info('%s: %.2f' % (name, score))
if tb_writer:
tb_writer.add_scalar(name, score, step)
if official:
udapi_docs = udapi_io.map_to_udapi(udapi_io.read_data(self.config["conll_pred_path"]), doc_to_prediction, stored_info['subtoken_maps'])
if save_predictions is not None:
fd = open(save_predictions, "wt", encoding="utf-8")
else:
fd = tempfile.NamedTemporaryFile("w", delete=True, encoding="utf-8")
udapi_io.write_data(udapi_docs, fd)
fd.flush()
evaluate_coreud(self.config["conll_pred_path"], fd.name)
fd.close()
# with open(save_predictions, "w", encoding="utf-8") as w, open(self.config["conll_pred_path"], encoding="utf-8") as r:
# print("predicting...")
# conll.output_conll_corefud(r, w, doc_to_prediction, stored_info['subtoken_maps'])
return f * 100, metrics
def predict(self, model, tensor_examples):
logger.info('Predicting %d samples...' % len(tensor_examples))
model.to(self.device)
predicted_spans, predicted_antecedents, predicted_clusters = [], [], []
for i, tensor_example in enumerate(tensor_examples):
tensor_example = tensor_example[:7]
example_gpu = [d.to(self.device) for d in tensor_example]
with torch.no_grad():
_, _, _, span_starts, span_ends, antecedent_idx, antecedent_scores = model(*example_gpu)
span_starts, span_ends = span_starts.tolist(), span_ends.tolist()
antecedent_idx, antecedent_scores = antecedent_idx.tolist(), antecedent_scores.tolist()
clusters, mention_to_cluster_id, antecedents = model.get_predicted_clusters(span_starts, span_ends, antecedent_idx, antecedent_scores)
spans = [(span_start, span_end) for span_start, span_end in zip(span_starts, span_ends)]
predicted_spans.append(spans)
predicted_antecedents.append(antecedents)
predicted_clusters.append(clusters)
return predicted_clusters, predicted_spans, predicted_antecedents
def get_optimizer(self, model):
no_decay = ['bias', 'LayerNorm.weight']
bert_param, task_param = model.get_params(named=True)
grouped_bert_param = [
{
'params': [p for n, p in bert_param if not any(nd in n for nd in no_decay)],
'lr': self.config['bert_learning_rate'],
'weight_decay': self.config['adam_weight_decay']
}, {
'params': [p for n, p in bert_param if any(nd in n for nd in no_decay)],
'lr': self.config['bert_learning_rate'],
'weight_decay': 0.0
}
]
optimizers = [
AdamW(grouped_bert_param, lr=self.config['bert_learning_rate'], eps=self.config['adam_eps']),
Adam(model.get_params()[1], lr=self.config['task_learning_rate'], eps=self.config['adam_eps'], weight_decay=0)
# SGD(model.get_params()[1], lr=self.config['task_learning_rate'], weight_decay=0)
]
return optimizers
# grouped_parameters = [
# {
# 'params': [p for n, p in bert_param if not any(nd in n for nd in no_decay)],
# 'lr': self.config['bert_learning_rate'],
# 'weight_decay': self.config['adam_weight_decay']
# }, {
# 'params': [p for n, p in bert_param if any(nd in n for nd in no_decay)],
# 'lr': self.config['bert_learning_rate'],
# 'weight_decay': 0.0
# }, {
# 'params': [p for n, p in task_param if not any(nd in n for nd in no_decay)],
# 'lr': self.config['task_learning_rate'],
# 'weight_decay': self.config['adam_weight_decay']
# }, {
# 'params': [p for n, p in task_param if any(nd in n for nd in no_decay)],
# 'lr': self.config['task_learning_rate'],
# 'weight_decay': 0.0
# }
# ]
# optimizer = AdamW(grouped_parameters, lr=self.config['task_learning_rate'], eps=self.config['adam_eps'])
# return optimizer
def get_scheduler(self, optimizers, total_update_steps):
# Only warm up bert lr
warmup_steps = int(total_update_steps * self.config['warmup_ratio'])
def lr_lambda_bert(current_step):
if current_step < warmup_steps:
return float(current_step) / float(max(1, warmup_steps))
return max(
0.0, float(total_update_steps - current_step) / float(max(1, total_update_steps - warmup_steps))
)
def lr_lambda_task(current_step):
return max(0.0, float(total_update_steps - current_step) / float(max(1, total_update_steps)))
schedulers = [
LambdaLR(optimizers[0], lr_lambda_bert),
LambdaLR(optimizers[1], lr_lambda_task)
]
return schedulers
# return LambdaLR(optimizer, [lr_lambda_bert, lr_lambda_bert, lr_lambda_task, lr_lambda_task])
def save_model_checkpoint(self, model, step):
if step < 30000:
return # Debug
path_ckpt = join(self.config['log_dir'], f'model_{self.name_suffix}_{step}.bin')
torch.save(model.state_dict(), path_ckpt)
logger.info('Saved model to %s' % path_ckpt)
return path_ckpt
def load_model_checkpoint(self, model, suffix):
path_ckpt = join(self.config['log_dir'], f'model_{suffix}.bin')
model.load_state_dict(torch.load(path_ckpt, map_location=torch.device('cpu')), strict=False)
logger.info('Loaded model from %s' % path_ckpt)
if __name__ == '__main__':
config_name, gpu_id = sys.argv[1], int(sys.argv[2])
runner = Runner(config_name, gpu_id)
model = runner.initialize_model()
runner.train(model)