transformers
702 строки · 27.4 Кб
1# coding=utf-8
2# Copyright 2020 The HuggingFace Team. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17from __future__ import annotations
18
19import unittest
20
21from transformers import RobertaConfig, is_tf_available
22from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
23
24from ...test_configuration_common import ConfigTester
25from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
26from ...test_pipeline_mixin import PipelineTesterMixin
27
28
29if is_tf_available():
30import numpy
31import tensorflow as tf
32
33from transformers.models.roberta.modeling_tf_roberta import (
34TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
35TFRobertaForCausalLM,
36TFRobertaForMaskedLM,
37TFRobertaForMultipleChoice,
38TFRobertaForQuestionAnswering,
39TFRobertaForSequenceClassification,
40TFRobertaForTokenClassification,
41TFRobertaModel,
42)
43
44
45class TFRobertaModelTester:
46def __init__(
47self,
48parent,
49):
50self.parent = parent
51self.batch_size = 13
52self.seq_length = 7
53self.is_training = True
54self.use_input_mask = True
55self.use_token_type_ids = True
56self.use_labels = True
57self.vocab_size = 99
58self.hidden_size = 32
59self.num_hidden_layers = 2
60self.num_attention_heads = 4
61self.intermediate_size = 37
62self.hidden_act = "gelu"
63self.hidden_dropout_prob = 0.1
64self.attention_probs_dropout_prob = 0.1
65self.max_position_embeddings = 512
66self.type_vocab_size = 16
67self.type_sequence_label_size = 2
68self.initializer_range = 0.02
69self.num_labels = 3
70self.num_choices = 4
71self.scope = None
72
73def prepare_config_and_inputs(self):
74input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size)
75
76input_mask = None
77if self.use_input_mask:
78input_mask = random_attention_mask([self.batch_size, self.seq_length])
79
80token_type_ids = None
81if self.use_token_type_ids:
82token_type_ids = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size)
83
84sequence_labels = None
85token_labels = None
86choice_labels = None
87if self.use_labels:
88sequence_labels = ids_tensor([self.batch_size], self.type_sequence_label_size)
89token_labels = ids_tensor([self.batch_size, self.seq_length], self.num_labels)
90choice_labels = ids_tensor([self.batch_size], self.num_choices)
91
92config = RobertaConfig(
93vocab_size=self.vocab_size,
94hidden_size=self.hidden_size,
95num_hidden_layers=self.num_hidden_layers,
96num_attention_heads=self.num_attention_heads,
97intermediate_size=self.intermediate_size,
98hidden_act=self.hidden_act,
99hidden_dropout_prob=self.hidden_dropout_prob,
100attention_probs_dropout_prob=self.attention_probs_dropout_prob,
101max_position_embeddings=self.max_position_embeddings,
102type_vocab_size=self.type_vocab_size,
103initializer_range=self.initializer_range,
104)
105
106return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
107
108def prepare_config_and_inputs_for_decoder(self):
109(
110config,
111input_ids,
112token_type_ids,
113input_mask,
114sequence_labels,
115token_labels,
116choice_labels,
117) = self.prepare_config_and_inputs()
118
119config.is_decoder = True
120encoder_hidden_states = floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
121encoder_attention_mask = ids_tensor([self.batch_size, self.seq_length], vocab_size=2)
122
123return (
124config,
125input_ids,
126token_type_ids,
127input_mask,
128sequence_labels,
129token_labels,
130choice_labels,
131encoder_hidden_states,
132encoder_attention_mask,
133)
134
135def create_and_check_model(
136self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
137):
138model = TFRobertaModel(config=config)
139inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
140result = model(inputs)
141
142inputs = [input_ids, input_mask]
143result = model(inputs)
144
145result = model(input_ids)
146
147self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
148
149def create_and_check_causal_lm_base_model(
150self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
151):
152config.is_decoder = True
153
154model = TFRobertaModel(config=config)
155inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
156result = model(inputs)
157
158inputs = [input_ids, input_mask]
159result = model(inputs)
160
161result = model(input_ids)
162
163self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
164
165def create_and_check_model_as_decoder(
166self,
167config,
168input_ids,
169token_type_ids,
170input_mask,
171sequence_labels,
172token_labels,
173choice_labels,
174encoder_hidden_states,
175encoder_attention_mask,
176):
177config.add_cross_attention = True
178
179model = TFRobertaModel(config=config)
180inputs = {
181"input_ids": input_ids,
182"attention_mask": input_mask,
183"token_type_ids": token_type_ids,
184"encoder_hidden_states": encoder_hidden_states,
185"encoder_attention_mask": encoder_attention_mask,
186}
187result = model(inputs)
188
189inputs = [input_ids, input_mask]
190result = model(inputs, token_type_ids=token_type_ids, encoder_hidden_states=encoder_hidden_states)
191
192# Also check the case where encoder outputs are not passed
193result = model(input_ids, attention_mask=input_mask, token_type_ids=token_type_ids)
194
195self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
196
197def create_and_check_causal_lm_model(
198self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
199):
200config.is_decoder = True
201
202model = TFRobertaForCausalLM(config=config)
203inputs = {
204"input_ids": input_ids,
205"attention_mask": input_mask,
206"token_type_ids": token_type_ids,
207}
208prediction_scores = model(inputs)["logits"]
209self.parent.assertListEqual(
210list(prediction_scores.numpy().shape), [self.batch_size, self.seq_length, self.vocab_size]
211)
212
213def create_and_check_causal_lm_model_as_decoder(
214self,
215config,
216input_ids,
217token_type_ids,
218input_mask,
219sequence_labels,
220token_labels,
221choice_labels,
222encoder_hidden_states,
223encoder_attention_mask,
224):
225config.add_cross_attention = True
226
227model = TFRobertaForCausalLM(config=config)
228inputs = {
229"input_ids": input_ids,
230"attention_mask": input_mask,
231"token_type_ids": token_type_ids,
232"encoder_hidden_states": encoder_hidden_states,
233"encoder_attention_mask": encoder_attention_mask,
234}
235result = model(inputs)
236
237inputs = [input_ids, input_mask]
238result = model(inputs, token_type_ids=token_type_ids, encoder_hidden_states=encoder_hidden_states)
239
240prediction_scores = result["logits"]
241self.parent.assertListEqual(
242list(prediction_scores.numpy().shape), [self.batch_size, self.seq_length, self.vocab_size]
243)
244
245def create_and_check_causal_lm_model_past(
246self,
247config,
248input_ids,
249token_type_ids,
250input_mask,
251sequence_labels,
252token_labels,
253choice_labels,
254):
255config.is_decoder = True
256
257model = TFRobertaForCausalLM(config=config)
258
259# special to `RobertaEmbeddings` in `Roberta`:
260# - its `padding_idx` and its effect on `position_ids`
261# (TFRobertaEmbeddings.create_position_ids_from_input_ids)
262# - `1` here is `TFRobertaEmbeddings.padding_idx`
263input_ids = tf.where(input_ids == 1, 2, input_ids)
264
265# first forward pass
266outputs = model(input_ids, use_cache=True)
267outputs_use_cache_conf = model(input_ids)
268outputs_no_past = model(input_ids, use_cache=False)
269
270self.parent.assertTrue(len(outputs) == len(outputs_use_cache_conf))
271self.parent.assertTrue(len(outputs) == len(outputs_no_past) + 1)
272
273past_key_values = outputs.past_key_values
274
275# create hypothetical next token and extent to next_input_ids
276next_tokens = ids_tensor((self.batch_size, 1), config.vocab_size)
277
278# append to next input_ids and attn_mask
279next_input_ids = tf.concat([input_ids, next_tokens], axis=-1)
280
281output_from_no_past = model(next_input_ids, output_hidden_states=True).hidden_states[0]
282output_from_past = model(
283next_tokens, past_key_values=past_key_values, output_hidden_states=True
284).hidden_states[0]
285
286# select random slice
287random_slice_idx = int(ids_tensor((1,), output_from_past.shape[-1]))
288output_from_no_past_slice = output_from_no_past[:, -1, random_slice_idx]
289output_from_past_slice = output_from_past[:, 0, random_slice_idx]
290
291# test that outputs are equal for slice
292tf.debugging.assert_near(output_from_past_slice, output_from_no_past_slice, rtol=1e-6)
293
294def create_and_check_causal_lm_model_past_with_attn_mask(
295self,
296config,
297input_ids,
298token_type_ids,
299input_mask,
300sequence_labels,
301token_labels,
302choice_labels,
303):
304config.is_decoder = True
305
306model = TFRobertaForCausalLM(config=config)
307
308# special to `RobertaEmbeddings` in `Roberta`:
309# - its `padding_idx` and its effect on `position_ids`
310# (TFRobertaEmbeddings.create_position_ids_from_input_ids)
311# - `1` here is `TFRobertaEmbeddings.padding_idx`
312# avoid `padding_idx` in the past
313input_ids = tf.where(input_ids == 1, 2, input_ids)
314
315# create attention mask
316half_seq_length = self.seq_length // 2
317attn_mask_begin = tf.ones((self.batch_size, half_seq_length), dtype=tf.int32)
318attn_mask_end = tf.zeros((self.batch_size, self.seq_length - half_seq_length), dtype=tf.int32)
319attn_mask = tf.concat([attn_mask_begin, attn_mask_end], axis=1)
320
321# first forward pass
322outputs = model(input_ids, attention_mask=attn_mask, use_cache=True)
323
324# create hypothetical next token and extent to next_input_ids
325next_tokens = ids_tensor((self.batch_size, 1), config.vocab_size)
326
327past_key_values = outputs.past_key_values
328
329# change a random masked slice from input_ids
330random_seq_idx_to_change = ids_tensor((1,), half_seq_length).numpy() + 1
331random_other_next_tokens = ids_tensor((self.batch_size, self.seq_length), config.vocab_size)
332vector_condition = tf.range(self.seq_length) == (self.seq_length - random_seq_idx_to_change)
333condition = tf.transpose(
334tf.broadcast_to(tf.expand_dims(vector_condition, -1), (self.seq_length, self.batch_size))
335)
336input_ids = tf.where(condition, random_other_next_tokens, input_ids)
337# avoid `padding_idx` in the past
338input_ids = tf.where(input_ids == 1, 2, input_ids)
339
340# append to next input_ids and
341next_input_ids = tf.concat([input_ids, next_tokens], axis=-1)
342attn_mask = tf.concat(
343[attn_mask, tf.ones((attn_mask.shape[0], 1), dtype=tf.int32)],
344axis=1,
345)
346
347output_from_no_past = model(
348next_input_ids,
349attention_mask=attn_mask,
350output_hidden_states=True,
351).hidden_states[0]
352output_from_past = model(
353next_tokens, past_key_values=past_key_values, attention_mask=attn_mask, output_hidden_states=True
354).hidden_states[0]
355
356# select random slice
357random_slice_idx = int(ids_tensor((1,), output_from_past.shape[-1]))
358output_from_no_past_slice = output_from_no_past[:, -1, random_slice_idx]
359output_from_past_slice = output_from_past[:, 0, random_slice_idx]
360
361# test that outputs are equal for slice
362tf.debugging.assert_near(output_from_past_slice, output_from_no_past_slice, rtol=1e-6)
363
364def create_and_check_causal_lm_model_past_large_inputs(
365self,
366config,
367input_ids,
368token_type_ids,
369input_mask,
370sequence_labels,
371token_labels,
372choice_labels,
373):
374config.is_decoder = True
375
376model = TFRobertaForCausalLM(config=config)
377
378# special to `RobertaEmbeddings` in `Roberta`:
379# - its `padding_idx` and its effect on `position_ids`
380# (TFRobertaEmbeddings.create_position_ids_from_input_ids)
381# - `1` here is `TFRobertaEmbeddings.padding_idx`
382# avoid `padding_idx` in the past
383input_ids = tf.where(input_ids == 1, 2, input_ids)
384
385input_ids = input_ids[:1, :]
386input_mask = input_mask[:1, :]
387self.batch_size = 1
388
389# first forward pass
390outputs = model(input_ids, attention_mask=input_mask, use_cache=True)
391past_key_values = outputs.past_key_values
392
393# create hypothetical next token and extent to next_input_ids
394next_tokens = ids_tensor((self.batch_size, 3), config.vocab_size)
395next_attn_mask = ids_tensor((self.batch_size, 3), 2)
396
397# append to next input_ids and
398next_input_ids = tf.concat([input_ids, next_tokens], axis=-1)
399next_attention_mask = tf.concat([input_mask, next_attn_mask], axis=-1)
400
401output_from_no_past = model(
402next_input_ids,
403attention_mask=next_attention_mask,
404output_hidden_states=True,
405).hidden_states[0]
406output_from_past = model(
407next_tokens,
408attention_mask=next_attention_mask,
409past_key_values=past_key_values,
410output_hidden_states=True,
411).hidden_states[0]
412
413self.parent.assertEqual(next_tokens.shape[1], output_from_past.shape[1])
414
415# select random slice
416random_slice_idx = int(ids_tensor((1,), output_from_past.shape[-1]))
417output_from_no_past_slice = output_from_no_past[:, -3:, random_slice_idx]
418output_from_past_slice = output_from_past[:, :, random_slice_idx]
419
420# test that outputs are equal for slice
421tf.debugging.assert_near(output_from_past_slice, output_from_no_past_slice, rtol=1e-3)
422
423def create_and_check_decoder_model_past_large_inputs(
424self,
425config,
426input_ids,
427token_type_ids,
428input_mask,
429sequence_labels,
430token_labels,
431choice_labels,
432encoder_hidden_states,
433encoder_attention_mask,
434):
435config.add_cross_attention = True
436
437model = TFRobertaForCausalLM(config=config)
438
439# special to `RobertaEmbeddings` in `Roberta`:
440# - its `padding_idx` and its effect on `position_ids`
441# (TFRobertaEmbeddings.create_position_ids_from_input_ids)
442# - `1` here is `TFRobertaEmbeddings.padding_idx`
443# avoid `padding_idx` in the past
444input_ids = tf.where(input_ids == 1, 2, input_ids)
445
446input_ids = input_ids[:1, :]
447input_mask = input_mask[:1, :]
448encoder_hidden_states = encoder_hidden_states[:1, :, :]
449encoder_attention_mask = encoder_attention_mask[:1, :]
450self.batch_size = 1
451
452# first forward pass
453outputs = model(
454input_ids,
455attention_mask=input_mask,
456encoder_hidden_states=encoder_hidden_states,
457encoder_attention_mask=encoder_attention_mask,
458use_cache=True,
459)
460past_key_values = outputs.past_key_values
461
462# create hypothetical next token and extent to next_input_ids
463next_tokens = ids_tensor((self.batch_size, 3), config.vocab_size)
464next_attn_mask = ids_tensor((self.batch_size, 3), 2)
465
466# append to next input_ids and
467next_input_ids = tf.concat([input_ids, next_tokens], axis=-1)
468next_attention_mask = tf.concat([input_mask, next_attn_mask], axis=-1)
469
470output_from_no_past = model(
471next_input_ids,
472attention_mask=next_attention_mask,
473encoder_hidden_states=encoder_hidden_states,
474encoder_attention_mask=encoder_attention_mask,
475output_hidden_states=True,
476).hidden_states[0]
477output_from_past = model(
478next_tokens,
479attention_mask=next_attention_mask,
480encoder_hidden_states=encoder_hidden_states,
481encoder_attention_mask=encoder_attention_mask,
482past_key_values=past_key_values,
483output_hidden_states=True,
484).hidden_states[0]
485
486self.parent.assertEqual(next_tokens.shape[1], output_from_past.shape[1])
487
488# select random slice
489random_slice_idx = int(ids_tensor((1,), output_from_past.shape[-1]))
490output_from_no_past_slice = output_from_no_past[:, -3:, random_slice_idx]
491output_from_past_slice = output_from_past[:, :, random_slice_idx]
492
493# test that outputs are equal for slice
494tf.debugging.assert_near(output_from_past_slice, output_from_no_past_slice, rtol=1e-3)
495
496def create_and_check_for_masked_lm(
497self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
498):
499model = TFRobertaForMaskedLM(config=config)
500result = model([input_ids, input_mask, token_type_ids])
501self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size))
502
503def create_and_check_for_token_classification(
504self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
505):
506config.num_labels = self.num_labels
507model = TFRobertaForTokenClassification(config=config)
508inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
509result = model(inputs)
510self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels))
511
512def create_and_check_for_question_answering(
513self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
514):
515model = TFRobertaForQuestionAnswering(config=config)
516inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
517result = model(inputs)
518self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length))
519self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length))
520
521def create_and_check_for_multiple_choice(
522self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
523):
524config.num_choices = self.num_choices
525model = TFRobertaForMultipleChoice(config=config)
526multiple_choice_inputs_ids = tf.tile(tf.expand_dims(input_ids, 1), (1, self.num_choices, 1))
527multiple_choice_input_mask = tf.tile(tf.expand_dims(input_mask, 1), (1, self.num_choices, 1))
528multiple_choice_token_type_ids = tf.tile(tf.expand_dims(token_type_ids, 1), (1, self.num_choices, 1))
529inputs = {
530"input_ids": multiple_choice_inputs_ids,
531"attention_mask": multiple_choice_input_mask,
532"token_type_ids": multiple_choice_token_type_ids,
533}
534result = model(inputs)
535self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_choices))
536
537def prepare_config_and_inputs_for_common(self):
538config_and_inputs = self.prepare_config_and_inputs()
539(
540config,
541input_ids,
542token_type_ids,
543input_mask,
544sequence_labels,
545token_labels,
546choice_labels,
547) = config_and_inputs
548inputs_dict = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
549return config, inputs_dict
550
551
552@require_tf
553class TFRobertaModelTest(TFModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
554all_model_classes = (
555(
556TFRobertaModel,
557TFRobertaForCausalLM,
558TFRobertaForMaskedLM,
559TFRobertaForSequenceClassification,
560TFRobertaForTokenClassification,
561TFRobertaForQuestionAnswering,
562)
563if is_tf_available()
564else ()
565)
566pipeline_model_mapping = (
567{
568"feature-extraction": TFRobertaModel,
569"fill-mask": TFRobertaForMaskedLM,
570"question-answering": TFRobertaForQuestionAnswering,
571"text-classification": TFRobertaForSequenceClassification,
572"text-generation": TFRobertaForCausalLM,
573"token-classification": TFRobertaForTokenClassification,
574"zero-shot": TFRobertaForSequenceClassification,
575}
576if is_tf_available()
577else {}
578)
579test_head_masking = False
580test_onnx = False
581
582def setUp(self):
583self.model_tester = TFRobertaModelTester(self)
584self.config_tester = ConfigTester(self, config_class=RobertaConfig, hidden_size=37)
585
586def test_config(self):
587self.config_tester.run_common_tests()
588
589def test_model(self):
590"""Test the base model"""
591config_and_inputs = self.model_tester.prepare_config_and_inputs()
592self.model_tester.create_and_check_model(*config_and_inputs)
593
594def test_causal_lm_base_model(self):
595"""Test the base model of the causal LM model
596
597is_deocder=True, no cross_attention, no encoder outputs
598"""
599config_and_inputs = self.model_tester.prepare_config_and_inputs()
600self.model_tester.create_and_check_causal_lm_base_model(*config_and_inputs)
601
602def test_model_as_decoder(self):
603"""Test the base model as a decoder (of an encoder-decoder architecture)
604
605is_deocder=True + cross_attention + pass encoder outputs
606"""
607config_and_inputs = self.model_tester.prepare_config_and_inputs_for_decoder()
608self.model_tester.create_and_check_model_as_decoder(*config_and_inputs)
609
610def test_for_masked_lm(self):
611config_and_inputs = self.model_tester.prepare_config_and_inputs()
612self.model_tester.create_and_check_for_masked_lm(*config_and_inputs)
613
614def test_for_causal_lm(self):
615"""Test the causal LM model"""
616config_and_inputs = self.model_tester.prepare_config_and_inputs()
617self.model_tester.create_and_check_causal_lm_model(*config_and_inputs)
618
619def test_causal_lm_model_as_decoder(self):
620"""Test the causal LM model as a decoder"""
621config_and_inputs = self.model_tester.prepare_config_and_inputs_for_decoder()
622self.model_tester.create_and_check_causal_lm_model_as_decoder(*config_and_inputs)
623
624def test_causal_lm_model_past(self):
625"""Test causal LM model with `past_key_values`"""
626config_and_inputs = self.model_tester.prepare_config_and_inputs()
627self.model_tester.create_and_check_causal_lm_model_past(*config_and_inputs)
628
629def test_causal_lm_model_past_with_attn_mask(self):
630"""Test the causal LM model with `past_key_values` and `attention_mask`"""
631config_and_inputs = self.model_tester.prepare_config_and_inputs()
632self.model_tester.create_and_check_causal_lm_model_past_with_attn_mask(*config_and_inputs)
633
634def test_causal_lm_model_past_with_large_inputs(self):
635"""Test the causal LM model with `past_key_values` and a longer decoder sequence length"""
636config_and_inputs = self.model_tester.prepare_config_and_inputs()
637self.model_tester.create_and_check_causal_lm_model_past_large_inputs(*config_and_inputs)
638
639def test_decoder_model_past_with_large_inputs(self):
640"""Similar to `test_causal_lm_model_past_with_large_inputs` but with cross-attention"""
641config_and_inputs = self.model_tester.prepare_config_and_inputs_for_decoder()
642self.model_tester.create_and_check_decoder_model_past_large_inputs(*config_and_inputs)
643
644def test_for_token_classification(self):
645config_and_inputs = self.model_tester.prepare_config_and_inputs()
646self.model_tester.create_and_check_for_token_classification(*config_and_inputs)
647
648def test_for_question_answering(self):
649config_and_inputs = self.model_tester.prepare_config_and_inputs()
650self.model_tester.create_and_check_for_question_answering(*config_and_inputs)
651
652def test_for_multiple_choice(self):
653config_and_inputs = self.model_tester.prepare_config_and_inputs()
654self.model_tester.create_and_check_for_multiple_choice(*config_and_inputs)
655
656@slow
657def test_model_from_pretrained(self):
658for model_name in TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
659model = TFRobertaModel.from_pretrained(model_name)
660self.assertIsNotNone(model)
661
662
663@require_tf
664@require_sentencepiece
665@require_tokenizers
666class TFRobertaModelIntegrationTest(unittest.TestCase):
667@slow
668def test_inference_masked_lm(self):
669model = TFRobertaForMaskedLM.from_pretrained("FacebookAI/roberta-base")
670
671input_ids = tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]])
672output = model(input_ids)[0]
673expected_shape = [1, 11, 50265]
674self.assertEqual(list(output.numpy().shape), expected_shape)
675# compare the actual values for a slice.
676expected_slice = tf.constant(
677[[[33.8802, -4.3103, 22.7761], [4.6539, -2.8098, 13.6253], [1.8228, -3.6898, 8.8600]]]
678)
679self.assertTrue(numpy.allclose(output[:, :3, :3].numpy(), expected_slice.numpy(), atol=1e-4))
680
681@slow
682def test_inference_no_head(self):
683model = TFRobertaModel.from_pretrained("FacebookAI/roberta-base")
684
685input_ids = tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]])
686output = model(input_ids)[0]
687# compare the actual values for a slice.
688expected_slice = tf.constant(
689[[[-0.0231, 0.0782, 0.0074], [-0.1854, 0.0540, -0.0175], [0.0548, 0.0799, 0.1687]]]
690)
691self.assertTrue(numpy.allclose(output[:, :3, :3].numpy(), expected_slice.numpy(), atol=1e-4))
692
693@slow
694def test_inference_classification_head(self):
695model = TFRobertaForSequenceClassification.from_pretrained("FacebookAI/roberta-large-mnli")
696
697input_ids = tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]])
698output = model(input_ids)[0]
699expected_shape = [1, 3]
700self.assertEqual(list(output.numpy().shape), expected_shape)
701expected_tensor = tf.constant([[-0.9469, 0.3913, 0.5118]])
702self.assertTrue(numpy.allclose(output.numpy(), expected_tensor.numpy(), atol=1e-4))
703