





import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        XLMForMultipleChoice,
        XLMForQuestionAnswering,
        XLMForQuestionAnsweringSimple,
        XLMForSequenceClassification,
        XLMForTokenClassification,
        XLMModel,
        XLMWithLMHeadModel,
    )
    from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST

class   lowerCAmelCase				:
 def __init__( self    : int     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : str=13     ,	UpperCAmelCase    : Tuple=7     ,	UpperCAmelCase    : Optional[Any]=True     ,	UpperCAmelCase    : int=True     ,	UpperCAmelCase    : Any=True     ,	UpperCAmelCase    : Any=True     ,	UpperCAmelCase    : Optional[Any]=True     ,	UpperCAmelCase    : Any=False     ,	UpperCAmelCase    : Any=False     ,	UpperCAmelCase    : List[Any]=False     ,	UpperCAmelCase    : Tuple=2     ,	UpperCAmelCase    : List[Any]=99     ,	UpperCAmelCase    : Union[str, Any]=0     ,	UpperCAmelCase    : Union[str, Any]=32     ,	UpperCAmelCase    : Union[str, Any]=5     ,	UpperCAmelCase    : Tuple=4     ,	UpperCAmelCase    : List[Any]=0.1     ,	UpperCAmelCase    : Union[str, Any]=0.1     ,	UpperCAmelCase    : List[str]=512     ,	UpperCAmelCase    : Union[str, Any]=2     ,	UpperCAmelCase    : Union[str, Any]=0.0_2     ,	UpperCAmelCase    : List[Any]=2     ,	UpperCAmelCase    : int=4     ,	UpperCAmelCase    : Tuple="last"     ,	UpperCAmelCase    : Union[str, Any]=True     ,	UpperCAmelCase    : Optional[int]=None     ,	UpperCAmelCase    : Optional[Any]=0     ,	)     ->      Union[str, Any]:
     lowerCamelCase__       : Optional[Any]   					=						parent
     lowerCamelCase__       : Any   					=						batch_size
     lowerCamelCase__       : Tuple   					=						seq_length
     lowerCamelCase__       : List[Any]   					=						is_training
     lowerCamelCase__       : Optional[int]   					=						use_input_lengths
     lowerCamelCase__       : Optional[int]   					=						use_token_type_ids
     lowerCamelCase__       : List[str]   					=						use_labels
     lowerCamelCase__       : Tuple   					=						gelu_activation
     lowerCamelCase__       : Union[str, Any]   					=						sinusoidal_embeddings
     lowerCamelCase__       : List[str]   					=						causal
     lowerCamelCase__       : Optional[Any]   					=						asm
     lowerCamelCase__       : Dict   					=						n_langs
     lowerCamelCase__       : List[Any]   					=						vocab_size
     lowerCamelCase__       : List[Any]   					=						n_special
     lowerCamelCase__       : List[Any]   					=						hidden_size
     lowerCamelCase__       : Union[str, Any]   					=						num_hidden_layers
     lowerCamelCase__       : Optional[int]   					=						num_attention_heads
     lowerCamelCase__       : Optional[int]   					=						hidden_dropout_prob
     lowerCamelCase__       : Dict   					=						attention_probs_dropout_prob
     lowerCamelCase__       : int   					=						max_position_embeddings
     lowerCamelCase__       : Optional[int]   					=						type_sequence_label_size
     lowerCamelCase__       : str   					=						initializer_range
     lowerCamelCase__       : Optional[Any]   					=						num_labels
     lowerCamelCase__       : str   					=						num_choices
     lowerCamelCase__       : Tuple   					=						summary_type
     lowerCamelCase__       : Union[str, Any]   					=						use_proj
     lowerCamelCase__       : str   					=						scope
     lowerCamelCase__       : Any   					=						bos_token_id
 def 	A_      ( self    : Union[str, Any]      )     ->      Optional[int]:
     lowerCamelCase__       : Tuple   					=						ids_tensor([self.batch_size, self.seq_length]     ,	self.vocab_size      )
     lowerCamelCase__       : List[Any]   					=						random_attention_mask([self.batch_size, self.seq_length]      )

     lowerCamelCase__       : Tuple   					=						None
     if self.use_input_lengths:
         lowerCamelCase__       : str   					=						(
             ids_tensor([self.batch_size]     ,	vocab_size=2      ) + self.seq_length - 2
         )  # small variation of seq_length

     lowerCamelCase__       : List[str]   					=						None
     if self.use_token_type_ids:
         lowerCamelCase__       : str   					=						ids_tensor([self.batch_size, self.seq_length]     ,	self.n_langs      )

     lowerCamelCase__       : str   					=						None
     lowerCamelCase__       : List[Any]   					=						None
     lowerCamelCase__       : int   					=						None
     if self.use_labels:
         lowerCamelCase__       : List[str]   					=						ids_tensor([self.batch_size]     ,	self.type_sequence_label_size      )
         lowerCamelCase__       : List[str]   					=						ids_tensor([self.batch_size, self.seq_length]     ,	self.num_labels      )
         lowerCamelCase__       : Tuple   					=						ids_tensor([self.batch_size]     ,	2      ).float()
         lowerCamelCase__       : Optional[Any]   					=						ids_tensor([self.batch_size]     ,	self.num_choices      )

     lowerCamelCase__       : Optional[Any]   					=						self.get_config()

     return (
         config,
         input_ids,
         token_type_ids,
         input_lengths,
         sequence_labels,
         token_labels,
         is_impossible_labels,
         choice_labels,
         input_mask,
     )
 def 	A_      ( self    : int      )     ->      str:
     return XLMConfig(
         vocab_size=self.vocab_size     ,	n_special=self.n_special     ,	emb_dim=self.hidden_size     ,	n_layers=self.num_hidden_layers     ,	n_heads=self.num_attention_heads     ,	dropout=self.hidden_dropout_prob     ,	attention_dropout=self.attention_probs_dropout_prob     ,	gelu_activation=self.gelu_activation     ,	sinusoidal_embeddings=self.sinusoidal_embeddings     ,	asm=self.asm     ,	causal=self.causal     ,	n_langs=self.n_langs     ,	max_position_embeddings=self.max_position_embeddings     ,	initializer_range=self.initializer_range     ,	summary_type=self.summary_type     ,	use_proj=self.use_proj     ,	num_labels=self.num_labels     ,	bos_token_id=self.bos_token_id     ,	)
 def 	A_      ( self    : str     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : List[Any]     ,	)     ->      List[Any]:
     lowerCamelCase__       : int   					=						XLMModel(config=UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : Any   					=						model(UpperCAmelCase     ,	lengths=UpperCAmelCase     ,	langs=UpperCAmelCase      )
     lowerCamelCase__       : Dict   					=						model(UpperCAmelCase     ,	langs=UpperCAmelCase      )
     lowerCamelCase__       : int   					=						model(UpperCAmelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape     ,	(self.batch_size, self.seq_length, self.hidden_size)      )
 def 	A_      ( self    : Tuple     ,	UpperCAmelCase    : List[Any]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : str     ,	)     ->      int:
     lowerCamelCase__       : Optional[int]   					=						XLMWithLMHeadModel(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()

     lowerCamelCase__       : Optional[Any]   					=						model(UpperCAmelCase     ,	token_type_ids=UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.parent.assertEqual(result.loss.shape     ,	()      )
     self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.seq_length, self.vocab_size)      )
 def 	A_      ( self    : List[Any]     ,	UpperCAmelCase    : List[Any]     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : int     ,	)     ->      int:
     lowerCamelCase__       : Optional[int]   					=						XLMForQuestionAnsweringSimple(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()

     lowerCamelCase__       : Dict   					=						model(UpperCAmelCase      )

     lowerCamelCase__       : int   					=						model(UpperCAmelCase     ,	start_positions=UpperCAmelCase     ,	end_positions=UpperCAmelCase      )
     lowerCamelCase__       : Any   					=						outputs
     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)      )
 def 	A_      ( self    : Dict     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : List[Any]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Tuple     ,	)     ->      int:
     lowerCamelCase__       : Optional[Any]   					=						XLMForQuestionAnswering(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()

     lowerCamelCase__       : Optional[Any]   					=						model(UpperCAmelCase      )

     lowerCamelCase__       : Optional[Any]   					=						model(
         UpperCAmelCase     ,	start_positions=UpperCAmelCase     ,	end_positions=UpperCAmelCase     ,	cls_index=UpperCAmelCase     ,	is_impossible=UpperCAmelCase     ,	p_mask=UpperCAmelCase     ,	)

     lowerCamelCase__       : Optional[int]   					=						model(
         UpperCAmelCase     ,	start_positions=UpperCAmelCase     ,	end_positions=UpperCAmelCase     ,	cls_index=UpperCAmelCase     ,	is_impossible=UpperCAmelCase     ,	)

     ((lowerCamelCase__)					,				)       : List[Any]   					=						result_with_labels.to_tuple()

     lowerCamelCase__       : int   					=						model(UpperCAmelCase     ,	start_positions=UpperCAmelCase     ,	end_positions=UpperCAmelCase      )

     ((lowerCamelCase__)					,				)       : Dict   					=						result_with_labels.to_tuple()

     self.parent.assertEqual(result_with_labels.loss.shape     ,	()      )
     self.parent.assertEqual(result.start_top_log_probs.shape     ,	(self.batch_size, model.config.start_n_top)      )
     self.parent.assertEqual(result.start_top_index.shape     ,	(self.batch_size, model.config.start_n_top)      )
     self.parent.assertEqual(
         result.end_top_log_probs.shape     ,	(self.batch_size, model.config.start_n_top * model.config.end_n_top)      )
     self.parent.assertEqual(
         result.end_top_index.shape     ,	(self.batch_size, model.config.start_n_top * model.config.end_n_top)      )
     self.parent.assertEqual(result.cls_logits.shape     ,	(self.batch_size,)      )
 def 	A_      ( self    : List[str]     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Optional[int]     ,	)     ->      List[str]:
     lowerCamelCase__       : Union[str, Any]   					=						XLMForSequenceClassification(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()

     lowerCamelCase__       : Optional[int]   					=						model(UpperCAmelCase      )
     lowerCamelCase__       : List[str]   					=						model(UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.parent.assertEqual(result.loss.shape     ,	()      )
     self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.type_sequence_label_size)      )
 def 	A_      ( self    : List[Any]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : str     ,	)     ->      Dict:
     lowerCamelCase__       : int   					=						self.num_labels
     lowerCamelCase__       : Tuple   					=						XLMForTokenClassification(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()

     lowerCamelCase__       : str   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.seq_length, self.num_labels)      )
 def 	A_      ( self    : Optional[int]     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Optional[int]     ,	)     ->      Tuple:
     lowerCamelCase__       : Optional[int]   					=						self.num_choices
     lowerCamelCase__       : Tuple   					=						XLMForMultipleChoice(config=UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : str   					=						input_ids.unsqueeze(1      ).expand(-1     ,	self.num_choices     ,	-1      ).contiguous()
     lowerCamelCase__       : List[Any]   					=						token_type_ids.unsqueeze(1      ).expand(-1     ,	self.num_choices     ,	-1      ).contiguous()
     lowerCamelCase__       : Optional[Any]   					=						input_mask.unsqueeze(1      ).expand(-1     ,	self.num_choices     ,	-1      ).contiguous()
     lowerCamelCase__       : Union[str, Any]   					=						model(
         UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	token_type_ids=UpperCAmelCase     ,	labels=UpperCAmelCase     ,	)
     self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.num_choices)      )







 def 	A_      ( self    : Any      )     ->      Optional[Any]:
     lowerCamelCase__       : List[str]   					=						self.prepare_config_and_inputs()
     (
         (
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				
     )       : Tuple   					=						config_and_inputs
     lowerCamelCase__       : str   					=						{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths}
     return config, inputs_dict


@require_torch
class   lowerCAmelCase				(   __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase,    unittest.TestCase     ):
 UpperCAmelCase__						=						(
     (
         XLMModel,
         XLMWithLMHeadModel,
         XLMForQuestionAnswering,
         XLMForSequenceClassification,
         XLMForQuestionAnsweringSimple,
         XLMForTokenClassification,
         XLMForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase__						=						(
     (XLMWithLMHeadModel,) if is_torch_available() else ()
 )  # TODO (PVP): Check other models whether language generation is also applicable
 UpperCAmelCase__						=						(
     {
         """feature-extraction""": XLMModel,
         """fill-mask""": XLMWithLMHeadModel,
         """question-answering""": XLMForQuestionAnsweringSimple,
         """text-classification""": XLMForSequenceClassification,
         """text-generation""": XLMWithLMHeadModel,
         """token-classification""": XLMForTokenClassification,
         """zero-shot""": XLMForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 def 	A_      ( self    : Dict     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Union[str, Any]      )     ->      List[str]:
     if (
         pipeline_test_casse_name == "QAPipelineTests"
         and tokenizer_name is not None
         and not tokenizer_name.endswith('Fast'      )
     ):
         # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
         # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
         # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
         return True

     return False
 def 	A_      ( self    : int     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Any=False      )     ->      List[str]:
     lowerCamelCase__       : List[Any]   					=						super()._prepare_for_class(UpperCAmelCase     ,	UpperCAmelCase     ,	return_labels=UpperCAmelCase      )

     if return_labels:
         if model_class.__name__ == "XLMForQuestionAnswering":
             lowerCamelCase__       : Optional[int]   					=						torch.zeros(
                 self.model_tester.batch_size     ,	dtype=torch.long     ,	device=UpperCAmelCase      )
             lowerCamelCase__       : List[str]   					=						torch.zeros(
                 self.model_tester.batch_size     ,	dtype=torch.long     ,	device=UpperCAmelCase      )

     return inputs_dict
 def 	A_      ( self    : Dict      )     ->      Dict:
     lowerCamelCase__       : Any   					=						XLMModelTester(self      )
     lowerCamelCase__       : List[Any]   					=						ConfigTester(self     ,	config_class=UpperCAmelCase     ,	emb_dim=37      )
 def 	A_      ( self    : List[str]      )     ->      Dict:
     self.config_tester.run_common_tests()
 def 	A_      ( self    : str      )     ->      Any:
     lowerCamelCase__       : Union[str, Any]   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_model(*UpperCAmelCase      )
 def 	A_      ( self    : Optional[int]      )     ->      List[Any]:
     lowerCamelCase__       : str   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_lm_head(*UpperCAmelCase      )
 def 	A_      ( self    : Union[str, Any]      )     ->      Any:
     lowerCamelCase__       : int   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_simple_qa(*UpperCAmelCase      )
 def 	A_      ( self    : Union[str, Any]      )     ->      Union[str, Any]:
     lowerCamelCase__       : List[str]   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_qa(*UpperCAmelCase      )
 def 	A_      ( self    : Any      )     ->      int:
     lowerCamelCase__       : List[Any]   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_sequence_classif(*UpperCAmelCase      )
 def 	A_      ( self    : Optional[Any]      )     ->      Optional[Any]:
     lowerCamelCase__       : str   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_token_classif(*UpperCAmelCase      )
 def 	A_      ( self    : int      )     ->      Union[str, Any]:
     lowerCamelCase__       : Optional[Any]   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCAmelCase      )
 def 	A_      ( self    : Optional[Any]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Union[str, Any]=False     ,	UpperCAmelCase    : Union[str, Any]=1      )     ->      List[Any]:
     self.assertIsInstance(UpperCAmelCase     ,	UpperCAmelCase      )
     self.assertListEqual(
         [isinstance(UpperCAmelCase     ,	UpperCAmelCase      ) for iter_attentions in attentions]     ,	[True] * len(UpperCAmelCase      )      )
     self.assertEqual(len(UpperCAmelCase      )     ,	(max_length - min_length) * num_beam_groups      )

     for idx, iter_attentions in enumerate(UpperCAmelCase      ):
         # adds PAD dummy token
         lowerCamelCase__       : Union[str, Any]   					=						min_length + idx + 1
         lowerCamelCase__       : Optional[int]   					=						min_length + idx + 1

         lowerCamelCase__       : Optional[int]   					=						(
             batch_size * num_beam_groups,
             config.num_attention_heads,
             tgt_len,
             src_len,
         )
         # check attn size
         self.assertListEqual(
             [layer_attention.shape for layer_attention in iter_attentions]     ,	[expected_shape] * len(UpperCAmelCase      )      )
 def 	A_      ( self    : List[str]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : List[Any]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Union[str, Any]=False     ,	UpperCAmelCase    : Tuple=1      )     ->      int:
     self.assertIsInstance(UpperCAmelCase     ,	UpperCAmelCase      )
     self.assertListEqual(
         [isinstance(UpperCAmelCase     ,	UpperCAmelCase      ) for iter_hidden_states in hidden_states]     ,	[True] * len(UpperCAmelCase      )     ,	)
     self.assertEqual(len(UpperCAmelCase      )     ,	(max_length - min_length) * num_beam_groups      )

     for idx, iter_hidden_states in enumerate(UpperCAmelCase      ):
         # adds PAD dummy token
         lowerCamelCase__       : int   					=						min_length + idx + 1
         lowerCamelCase__       : Union[str, Any]   					=						(batch_size * num_beam_groups, seq_len, config.hidden_size)
         # check hidden size
         self.assertListEqual(
             [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states]     ,	[expected_shape] * len(UpperCAmelCase      )     ,	)
     pass







 @slow
 def 	A_      ( self    : Tuple      )     ->      Tuple:
     for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowerCamelCase__       : Optional[int]   					=						XLMModel.from_pretrained(UpperCAmelCase      )
         self.assertIsNotNone(UpperCAmelCase      )


@require_torch
class   lowerCAmelCase				(   unittest.TestCase     ):
 @slow
 def 	A_      ( self    : List[Any]      )     ->      int:
     lowerCamelCase__       : int   					=						XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048'      )
     model.to(UpperCAmelCase      )
     lowerCamelCase__       : Optional[Any]   					=						torch.tensor([[14, 447]]     ,	dtype=torch.long     ,	device=UpperCAmelCase      )  # the president
     lowerCamelCase__       : Tuple   					=						[
         14,
         447,
         14,
         447,
         14,
         447,
         14,
         447,
         14,
         447,
         14,
         447,
         14,
         447,
         14,
         447,
         14,
         447,
         14,
         447,
     ]  # the president the president the president the president the president the president the president the president the president the president
     # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
     lowerCamelCase__       : Union[str, Any]   					=						model.generate(UpperCAmelCase     ,	do_sample=UpperCAmelCase      )
     self.assertListEqual(output_ids[0].cpu().numpy().tolist()     ,	UpperCAmelCase      )












from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
    import tensorflow as tf

    from transformers import AutoTokenizer, TFAutoModelForSeqaSeqLM

@require_tf
@require_sentencepiece
@require_tokenizers
class   lowerCAmelCase				(   unittest.TestCase     ):
 @slow
 def 	A_      ( self    : int      )     ->      Optional[Any]:
     lowerCamelCase__       : Tuple   					=						TFAutoModelForSeqaSeqLM.from_pretrained('google/mt5-small'      )
     lowerCamelCase__       : Any   					=						AutoTokenizer.from_pretrained('google/mt5-small'      )

     lowerCamelCase__       : int   					=						tokenizer('Hello there'     ,	return_tensors='tf'      ).input_ids
     lowerCamelCase__       : int   					=						tokenizer('Hi I am'     ,	return_tensors='tf'      ).input_ids

     lowerCamelCase__       : List[str]   					=						model(UpperCAmelCase     ,	labels=UpperCAmelCase      ).loss
     lowerCamelCase__       : List[Any]   					=						-tf.math.reduce_mean(UpperCAmelCase      ).numpy()

     lowerCamelCase__       : Dict   					=						-2_1.2_2_8_1_6_8
     self.assertTrue(abs(mtf_score - EXPECTED_SCORE      ) < 2e-4      )





