"""simple docstring"""







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 	UpperCamelCase    :



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



				def       _lowercase  (self       :   Optional[Any])					->			Any:
					__snake_case    :							Optional[int]   	=				ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size)
					__snake_case    :							Any   	=				random_attention_mask([self.batch_size, self.seq_length])

					__snake_case    :							Optional[int]   	=				None
					if self.use_input_lengths:
						__snake_case    :							Optional[Any]   	=				(
						    ids_tensor([self.batch_size]			,						vocab_size=2) + self.seq_length - 2
						)  # small variation of seq_length

					__snake_case    :							List[Any]   	=				None
					if self.use_token_type_ids:
						__snake_case    :							Tuple   	=				ids_tensor([self.batch_size, self.seq_length]			,						self.n_langs)

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

					__snake_case    :							List[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       _lowercase  (self       :   int)					->			List[Any]:
					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       _lowercase  (self       :   Optional[Any]			,						_A       :   List[str]			,						_A       :   List[Any]			,						_A       :   List[str]			,						_A       :   Union[str, Any]			,						_A       :   Tuple			,						_A       :   Tuple			,						_A       :   Optional[Any]			,						_A       :   Optional[int]			,						_A       :   str			,						)					->			Union[str, Any]:
					__snake_case    :							str   	=				XLMModel(config=_A)
					model.to(_A)
					model.eval()
					__snake_case    :							List[Any]   	=				model(_A			,						lengths=_A			,						langs=_A)
					__snake_case    :							Dict   	=				model(_A			,						langs=_A)
					__snake_case    :							Optional[int]   	=				model(_A)
					self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size))



				def       _lowercase  (self       :   Optional[Any]			,						_A       :   List[str]			,						_A       :   Optional[Any]			,						_A       :   int			,						_A       :   List[Any]			,						_A       :   str			,						_A       :   Optional[int]			,						_A       :   Optional[Any]			,						_A       :   str			,						_A       :   Dict			,						)					->			Optional[int]:
					__snake_case    :							Tuple   	=				XLMWithLMHeadModel(_A)
					model.to(_A)
					model.eval()

					__snake_case    :							Tuple   	=				model(_A			,						token_type_ids=_A			,						labels=_A)
					self.parent.assertEqual(result.loss.shape			,						())
					self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.vocab_size))



				def       _lowercase  (self       :   Dict			,						_A       :   Any			,						_A       :   Tuple			,						_A       :   str			,						_A       :   Optional[int]			,						_A       :   List[Any]			,						_A       :   Tuple			,						_A       :   Dict			,						_A       :   Tuple			,						_A       :   Tuple			,						)					->			Dict:
					__snake_case    :							List[Any]   	=				XLMForQuestionAnsweringSimple(_A)
					model.to(_A)
					model.eval()

					__snake_case    :							List[Any]   	=				model(_A)

					__snake_case    :							Dict   	=				model(_A			,						start_positions=_A			,						end_positions=_A)
					__snake_case    :							str   	=				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       _lowercase  (self       :   Any			,						_A       :   Tuple			,						_A       :   Tuple			,						_A       :   int			,						_A       :   Optional[Any]			,						_A       :   Union[str, Any]			,						_A       :   Union[str, Any]			,						_A       :   Optional[Any]			,						_A       :   Optional[int]			,						_A       :   Optional[int]			,						)					->			str:
					__snake_case    :							Tuple   	=				XLMForQuestionAnswering(_A)
					model.to(_A)
					model.eval()

					__snake_case    :							Optional[int]   	=				model(_A)

					__snake_case    :							Optional[Any]   	=				model(
					    _A			,						start_positions=_A			,						end_positions=_A			,						cls_index=_A			,						is_impossible=_A			,						p_mask=_A			,						)

					__snake_case    :							List[str]   	=				model(
					    _A			,						start_positions=_A			,						end_positions=_A			,						cls_index=_A			,						is_impossible=_A			,						)

					((__snake_case)	,       )    :							List[str]   	=				result_with_labels.to_tuple()

					__snake_case    :							Optional[Any]   	=				model(_A			,						start_positions=_A			,						end_positions=_A)

					((__snake_case)	,       )    :							List[Any]   	=				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       _lowercase  (self       :   Tuple			,						_A       :   List[str]			,						_A       :   str			,						_A       :   Dict			,						_A       :   Optional[int]			,						_A       :   List[str]			,						_A       :   int			,						_A       :   Optional[Any]			,						_A       :   Any			,						_A       :   Optional[int]			,						)					->			Optional[Any]:
					__snake_case    :							int   	=				XLMForSequenceClassification(_A)
					model.to(_A)
					model.eval()

					__snake_case    :							Any   	=				model(_A)
					__snake_case    :							Any   	=				model(_A			,						labels=_A)
					self.parent.assertEqual(result.loss.shape			,						())
					self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size))



				def       _lowercase  (self       :   List[str]			,						_A       :   Optional[Any]			,						_A       :   int			,						_A       :   Dict			,						_A       :   Optional[Any]			,						_A       :   Tuple			,						_A       :   Optional[int]			,						_A       :   str			,						_A       :   Optional[Any]			,						_A       :   Any			,						)					->			Union[str, Any]:
					__snake_case    :							List[str]   	=				self.num_labels
					__snake_case    :							Any   	=				XLMForTokenClassification(_A)
					model.to(_A)
					model.eval()

					__snake_case    :							List[str]   	=				model(_A			,						attention_mask=_A			,						labels=_A)
					self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.num_labels))



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






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







@require_torch
class 	UpperCamelCase    (					lowercase   ,			lowercase   ,			lowercase   ,			unittest.TestCase		):
				UpperCAmelCase       :   Any					=	(
				    (
				        XLMModel,
				        XLMWithLMHeadModel,
				        XLMForQuestionAnswering,
				        XLMForSequenceClassification,
				        XLMForQuestionAnsweringSimple,
				        XLMForTokenClassification,
				        XLMForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				UpperCAmelCase       :   Optional[int]					=	(
				    (XLMWithLMHeadModel,) if is_torch_available() else ()
				)  # TODO (PVP): Check other models whether language generation is also applicable
				UpperCAmelCase       :   str					=	(
				    {
				        """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       _lowercase  (self       :   Union[str, Any]			,						_A       :   Optional[int]			,						_A       :   Dict			,						_A       :   List[Any]			,						_A       :   int			,						_A       :   Optional[int])					->			int:
					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       _lowercase  (self       :   List[Any]			,						_A       :   int			,						_A       :   Union[str, Any]			,						_A       :   List[str]=False)					->			Union[str, Any]:
					__snake_case    :							Any   	=				super()._prepare_for_class(_A			,						_A			,						return_labels=_A)

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

					return inputs_dict



				def       _lowercase  (self       :   Optional[int])					->			Tuple:
					__snake_case    :							List[Any]   	=				XLMModelTester(self)
					__snake_case    :							str   	=				ConfigTester(self			,						config_class=_A			,						emb_dim=37)



				def       _lowercase  (self       :   List[str])					->			Optional[int]:
					self.config_tester.run_common_tests()



				def       _lowercase  (self       :   Optional[int])					->			List[str]:
					__snake_case    :							Tuple   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_xlm_model(*_A)



				def       _lowercase  (self       :   str)					->			int:
					__snake_case    :							Optional[int]   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_xlm_lm_head(*_A)



				def       _lowercase  (self       :   Union[str, Any])					->			int:
					__snake_case    :							str   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_xlm_simple_qa(*_A)



				def       _lowercase  (self       :   Any)					->			Tuple:
					__snake_case    :							Optional[Any]   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_xlm_qa(*_A)



				def       _lowercase  (self       :   Union[str, Any])					->			List[Any]:
					__snake_case    :							int   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_xlm_sequence_classif(*_A)



				def       _lowercase  (self       :   Union[str, Any])					->			List[Any]:
					__snake_case    :							Union[str, Any]   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_xlm_token_classif(*_A)



				def       _lowercase  (self       :   Union[str, Any])					->			str:
					__snake_case    :							Any   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_xlm_for_multiple_choice(*_A)



				def       _lowercase  (self       :   Dict			,						_A       :   List[str]			,						_A       :   Tuple			,						_A       :   Union[str, Any]			,						_A       :   Tuple			,						_A       :   Optional[Any]			,						_A       :   List[str]=False			,						_A       :   List[str]=1)					->			str:
					self.assertIsInstance(_A			,						_A)
					self.assertListEqual(
					    [isinstance(_A			,						_A) for iter_attentions in attentions]			,						[True] * len(_A))
					self.assertEqual(len(_A)			,						(max_length - min_length) * num_beam_groups)

					for idx, iter_attentions in enumerate(_A):
						# adds PAD dummy token
						__snake_case    :							Tuple   	=				min_length + idx + 1
						__snake_case    :							int   	=				min_length + idx + 1

						__snake_case    :							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(_A))



				def       _lowercase  (self       :   Dict			,						_A       :   Dict			,						_A       :   Union[str, Any]			,						_A       :   Tuple			,						_A       :   Union[str, Any]			,						_A       :   int			,						_A       :   Dict=False			,						_A       :   Any=1)					->			Union[str, Any]:
					self.assertIsInstance(_A			,						_A)
					self.assertListEqual(
					    [isinstance(_A			,						_A) for iter_hidden_states in hidden_states]			,						[True] * len(_A)			,						)
					self.assertEqual(len(_A)			,						(max_length - min_length) * num_beam_groups)

					for idx, iter_hidden_states in enumerate(_A):
						# adds PAD dummy token
						__snake_case    :							Optional[int]   	=				min_length + idx + 1
						__snake_case    :							int   	=				(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(_A)			,						)
					pass






				@slow
				def       _lowercase  (self       :   Union[str, Any])					->			List[Any]:
					for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case    :							Tuple   	=				XLMModel.from_pretrained(_A)
						self.assertIsNotNone(_A)







@require_torch
class 	UpperCamelCase    (					unittest.TestCase		):



				@slow
				def       _lowercase  (self       :   str)					->			Tuple:
					__snake_case    :							Any   	=				XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048')
					model.to(_A)
					__snake_case    :							str   	=				torch.tensor([[14, 4_47]]			,						dtype=torch.long			,						device=_A)  # the president
					__snake_case    :							Optional[int]   	=				[
					    14,
					    4_47,
					    14,
					    4_47,
					    14,
					    4_47,
					    14,
					    4_47,
					    14,
					    4_47,
					    14,
					    4_47,
					    14,
					    4_47,
					    14,
					    4_47,
					    14,
					    4_47,
					    14,
					    4_47,
					]  # 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
					__snake_case    :							Optional[Any]   	=				model.generate(_A			,						do_sample=_A)
					self.assertListEqual(output_ids[0].cpu().numpy().tolist()			,						_A)






"""simple docstring"""







from abc import ABC, abstractmethod
from argparse import ArgumentParser

class 	UpperCamelCase    (					lowercase		):



				@staticmethod
				@abstractmethod
				def       _lowercase  (_A       :   ArgumentParser)					->			Tuple:
					raise NotImplementedError()






				@abstractmethod
				def       _lowercase  (self       :   Any)					->			Optional[Any]:
					raise NotImplementedError()





