forked from ArvinZhuang/DSI-QG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
195 lines (172 loc) · 8.04 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
from data import IndexingTrainDataset, GenerateDataset, IndexingCollator, QueryEvalCollator
from transformers import (
T5Tokenizer,
T5TokenizerFast,
T5ForConditionalGeneration,
TrainingArguments,
TrainerCallback,
MT5Tokenizer,
MT5TokenizerFast,
MT5ForConditionalGeneration,
HfArgumentParser,
set_seed,
)
from trainer import DSITrainer, DocTqueryTrainer
import numpy as np
import torch
import wandb
from torch.utils.data import DataLoader
from dataclasses import dataclass, field
from typing import Optional
import json
from tqdm import tqdm
set_seed(313)
@dataclass
class RunArguments:
model_name: str = field(default=None)
model_path: Optional[str] = field(default=None)
max_length: Optional[int] = field(default=32)
id_max_length: Optional[int] = field(default=20)
remove_prompt: Optional[bool] = field(default=False)
train_file: str = field(default=None)
valid_file: str = field(default=None)
task: str = field(default=None, metadata={"help": "DSI, docTquery, generation"})
top_k: Optional[int] = field(default=10)
num_return_sequences: Optional[int] = field(default=10)
q_max_length: Optional[int] = field(default=32)
def make_compute_metrics(tokenizer, valid_ids):
def compute_metrics(eval_preds):
hit_at_1 = 0
hit_at_10 = 0
for beams, label in zip(eval_preds.predictions, eval_preds.label_ids):
rank_list = tokenizer.batch_decode(beams,
skip_special_tokens=True)
label_id = tokenizer.decode(label, skip_special_tokens=True)
# filter out duplicates and invalid docids
filtered_rank_list = []
for docid in rank_list:
if docid not in filtered_rank_list and docid in valid_ids:
filtered_rank_list.append(docid)
hits = np.where(np.array(filtered_rank_list)[:10] == label_id)[0]
if len(hits) != 0:
hit_at_10 += 1
if hits[0] == 0:
hit_at_1 += 1
return {"Hits@1": hit_at_1 / len(eval_preds.predictions), "Hits@10": hit_at_10 / len(eval_preds.predictions)}
return compute_metrics
def main():
parser = HfArgumentParser((TrainingArguments, RunArguments))
training_args, run_args = parser.parse_args_into_dataclasses()
# We use wandb logger: https://wandb.ai/site.
if training_args.local_rank == 0: # only on main process
# Initialize wandb run
wandb.login()
wandb.init(project="DSI", name=training_args.run_name)
if 'mt5' in run_args.model_name:
tokenizer = MT5Tokenizer.from_pretrained(run_args.model_name, cache_dir='cache')
fast_tokenizer = MT5TokenizerFast.from_pretrained(run_args.model_name, cache_dir='cache')
if run_args.model_path:
model = MT5ForConditionalGeneration.from_pretrained(run_args.model_path, cache_dir='cache')
else:
model = MT5ForConditionalGeneration.from_pretrained(run_args.model_name, cache_dir='cache')
else:
tokenizer = T5Tokenizer.from_pretrained(run_args.model_name, cache_dir='cache')
fast_tokenizer = T5TokenizerFast.from_pretrained(run_args.model_name, cache_dir='cache')
if run_args.model_path:
model = T5ForConditionalGeneration.from_pretrained(run_args.model_path, cache_dir='cache')
else:
model = T5ForConditionalGeneration.from_pretrained(run_args.model_name, cache_dir='cache')
if run_args.task == "docTquery":
train_dataset = IndexingTrainDataset(path_to_data=run_args.train_file,
max_length=run_args.max_length,
cache_dir='cache',
tokenizer=tokenizer)
valid_dataset = IndexingTrainDataset(path_to_data=run_args.valid_file,
max_length=run_args.max_length,
cache_dir='cache',
remove_prompt=run_args.remove_prompt,
tokenizer=tokenizer)
trainer = DocTqueryTrainer(
do_generation=False,
model=model,
tokenizer=tokenizer,
args=training_args,
train_dataset=train_dataset,
eval_dataset=valid_dataset,
data_collator=IndexingCollator(
tokenizer,
padding='longest',
),
)
trainer.train()
elif run_args.task == "DSI":
train_dataset = IndexingTrainDataset(path_to_data=run_args.train_file,
max_length=run_args.max_length,
cache_dir='cache',
tokenizer=tokenizer)
valid_dataset = IndexingTrainDataset(path_to_data=run_args.valid_file,
max_length=run_args.max_length,
cache_dir='cache',
remove_prompt=run_args.remove_prompt,
tokenizer=tokenizer)
################################################################
# docid generation constrain, we only generate integer docids.
SPIECE_UNDERLINE = "▁"
INT_TOKEN_IDS = []
for token, id in tokenizer.get_vocab().items():
if token[0] == SPIECE_UNDERLINE:
if token[1:].isdigit():
INT_TOKEN_IDS.append(id)
if token == SPIECE_UNDERLINE:
INT_TOKEN_IDS.append(id)
elif token.isdigit():
INT_TOKEN_IDS.append(id)
INT_TOKEN_IDS.append(tokenizer.eos_token_id)
def restrict_decode_vocab(batch_idx, prefix_beam):
return INT_TOKEN_IDS
################################################################
trainer = DSITrainer(
model=model,
tokenizer=tokenizer,
args=training_args,
train_dataset=train_dataset,
eval_dataset=valid_dataset,
data_collator=IndexingCollator(
tokenizer,
padding='longest',
),
compute_metrics=make_compute_metrics(fast_tokenizer, train_dataset.valid_ids),
restrict_decode_vocab=restrict_decode_vocab,
id_max_length=run_args.id_max_length
)
trainer.train()
elif run_args.task == 'generation':
generate_dataset = GenerateDataset(path_to_data=run_args.valid_file,
max_length=run_args.max_length,
cache_dir='cache',
tokenizer=tokenizer)
trainer = DocTqueryTrainer(
do_generation=True,
model=model,
tokenizer=tokenizer,
args=training_args,
data_collator=QueryEvalCollator(
tokenizer,
padding='longest',
),
)
predict_results = trainer.predict(generate_dataset,
top_k=run_args.top_k,
num_return_sequences=run_args.num_return_sequences,
max_length=run_args.q_max_length)
with open(f"{run_args.valid_file}.q{run_args.num_return_sequences}.docTquery", 'w') as f:
for batch_tokens, batch_ids in tqdm(zip(predict_results.predictions, predict_results.label_ids),
desc="Writing file"):
for tokens, docid in zip(batch_tokens, batch_ids):
query = fast_tokenizer.decode(tokens, skip_special_tokens=True)
jitem = json.dumps({'text_id': docid.item(), 'text': query})
f.write(jitem + '\n')
else:
raise NotImplementedError("--task should be in 'DSI' or 'docTquery' or 'generation'")
if __name__ == "__main__":
main()