-
Notifications
You must be signed in to change notification settings - Fork 86
/
hf_utils.py
executable file
·250 lines (221 loc) · 8.25 KB
/
hf_utils.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
# Copyright (c) Meta Platforms, Inc. and affiliates
# This file contains code to generate inputs for all of the models in the
# support list. The `generate_inputs_for_model` function is extracted from
# pytorch/benchmarks/dynamo/huggingface.py
#!/usr/bin/env python3
import importlib
import logging
import subprocess
import sys
import torch
log = logging.getLogger(__name__)
def pip_install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
# Disable the flake warnings for the imports. Flake8 does not provide a way to
# disable just warning for the entire file. Disabling flake8 entirely.
# flake8: noqa
imports = [
"AlbertForPreTraining",
"AutoConfig",
"AutoModelForCausalLM",
"AutoModelForMaskedLM",
"AutoModelForSeq2SeqLM",
"BigBirdConfig",
"BlenderbotForConditionalGeneration",
"BlenderbotModel",
"BlenderbotSmallForConditionalGeneration",
"BlenderbotSmallModel",
"CLIPModel",
"CLIPVisionModel",
"ElectraForPreTraining",
"GPT2ForSequenceClassification",
"GPTJForSequenceClassification",
"GPTNeoForSequenceClassification",
"HubertForSequenceClassification",
"LxmertForPreTraining",
"LxmertForQuestionAnswering",
"MarianForCausalLM",
"MarianModel",
"MarianMTModel",
"PegasusForConditionalGeneration",
"PegasusModel",
"ReformerConfig",
"ViTForImageClassification",
"ViTForMaskedImageModeling",
"ViTModel",
]
try:
mod = importlib.import_module("transformers")
for cls in imports:
if not hasattr(mod, cls):
raise ModuleNotFoundError
except ModuleNotFoundError:
print("Installing HuggingFace Transformers...")
pip_install("git+https://github.com/huggingface/transformers.git#egg=transformers")
finally:
for cls in imports:
exec(f"from transformers import {cls}")
def get_sequence_length(model_cls, model_name):
if model_name.startswith(("Blenderbot",)):
seq_length = 128
elif model_name.startswith(("GPT2", "Bart", "T5", "PLBart", "MBart")):
seq_length = 1024
elif model_name in ("AllenaiLongformerBase", "BigBird"):
seq_length = 1024
elif model_name.startswith("OPT"):
seq_length = 2048
elif "Reformer" in model_name:
seq_length = 4096
elif model_name.startswith(
(
"Albert",
"Deberta",
"Layout",
"Electra",
"XLNet",
"MegatronBert",
"Bert",
"Roberta",
)
) or model_name in ("DistillGPT2", "GoogleFnet", "YituTechConvBert", "CamemBert"):
seq_length = 512
elif model_name in ("TrOCRForCausalLM"):
seq_length = 256
elif model_name.startswith("MobileBert"):
seq_length = 128
elif model_name.startswith("Wav2Vec2"):
# If too short, will fail with something like
# ValueError: `mask_length` has to be smaller than `sequence_length`,
# but got `mask_length`: 10 and `sequence_length`: 9`
seq_length = 10000 # NB: a more realistic size is 155136
else:
log.info(
f"Sequence Length not defined for {model_name}. Choosing 128 arbitrarily"
)
seq_length = 128
return seq_length
def generate_inputs_for_model(
model_cls, model, model_name, bs, device, include_loss_args=False
):
# TODO - Check if following values are representative
num_choices = 3
num_visual_features = 42
seq_length = get_sequence_length(model_cls, model_name)
vocab_size = model.config.vocab_size
if model_name.startswith("Wav2Vec2"):
# TODO: If we add more input_values style models, try to work this
# into the overall control flow
target_length = 100
return {
"input_values": torch.randn((bs, seq_length), device=device),
# Added because that's what the example training script has
"attention_mask": rand_int_tensor(device, 0, 2, (bs, seq_length)),
"labels": rand_int_tensor(device, 0, vocab_size, (bs, target_length)),
}
if model_name.endswith("MultipleChoice"):
input = rand_int_tensor(device, 0, vocab_size, (bs, num_choices, seq_length))
elif model_name.startswith("Roberta"):
input = rand_int_tensor(device, 0, 1, (bs, seq_length))
else:
input = rand_int_tensor(device, 0, vocab_size, (bs, seq_length))
if "Bart" in model_name:
input[:, -1] = model.config.eos_token_id
input_dict = {"input_ids": input}
if (
model_name.startswith("T5")
or model_name.startswith("M2M100")
or model_name.startswith("MT5")
or model_cls
in [
BlenderbotModel,
BlenderbotSmallModel,
BlenderbotForConditionalGeneration,
BlenderbotSmallForConditionalGeneration,
PegasusModel,
PegasusForConditionalGeneration,
MarianModel,
MarianMTModel,
]
):
input_dict["decoder_input_ids"] = input
if model_name.startswith("Lxmert"):
visual_feat_dim, visual_pos_dim = (
model.config.visual_feat_dim,
model.config.visual_pos_dim,
)
input_dict["visual_feats"] = torch.randn(
bs, num_visual_features, visual_feat_dim
)
input_dict["visual_pos"] = torch.randn(bs, num_visual_features, visual_pos_dim)
if include_loss_args:
if model_name.endswith("PreTraining"):
if model_cls in [ElectraForPreTraining, LxmertForPreTraining]:
input_dict["labels"] = rand_int_tensor(device, 0, 1, (bs, seq_length))
else:
label_name = (
"sentence_order_label"
if model_cls in [AlbertForPreTraining]
else "next_sentence_label"
)
input_dict["labels"] = (
rand_int_tensor(device, 0, vocab_size, (bs, seq_length)),
)
input_dict[label_name] = rand_int_tensor(device, 0, 1, (bs,))
elif model_name.endswith("QuestionAnswering"):
input_dict["start_positions"] = rand_int_tensor(
device, 0, seq_length, (bs,)
)
input_dict["end_positions"] = rand_int_tensor(device, 0, seq_length, (bs,))
elif (
model_name.endswith("MaskedLM")
or model_name.endswith("HeadModel")
or model_name.endswith("CausalLM")
or model_name.endswith("DoubleHeadsModel")
):
input_dict["labels"] = rand_int_tensor(
device, 0, vocab_size, (bs, seq_length)
)
elif model_name.endswith("TokenClassification"):
input_dict["labels"] = rand_int_tensor(
device, 0, model.config.num_labels - 1, (bs, seq_length)
)
elif model_name.endswith("MultipleChoice"):
input_dict["labels"] = rand_int_tensor(device, 0, num_choices, (bs,))
elif model_name.endswith("SequenceClassification"):
input_dict["labels"] = rand_int_tensor(
device, 0, model.config.num_labels - 1, (bs,)
)
elif model_name.endswith("NextSentencePrediction"):
input_dict["labels"] = rand_int_tensor(device, 0, 1, (bs,))
elif model_name.endswith("ForConditionalGeneration"):
input_dict["labels"] = rand_int_tensor(
device, 0, vocab_size - 1, (bs, seq_length)
)
elif model_name in EXTRA_MODELS:
input_dict["labels"] = rand_int_tensor(
device, 0, vocab_size, (bs, seq_length)
)
else:
raise NotImplementedError(
f"Class {model_name} unsupported for training test "
)
return input_dict
def rand_int_tensor(device, low, high, shape):
return torch.randint(
low,
high,
shape,
device=device,
dtype=torch.int64,
requires_grad=False,
)
def get_number_of_params(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def calc_flop(args, conf):
# https://arxiv.org/pdf/2104.04473.pdf page 8, formula 3
B = args.batch_size
s = args.seq_length
l = conf.n_layer
h = conf.n_embd
V = conf.vocab_size
return 96 * B * s * l * h * h * (1 + s/6/h + V/16/l/h)