






'''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  _snake_case :

					"""simple docstring"""





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




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Optional[Any]:
										a_						= ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size  )
										a_						= random_attention_mask([self.batch_size, self.seq_length]  )

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

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

										a_						= None
										a_						= None
										a_						= None
										if self.use_labels:
															a_						= ids_tensor([self.batch_size] ,			self.type_sequence_label_size  )
															a_						= ids_tensor([self.batch_size, self.seq_length] ,			self.num_labels  )
															a_						= ids_tensor([self.batch_size] ,			2  ).float()
															a_						= ids_tensor([self.batch_size] ,			self.num_choices  )

										a_						= self.get_config()

										return (
										    config,
										    input_ids,
										    token_type_ids,
										    input_lengths,
										    sequence_labels,
										    token_labels,
										    is_impossible_labels,
										    choice_labels,
										    input_mask,
										)




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						List[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 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			)   ->						Dict:
										a_						= XLMModel(config=UpperCAmelCase__  )
										model.to(UpperCAmelCase__  )
										model.eval()
										a_						= model(UpperCAmelCase__ ,			lengths=UpperCAmelCase__ ,			langs=UpperCAmelCase__  )
										a_						= model(UpperCAmelCase__ ,			langs=UpperCAmelCase__  )
										a_						= model(UpperCAmelCase__  )
										self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)  )




					def 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			)   ->						Any:
										a_						= XLMWithLMHeadModel(UpperCAmelCase__  )
										model.to(UpperCAmelCase__  )
										model.eval()

										a_						= 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 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			)   ->						Any:
										a_						= XLMForQuestionAnsweringSimple(UpperCAmelCase__  )
										model.to(UpperCAmelCase__  )
										model.eval()

										a_						= model(UpperCAmelCase__  )

										a_						= model(UpperCAmelCase__ ,			start_positions=UpperCAmelCase__ ,			end_positions=UpperCAmelCase__  )
										a_						= 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 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			)   ->						Optional[int]:
										a_						= XLMForQuestionAnswering(UpperCAmelCase__  )
										model.to(UpperCAmelCase__  )
										model.eval()

										a_						= model(UpperCAmelCase__  )

										a_						= model(
										    UpperCAmelCase__ ,			start_positions=UpperCAmelCase__ ,			end_positions=UpperCAmelCase__ ,			cls_index=UpperCAmelCase__ ,			is_impossible=UpperCAmelCase__ ,			p_mask=UpperCAmelCase__ ,			)

										a_						= model(
										    UpperCAmelCase__ ,			start_positions=UpperCAmelCase__ ,			end_positions=UpperCAmelCase__ ,			cls_index=UpperCAmelCase__ ,			is_impossible=UpperCAmelCase__ ,			)

										(a_			)						= result_with_labels.to_tuple()

										a_						= model(UpperCAmelCase__ ,			start_positions=UpperCAmelCase__ ,			end_positions=UpperCAmelCase__  )

										(a_			)						= 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 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			)   ->						Any:
										a_						= XLMForSequenceClassification(UpperCAmelCase__  )
										model.to(UpperCAmelCase__  )
										model.eval()

										a_						= model(UpperCAmelCase__  )
										a_						= 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 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			)   ->						Optional[Any]:
										a_						= self.num_labels
										a_						= XLMForTokenClassification(UpperCAmelCase__  )
										model.to(UpperCAmelCase__  )
										model.eval()

										a_						= model(UpperCAmelCase__ ,			attention_mask=UpperCAmelCase__ ,			labels=UpperCAmelCase__  )
										self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.num_labels)  )




					def 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			)   ->						Any:
										a_						= self.num_choices
										a_						= XLMForMultipleChoice(config=UpperCAmelCase__  )
										model.to(UpperCAmelCase__  )
										model.eval()
										a_						= input_ids.unsqueeze(1  ).expand(-1 ,			self.num_choices ,			-1  ).contiguous()
										a_						= token_type_ids.unsqueeze(1  ).expand(-1 ,			self.num_choices ,			-1  ).contiguous()
										a_						= input_mask.unsqueeze(1  ).expand(-1 ,			self.num_choices ,			-1  ).contiguous()
										a_						= model(
										    UpperCAmelCase__ ,			attention_mask=UpperCAmelCase__ ,			token_type_ids=UpperCAmelCase__ ,			labels=UpperCAmelCase__ ,			)
										self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_choices)  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Union[str, Any]:
										a_						= self.prepare_config_and_inputs()
										(
										    a_			
										)						= config_and_inputs
										a_						= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths}
										return config, inputs_dict



@require_torch
class  _snake_case (					snake_case  ,       snake_case  ,       snake_case  ,       unittest.TestCase       ):

					"""simple docstring"""





					_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 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__  )   ->						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 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__=False  )   ->						List[str]:
										a_						= super()._prepare_for_class(UpperCAmelCase__ ,			UpperCAmelCase__ ,			return_labels=UpperCAmelCase__  )

										if return_labels:
															if model_class.__name__ == "XLMForQuestionAnswering":
																				a_						= torch.zeros(
																				    self.model_tester.batch_size ,			dtype=torch.long ,			device=UpperCAmelCase__  )
																				a_						= torch.zeros(
																				    self.model_tester.batch_size ,			dtype=torch.long ,			device=UpperCAmelCase__  )

										return inputs_dict




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Dict:
										a_						= XLMModelTester(self  )
										a_						= ConfigTester(self ,			config_class=UpperCAmelCase__ ,			emb_dim=37  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						List[str]:
										self.config_tester.run_common_tests()




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Optional[int]:
										a_						= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_model(*UpperCAmelCase__  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Union[str, Any]:
										a_						= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_lm_head(*UpperCAmelCase__  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						int:
										a_						= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_simple_qa(*UpperCAmelCase__  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						List[str]:
										a_						= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_qa(*UpperCAmelCase__  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						List[str]:
										a_						= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_sequence_classif(*UpperCAmelCase__  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Optional[int]:
										a_						= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_token_classif(*UpperCAmelCase__  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Dict:
										a_						= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCAmelCase__  )




					def 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__=False ,			UpperCAmelCase__=1  )   ->						int:
										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
															a_						= min_length + idx + 1
															a_						= min_length + idx + 1

															a_						= (
															    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 			__SCREAMING_SNAKE_CASE			(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__ ,			UpperCAmelCase__=False ,			UpperCAmelCase__=1  )   ->						List[str]:
										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
															a_						= min_length + idx + 1
															a_						= (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 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Dict:
										for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															a_						= XLMModel.from_pretrained(UpperCAmelCase__  )
															self.assertIsNotNone(UpperCAmelCase__  )



@require_torch
class  _snake_case (					unittest.TestCase       ):

					"""simple docstring"""





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





'''simple docstring'''






import warnings
from functools import wraps
from typing import Callable





def lowerCamelCase__     (		__lowerCamelCase			:				Callable				):







   '''simple docstring'''




   @wraps(__lowerCamelCase				)
   def _inner_fn(*__lowerCamelCase			:				int			,       **__lowerCamelCase			:				Optional[Any]				):
      warnings.warn(
          (f"'{fn.__name__}' is experimental and might be subject to breaking changes in the future.")			,       __lowerCamelCase			,       )
      return fn(*__lowerCamelCase			,       **__lowerCamelCase				)

   return _inner_fn

