[test] added transformers models to test model zoo (#3135)

This commit is contained in:
Frank Lee
2023-03-15 11:26:10 +08:00
committed by GitHub
parent a674c63348
commit 6d48eb0560
12 changed files with 339 additions and 193 deletions

View File

@@ -1,66 +1,18 @@
import pytest
import torch
import transformers
from hf_tracer_utils import trace_model_and_compare_output
from tests.kit.model_zoo import model_zoo
BATCH_SIZE = 2
SEQ_LENGTH = 16
def test_single_sentence_albert():
MODEL_LIST = [
transformers.AlbertModel,
transformers.AlbertForPreTraining,
transformers.AlbertForMaskedLM,
transformers.AlbertForSequenceClassification,
transformers.AlbertForTokenClassification,
]
def test_albert():
sub_registry = model_zoo.get_sub_registry('transformers_albert')
config = transformers.AlbertConfig(embedding_size=128,
hidden_size=128,
num_hidden_layers=2,
num_attention_heads=4,
intermediate_size=256)
def data_gen():
input_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
token_type_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
attention_mask = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
meta_args = dict(input_ids=input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask)
return meta_args
for model_cls in MODEL_LIST:
model = model_cls(config=config)
trace_model_and_compare_output(model, data_gen)
def test_multi_sentence_albert():
config = transformers.AlbertConfig(hidden_size=128,
num_hidden_layers=2,
num_attention_heads=4,
intermediate_size=256)
tokenizer = transformers.BertTokenizer.from_pretrained("bert-base-uncased")
def data_gen_for_qa():
question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
inputs = tokenizer(question, text, return_tensors="pt")
return inputs
model = transformers.AlbertForQuestionAnswering(config)
trace_model_and_compare_output(model, data_gen_for_qa)
def data_gen_for_mcq():
prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
choice0 = "It is eaten with a fork and a knife."
choice1 = "It is eaten while held in the hand."
encoding = tokenizer([prompt, prompt], [choice0, choice1], return_tensors="pt", padding=True)
encoding = {k: v.unsqueeze(0) for k, v in encoding.items()}
return encoding
model = transformers.AlbertForMultipleChoice(config)
trace_model_and_compare_output(model, data_gen_for_mcq)
for name, (model_fn, data_gen_fn, _, _) in sub_registry.items():
model = model_fn()
trace_model_and_compare_output(model, data_gen_fn)
if __name__ == '__main__':
test_single_sentence_albert()
test_multi_sentence_albert()
test_albert()

View File

@@ -1,69 +1,15 @@
import pytest
import torch
import transformers
from hf_tracer_utils import trace_model_and_compare_output
BATCH_SIZE = 2
SEQ_LENGTH = 16
from tests.kit.model_zoo import model_zoo
def test_single_sentence_bert():
MODEL_LIST = [
transformers.BertModel,
transformers.BertForPreTraining,
transformers.BertLMHeadModel,
transformers.BertForMaskedLM,
transformers.BertForSequenceClassification,
transformers.BertForTokenClassification,
]
def test_bert():
sub_registry = model_zoo.get_sub_registry('transformers_bert')
config = transformers.BertConfig(hidden_size=128, num_hidden_layers=2, num_attention_heads=4, intermediate_size=256)
def data_gen():
input_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
token_type_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
attention_mask = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
meta_args = dict(input_ids=input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask)
return meta_args
for model_cls in MODEL_LIST:
model = model_cls(config=config)
trace_model_and_compare_output(model, data_gen)
def test_multi_sentence_bert():
config = transformers.BertConfig(hidden_size=128, num_hidden_layers=2, num_attention_heads=4, intermediate_size=256)
tokenizer = transformers.BertTokenizer.from_pretrained("bert-base-uncased")
def data_gen_for_next_sentence():
prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
next_sentence = "The sky is blue due to the shorter wavelength of blue light."
encoding = tokenizer(prompt, next_sentence, return_tensors="pt")
return encoding
model = transformers.BertForNextSentencePrediction(config)
trace_model_and_compare_output(model, data_gen_for_next_sentence)
def data_gen_for_qa():
question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
inputs = tokenizer(question, text, return_tensors="pt")
return inputs
model = transformers.BertForQuestionAnswering(config)
trace_model_and_compare_output(model, data_gen_for_qa)
def data_gen_for_mcq():
prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
choice0 = "It is eaten with a fork and a knife."
choice1 = "It is eaten while held in the hand."
encoding = tokenizer([prompt, prompt], [choice0, choice1], return_tensors="pt", padding=True)
encoding = {k: v.unsqueeze(0) for k, v in encoding.items()}
return encoding
model = transformers.BertForMultipleChoice(config)
trace_model_and_compare_output(model, data_gen_for_mcq)
for name, (model_fn, data_gen_fn, _, _) in sub_registry.items():
model = model_fn()
trace_model_and_compare_output(model, data_gen_fn)
if __name__ == '__main__':
test_single_sentence_bert()
test_multi_sentence_bert()
test_bert()

View File

@@ -1,35 +1,17 @@
import pytest
import torch
import transformers
from hf_tracer_utils import trace_model_and_compare_output
BATCH_SIZE = 1
SEQ_LENGTH = 16
from tests.kit.model_zoo import model_zoo
# TODO: remove this skip once we handle the latest gpt model
@pytest.mark.skip
def test_gpt():
MODEL_LIST = [
transformers.GPT2Model,
transformers.GPT2LMHeadModel,
transformers.GPT2DoubleHeadsModel,
transformers.GPT2ForTokenClassification,
# transformers.GPT2ForSequenceClassification, # not supported yet
]
sub_registry = model_zoo.get_sub_registry('transformers_gpt')
config = transformers.GPT2Config(n_position=64, n_layer=2, n_head=4)
def data_gen():
input_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
token_type_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
attention_mask = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
kwargs = dict(input_ids=input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask)
return kwargs
for model_cls in MODEL_LIST:
model = model_cls(config=config)
trace_model_and_compare_output(model, data_gen)
for name, (model_fn, data_gen_fn, _, _) in sub_registry.items():
model = model_fn()
trace_model_and_compare_output(model, data_gen_fn)
if __name__ == '__main__':

View File

@@ -1,29 +1,14 @@
import pytest
import torch
import transformers
from hf_tracer_utils import trace_model_and_compare_output
BATCH_SIZE = 1
SEQ_LENGTH = 16
from tests.kit.model_zoo import model_zoo
def test_opt():
MODEL_LIST = [
transformers.OPTModel,
transformers.OPTForCausalLM,
]
sub_registry = model_zoo.get_sub_registry('transformers_opt')
config = transformers.OPTConfig(hidden_size=128, num_hidden_layers=2, num_attention_heads=4)
def data_gen():
input_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
attention_mask = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
kwargs = dict(input_ids=input_ids, attention_mask=attention_mask)
return kwargs
for model_cls in MODEL_LIST:
model = model_cls(config=config)
trace_model_and_compare_output(model, data_gen)
for name, (model_fn, data_gen_fn, _, _) in sub_registry.items():
model = model_fn()
trace_model_and_compare_output(model, data_gen_fn)
if __name__ == '__main__':

View File

@@ -1,41 +1,14 @@
import pytest
import torch
import transformers
from hf_tracer_utils import trace_model_and_compare_output
BATCH_SIZE = 1
SEQ_LENGTH = 16
from tests.kit.model_zoo import model_zoo
def test_t5():
MODEL_LIST = [
transformers.T5Model,
transformers.T5ForConditionalGeneration,
transformers.T5EncoderModel,
]
sub_registry = model_zoo.get_sub_registry('transformers_t5')
config = transformers.T5Config(d_model=128, num_layers=2)
def data_gen():
input_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
decoder_input_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
kwargs = dict(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
return kwargs
def data_gen_for_encoder_only():
input_ids = torch.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=torch.int64)
kwargs = dict(input_ids=input_ids)
return kwargs
for model_cls in MODEL_LIST:
model = model_cls(config=config)
if isinstance(model, transformers.T5EncoderModel):
data_gen_func = data_gen_for_encoder_only
else:
data_gen_func = data_gen
trace_model_and_compare_output(model, data_gen_func)
for name, (model_fn, data_gen_fn, _, _) in sub_registry.items():
model = model_fn()
trace_model_and_compare_output(model, data_gen_fn)
if __name__ == '__main__':