-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_finetune.py
403 lines (341 loc) · 16.4 KB
/
run_finetune.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# coding=utf-8
# BERT-Japanease Pretrain
#
# This program pre-trains the Japanese version of BERT.
# As for the learning method, the following FEATUREs were
# added from the original BERT with reference to RoBERTa.
# - dynamic mask
# - not use NSP
# - FULL-SENTENCES trainig
# - optimize batch size and learning ratio
# Use Japanese BPE Encoder for word-separation and encoding.
#
# REFERENCEs:
# BERT(original): https://github.com/google-research/bert
# Japanese BPE Encoder: https://github.com/tanreinama/Japanese-BPEEncoder
# RoBERTa: https://arxiv.org/pdf/1907.11692.pdf
#
# This software includes the work that is distributed in the Apache License 2.0
import argparse
import json
import os
import numpy as np
import tensorflow.compat.v1 as tf
import time
import tqdm
from copy import copy
from tensorflow.core.protobuf import rewriter_config_pb2
from model import modeling
from model.modeling import BertConfig, BertModel
CHECKPOINT_DIR = 'checkpoint'
SAMPLE_DIR = 'samples'
parser = argparse.ArgumentParser(
description='Pretraining BERT(RoBERTa) on your custom dataset.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--dataset', metavar='PATH', type=str, required=True, help='Input npz file')
parser.add_argument('--base_model', type=str, default='RoBERTa-ja_small', help='a path to a model file')
parser.add_argument('--batch_size', metavar='SIZE', type=int, default=1, help='Batch size')
parser.add_argument('--optim', type=str, default='adam', help='"adam", "adagrad", or "sgd" to use optimizer')
parser.add_argument('--learning_rate', metavar='LR', type=float, default=5e-5, help='Learning rate for optimizer')
parser.add_argument('--warmup_steps', metavar='WR', type=int, default=0, help='Learning rate warming up steps')
parser.add_argument('--run_name', type=str, default='RoBERTa-ja_finetune', help='Run id. Name of subdirectory in checkpoint/ and samples/')
parser.add_argument('--save_every', metavar='N', type=int, default=1000, help='Write a checkpoint every N steps')
parser.add_argument('--max_predictions_per_seq', default=160, type=int, help='Maximum number of mask words')
parser.add_argument('--gpu', default='0', help='visible gpu number.')
def maketree(path):
try:
os.makedirs(path)
except:
pass
def get_masked_lm_output(bert_config, input_tensor, output_weights, positions,
label_ids, label_weights):
"""Get loss and log probs for the masked LM."""
input_tensor = gather_indexes(input_tensor, positions)
with tf.variable_scope("cls/predictions"):
# We apply one more non-linear transformation before the output layer.
# This matrix is not used after pre-training.
with tf.variable_scope("transform"):
input_tensor = tf.layers.dense(
input_tensor,
units=bert_config.hidden_size,
activation=modeling.get_activation(bert_config.hidden_act),
kernel_initializer=modeling.create_initializer(
bert_config.initializer_range))
input_tensor = modeling.layer_norm(input_tensor)
# The output weights are the same as the input embeddings, but there is
# an output-only bias for each token.
output_bias = tf.get_variable(
"output_bias",
shape=[bert_config.vocab_size],
initializer=tf.zeros_initializer())
logits = tf.matmul(input_tensor, output_weights, transpose_b=True)
logits = tf.nn.bias_add(logits, output_bias)
log_probs = tf.nn.log_softmax(logits, axis=-1)
label_ids = tf.reshape(label_ids, [-1])
label_weights = tf.reshape(label_weights, [-1])
one_hot_labels = tf.one_hot(
label_ids, depth=bert_config.vocab_size, dtype=tf.float32)
# The `positions` tensor might be zero-padded (if the sequence is too
# short to have the maximum number of predictions). The `label_weights`
# tensor has a value of 1.0 for every real prediction and 0.0 for the
# padding predictions.
per_example_loss = -tf.reduce_sum(log_probs * one_hot_labels, axis=[-1])
numerator = tf.reduce_sum(label_weights * per_example_loss)
denominator = tf.reduce_sum(label_weights) + 1e-5
loss = numerator / denominator
return (loss, per_example_loss, log_probs)
def get_next_sentence_output(bert_config, input_tensor, labels):
"""Get loss and log probs for the next sentence prediction."""
# Simple binary classification. Note that 0 is "next sentence" and 1 is
# "random sentence". This weight matrix is not used after pre-training.
with tf.variable_scope("cls/seq_relationship"):
output_weights = tf.get_variable(
"output_weights",
shape=[2, bert_config.hidden_size],
initializer=modeling.create_initializer(bert_config.initializer_range))
output_bias = tf.get_variable(
"output_bias", shape=[2], initializer=tf.zeros_initializer())
logits = tf.matmul(input_tensor, output_weights, transpose_b=True)
logits = tf.nn.bias_add(logits, output_bias)
log_probs = tf.nn.log_softmax(logits, axis=-1)
labels = tf.reshape(labels, [-1])
one_hot_labels = tf.one_hot(labels, depth=2, dtype=tf.float32)
per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)
loss = tf.reduce_mean(per_example_loss)
return (loss, per_example_loss, log_probs)
def gather_indexes(sequence_tensor, positions):
"""Gathers the vectors at the specific positions over a minibatch."""
sequence_shape = modeling.get_shape_list(sequence_tensor, expected_rank=3)
batch_size = sequence_shape[0]
seq_length = sequence_shape[1]
width = sequence_shape[2]
flat_offsets = tf.reshape(
tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1, 1])
flat_positions = tf.reshape(positions + flat_offsets, [-1])
flat_sequence_tensor = tf.reshape(sequence_tensor,
[batch_size * seq_length, width])
output_tensor = tf.gather(flat_sequence_tensor, flat_positions)
return output_tensor
def main():
args = parser.parse_args()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = args.gpu
config.graph_options.rewrite_options.layout_optimizer = rewriter_config_pb2.RewriterConfig.OFF
vocab_size = 20573 + 3 # [MASK] [CLS] [SEP]
EOT_TOKEN = vocab_size - 4
MASK_TOKEN = vocab_size - 3
CLS_TOKEN = vocab_size - 2
SEP_TOKEN = vocab_size - 1
max_predictions_per_seq = args.max_predictions_per_seq
batch_size = args.batch_size
with tf.Session(config=config) as sess:
input_ids = tf.placeholder(tf.int32, [batch_size, None])
input_mask = tf.placeholder(tf.int32, [batch_size, None])
segment_ids = tf.placeholder(tf.int32, [batch_size, None])
masked_lm_positions = tf.placeholder(tf.int32, [batch_size, None])
masked_lm_ids = tf.placeholder(tf.int32, [batch_size, None])
masked_lm_weights = tf.placeholder(tf.float32, [batch_size, None])
next_sentence_labels = tf.placeholder(tf.int32, [None])
if os.path.isfile(args.base_model+'/hparams.json'):
with open(args.base_model+'/hparams.json') as f:
bert_config_params = json.loads(f.read())
else:
raise ValueError('invalid model name.')
max_seq_length = bert_config_params['max_position_embeddings']
bert_config = BertConfig(**bert_config_params)
model = BertModel(
config=bert_config,
is_training=True,
input_ids=input_ids,
input_mask=input_mask,
use_one_hot_embeddings=False)
(masked_lm_loss,_,_) = get_masked_lm_output(
bert_config, model.get_sequence_output(), model.get_embedding_table(),
masked_lm_positions, masked_lm_ids, masked_lm_weights)
(next_sentence_loss,_,_) = get_next_sentence_output(
bert_config, model.get_pooled_output(), next_sentence_labels)
loss = masked_lm_loss + next_sentence_loss
train_vars = tf.trainable_variables()
global_step = tf.Variable(0, trainable=False)
if args.warmup_steps > 0:
learning_rate = tf.compat.v1.train.polynomial_decay(
learning_rate=1e-10,
end_learning_rate=args.learning_rate,
global_step=global_step,
decay_steps=args.warmup_steps
)
else:
learning_rate = args.learning_rate
if args.optim=='adam':
opt = tf.train.AdamOptimizer(learning_rate=learning_rate,
beta1=0.9,
beta2=0.98,
epsilon=1e-7)
elif args.optim=='adagrad':
opt = tf.train.AdagradOptimizer(learning_rate=learning_rate)
elif args.optim=='sgd':
opt = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
else:
raise ValueError('invalid optimizer name.')
train_vars = tf.trainable_variables()
opt_grads = tf.gradients(loss, train_vars)
opt_grads = list(zip(opt_grads, train_vars))
opt_apply = opt.apply_gradients(opt_grads)
summaries = tf.summary.scalar('loss', loss)
summary_log = tf.summary.FileWriter(
os.path.join(CHECKPOINT_DIR, args.run_name))
saver = tf.train.Saver(
var_list=train_vars,
max_to_keep=5,
keep_checkpoint_every_n_hours=2)
sess.run(tf.global_variables_initializer())
ckpt = tf.train.latest_checkpoint(args.base_model)
saver.restore(sess, ckpt)
print('Loading checkpoint', ckpt)
print('Loading dataset...')
global_chunks = np.load(args.dataset)
global_chunk_index = copy(global_chunks.files)
global_chunk_step = 0
global_epochs = 0
np.random.shuffle(global_chunk_index)
def get_epoch():
return global_epochs + (1 - len(global_chunk_index) / len(global_chunks.files))
def pop_feature():
nonlocal global_chunks,global_chunk_index,global_chunk_step, global_epochs
# FULL-SENTENCES
token = [np.uint16(CLS_TOKEN)]
chunk = global_chunks[global_chunk_index[-1]][global_chunk_step:].astype(np.uint16)
if len(chunk) >= max_seq_length-1:
token.extend(chunk[:max_seq_length-1].tolist())
global_chunk_step += max_seq_length-1
else:
if len(chunk) > 0:
token.extend(chunk.tolist())
token.append(np.uint16(EOT_TOKEN))
global_chunk_step += len(chunk)+1
while len(token) < max_seq_length:
global_chunk_index.pop()
global_chunk_step = 0
if len(global_chunk_index) == 0:
global_chunk_index = copy(global_chunks.files)
np.random.shuffle(global_chunk_index)
global_epochs += 1
cur = len(token)
chunk = global_chunks[global_chunk_index[-1]].astype(np.uint16)
token.extend(chunk[:max_seq_length-cur].tolist())
global_chunk_step += max_seq_length-cur
if len(token) < max_seq_length:
token.append(np.uint16(EOT_TOKEN))
return token
print('Training...')
def sample_feature():
nonlocal global_chunks,global_chunk_index,global_chunk_step
# Use dynamic mask
p_input_ids = []
p_input_mask = []
p_segment_ids = []
p_masked_lm_positions = []
p_masked_lm_ids = []
p_masked_lm_weights = []
p_next_sentence_labels = [0] * batch_size
for b in range(batch_size): # FULL-SENTENCES
sampled_token = pop_feature()
# Make Sequence
ids = copy(sampled_token)
masks = [1]*len(ids)
segments = [1]*len(ids)
# Make Masks
mask_indexs = []
for i in np.random.permutation(max_seq_length):
if ids[i] < EOT_TOKEN:
mask_indexs.append(i)
if len(mask_indexs) >= max_predictions_per_seq:
break
lm_positions = []
lm_ids = []
lm_weights = []
for i in sorted(mask_indexs):
masked_token = None
# 80% of the time, replace with [MASK]
if np.random.random() < 0.8:
masked_token = MASK_TOKEN # [MASK]
else:
# 10% of the time, keep original
if np.random.random() < 0.5:
masked_token = ids[i]
# 10% of the time, replace with random word
else:
masked_token = np.random.randint(EOT_TOKEN-1)
lm_positions.append(i)
lm_ids.append(ids[i])
lm_weights.append(1.0)
# apply mask
ids[i] = masked_token
while len(lm_positions) < max_predictions_per_seq:
lm_positions.append(0)
lm_ids.append(0)
lm_weights.append(0.0)
p_input_ids.append(ids)
p_input_mask.append(masks)
p_segment_ids.append(segments)
p_masked_lm_positions.append(lm_positions)
p_masked_lm_ids.append(lm_ids)
p_masked_lm_weights.append(lm_weights)
return {input_ids:p_input_ids,
input_mask:p_input_mask,
segment_ids:p_segment_ids,
masked_lm_positions:p_masked_lm_positions,
masked_lm_ids:p_masked_lm_ids,
masked_lm_weights:p_masked_lm_weights,
next_sentence_labels:p_next_sentence_labels}
counter = 1
counter_path = os.path.join(CHECKPOINT_DIR, args.run_name, 'counter')
if os.path.exists(counter_path):
# Load the step number if we're resuming a run
# Add 1 so we don't immediately try to save again
with open(counter_path, 'r') as fp:
counter = int(fp.read()) + 1
hparams_path = os.path.join(CHECKPOINT_DIR, args.run_name, 'hparams.json')
maketree(os.path.join(CHECKPOINT_DIR, args.run_name))
with open(hparams_path, 'w') as fp:
fp.write(json.dumps(bert_config_params))
def save():
maketree(os.path.join(CHECKPOINT_DIR, args.run_name))
print(
'Saving',
os.path.join(CHECKPOINT_DIR, args.run_name,
'model-{}').format(counter))
saver.save(
sess,
os.path.join(CHECKPOINT_DIR, args.run_name, 'model'),
global_step=counter)
with open(counter_path, 'w') as fp:
fp.write(str(counter) + '\n')
avg_loss = (0.0, 0.0)
start_time = time.time()
try:
while True:
if counter % args.save_every == 0:
save()
(_, v_loss, v_summary) = sess.run(
(opt_apply, loss, summaries),
feed_dict=sample_feature())
summary_log.add_summary(v_summary, counter)
avg_loss = (avg_loss[0] * 0.99 + v_loss,
avg_loss[1] * 0.99 + 1.0)
print(
'[{counter} | {time:2.2f}] loss={loss:2.2f} avg={avg:2.2f}'
.format(
counter=counter,
time=time.time() - start_time,
loss=v_loss,
avg=avg_loss[0] / avg_loss[1]))
counter = counter+1
if args.warmup_steps > 0:
global_step = global_step+1
except KeyboardInterrupt:
print('interrupted')
save()
if __name__ == '__main__':
main()