-
Notifications
You must be signed in to change notification settings - Fork 3
/
huggingface_models.py
174 lines (150 loc) · 9.73 KB
/
huggingface_models.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
import math
import importlib
import pandas as pd
import torch
import subprocess
import sys
pd.options.display.max_colwidth=100
from execution import runner
from components.apex_adam_optimizer import optim_func
def pip_install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
try:
importlib.import_module('transformers')
except ModuleNotFoundError:
print("Installing HuggingFace Transformers...")
pip_install('git+https://github.com/huggingface/transformers.git#egg=transformers')
finally:
from transformers import BertConfig, BertForPreTraining
from transformers import GPT2Config, GPT2LMHeadModel
from transformers import RobertaConfig, RobertaForMaskedLM
from transformers import AlbertConfig, AlbertForPreTraining
from transformers import T5Config, T5ForConditionalGeneration
from transformers import BartConfig, BartForConditionalGeneration
from transformers import DebertaConfig, DebertaForMaskedLM
from transformers import XLNetConfig, XLNetLMHeadModel
def bert_p1_input_func(steps, dtype, device) :
vocab_size = 30522
sequences = 64
sequence_length = 128
results = []
for _ in range(steps) :
input_ids = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
attention_mask = torch.randint(0, 2, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
labels = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
next_sentence_labels = torch.randint(0, 2, (sequences,), device=device, dtype=torch.int64, requires_grad=False)
results.append([input_ids, attention_mask, None, None, None, None, labels, next_sentence_labels, None, None, False])
return results
def bert_p2_input_func(steps, dtype, device) :
vocab_size = 30522
sequences = 16
sequence_length = 512
results = []
for _ in range(steps) :
input_ids = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
attention_mask = torch.randint(0, 2, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
labels = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
next_sentence_labels = torch.randint(0, 2, (sequences,), device=device, dtype=torch.int64, requires_grad=False)
results.append([input_ids, attention_mask, None, None, None, None, labels, next_sentence_labels, None, None, False])
return results
def gpt2_input_func(steps, dtype, device) :
vocab_size = 50257
sequences = 2
sequence_length = 1024
results = []
for _ in range(steps) :
input_ids = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
attention_mask = torch.randint(0, 2, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
labels = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
results.append([input_ids, None, attention_mask, None, None, None, None, None, None, labels, None, None, None, False])
return results
def roberta_input_func(steps, dtype, device) :
vocab_size = 50265
sequences = 64
sequence_length = 128
results = []
for _ in range(steps) :
input_ids = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
attention_mask = torch.randint(0, 2, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
labels = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
results.append([input_ids, attention_mask, None, None, None, None, None, None, labels, None, None, False])
return results
def albert_input_func(steps, dtype, device) :
vocab_size = 30000
sequences = 8
sequence_length = 512
results = []
for _ in range(steps) :
input_ids = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
attention_mask = torch.randint(0, 2, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
labels = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
sentence_order_label = torch.randint(0, 2, (sequences,), device=device, dtype=torch.int64, requires_grad=False)
results.append([input_ids, attention_mask, None, None, None, None, labels, sentence_order_label, None, None, False])
return results
def t5_input_func(steps, dtype, device) :
vocab_size = 32128
sequences = 8
src_sequence_length = 512
tgt_sequence_length = 128
results = []
for _ in range(steps) :
input_ids = torch.randint(0, vocab_size, (sequences, src_sequence_length), device=device, dtype=torch.int64, requires_grad=False)
attention_mask = torch.randint(0, 2, (sequences, src_sequence_length), device=device, dtype=torch.int64, requires_grad=False)
labels = torch.randint(0, vocab_size, (sequences, tgt_sequence_length), device=device, dtype=torch.int64, requires_grad=False)
results.append([input_ids, attention_mask, None, None, None, None, None, None, None, None, None, labels, None, None, None, False])
return results
def bart_input_func(steps, dtype, device) :
vocab_size = 50265
sequences = 8
src_sequence_length = 1024
tgt_sequence_length = 128
results = []
for _ in range(steps) :
input_ids = torch.randint(0, vocab_size, (sequences, src_sequence_length), device=device, dtype=torch.int64, requires_grad=False)
attention_mask = torch.randint(0, 2, (sequences, src_sequence_length), device=device, dtype=torch.int64, requires_grad=False)
labels = torch.randint(0, vocab_size, (sequences, tgt_sequence_length), device=device, dtype=torch.int64, requires_grad=False)
results.append([input_ids, attention_mask, None, None, None, None, None, None, None, None, None, labels, None, None, None, False])
return results
def deberta_input_func(steps, dtype, device) :
vocab_size = 50265
sequences = 8
sequence_length = 512
results = []
for _ in range(steps) :
input_ids = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
attention_mask = torch.randint(0, 2, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
labels = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
results.append([input_ids, attention_mask, None, None, None, labels, None, None, False])
return results
def xlnet_input_func(steps, dtype, device) :
vocab_size = 32000
sequences = 16
sequence_length = 512
results = []
for _ in range(steps) :
input_ids = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
attention_mask = torch.randint(0, 2, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
labels = torch.randint(0, vocab_size, (sequences, sequence_length), device=device, dtype=torch.int64, requires_grad=False)
results.append([input_ids, attention_mask, None, None, None, None, None, None, None, labels, None, None, False])
return results
if __name__ == "__main__" :
final_results = []
config = BertConfig.from_pretrained('bert-large-uncased')
final_results += runner.run(sys.argv, 'BertForPreTraining_P1_bert-large-uncased_[seqs=64,seql=128]', BertForPreTraining(config), optim_func, bert_p1_input_func, None)
final_results += runner.run(sys.argv, 'BertForPreTraining_P2_bert-large-uncased_[seqs=16,seql=512]', BertForPreTraining(config), optim_func, bert_p2_input_func, None)
config = GPT2Config.from_pretrained('gpt2-large')
final_results += runner.run(sys.argv, 'GPT2LMHeadModel_gpt2-large_[seqs=2,seql=1024]', GPT2LMHeadModel(config), optim_func, gpt2_input_func, None)
config = RobertaConfig.from_pretrained('roberta-large')
final_results += runner.run(sys.argv, 'RobertaForMaskedLM_roberta-large_[seqs=64,seql=128]', RobertaForMaskedLM(config), optim_func, roberta_input_func, None)
config = AlbertConfig.from_pretrained('albert-xxlarge-v2')
final_results += runner.run(sys.argv, 'AlbertForPreTraining_albert-xxlarge-v2_[seqs=8,seql=512]', AlbertForPreTraining(config), optim_func, albert_input_func, None)
config = T5Config.from_pretrained('t5-large')
final_results += runner.run(sys.argv, 'T5ForConditionalGeneration_t5-large_[seqs=8,src_seql=512,tgt_seql=128]', T5ForConditionalGeneration(config), optim_func, t5_input_func, None)
config = BartConfig.from_pretrained('facebook/bart-large')
final_results += runner.run(sys.argv, 'BartForConditionalGeneration_bart-large_[seqs=8,src_seql=1024,tgt_seql=128]', BartForConditionalGeneration(config), optim_func, bart_input_func, None)
config = DebertaConfig.from_pretrained('microsoft/deberta-large')
final_results += runner.run(sys.argv, 'DebertaForMaskedLM_deberata-large_[seqs=8,seql=512]', DebertaForMaskedLM(config), optim_func, deberta_input_func, None)
config = XLNetConfig.from_pretrained('xlnet-large-cased')
final_results += runner.run(sys.argv, 'XLNetLMHeadModel_xlnet-large-cased_[seqs=16,seql=512]', XLNetLMHeadModel(config), optim_func, xlnet_input_func, None)
print('=========================== Final Results ===========================')
print(pd.DataFrame(final_results))