-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlanguage_model.py
136 lines (120 loc) · 3.59 KB
/
language_model.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
import threading
import time
from collections import namedtuple
import stanza
import torch
from loguru import logger
from transformers import (
AutoModelForCausalLM,
AutoModelForPreTraining,
AutoModelForSeq2SeqLM,
AutoTokenizer,
BartForConditionalGeneration,
BartTokenizerFast,
BertTokenizerFast,
RobertaForMultipleChoice,
RobertaTokenizer,
)
ModelSpec = namedtuple("ModelSpec", ["model_class", "tokenizer_class", "name", "alias"])
MODELS_SPECS = [
ModelSpec(
AutoModelForSeq2SeqLM,
AutoTokenizer,
"p208p2002/bart-squad-qg-hl",
"qg_en",
),
ModelSpec(
AutoModelForCausalLM,
BertTokenizerFast,
"p208p2002/gpt2-drcd-qg-hl",
"qg_zh",
),
ModelSpec(
BartForConditionalGeneration,
BartTokenizerFast,
"p208p2002/qmst-qgg",
"qgg_en",
),
ModelSpec(
AutoModelForCausalLM,
AutoTokenizer,
"gpt2",
"pplscorer",
),
ModelSpec(
AutoModelForSeq2SeqLM,
AutoTokenizer,
"voidful/bart-distractor-generation",
"_dg_en",
),
ModelSpec(
AutoModelForSeq2SeqLM,
AutoTokenizer,
"voidful/bart-distractor-generation-pm",
"_dg_pm",
),
ModelSpec(
AutoModelForSeq2SeqLM,
AutoTokenizer,
"voidful/bart-distractor-generation-both",
"_dg_both",
),
ModelSpec(
RobertaForMultipleChoice,
RobertaTokenizer,
"LIAMF-USP/roberta-large-finetuned-race",
"_dg_rl",
),
ModelSpec(
AutoModelForPreTraining,
AutoTokenizer,
"nlplab/PhishingEmailGeneration",
"fm_en",
),
]
class LanguageModels:
def __init__(self, download_only=False):
threads = [
threading.Thread(target=self._init, args=(spec, download_only))
for spec in MODELS_SPECS
]
if download_only:
threads.append(threading.Thread(target=stanza.download, args=("en",)))
start_at = time.time()
for thread in threads:
thread.start()
for thread in threads:
thread.join()
if not download_only:
self._post_dg_load()
logger.info(f"Model loading took {(time.time() - start_at):.2f} secs")
def _init(self, spec: ModelSpec, download_only: bool = False):
logger.info(f"Start loading <{spec.name}>...")
model = spec.model_class.from_pretrained(spec.name)
if not download_only:
from config import CUDA_MODELS
model.to(
"cuda"
if spec.name in CUDA_MODELS and torch.cuda.is_available()
else "cpu"
)
tokenizer = spec.tokenizer_class.from_pretrained(spec.name)
setattr(self, f"{spec.alias}_model", model)
setattr(self, f"{spec.alias}_tokenizer", tokenizer)
logger.info(f"<{spec.name}> loaded!")
def _post_dg_load(self):
from distractor_generation import BartDistractorGeneration
self.dis_en_model = BartDistractorGeneration(
dg_models=[self._dg_en_model, self._dg_pm_model, self._dg_both_model],
dg_tokenizer=[
self._dg_en_tokenizer,
self._dg_pm_tokenizer,
self._dg_both_tokenizer,
],
dg_selection_models=self._dg_rl_model,
dg_selection_tokenizer=self._dg_rl_tokenizer,
pplscorer_model=self.pplscorer_model,
pplscorer_tokenizer=self.pplscorer_tokenizer,
)
if __name__ == "__main__":
models = LanguageModels(download_only=True)