2019-09-05 08:27:39 +08:00
|
|
|
# coding=utf-8
|
2020-12-08 07:36:34 +08:00
|
|
|
# Copyright 2020 The HuggingFace Team. All rights reserved.
|
2019-09-05 08:27:39 +08:00
|
|
|
#
|
|
|
|
# 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.
|
2019-12-22 23:20:32 +08:00
|
|
|
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2019-12-22 21:57:20 +08:00
|
|
|
import unittest
|
|
|
|
|
2019-12-21 22:57:32 +08:00
|
|
|
from transformers import BertConfig, is_tf_available
|
2021-04-09 06:41:36 +08:00
|
|
|
from transformers.models.auto import get_values
|
2020-11-11 03:02:33 +08:00
|
|
|
from transformers.testing_utils import require_tf, slow
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2019-12-22 20:44:13 +08:00
|
|
|
from .test_configuration_common import ConfigTester
|
2019-12-22 21:57:20 +08:00
|
|
|
from .test_modeling_tf_common import TFModelTesterMixin, ids_tensor
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2019-09-05 17:18:55 +08:00
|
|
|
|
2019-09-23 22:39:57 +08:00
|
|
|
if is_tf_available():
|
2019-09-05 08:53:52 +08:00
|
|
|
import tensorflow as tf
|
2020-08-24 23:03:01 +08:00
|
|
|
|
2020-11-13 03:08:26 +08:00
|
|
|
from transformers import TF_MODEL_FOR_PRETRAINING_MAPPING
|
2020-11-17 10:43:42 +08:00
|
|
|
from transformers.models.bert.modeling_tf_bert import (
|
2019-12-21 22:46:46 +08:00
|
|
|
TFBertForMaskedLM,
|
2020-08-24 23:03:01 +08:00
|
|
|
TFBertForMultipleChoice,
|
2019-12-21 22:46:46 +08:00
|
|
|
TFBertForNextSentencePrediction,
|
|
|
|
TFBertForPreTraining,
|
2020-08-24 23:03:01 +08:00
|
|
|
TFBertForQuestionAnswering,
|
2019-12-21 22:46:46 +08:00
|
|
|
TFBertForSequenceClassification,
|
|
|
|
TFBertForTokenClassification,
|
2020-08-24 23:03:01 +08:00
|
|
|
TFBertLMHeadModel,
|
|
|
|
TFBertModel,
|
2019-12-21 22:46:46 +08:00
|
|
|
)
|
2019-09-05 08:53:52 +08:00
|
|
|
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
class TFBertModelTester:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
parent,
|
|
|
|
batch_size=13,
|
|
|
|
seq_length=7,
|
|
|
|
is_training=True,
|
|
|
|
use_input_mask=True,
|
|
|
|
use_token_type_ids=True,
|
|
|
|
use_labels=True,
|
|
|
|
vocab_size=99,
|
|
|
|
hidden_size=32,
|
|
|
|
num_hidden_layers=5,
|
|
|
|
num_attention_heads=4,
|
|
|
|
intermediate_size=37,
|
|
|
|
hidden_act="gelu",
|
|
|
|
hidden_dropout_prob=0.1,
|
|
|
|
attention_probs_dropout_prob=0.1,
|
|
|
|
max_position_embeddings=512,
|
|
|
|
type_vocab_size=16,
|
|
|
|
type_sequence_label_size=2,
|
|
|
|
initializer_range=0.02,
|
|
|
|
num_labels=3,
|
|
|
|
num_choices=4,
|
|
|
|
scope=None,
|
|
|
|
):
|
|
|
|
self.parent = parent
|
|
|
|
self.batch_size = 13
|
|
|
|
self.seq_length = 7
|
|
|
|
self.is_training = True
|
|
|
|
self.use_input_mask = True
|
|
|
|
self.use_token_type_ids = True
|
|
|
|
self.use_labels = True
|
|
|
|
self.vocab_size = 99
|
|
|
|
self.hidden_size = 32
|
|
|
|
self.num_hidden_layers = 5
|
|
|
|
self.num_attention_heads = 4
|
|
|
|
self.intermediate_size = 37
|
|
|
|
self.hidden_act = "gelu"
|
|
|
|
self.hidden_dropout_prob = 0.1
|
|
|
|
self.attention_probs_dropout_prob = 0.1
|
|
|
|
self.max_position_embeddings = 512
|
|
|
|
self.type_vocab_size = 16
|
|
|
|
self.type_sequence_label_size = 2
|
|
|
|
self.initializer_range = 0.02
|
|
|
|
self.num_labels = 3
|
|
|
|
self.num_choices = 4
|
|
|
|
self.scope = None
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def prepare_config_and_inputs(self):
|
|
|
|
input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size)
|
|
|
|
|
|
|
|
input_mask = None
|
|
|
|
if self.use_input_mask:
|
|
|
|
input_mask = ids_tensor([self.batch_size, self.seq_length], vocab_size=2)
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
token_type_ids = None
|
|
|
|
if self.use_token_type_ids:
|
|
|
|
token_type_ids = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size)
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
sequence_labels = None
|
|
|
|
token_labels = None
|
|
|
|
choice_labels = None
|
|
|
|
if self.use_labels:
|
|
|
|
sequence_labels = ids_tensor([self.batch_size], self.type_sequence_label_size)
|
|
|
|
token_labels = ids_tensor([self.batch_size, self.seq_length], self.num_labels)
|
|
|
|
choice_labels = ids_tensor([self.batch_size], self.num_choices)
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
config = BertConfig(
|
|
|
|
vocab_size=self.vocab_size,
|
|
|
|
hidden_size=self.hidden_size,
|
|
|
|
num_hidden_layers=self.num_hidden_layers,
|
|
|
|
num_attention_heads=self.num_attention_heads,
|
|
|
|
intermediate_size=self.intermediate_size,
|
|
|
|
hidden_act=self.hidden_act,
|
|
|
|
hidden_dropout_prob=self.hidden_dropout_prob,
|
|
|
|
attention_probs_dropout_prob=self.attention_probs_dropout_prob,
|
|
|
|
max_position_embeddings=self.max_position_embeddings,
|
|
|
|
type_vocab_size=self.type_vocab_size,
|
|
|
|
initializer_range=self.initializer_range,
|
|
|
|
)
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def create_and_check_bert_model(
|
|
|
|
self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
|
|
|
):
|
|
|
|
model = TFBertModel(config=config)
|
|
|
|
inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
|
|
|
|
sequence_output, pooled_output = model(inputs)
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
inputs = [input_ids, input_mask]
|
2020-08-05 23:34:39 +08:00
|
|
|
result = model(inputs)
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-08-05 23:34:39 +08:00
|
|
|
result = model(input_ids)
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-08-13 16:29:06 +08:00
|
|
|
self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
|
|
|
|
self.parent.assertEqual(result.pooler_output.shape, (self.batch_size, self.hidden_size))
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-07-08 00:15:53 +08:00
|
|
|
def create_and_check_bert_lm_head(
|
|
|
|
self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
|
|
|
):
|
|
|
|
config.is_decoder = True
|
|
|
|
model = TFBertLMHeadModel(config=config)
|
|
|
|
inputs = {
|
|
|
|
"input_ids": input_ids,
|
|
|
|
"attention_mask": input_mask,
|
|
|
|
"token_type_ids": token_type_ids,
|
|
|
|
}
|
2020-08-05 23:34:39 +08:00
|
|
|
prediction_scores = model(inputs)["logits"]
|
2020-07-08 00:15:53 +08:00
|
|
|
self.parent.assertListEqual(
|
|
|
|
list(prediction_scores.numpy().shape), [self.batch_size, self.seq_length, self.vocab_size]
|
|
|
|
)
|
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def create_and_check_bert_for_masked_lm(
|
|
|
|
self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
|
|
|
):
|
|
|
|
model = TFBertForMaskedLM(config=config)
|
2020-07-08 00:15:53 +08:00
|
|
|
inputs = {
|
|
|
|
"input_ids": input_ids,
|
|
|
|
"attention_mask": input_mask,
|
|
|
|
"token_type_ids": token_type_ids,
|
|
|
|
}
|
2020-08-05 23:34:39 +08:00
|
|
|
result = model(inputs)
|
2020-08-13 16:29:06 +08:00
|
|
|
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size))
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def create_and_check_bert_for_next_sequence_prediction(
|
|
|
|
self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
|
|
|
):
|
|
|
|
model = TFBertForNextSentencePrediction(config=config)
|
|
|
|
inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
|
2020-08-05 23:34:39 +08:00
|
|
|
result = model(inputs)
|
2020-08-13 16:29:06 +08:00
|
|
|
self.parent.assertEqual(result.logits.shape, (self.batch_size, 2))
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def create_and_check_bert_for_pretraining(
|
|
|
|
self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
|
|
|
):
|
|
|
|
model = TFBertForPreTraining(config=config)
|
|
|
|
inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
|
2020-08-05 23:34:39 +08:00
|
|
|
result = model(inputs)
|
2020-08-13 16:29:06 +08:00
|
|
|
self.parent.assertEqual(result.prediction_logits.shape, (self.batch_size, self.seq_length, self.vocab_size))
|
|
|
|
self.parent.assertEqual(result.seq_relationship_logits.shape, (self.batch_size, 2))
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def create_and_check_bert_for_sequence_classification(
|
|
|
|
self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
|
|
|
):
|
|
|
|
config.num_labels = self.num_labels
|
|
|
|
model = TFBertForSequenceClassification(config=config)
|
2020-07-08 00:15:53 +08:00
|
|
|
inputs = {
|
|
|
|
"input_ids": input_ids,
|
|
|
|
"attention_mask": input_mask,
|
|
|
|
"token_type_ids": token_type_ids,
|
2020-06-16 20:03:43 +08:00
|
|
|
}
|
2020-07-08 00:15:53 +08:00
|
|
|
|
2020-08-05 23:34:39 +08:00
|
|
|
result = model(inputs)
|
2020-08-13 16:29:06 +08:00
|
|
|
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels))
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def create_and_check_bert_for_multiple_choice(
|
|
|
|
self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
|
|
|
):
|
|
|
|
config.num_choices = self.num_choices
|
|
|
|
model = TFBertForMultipleChoice(config=config)
|
|
|
|
multiple_choice_inputs_ids = tf.tile(tf.expand_dims(input_ids, 1), (1, self.num_choices, 1))
|
|
|
|
multiple_choice_input_mask = tf.tile(tf.expand_dims(input_mask, 1), (1, self.num_choices, 1))
|
|
|
|
multiple_choice_token_type_ids = tf.tile(tf.expand_dims(token_type_ids, 1), (1, self.num_choices, 1))
|
|
|
|
inputs = {
|
|
|
|
"input_ids": multiple_choice_inputs_ids,
|
|
|
|
"attention_mask": multiple_choice_input_mask,
|
|
|
|
"token_type_ids": multiple_choice_token_type_ids,
|
|
|
|
}
|
2020-08-05 23:34:39 +08:00
|
|
|
result = model(inputs)
|
2020-08-13 16:29:06 +08:00
|
|
|
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_choices))
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def create_and_check_bert_for_token_classification(
|
|
|
|
self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
|
|
|
):
|
|
|
|
config.num_labels = self.num_labels
|
|
|
|
model = TFBertForTokenClassification(config=config)
|
2020-07-08 00:15:53 +08:00
|
|
|
inputs = {
|
|
|
|
"input_ids": input_ids,
|
|
|
|
"attention_mask": input_mask,
|
|
|
|
"token_type_ids": token_type_ids,
|
|
|
|
}
|
2020-08-05 23:34:39 +08:00
|
|
|
result = model(inputs)
|
2020-08-13 16:29:06 +08:00
|
|
|
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels))
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def create_and_check_bert_for_question_answering(
|
|
|
|
self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
|
|
|
|
):
|
|
|
|
model = TFBertForQuestionAnswering(config=config)
|
2020-07-08 00:15:53 +08:00
|
|
|
inputs = {
|
|
|
|
"input_ids": input_ids,
|
|
|
|
"attention_mask": input_mask,
|
|
|
|
"token_type_ids": token_type_ids,
|
2020-06-16 20:03:43 +08:00
|
|
|
}
|
2020-07-08 00:15:53 +08:00
|
|
|
|
2020-08-05 23:34:39 +08:00
|
|
|
result = model(inputs)
|
2020-08-13 16:29:06 +08:00
|
|
|
self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length))
|
|
|
|
self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length))
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
def prepare_config_and_inputs_for_common(self):
|
|
|
|
config_and_inputs = self.prepare_config_and_inputs()
|
|
|
|
(
|
|
|
|
config,
|
|
|
|
input_ids,
|
|
|
|
token_type_ids,
|
|
|
|
input_mask,
|
|
|
|
sequence_labels,
|
|
|
|
token_labels,
|
|
|
|
choice_labels,
|
|
|
|
) = config_and_inputs
|
|
|
|
inputs_dict = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
|
|
|
|
return config, inputs_dict
|
2019-09-05 08:27:39 +08:00
|
|
|
|
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
@require_tf
|
|
|
|
class TFBertModelTest(TFModelTesterMixin, unittest.TestCase):
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-06-16 20:03:43 +08:00
|
|
|
all_model_classes = (
|
|
|
|
(
|
|
|
|
TFBertModel,
|
|
|
|
TFBertForMaskedLM,
|
2020-08-10 22:39:17 +08:00
|
|
|
TFBertLMHeadModel,
|
2020-06-16 20:03:43 +08:00
|
|
|
TFBertForNextSentencePrediction,
|
|
|
|
TFBertForPreTraining,
|
|
|
|
TFBertForQuestionAnswering,
|
|
|
|
TFBertForSequenceClassification,
|
|
|
|
TFBertForTokenClassification,
|
|
|
|
TFBertForMultipleChoice,
|
|
|
|
)
|
|
|
|
if is_tf_available()
|
|
|
|
else ()
|
|
|
|
)
|
2021-01-26 16:50:00 +08:00
|
|
|
test_head_masking = False
|
2021-02-15 20:55:10 +08:00
|
|
|
test_onnx = True
|
|
|
|
onnx_min_opset = 10
|
2019-09-05 08:27:39 +08:00
|
|
|
|
2020-11-13 03:08:26 +08:00
|
|
|
# special case for ForPreTraining model
|
|
|
|
def _prepare_for_class(self, inputs_dict, model_class, return_labels=False):
|
|
|
|
inputs_dict = super()._prepare_for_class(inputs_dict, model_class, return_labels=return_labels)
|
|
|
|
|
|
|
|
if return_labels:
|
2021-04-09 06:41:36 +08:00
|
|
|
if model_class in get_values(TF_MODEL_FOR_PRETRAINING_MAPPING):
|
2020-11-13 03:08:26 +08:00
|
|
|
inputs_dict["next_sentence_label"] = tf.zeros(self.model_tester.batch_size, dtype=tf.int32)
|
|
|
|
|
|
|
|
return inputs_dict
|
|
|
|
|
2019-09-05 08:27:39 +08:00
|
|
|
def setUp(self):
|
2020-06-16 20:03:43 +08:00
|
|
|
self.model_tester = TFBertModelTester(self)
|
2019-09-05 08:27:39 +08:00
|
|
|
self.config_tester = ConfigTester(self, config_class=BertConfig, hidden_size=37)
|
|
|
|
|
|
|
|
def test_config(self):
|
|
|
|
self.config_tester.run_common_tests()
|
|
|
|
|
|
|
|
def test_bert_model(self):
|
|
|
|
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
|
|
|
self.model_tester.create_and_check_bert_model(*config_and_inputs)
|
|
|
|
|
|
|
|
def test_for_masked_lm(self):
|
|
|
|
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
|
|
|
self.model_tester.create_and_check_bert_for_masked_lm(*config_and_inputs)
|
|
|
|
|
2020-07-08 00:15:53 +08:00
|
|
|
def test_for_causal_lm(self):
|
|
|
|
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
|
|
|
self.model_tester.create_and_check_bert_lm_head(*config_and_inputs)
|
|
|
|
|
2019-09-05 08:27:39 +08:00
|
|
|
def test_for_multiple_choice(self):
|
|
|
|
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
|
|
|
self.model_tester.create_and_check_bert_for_multiple_choice(*config_and_inputs)
|
|
|
|
|
|
|
|
def test_for_next_sequence_prediction(self):
|
|
|
|
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
|
|
|
self.model_tester.create_and_check_bert_for_next_sequence_prediction(*config_and_inputs)
|
|
|
|
|
|
|
|
def test_for_pretraining(self):
|
|
|
|
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
|
|
|
self.model_tester.create_and_check_bert_for_pretraining(*config_and_inputs)
|
|
|
|
|
|
|
|
def test_for_question_answering(self):
|
|
|
|
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
|
|
|
self.model_tester.create_and_check_bert_for_question_answering(*config_and_inputs)
|
|
|
|
|
|
|
|
def test_for_sequence_classification(self):
|
|
|
|
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
|
|
|
self.model_tester.create_and_check_bert_for_sequence_classification(*config_and_inputs)
|
|
|
|
|
|
|
|
def test_for_token_classification(self):
|
|
|
|
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
|
|
|
self.model_tester.create_and_check_bert_for_token_classification(*config_and_inputs)
|
|
|
|
|
|
|
|
def test_model_from_pretrained(self):
|
2020-10-05 21:58:45 +08:00
|
|
|
model = TFBertModel.from_pretrained("jplu/tiny-tf-bert-random")
|
|
|
|
self.assertIsNotNone(model)
|
|
|
|
|
2020-12-14 12:05:24 +08:00
|
|
|
def test_model_common_attributes(self):
|
|
|
|
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
|
|
|
list_lm_models = [TFBertForMaskedLM, TFBertForPreTraining, TFBertLMHeadModel]
|
|
|
|
|
|
|
|
for model_class in self.all_model_classes:
|
|
|
|
model = model_class(config)
|
|
|
|
assert isinstance(model.get_input_embeddings(), tf.keras.layers.Layer)
|
|
|
|
|
|
|
|
if model_class in list_lm_models:
|
2021-01-11 19:27:28 +08:00
|
|
|
x = model.get_output_embeddings()
|
2020-12-14 12:05:24 +08:00
|
|
|
assert isinstance(x, tf.keras.layers.Layer)
|
2021-01-11 19:27:28 +08:00
|
|
|
name = model.get_bias()
|
|
|
|
assert isinstance(name, dict)
|
|
|
|
for k, v in name.items():
|
|
|
|
assert isinstance(v, tf.Variable)
|
2020-12-14 12:05:24 +08:00
|
|
|
else:
|
2021-01-11 19:27:28 +08:00
|
|
|
x = model.get_output_embeddings()
|
2020-12-14 12:05:24 +08:00
|
|
|
assert x is None
|
2021-01-11 19:27:28 +08:00
|
|
|
name = model.get_bias()
|
|
|
|
assert name is None
|
2020-12-14 12:05:24 +08:00
|
|
|
|
2020-10-05 21:58:45 +08:00
|
|
|
def test_custom_load_tf_weights(self):
|
|
|
|
model, output_loading_info = TFBertForTokenClassification.from_pretrained(
|
2020-11-10 20:11:02 +08:00
|
|
|
"jplu/tiny-tf-bert-random", output_loading_info=True
|
2020-10-05 21:58:45 +08:00
|
|
|
)
|
2020-12-08 22:14:09 +08:00
|
|
|
self.assertEqual(sorted(output_loading_info["unexpected_keys"]), [])
|
2020-10-05 21:58:45 +08:00
|
|
|
for layer in output_loading_info["missing_keys"]:
|
|
|
|
self.assertTrue(layer.split("_")[0] in ["dropout", "classifier"])
|
2020-11-11 03:02:33 +08:00
|
|
|
|
|
|
|
|
2020-11-19 23:41:41 +08:00
|
|
|
@require_tf
|
2020-11-11 03:02:33 +08:00
|
|
|
class TFBertModelIntegrationTest(unittest.TestCase):
|
|
|
|
@slow
|
|
|
|
def test_inference_masked_lm(self):
|
|
|
|
model = TFBertForPreTraining.from_pretrained("lysandre/tiny-bert-random")
|
|
|
|
input_ids = tf.constant([[0, 1, 2, 3, 4, 5]])
|
|
|
|
output = model(input_ids)[0]
|
|
|
|
|
2020-11-24 07:19:12 +08:00
|
|
|
expected_shape = [1, 6, 32000]
|
2020-11-11 03:02:33 +08:00
|
|
|
self.assertEqual(output.shape, expected_shape)
|
|
|
|
|
|
|
|
print(output[:, :3, :3])
|
|
|
|
|
|
|
|
expected_slice = tf.constant(
|
|
|
|
[
|
|
|
|
[
|
2020-11-24 07:19:54 +08:00
|
|
|
[-0.05243197, -0.04498899, 0.05512108],
|
|
|
|
[-0.07444685, -0.01064632, 0.04352357],
|
|
|
|
[-0.05020351, 0.05530146, 0.00700043],
|
2020-11-11 03:02:33 +08:00
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
tf.debugging.assert_near(output[:, :3, :3], expected_slice, atol=1e-4)
|