[WIP] Test TF Flaubert + Add {XLM, Flaubert}{TokenClassification, MultipleC… (#5614)
* Test TF Flaubert + Add {XLM, Flaubert}{TokenClassification, MultipleChoice} models and tests
* AutoModels
Tiny tweaks
* Style
* Final changes before merge
* Re-order for simpler review
* Final fixes
* Addressing @sgugger's comments
* Test MultipleChoice
This commit is contained in:
@@ -32,6 +32,7 @@ if is_tf_available():
|
||||
TFXLMForSequenceClassification,
|
||||
TFXLMForQuestionAnsweringSimple,
|
||||
TFXLMForTokenClassification,
|
||||
TFXLMForMultipleChoice,
|
||||
TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||
)
|
||||
|
||||
@@ -91,6 +92,7 @@ class TFXLMModelTester:
|
||||
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)
|
||||
is_impossible_labels = ids_tensor([self.batch_size], 2, dtype=tf.float32)
|
||||
choice_labels = ids_tensor([self.batch_size], self.num_choices)
|
||||
|
||||
config = XLMConfig(
|
||||
vocab_size=self.vocab_size,
|
||||
@@ -120,6 +122,7 @@ class TFXLMModelTester:
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
is_impossible_labels,
|
||||
choice_labels,
|
||||
input_mask,
|
||||
)
|
||||
|
||||
@@ -132,6 +135,7 @@ class TFXLMModelTester:
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
is_impossible_labels,
|
||||
choice_labels,
|
||||
input_mask,
|
||||
):
|
||||
model = TFXLMModel(config=config)
|
||||
@@ -157,6 +161,7 @@ class TFXLMModelTester:
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
is_impossible_labels,
|
||||
choice_labels,
|
||||
input_mask,
|
||||
):
|
||||
model = TFXLMWithLMHeadModel(config)
|
||||
@@ -181,6 +186,7 @@ class TFXLMModelTester:
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
is_impossible_labels,
|
||||
choice_labels,
|
||||
input_mask,
|
||||
):
|
||||
model = TFXLMForQuestionAnsweringSimple(config)
|
||||
@@ -206,6 +212,7 @@ class TFXLMModelTester:
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
is_impossible_labels,
|
||||
choice_labels,
|
||||
input_mask,
|
||||
):
|
||||
model = TFXLMForSequenceClassification(config)
|
||||
@@ -229,6 +236,7 @@ class TFXLMModelTester:
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
is_impossible_labels,
|
||||
choice_labels,
|
||||
input_mask,
|
||||
):
|
||||
config.num_labels = self.num_labels
|
||||
@@ -240,6 +248,32 @@ class TFXLMModelTester:
|
||||
}
|
||||
self.parent.assertListEqual(list(result["logits"].shape), [self.batch_size, self.seq_length, self.num_labels])
|
||||
|
||||
def create_and_check_xlm_for_multiple_choice(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
token_type_ids,
|
||||
input_lengths,
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
is_impossible_labels,
|
||||
choice_labels,
|
||||
input_mask,
|
||||
):
|
||||
config.num_choices = self.num_choices
|
||||
model = TFXLMForMultipleChoice(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,
|
||||
}
|
||||
(logits,) = model(inputs)
|
||||
result = {"logits": logits.numpy()}
|
||||
self.parent.assertListEqual(list(result["logits"].shape), [self.batch_size, self.num_choices])
|
||||
|
||||
def prepare_config_and_inputs_for_common(self):
|
||||
config_and_inputs = self.prepare_config_and_inputs()
|
||||
(
|
||||
@@ -250,6 +284,7 @@ class TFXLMModelTester:
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
is_impossible_labels,
|
||||
choice_labels,
|
||||
input_mask,
|
||||
) = config_and_inputs
|
||||
inputs_dict = {
|
||||
@@ -265,13 +300,13 @@ class TFXLMModelTester:
|
||||
class TFXLMModelTest(TFModelTesterMixin, unittest.TestCase):
|
||||
|
||||
all_model_classes = (
|
||||
# TODO The multiple choice model is missing and should be added.
|
||||
(
|
||||
TFXLMModel,
|
||||
TFXLMWithLMHeadModel,
|
||||
TFXLMForSequenceClassification,
|
||||
TFXLMForQuestionAnsweringSimple,
|
||||
TFXLMForTokenClassification,
|
||||
TFXLMForMultipleChoice,
|
||||
)
|
||||
if is_tf_available()
|
||||
else ()
|
||||
@@ -307,6 +342,10 @@ class TFXLMModelTest(TFModelTesterMixin, unittest.TestCase):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_xlm_for_token_classification(*config_and_inputs)
|
||||
|
||||
def test_for_multiple_choice(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_xlm_for_multiple_choice(*config_and_inputs)
|
||||
|
||||
@slow
|
||||
def test_model_from_pretrained(self):
|
||||
for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
|
||||
|
||||
Reference in New Issue
Block a user