-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_KL.py
198 lines (164 loc) · 7.55 KB
/
train_KL.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
import os
import sys
import random
from datetime import datetime
import numpy as np
# PyTorch
import torch
import torch.nn as nn
import torch.optim as optim
# HuggingFace
from transformers import BartTokenizer
from datasets import load_dataset
# This project
from models.efficient_decoder_expC import BartEfficientDecoder
from utils import get_boundary_matrix, adjust_lr
from utils import parse_config, print_config
def run_training(config_path):
# Load Config
config = parse_config("config", config_path)
print_config(config)
# uses GPU in training or not
if torch.cuda.is_available() and config['use_gpu']: torch_device = 'cuda'
else: torch_device = 'cpu'
num_heads = config['num_heads']
num_layers = config['num_layers']
eos_id = config['eos_id']
batch_size = config['batch_size']
lr0 = config['lr0']
warmup = config['warmup']
temperature = config['temperature']
eps = config['eps']
gradient_accum = config['gradient_accum']
valid_step = config['valid_step']
total_step = config['total_step']
# early_stop = config['early_stop'] # currently we validation script to do this separately -- save time in training
random_seed = config['random_seed']
task = config['task'] # CNNDM | XSUM
max_target_len = config['max_target_len']
num_encoder_sent_nn_layers = config['num_encoder_sent_nn_layers']
bart_tokenizer = BartTokenizer.from_pretrained(config['bart_tokenizer'])
bart = BartEfficientDecoder.from_pretrained(config['bart_weights'])
bart.sent_nn_init(num_encoder_sent_nn_layers)
bart.swap_crossattn_to_hier(num_layers)
if torch_device == 'cuda': bart.cuda()
for p in bart.parameters(): p.requires_grad = False
for p in bart.encoder_sent_nn.parameters(): p.requires_grad = True
for key, p in bart.named_parameters():
if "sent_q" in key or "sent_k" in key: p.requires_grad = True
print("#parameters:", sum(p.numel() for p in bart.parameters() if p.requires_grad))
bart_config = bart.config
bart.config.output_attentions = True
bart.model.encoder.output_attentions = True
bart.model.decoder.output_attentions = True
for i in range(num_layers):
bart.model.encoder.layers[i].output_attentions = True
bart.model.decoder.layers[i].output_attentions = True
if task == 'CNNDM':
train_data = load_dataset('cnn_dailymail', '3.0.0', split='train')
print("cnndm data loaded")
elif task == 'XSUM':
train_data = load_dataset('xsum', split='train')
print("xsum data loaded")
else:
raise ValueError("task not supported, only CNNDM | XSUM")
# Optimizer --- currently only support Adam
if config['optimizer'] == 'adam':
# lr here doesn't matter as it will be changed by .adjust_lr()
optimizer = optim.Adam(filter(lambda p: p.requires_grad, bart.parameters()), lr=0.001,betas=(0.9,0.999),eps=1e-08,weight_decay=0)
optimizer.zero_grad()
else:
raise ValueError("Current version only supports Adam")
# Criterion
criterion = nn.KLDivLoss(reduction='mean')
training_step = 0
best_val_loss = 9e9
stop_counter = 0
# Randomness
random.seed(random_seed)
torch.manual_seed(random_seed)
assert batch_size == 1, "batch_size > 1 not supported"
epoch_size = len(train_data)
print("epoch_size:", epoch_size)
instance_ids = [i for i in range(epoch_size)]
random.shuffle(instance_ids)
# by default, it's not training!!!
bart.train()
while training_step < total_step:
# batch_size = 1
if len(instance_ids) > 0:
i = instance_ids.pop()
else:
instance_ids = [i for i in range(epoch_size)]
random.shuffle(instance_ids)
i = instance_ids.pop()
if task == 'CNNDM':
document = train_data[i]['article']
summary = train_data[i]['highlights']
elif task == 'XSUM':
document = train_data[i]['document']
summary = train_data[i]['summary']
try:
# some training data instances are corrupted
batch_encoded_inputs = bart_tokenizer.batch_encode_plus([document], return_tensors='pt',
add_special_tokens=True, max_length=bart.config.max_position_embeddings, pad_to_max_length=False)
input_ids = batch_encoded_inputs['input_ids'].to(torch_device)
batch_encoded_target = bart_tokenizer.batch_encode_plus([summary], return_tensors='pt',
add_special_tokens=True, max_length=max_target_len, pad_to_max_length=False)
target_ids = batch_encoded_target['input_ids'].to(torch_device)
except IndexError:
print("[IndexError]: id = {}".format(i))
continue
# find boundaries
_input_ids = input_ids[0].cpu().numpy()
# boundary['boundary'].shape => [num_head, num_words, num_sent]
boundary = get_boundary_matrix(_input_ids, num_heads*batch_size, eos_id)
bart_outputs = bart(input_ids=input_ids, decoder_input_ids=target_ids, boundary=boundary)
# bart_outputs[0] --- predictive distribution (batch_size, target_len, vocab_size)
# bart_outputs[1] --- decoder cross attention [(batch_size, num_heads, target_len, num_sent)] x num_layers
# bart_outputs[2] --- encoder outputs (batch_size, input_len, d)
# bart_outputs[3] --- encoder self-attention [(batch_size, num_heads, input_len, input_len)] x num_layers
# lm_logits = bart_outputs[0]
sent_attns = bart_outputs[1]
num_sentences = len(boundary['sentid2wordid'])
loss = 0
for l in range(num_layers):
exact_attn = sent_attns[l]['exact_sent_attn_weights'].squeeze(0) # # (num_heads, tgt_len, num_sentences)
exact_attn_hot = torch.softmax(torch.log(exact_attn + eps) / temperature, dim=-1)
rnn_attn = sent_attns[l]['rnn_sent_attn_weights'].squeeze(0) # # (num_heads, tgt_len, num_sentences)
loss += criterion(torch.log(rnn_attn + eps).view(-1, num_sentences), exact_attn_hot.view(-1, num_sentences))
if torch.isinf(loss): # this works for both +ve and -ve inf
print("Loss is inf: id = {}".format(i))
training_step += 1
continue
if torch.isnan(loss):
print("Loss is NaN: id = {}".format(i))
training_step += 1
continue
loss.backward()
if training_step % gradient_accum == 0:
adjust_lr(optimizer, training_step, lr0, warmup)
optimizer.step()
optimizer.zero_grad()
if training_step % 1 == 0:
print("[{}] step {}/{}: loss = {:.8f}".format(str(datetime.now()), training_step, total_step, loss))
sys.stdout.flush()
if training_step % valid_step == 0:
state = {
'training_step': training_step,
'model': bart.state_dict(),
'optimizer': optimizer.state_dict(),
'best_val_loss': best_val_loss
}
savepath = "{}/{}-step{}.pt".format(config['save_dir'], config['model_name'], training_step)
torch.save(state, savepath)
del state
torch.cuda.empty_cache()
print("Saved at {}".format(savepath))
training_step += 1
print("finish the experiment!")
if __name__ == "__main__":
if(len(sys.argv) == 2):
run_training(sys.argv[1])
else:
print("Usage: python train_KL.py config_path")