forked from pytorch/text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_experimental_vectors.py
50 lines (39 loc) · 1.56 KB
/
benchmark_experimental_vectors.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
import time
import torch
from torchtext.prototype.datasets import AG_NEWS
from torchtext.prototype.vectors import FastText as FastTextExperimental
from torchtext.vocab import FastText
def benchmark_experimental_vectors():
def _run_benchmark_lookup(tokens, vector):
t0 = time.monotonic()
for token in tokens:
vector[token]
print("Lookup time:", time.monotonic() - t0)
train = AG_NEWS(split="train")
vocab = train.get_vocab()
tokens = []
for (label, text) in train:
for id in text.tolist():
tokens.append(vocab.itos[id])
# existing FastText construction
print("FastText Existing Construction")
t0 = time.monotonic()
fast_text = FastText()
print("Construction time:", time.monotonic() - t0)
# experimental FastText construction
print("FastText Experimental Construction")
t0 = time.monotonic()
fast_text_experimental = FastTextExperimental(validate_file=False)
print("Construction time:", time.monotonic() - t0)
# existing FastText eager lookup
print("FastText Existing - Eager Mode")
_run_benchmark_lookup(tokens, fast_text)
# experimental FastText eager lookup
print("FastText Experimental - Eager Mode")
_run_benchmark_lookup(tokens, fast_text_experimental)
# experimental FastText jit lookup
print("FastText Experimental - Jit Mode")
jit_fast_text_experimental = torch.jit.script(fast_text_experimental)
_run_benchmark_lookup(tokens, jit_fast_text_experimental)
if __name__ == "__main__":
benchmark_experimental_vectors()