-
Notifications
You must be signed in to change notification settings - Fork 0
/
random-baseline.py
364 lines (313 loc) · 12.2 KB
/
random-baseline.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
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import torch
import seaborn as sns
import transformers
import json
import glob
from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader
from transformers import RobertaModel, RobertaTokenizer
import logging
from transformers import AutoTokenizer, AutoModelForMaskedLM, AutoModel
logging.basicConfig(level=logging.ERROR)
from tokenizers import ByteLevelBPETokenizer
from tokenizers.processors import BertProcessing
import wandb
from torch import cuda
device = 'cuda' if cuda.is_available() else 'cpu'
from pathlib import Path
from typing import List, Dict, Any, Tuple
import yaml
import random
import argparse
wandb.init(project="cds", entity="adityay")
def load_sentences_from_file(file_path: Path,
include_punctuation: bool = True,
allow_discard: bool = False,
) -> List[str]:
"""
load sentences for language modeling from text file
"""
print(f'Loading {file_path}', flush=True)
res = []
num_too_small = 0
with open(file_path, 'r') as line_by_line_file:
for sentence in line_by_line_file.readlines():
if not sentence: # during probing, parsing logic above may produce empty sentences
continue
sentence = sentence.rstrip('\n')
# check length
if sentence.count(' ') < 3 - 1 and allow_discard:
num_too_small += 1
continue
if not include_punctuation:
sentence = sentence.rstrip('.')
sentence = sentence.rstrip('!')
sentence = sentence.rstrip('?')
res.append(sentence)
if num_too_small:
print(f'WARNING: Skipped {num_too_small:,} sentences which are shorter than {3}.')
return res
from itertools import islice
def make_sequences(sentences: List[str],
num_sentences_per_input: int,
) -> List[str]:
gen = (bs for bs in sentences)
# combine multiple sentences into 1 sequence
res = []
while True:
sentences_in_sequence: List[str] = list(islice(gen, 0, num_sentences_per_input))
if not sentences_in_sequence:
break
sequence = ' '.join(sentences_in_sequence)
res.append(sequence)
print(f'Num total sequences={len(res):,}', flush=True)
return res
def get_perplexity(model, tokenizer, sentence):
tensor_input = tokenizer.encode(sentence, return_tensors='pt')
repeat_input = tensor_input.repeat(tensor_input.size(-1)-2, 1)
mask = torch.ones(tensor_input.size(-1) - 1).diag(1)[:-2]
masked_input = repeat_input.masked_fill(mask == 1, tokenizer.mask_token_id)
labels = repeat_input.masked_fill(masked_input != tokenizer.mask_token_id, -100)
with torch.inference_mode():
loss = model(masked_input.cuda(), labels=labels.cuda()).loss
return np.exp(loss.item())
from datasets import Dataset, DatasetDict
from transformers.models.roberta import RobertaConfig, RobertaForMaskedLM, RobertaTokenizerFast
from transformers import DataCollatorForLanguageModeling, Trainer, set_seed, TrainingArguments
def get_scores_on_paradigm(model, tokenizer, file_path):
with open(file_path) as f:
data = list(f)
acc = 0
for item in data:
line = json.loads(item)
good = line["sentence_good"]
bad = line["sentence_bad"]
good_score = get_perplexity(sentence=good, model=model, tokenizer=tokenizer)
bad_score = get_perplexity(sentence=bad, model=model, tokenizer=tokenizer)
if bad_score >= good_score:
acc += 1
acc = acc / len(data)
return acc
def freeze(model):
print("Freezing all parameters except embeddings")
for name, param in model.named_parameters():
if not name.startswith("roberta.embeddings"):
param.requires_grad = False
return model
def finetune(model, ads_path):
rep = 0
path_out = '/scratch/pbsjobs/axy327/finetune/' + str(rep)
print(f'replication={rep}')
training_args = TrainingArguments(
report_to=None,
output_dir=str(path_out),
overwrite_output_dir=True,
do_train=True,
do_eval=False,
do_predict=False,
per_device_train_batch_size=16,
learning_rate=1e-4,
max_steps=160_000,
warmup_steps=24_000,
seed=rep,
save_steps=40_000
)
logger = logging.getLogger(__name__)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
)
logger.setLevel(logging.INFO)
set_seed(rep)
logger.info("Loading data")
data_path = ads_path
sentences = load_sentences_from_file(data_path,
include_punctuation=True,
allow_discard=True)
data_in_dict = {'text': make_sequences(sentences, 1)}
datasets = DatasetDict({'train': Dataset.from_dict(data_in_dict)})
print(datasets['train'])
# See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at
# https://huggingface.co/docs/datasets/loading_datasets.html.
logger.info("Loading tokenizer")
tokenizer = ByteLevelBPETokenizer()
tokenizer.train(files=ads_path, vocab_size=52_000, min_frequency=2, special_tokens=[
"<s>",
"<pad>",
"</s>",
"<unk>",
"<mask>",
])
tokenizer.save_model("Babyberta")
tokenizer.save("byte-level-BPE.tokenizer.json")
tokenizer = RobertaTokenizerFast(vocab_file=None,
merges_file=None,
tokenizer_file=str('byte-level-BPE.tokenizer.json')
)
logger.info("Finetuning Roberta")
# Preprocessing the datasets.
# First we tokenize all the texts.
text_column_name = "text"
def tokenize_function(examples):
# Remove empty lines
examples["text"] = [line for line in examples["text"] if len(line) > 0 and not line.isspace()]
return tokenizer(
examples["text"],
padding=True,
truncation=True,
max_length=128,
# We use this option because DataCollatorForLanguageModeling (see below) is more efficient when it
# receives the `special_tokens_mask`.
return_special_tokens_mask=True,
)
logger.info("Tokenising data")
tokenized_datasets = datasets.map(
tokenize_function,
batched=True,
num_proc=4,
remove_columns=[text_column_name],
load_from_cache_file=True,
)
train_dataset = tokenized_datasets["train"]
print(f'Length of train data={len(train_dataset)}')
# Data collator will take care of randomly masking the tokens.
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer,
mlm_probability=0.15)
# Initialize our Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=None,
tokenizer=tokenizer,
data_collator=data_collator,
)
# Training
trainer.train()
trainer.save_model() # Saves the tokenizer too
return model, tokenizer
def main(ads_path, cds_path, if_freeze):
if if_freeze == "False":
first_path = ads_path
second_path = "" # not needed
else:
first_path = cds_path
second_path = ads_path
rep = 0
path_out = '/scratch/pbsjobs/axy327/' + str(rep)
print(f'replication={rep}')
training_args = TrainingArguments(
report_to=None,
output_dir=str(path_out),
overwrite_output_dir=True,
do_train=True,
do_eval=False,
do_predict=False,
per_device_train_batch_size=16,
learning_rate=1e-4,
max_steps=160_000,
warmup_steps=24_000,
seed=rep,
save_steps=40_000
)
logger = logging.getLogger(__name__)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
)
logger.setLevel(logging.INFO)
set_seed(rep)
logger.info("Loading data")
data_path = first_path # we use aonewsela for reference implementation
sentences = load_sentences_from_file(data_path,
include_punctuation=True,
allow_discard=True)
data_in_dict = {'text': make_sequences(sentences, 1)}
datasets = DatasetDict({'train': Dataset.from_dict(data_in_dict)})
print(datasets['train'])
# See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at
# https://huggingface.co/docs/datasets/loading_datasets.html.
logger.info("Loading tokenizer")
tokenizer = ByteLevelBPETokenizer()
tokenizer.train(files=first_path, vocab_size=52_000, min_frequency=2, special_tokens=[
"<s>",
"<pad>",
"</s>",
"<unk>",
"<mask>",
])
tokenizer.save_model("Babyberta")
tokenizer.save("byte-level-BPE.tokenizer.json")
tokenizer = RobertaTokenizerFast(vocab_file=None,
merges_file=None,
tokenizer_file=str('byte-level-BPE.tokenizer.json')
)
logger.info("Initialising Roberta from scratch")
config = RobertaConfig(vocab_size=52_000,
hidden_size=256,
num_hidden_layers=8,
num_attention_heads=8,
intermediate_size=1024,
initializer_range=0.02,
)
model = RobertaForMaskedLM(config)
# Preprocessing the datasets.
# First we tokenize all the texts.
text_column_name = "text"
def tokenize_function(examples):
# Remove empty lines
examples["text"] = [line for line in examples["text"] if len(line) > 0 and not line.isspace()]
return tokenizer(
examples["text"],
padding=True,
truncation=True,
max_length=128,
# We use this option because DataCollatorForLanguageModeling (see below) is more efficient when it
# receives the `special_tokens_mask`.
return_special_tokens_mask=True,
)
logger.info("Tokenising data")
tokenized_datasets = datasets.map(
tokenize_function,
batched=True,
num_proc=4,
remove_columns=[text_column_name],
load_from_cache_file=True,
)
train_dataset = tokenized_datasets["train"]
print(f'Length of train data={len(train_dataset)}')
# Data collator will take care of randomly masking the tokens.
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer,
mlm_probability=0.15)
# Initialize our Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=None,
tokenizer=tokenizer,
data_collator=data_collator,
)
# Training
# trainer.train()
# trainer.save_model() # Saves the tokenizer too
if if_freeze == "True":
model = freeze(model)
model, tokenizer = finetune(model, second_path)
print(get_perplexity(sentence='London is the capital of Great Britain.', model=model, tokenizer=tokenizer))
print(get_perplexity(sentence='London is the capital of South America.', model=model, tokenizer=tokenizer))
# path = "tests/wh_vs_that_with_gap_long_distance.jsonl"
paths = glob.glob("tests/*.jsonl")
for path in paths:
acc = get_scores_on_paradigm(model, tokenizer, path)
print(path + " " + str(acc*100))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="script to train mini roberta model")
parser.add_argument("--ads_path", required=True, help="path to the ADS file")
parser.add_argument("--freeze", required=True, help="should I freeze the network?")
parser.add_argument("--cds_path", help="path to the CDS file")
args = parser.parse_args()
main(args.ads_path, args.cds_path, args.freeze)