"examples/text-classification/run_glue.py" did not exist on "abd7110e21102467448035ffdbf6b208a05ac80b"
benchmarks.py 27.5 KB
Newer Older
LysandreJik's avatar
LysandreJik committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# coding=utf-8
# Copyright 2018 The HuggingFace Inc. team.
# Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Benchmarking the library on inference and training """

# If checking the tensors placement
# tf.debugging.set_log_device_placement(True)

import argparse
import csv
Aymeric Augustin's avatar
Aymeric Augustin committed
23
24
25
26
import timeit
from time import time
from typing import List

27
28
29
30
31
32
33
34
35
from transformers import (
    AutoConfig,
    AutoTokenizer,
    MemorySummary,
    is_tf_available,
    is_torch_available,
    start_memory_tracing,
    stop_memory_tracing,
)
Aymeric Augustin's avatar
Aymeric Augustin committed
36

LysandreJik's avatar
LysandreJik committed
37

38
39
40
41
42
43
44
if is_tf_available():
    import tensorflow as tf
    from transformers import TFAutoModel

if is_torch_available():
    import torch
    from transformers import AutoModel
LysandreJik's avatar
LysandreJik committed
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
input_text = """Bent over their instruments, three hundred Fertilizers were plunged, as
the Director of Hatcheries and Conditioning entered the room, in the



scarcely breathing silence, the absent-minded, soliloquizing hum or
whistle, of absorbed concentration. A troop of newly arrived students,
very young, pink and callow, followed nervously, rather abjectly, at the
Director's heels. Each of them carried a notebook, in which, whenever
the great man spoke, he desperately scribbled. Straight from the
horse's mouth. It was a rare privilege. The D. H. C. for Central London
always made a point of personally conducting his new students round
the various departments.

"Just to give you a general idea," he would explain to them. For of
course some sort of general idea they must have, if they were to do
their work intelligently-though as little of one, if they were to be good
and happy members of society, as possible. For particulars, as every
one knows, make for virtue and happiness; generalities are intellectu-
ally necessary evils. Not philosophers but fret-sawyers and stamp col-
lectors compose the backbone of society.

"To-morrow," he would add, smiling at them with a slightly menacing
geniality, "you'll be settling down to serious work. You won't have time
for generalities. Meanwhile ..."

Meanwhile, it was a privilege. Straight from the horse's mouth into the
notebook. The boys scribbled like mad.

Tall and rather thin but upright, the Director advanced into the room.
He had a long chin and big rather prominent teeth, just covered, when
he was not talking, by his full, floridly curved lips. Old, young? Thirty?
Fifty? Fifty-five? It was hard to say. And anyhow the question didn't
arise; in this year of stability, A. F. 632, it didn't occur to you to ask it.

"I shall begin at the beginning," said the D.H.C. and the more zealous
students recorded his intention in their notebooks: Begin at the begin-
ning. "These," he waved his hand, "are the incubators." And opening
an insulated door he showed them racks upon racks of numbered test-
tubes. "The week's supply of ova. Kept," he explained, "at blood heat;
whereas the male gametes," and here he opened another door, "they
have to be kept at thirty-five instead of thirty-seven. Full blood heat
sterilizes." Rams wrapped in theremogene beget no lambs.

Still leaning against the incubators he gave them, while the pencils
scurried illegibly across the pages, a brief description of the modern



fertilizing process; spoke first, of course, of its surgical introduc-
tion-"the operation undergone voluntarily for the good of Society, not
to mention the fact that it carries a bonus amounting to six months'
salary"; continued with some account of the technique for preserving
the excised ovary alive and actively developing; passed on to a consid-
eration of optimum temperature, salinity, viscosity; referred to the liq-
uor in which the detached and ripened eggs were kept; and, leading
his charges to the work tables, actually showed them how this liquor
was drawn off from the test-tubes; how it was let out drop by drop
onto the specially warmed slides of the microscopes; how the eggs
which it contained were inspected for abnormalities, counted and
transferred to a porous receptacle; how (and he now took them to
watch the operation) this receptacle was immersed in a warm bouillon
containing free-swimming spermatozoa-at a minimum concentration
of one hundred thousand per cubic centimetre, he insisted; and how,
after ten minutes, the container was lifted out of the liquor and its
contents re-examined; how, if any of the eggs remained unfertilized, it
was again immersed, and, if necessary, yet again; how the fertilized
ova went back to the incubators; where the Alphas and Betas re-
mained until definitely bottled; while the Gammas, Deltas and Epsilons
were brought out again, after only thirty-six hours, to undergo Bo-
kanovsky's Process.

"Bokanovsky's Process," repeated the Director, and the students un-
derlined the words in their little notebooks.

One egg, one embryo, one adult-normality. But a bokanovskified egg
will bud, will proliferate, will divide. From eight to ninety-six buds, and
every bud will grow into a perfectly formed embryo, and every embryo
into a full-sized adult. Making ninety-six human beings grow where
only one grew before. Progress.

"Essentially," the D.H.C. concluded, "bokanovskification consists of a
series of arrests of development. We check the normal growth and,
paradoxically enough, the egg responds by budding."

Responds by budding. The pencils were busy.

He pointed. On a very slowly moving band a rack-full of test-tubes was
entering a large metal box, another, rack-full was emerging. Machinery
faintly purred. It took eight minutes for the tubes to go through, he



told them. Eight minutes of hard X-rays being about as much as an
egg can stand. A few died; of the rest, the least susceptible divided
into two; most put out four buds; some eight; all were returned to the
incubators, where the buds began to develop; then, after two days,
were suddenly chilled, chilled and checked. Two, four, eight, the buds
in their turn budded; and having budded were dosed almost to death
with alcohol; consequently burgeoned again and having budded-bud
out of bud out of bud-were thereafter-further arrest being generally
fatal-left to develop in peace. By which time the original egg was in a
fair way to becoming anything from eight to ninety-six embryos- a
prodigious improvement, you will agree, on nature. Identical twins-but
not in piddling twos and threes as in the old viviparous days, when an
egg would sometimes accidentally divide; actually by dozens, by
scores at a time.

"Scores," the Director repeated and flung out his arms, as though he
were distributing largesse. "Scores."

But one of the students was fool enough to ask where the advantage
lay.

"My good boy!" The Director wheeled sharply round on him. "Can't you
see? Can't you see?" He raised a hand; his expression was solemn.
"Bokanovsky's Process is one of the major instruments of social stabil-
ity!"

Major instruments of social stability.

Standard men and women; in uniform batches. The whole of a small
factory staffed with the products of a single bokanovskified egg.

"Ninety-six identical twins working ninety-six identical machines!" The
voice was almost tremulous with enthusiasm. "You really know where
you are. For the first time in history." He quoted the planetary motto.
"Community, Identity, Stability." Grand words. "If we could bo-
kanovskify indefinitely the whole problem would be solved."

Solved by standard Gammas, unvarying Deltas, uniform Epsilons. Mil-
lions of identical twins. The principle of mass production at last applied
to biology.



"But, alas," the Director shook his head, "we can't bokanovskify indefi-
nitely."

Ninety-six seemed to be the limit; seventy-two a good average. From
the same ovary and with gametes of the same male to manufacture as
many batches of identical twins as possible-that was the best (sadly a
second best) that they could do. And even that was difficult.

"For in nature it takes thirty years for two hundred eggs to reach ma-
turity. But our business is to stabilize the population at this moment,
here and now. Dribbling out twins over a quarter of a century-what
would be the use of that?"

Obviously, no use at all. But Podsnap's Technique had immensely ac-
celerated the process of ripening. They could make sure of at least a
hundred and fifty mature eggs within two years. Fertilize and bo-
kanovskify-in other words, multiply by seventy-two-and you get an
average of nearly eleven thousand brothers and sisters in a hundred
and fifty batches of identical twins, all within two years of the same
age.

"And in exceptional cases we can make one ovary yield us over fifteen
thousand adult individuals."

Beckoning to a fair-haired, ruddy young man who happened to be
passing at the moment. "Mr. Foster," he called. The ruddy young man
approached. "Can you tell us the record for a single ovary, Mr. Foster?"

"Sixteen thousand and twelve in this Centre," Mr. Foster replied with-
out hesitation. He spoke very quickly, had a vivacious blue eye, and
took an evident pleasure in quoting figures. "Sixteen thousand and
twelve; in one hundred and eighty-nine batches of identicals. But of
course they've done much better," he rattled on, "in some of the tropi-
cal Centres. Singapore has often produced over sixteen thousand five
hundred; and Mombasa has actually touched the seventeen thousand
mark. But then they have unfair advantages. You should see the way a
negro ovary responds to pituitary! It's quite astonishing, when you're
used to working with European material. Still," he added, with a laugh
(but the light of combat was in his eyes and the lift of his chin was
challenging), "still, we mean to beat them if we can. I'm working on a
wonderful Delta-Minus ovary at this moment. Only just eighteen



months old. Over twelve thousand seven hundred children already, ei-
ther decanted or in embryo. And still going strong. We'll beat them
yet."

"That's the spirit I like!" cried the Director, and clapped Mr. Foster on
the shoulder. "Come along with us, and give these boys the benefit of
your expert knowledge."

Mr. Foster smiled modestly. "With pleasure." They went.
In the Bottling Room all was harmonious bustle and ordered activity.
Flaps of fresh sow's peritoneum ready cut to the proper size came
shooting up in little lifts from the Organ Store in the sub-basement.
Whizz and then, click! the lift-hatches hew open; the bottle-liner had
only to reach out a hand, take the flap, insert, smooth-down, and be-
fore the lined bottle had had time to travel out of reach along the end-
less band, whizz, click! another flap of peritoneum had shot up from
the depths, ready to be slipped into yet another bottle, the next of that
slow interminable procession on the band.

Next to the Liners stood the Matriculators. The procession advanced;
one by one the eggs were transferred from their test-tubes to the
larger containers; deftly the peritoneal lining was slit, the morula
dropped into place, the saline solution poured in ... and already the
bottle had passed, and it was the turn of the labellers. Heredity, date
of fertilization, membership of Bokanovsky Group-details were trans-
ferred from test-tube to bottle. No longer anonymous, but named,
identified, the procession marched slowly on; on through an opening in
the wall, slowly on into the Social Predestination Room.
"Eighty-eight cubic metres of card-index," said Mr. Foster with relish,
LysandreJik's avatar
LysandreJik committed
256
257
258
as they entered."""


259
260
def create_setup_and_compute(
    model_names: List[str],
261
262
    batch_sizes: List[int],
    slice_sizes: List[int],
263
264
265
    gpu: bool = True,
    tensorflow: bool = False,
    average_over: int = 3,
266
267
268
    no_speed: bool = False,
    no_memory: bool = False,
    verbose: bool = False,
269
270
271
272
273
274
    torchscript: bool = False,
    xla: bool = False,
    amp: bool = False,
    fp16: bool = False,
    save_to_csv: bool = False,
    csv_filename: str = f"results_{round(time())}.csv",
275
    csv_memory_filename: str = f"memory_{round(time())}.csv",
276
):
LysandreJik's avatar
LysandreJik committed
277
278
    if xla:
        tf.config.optimizer.set_jit(True)
279
280
    if amp:
        tf.config.optimizer.set_experimental_options({"auto_mixed_precision": True})
LysandreJik's avatar
LysandreJik committed
281
282
283

    if tensorflow:
        dictionary = {model_name: {} for model_name in model_names}
284
285
286
        results = _compute_tensorflow(
            model_names, batch_sizes, slice_sizes, dictionary, average_over, amp, no_speed, no_memory, verbose
        )
LysandreJik's avatar
LysandreJik committed
287
    else:
288
        device = "cuda" if (gpu and torch.cuda.is_available()) else "cpu"
LysandreJik's avatar
LysandreJik committed
289
        dictionary = {model_name: {} for model_name in model_names}
290
291
292
293
294
295
296
297
298
299
300
301
302
        results = _compute_pytorch(
            model_names,
            batch_sizes,
            slice_sizes,
            dictionary,
            average_over,
            device,
            torchscript,
            fp16,
            no_speed,
            no_memory,
            verbose,
        )
LysandreJik's avatar
LysandreJik committed
303
304
305
306
307
308
309

    print("=========== RESULTS ===========")
    for model_name in model_names:
        print("\t" + f"======= MODEL CHECKPOINT: {model_name} =======")
        for batch_size in results[model_name]["bs"]:
            print("\t\t" + f"===== BATCH SIZE: {batch_size} =====")
            for slice_size in results[model_name]["ss"]:
310
                result = results[model_name]["results"][batch_size][slice_size]
311
                memory = results[model_name]["memory"][batch_size][slice_size]
LysandreJik's avatar
LysandreJik committed
312
                if isinstance(result, str):
313
                    print(f"\t\t{model_name}/{batch_size}/{slice_size}: " f"{result} " f"{memory}")
LysandreJik's avatar
LysandreJik committed
314
                else:
315
316
317
318
319
320
                    print(
                        f"\t\t{model_name}/{batch_size}/{slice_size}: "
                        f"{(round(1000 * result) / 1000)}"
                        f"s "
                        f"{memory}"
                    )
LysandreJik's avatar
LysandreJik committed
321
322

    if save_to_csv:
323
        with open(csv_filename, mode="w") as csv_file, open(csv_memory_filename, mode="w") as csv_memory_file:
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
            fieldnames = [
                "model",
                "1x8",
                "1x64",
                "1x128",
                "1x256",
                "1x512",
                "1x1024",
                "2x8",
                "2x64",
                "2x128",
                "2x256",
                "2x512",
                "2x1024",
                "4x8",
                "4x64",
                "4x128",
                "4x256",
                "4x512",
                "4x1024",
                "8x8",
                "8x64",
                "8x128",
                "8x256",
                "8x512",
                "8x1024",
            ]
LysandreJik's avatar
LysandreJik committed
351
352
353

            writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
            writer.writeheader()
354
355
            memory_writer = csv.DictWriter(csv_memory_file, fieldnames=fieldnames)
            memory_writer.writeheader()
LysandreJik's avatar
LysandreJik committed
356
357
358

            for model_name in model_names:
                model_results = {
359
                    f"{bs}x{ss}": results[model_name]["results"][bs][ss]
LysandreJik's avatar
LysandreJik committed
360
                    for bs in results[model_name]["results"]
361
                    for ss in results[model_name]["results"][bs]
LysandreJik's avatar
LysandreJik committed
362
                }
363
                writer.writerow({"model": model_name, **model_results})
LysandreJik's avatar
LysandreJik committed
364

365
366
367
368
369
370
371
                model_memory_results = {
                    f"{bs}x{ss}": results[model_name]["memory"][bs][ss]
                    for bs in results[model_name]["memory"]
                    for ss in results[model_name]["memory"][bs]
                }
                memory_writer.writerow({"model": model_name, **model_memory_results})

LysandreJik's avatar
LysandreJik committed
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
404
405
406
407
408
409
410
def print_summary_statistics(summary: MemorySummary):
    print(
        "\nLines by line memory consumption:\n"
        + "\n".join(
            f"{state.frame.filename}:{state.frame.line_number}: mem {state.cpu_gpu}: {state.frame.line_text}"
            for state in summary.sequential
        )
    )
    print(
        "\nLines with top memory consumption:\n"
        + "\n".join(
            f"=> {state.frame.filename}:{state.frame.line_number}: mem {state.cpu_gpu}: {state.frame.line_text}"
            for state in summary.cumulative[:6]
        )
    )
    print(
        "\nLines with lowest memory consumption:\n"
        + "\n".join(
            f"=> {state.frame.filename}:{state.frame.line_number}: mem {state.cpu_gpu}: {state.frame.line_text}"
            for state in summary.cumulative[-6:]
        )
    )
    print(f"\nTotal memory increase: {summary.total}")


def _compute_pytorch(
    model_names,
    batch_sizes,
    slice_sizes,
    dictionary,
    average_over,
    device,
    torchscript,
    fp16,
    no_speed,
    no_memory,
    verbose,
):
LysandreJik's avatar
LysandreJik committed
411
412
413
414
415
416
    for c, model_name in enumerate(model_names):
        print(f"{c + 1} / {len(model_names)}")
        config = AutoConfig.from_pretrained(model_name, torchscript=torchscript)
        model = AutoModel.from_pretrained(model_name, config=config)
        tokenizer = AutoTokenizer.from_pretrained(model_name)

Lysandre's avatar
Remove  
Lysandre committed
417
        tokenized_sequence = tokenizer.encode(input_text, add_special_tokens=False)
LysandreJik's avatar
LysandreJik committed
418
419
420

        max_input_size = tokenizer.max_model_input_sizes[model_name]

421
        dictionary[model_name] = {"bs": batch_sizes, "ss": slice_sizes, "results": {}, "memory": {}}
LysandreJik's avatar
LysandreJik committed
422
        dictionary[model_name]["results"] = {i: {} for i in batch_sizes}
423
        dictionary[model_name]["memory"] = {i: {} for i in batch_sizes}
LysandreJik's avatar
LysandreJik committed
424
425

        for batch_size in batch_sizes:
426
427
            if fp16:
                model.half()
LysandreJik's avatar
LysandreJik committed
428
429
            model.to(device)
            model.eval()
430

LysandreJik's avatar
LysandreJik committed
431
432
433
434
435
436
437
438
439
440
441
442
443
444
            for slice_size in slice_sizes:
                if max_input_size is not None and slice_size > max_input_size:
                    dictionary[model_name]["results"][batch_size][slice_size] = "N/A"
                else:
                    sequence = torch.tensor(tokenized_sequence[:slice_size], device=device).repeat(batch_size, 1)
                    try:
                        if torchscript:
                            print("Tracing model with sequence size", sequence.shape)
                            inference = torch.jit.trace(model, sequence)
                            inference(sequence)
                        else:
                            inference = model
                            inference(sequence)

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
                        if not no_memory:
                            # model.add_memory_hooks()  # Forward method tracing (only for PyTorch models)

                            # Line by line memory tracing (all code in the module `transformers`) works for all models/arbitrary code
                            trace = start_memory_tracing("transformers")
                            inference(sequence)
                            summary = stop_memory_tracing(trace)

                            if verbose:
                                print_summary_statistics(summary)

                            dictionary[model_name]["memory"][batch_size][slice_size] = str(summary.total)
                        else:
                            dictionary[model_name]["memory"][batch_size][slice_size] = "N/A"

                        if not no_speed:
                            print("Going through model with sequence of shape", sequence.shape)
                            runtimes = timeit.repeat(lambda: inference(sequence), repeat=average_over, number=3)
                            average_time = sum(runtimes) / float(len(runtimes)) / 3.0
                            dictionary[model_name]["results"][batch_size][slice_size] = average_time
                        else:
                            dictionary[model_name]["results"][batch_size][slice_size] = "N/A"

LysandreJik's avatar
LysandreJik committed
468
469
470
471
                    except RuntimeError as e:
                        print("Doesn't fit on GPU.", e)
                        torch.cuda.empty_cache()
                        dictionary[model_name]["results"][batch_size][slice_size] = "N/A"
472
                        dictionary[model_name]["memory"][batch_size][slice_size] = "N/A"
LysandreJik's avatar
LysandreJik committed
473
474
475
    return dictionary


476
477
478
def _compute_tensorflow(
    model_names, batch_sizes, slice_sizes, dictionary, average_over, amp, no_speed, no_memory, verbose
):
LysandreJik's avatar
LysandreJik committed
479
480
481
482
483
484
    for c, model_name in enumerate(model_names):
        print(f"{c + 1} / {len(model_names)}")
        config = AutoConfig.from_pretrained(model_name)
        model = TFAutoModel.from_pretrained(model_name, config=config)
        tokenizer = AutoTokenizer.from_pretrained(model_name)

Lysandre's avatar
Remove  
Lysandre committed
485
        tokenized_sequence = tokenizer.encode(input_text, add_special_tokens=False)
LysandreJik's avatar
LysandreJik committed
486
487
488

        max_input_size = tokenizer.max_model_input_sizes[model_name]

489
        dictionary[model_name] = {"bs": batch_sizes, "ss": slice_sizes, "results": {}, "memory": {}}
LysandreJik's avatar
LysandreJik committed
490
        dictionary[model_name]["results"] = {i: {} for i in batch_sizes}
491
        dictionary[model_name]["memory"] = {i: {} for i in batch_sizes}
LysandreJik's avatar
LysandreJik committed
492
493
494
495
496
497
498
499
500
501
502
503

        print("Using model", model)

        @tf.function
        def inference(inputs):
            return model(inputs)

        for batch_size in batch_sizes:
            for slice_size in slice_sizes:
                if max_input_size is not None and slice_size > max_input_size:
                    dictionary[model_name]["results"][batch_size][slice_size] = "N/A"
                else:
504
505
506
                    sequence = tf.stack(
                        [tf.squeeze(tf.constant(tokenized_sequence[:slice_size])[None, :])] * batch_size
                    )
LysandreJik's avatar
LysandreJik committed
507
508
509
510
511
512

                    try:
                        print("Going through model with sequence of shape", sequence.shape)
                        # To make sure that the model is traced + that the tensors are on the appropriate device
                        inference(sequence)

513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
                        if not no_memory:
                            # Line by line memory tracing (all code in the module `transformers`) works for all models/arbitrary code
                            trace = start_memory_tracing("transformers")
                            inference(sequence)
                            summary = stop_memory_tracing(trace)

                            if verbose:
                                print_summary_statistics(summary)

                            dictionary[model_name]["memory"][batch_size][slice_size] = str(summary.total)
                        else:
                            dictionary[model_name]["memory"][batch_size][slice_size] = "N/A"

                        if not no_speed:
                            runtimes = timeit.repeat(lambda: inference(sequence), repeat=average_over, number=3)
                            average_time = sum(runtimes) / float(len(runtimes)) / 3.0
                            dictionary[model_name]["results"][batch_size][slice_size] = average_time
                        else:
                            dictionary[model_name]["results"][batch_size][slice_size] = "N/A"

LysandreJik's avatar
LysandreJik committed
533
534
535
536
                    except tf.errors.ResourceExhaustedError as e:
                        print("Doesn't fit on GPU.", e)
                        torch.cuda.empty_cache()
                        dictionary[model_name]["results"][batch_size][slice_size] = "N/A"
537
                        dictionary[model_name]["memory"][batch_size][slice_size] = "N/A"
LysandreJik's avatar
LysandreJik committed
538
539
540
541
542
543
    return dictionary


def main():
    parser = argparse.ArgumentParser()

544
545
546
547
548
549
550
551
552
553
554
    parser.add_argument(
        "--models",
        required=False,
        type=str,
        default="all",
        help="Model checkpoints to be provided "
        "to the AutoModel classes. Leave "
        "blank to benchmark the base version "
        "of all available model "
        "architectures.",
    )
555
556
557
    parser.add_argument("--verbose", required=False, action="store_true", help="Verbose memory tracing")
    parser.add_argument("--no_speed", required=False, action="store_true", help="Don't perform speed measurments")
    parser.add_argument("--no_memory", required=False, action="store_true", help="Don't perform memory measurments")
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
    parser.add_argument(
        "--torch", required=False, action="store_true", help="Benchmark the Pytorch version of the " "models"
    )
    parser.add_argument(
        "--torch_cuda", required=False, action="store_true", help="Pytorch only: run on available " "cuda devices"
    )
    parser.add_argument(
        "--torchscript",
        required=False,
        action="store_true",
        help="Pytorch only: trace the models " "using torchscript",
    )
    parser.add_argument(
        "--tensorflow",
        required=False,
        action="store_true",
        help="Benchmark the TensorFlow version "
        "of the models. Will run on GPU if "
        "the correct dependencies are "
        "installed",
    )
LysandreJik's avatar
LysandreJik committed
579
    parser.add_argument("--xla", required=False, action="store_true", help="TensorFlow only: use XLA acceleration.")
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
    parser.add_argument(
        "--amp",
        required=False,
        action="store_true",
        help="TensorFlow only: use automatic mixed precision acceleration.",
    )
    parser.add_argument(
        "--fp16", required=False, action="store_true", help="PyTorch only: use FP16 to accelerate inference."
    )
    parser.add_argument(
        "--keras_predict",
        required=False,
        action="store_true",
        help="Whether to use model.predict " "instead of model() to do a " "forward pass.",
    )
LysandreJik's avatar
LysandreJik committed
595
    parser.add_argument("--save_to_csv", required=False, action="store_true", help="Save to a CSV file.")
596
597
598
599
600
601
    parser.add_argument(
        "--csv_filename", required=False, default=None, help="CSV filename used if saving results to csv."
    )
    parser.add_argument(
        "--average_over", required=False, default=30, type=int, help="Times an experiment will be run."
    )
602
603
    parser.add_argument("--batch_sizes", nargs="+", type=int, default=[1, 2, 4, 8])
    parser.add_argument("--slice_sizes", nargs="+", type=int, default=[8, 64, 128, 256, 512, 1024])
LysandreJik's avatar
LysandreJik committed
604
605

    args = parser.parse_args()
606
    if args.models == "all":
LysandreJik's avatar
LysandreJik committed
607
608
609
610
611
612
613
614
615
616
        args.models = [
            "gpt2",
            "bert-base-cased",
            "xlnet-base-cased",
            "xlm-mlm-en-2048",
            "transfo-xl-wt103",
            "openai-gpt",
            "distilbert-base-uncased",
            "distilgpt2",
            "roberta-base",
617
            "ctrl",
LysandreJik's avatar
LysandreJik committed
618
619
620
621
622
623
624
        ]
    else:
        args.models = args.models.split()

    print("Running with arguments", args)

    if args.torch:
625
626
627
        if is_torch_available():
            create_setup_and_compute(
                model_names=args.models,
628
629
                batch_sizes=args.batch_sizes,
                slice_sizes=args.slice_sizes,
630
631
632
                tensorflow=False,
                gpu=args.torch_cuda,
                torchscript=args.torchscript,
633
                fp16=args.fp16,
634
635
                save_to_csv=args.save_to_csv,
                csv_filename=args.csv_filename,
636
                average_over=args.average_over,
637
638
639
                no_speed=args.no_speed,
                no_memory=args.no_memory,
                verbose=args.verbose,
640
641
642
            )
        else:
            raise ImportError("Trying to run a PyTorch benchmark but PyTorch was not found in the environment.")
LysandreJik's avatar
LysandreJik committed
643
644

    if args.tensorflow:
645
646
647
        if is_tf_available():
            create_setup_and_compute(
                model_names=args.models,
648
649
                batch_sizes=args.batch_sizes,
                slice_sizes=args.slice_sizes,
650
651
                tensorflow=True,
                xla=args.xla,
652
                amp=args.amp,
653
654
                save_to_csv=args.save_to_csv,
                csv_filename=args.csv_filename,
655
                average_over=args.average_over,
656
657
658
                no_speed=args.no_speed,
                no_memory=args.no_memory,
                verbose=args.verbose,
659
660
661
            )
        else:
            raise ImportError("Trying to run a TensorFlow benchmark but TensorFlow was not found in the environment.")
LysandreJik's avatar
LysandreJik committed
662
663


664
665
if __name__ == "__main__":
    main()