



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




  '''simple docstring'''



  def __init__(self	,__lowerCamelCase	,__lowerCamelCase=13	,__lowerCamelCase=7	,__lowerCamelCase=True	,__lowerCamelCase=True	,__lowerCamelCase=True	,__lowerCamelCase=True	,__lowerCamelCase=True	,__lowerCamelCase=False	,__lowerCamelCase=False	,__lowerCamelCase=False	,__lowerCamelCase=2	,__lowerCamelCase=99	,__lowerCamelCase=0	,__lowerCamelCase=32	,__lowerCamelCase=5	,__lowerCamelCase=4	,__lowerCamelCase=0.1	,__lowerCamelCase=0.1	,__lowerCamelCase=5_12	,__lowerCamelCase=2	,__lowerCamelCase=0.02	,__lowerCamelCase=2	,__lowerCamelCase=4	,__lowerCamelCase="last"	,__lowerCamelCase=True	,__lowerCamelCase=None	,__lowerCamelCase=0	,)  ->    Dict:

      """simple docstring"""






      lowerCAmelCase__  :       Any				=  parent
      lowerCAmelCase__  :       List[Any]				=  batch_size
      lowerCAmelCase__  :       List[str]				=  seq_length
      lowerCAmelCase__  :       Any				=  is_training
      lowerCAmelCase__  :       str				=  use_input_lengths
      lowerCAmelCase__  :       Tuple				=  use_token_type_ids
      lowerCAmelCase__  :       int				=  use_labels
      lowerCAmelCase__  :       List[Any]				=  gelu_activation
      lowerCAmelCase__  :       List[str]				=  sinusoidal_embeddings
      lowerCAmelCase__  :       int				=  causal
      lowerCAmelCase__  :       Optional[int]				=  asm
      lowerCAmelCase__  :       Any				=  n_langs
      lowerCAmelCase__  :       Optional[int]				=  vocab_size
      lowerCAmelCase__  :       int				=  n_special
      lowerCAmelCase__  :       int				=  hidden_size
      lowerCAmelCase__  :       List[Any]				=  num_hidden_layers
      lowerCAmelCase__  :       List[Any]				=  num_attention_heads
      lowerCAmelCase__  :       Union[str, Any]				=  hidden_dropout_prob
      lowerCAmelCase__  :       Any				=  attention_probs_dropout_prob
      lowerCAmelCase__  :       Dict				=  max_position_embeddings
      lowerCAmelCase__  :       int				=  type_sequence_label_size
      lowerCAmelCase__  :       List[str]				=  initializer_range
      lowerCAmelCase__  :       Any				=  num_labels
      lowerCAmelCase__  :       List[str]				=  num_choices
      lowerCAmelCase__  :       Tuple				=  summary_type
      lowerCAmelCase__  :       Optional[Any]				=  use_proj
      lowerCAmelCase__  :       List[Any]				=  scope
      lowerCAmelCase__  :       str				=  bos_token_id



  def 	lowerCAmelCase__   (self				)  ->    Dict:

      """simple docstring"""






      lowerCAmelCase__  :       Tuple				=  ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size				)
      lowerCAmelCase__  :       Any				=  random_attention_mask([self.batch_size, self.seq_length]				)

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

      lowerCAmelCase__  :       str				=  None
      if self.use_token_type_ids:
          lowerCAmelCase__  :       Optional[Any]				=  ids_tensor([self.batch_size, self.seq_length]	,self.n_langs				)

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

      lowerCAmelCase__  :       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 	lowerCAmelCase__   (self				)  ->    Optional[Any]:

      """simple docstring"""






      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 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,)  ->    Optional[int]:

      """simple docstring"""






      lowerCAmelCase__  :       List[Any]				=  XLMModel(config=__lowerCamelCase				)
      model.to(__lowerCamelCase				)
      model.eval()
      lowerCAmelCase__  :       str				=  model(__lowerCamelCase	,lengths=__lowerCamelCase	,langs=__lowerCamelCase				)
      lowerCAmelCase__  :       List[str]				=  model(__lowerCamelCase	,langs=__lowerCamelCase				)
      lowerCAmelCase__  :       Any				=  model(__lowerCamelCase				)
      self.parent.assertEqual(result.last_hidden_state.shape	,(self.batch_size, self.seq_length, self.hidden_size)				)



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,)  ->    int:

      """simple docstring"""






      lowerCAmelCase__  :       str				=  XLMWithLMHeadModel(__lowerCamelCase				)
      model.to(__lowerCamelCase				)
      model.eval()

      lowerCAmelCase__  :       Any				=  model(__lowerCamelCase	,token_type_ids=__lowerCamelCase	,labels=__lowerCamelCase				)
      self.parent.assertEqual(result.loss.shape	,()				)
      self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.vocab_size)				)



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,)  ->    Optional[int]:

      """simple docstring"""






      lowerCAmelCase__  :       Any				=  XLMForQuestionAnsweringSimple(__lowerCamelCase				)
      model.to(__lowerCamelCase				)
      model.eval()

      lowerCAmelCase__  :       Any				=  model(__lowerCamelCase				)

      lowerCAmelCase__  :       Tuple				=  model(__lowerCamelCase	,start_positions=__lowerCamelCase	,end_positions=__lowerCamelCase				)
      lowerCAmelCase__  :       Optional[int]				=  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 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,)  ->    int:

      """simple docstring"""






      lowerCAmelCase__  :       Any				=  XLMForQuestionAnswering(__lowerCamelCase				)
      model.to(__lowerCamelCase				)
      model.eval()

      lowerCAmelCase__  :       List[Any]				=  model(__lowerCamelCase				)

      lowerCAmelCase__  :       List[str]				=  model(
          __lowerCamelCase	,start_positions=__lowerCamelCase	,end_positions=__lowerCamelCase	,cls_index=__lowerCamelCase	,is_impossible=__lowerCamelCase	,p_mask=__lowerCamelCase	,)

      lowerCAmelCase__  :       int				=  model(
          __lowerCamelCase	,start_positions=__lowerCamelCase	,end_positions=__lowerCamelCase	,cls_index=__lowerCamelCase	,is_impossible=__lowerCamelCase	,)

      (lowerCAmelCase__	)  :       Optional[Any]				=  result_with_labels.to_tuple()

      lowerCAmelCase__  :       Dict				=  model(__lowerCamelCase	,start_positions=__lowerCamelCase	,end_positions=__lowerCamelCase				)

      (lowerCAmelCase__	)  :       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 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,)  ->    str:

      """simple docstring"""






      lowerCAmelCase__  :       List[Any]				=  XLMForSequenceClassification(__lowerCamelCase				)
      model.to(__lowerCamelCase				)
      model.eval()

      lowerCAmelCase__  :       Optional[Any]				=  model(__lowerCamelCase				)
      lowerCAmelCase__  :       List[str]				=  model(__lowerCamelCase	,labels=__lowerCamelCase				)
      self.parent.assertEqual(result.loss.shape	,()				)
      self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.type_sequence_label_size)				)



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,)  ->    str:

      """simple docstring"""






      lowerCAmelCase__  :       Optional[Any]				=  self.num_labels
      lowerCAmelCase__  :       Optional[int]				=  XLMForTokenClassification(__lowerCamelCase				)
      model.to(__lowerCamelCase				)
      model.eval()

      lowerCAmelCase__  :       Tuple				=  model(__lowerCamelCase	,attention_mask=__lowerCamelCase	,labels=__lowerCamelCase				)
      self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.num_labels)				)



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,)  ->    Any:

      """simple docstring"""






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



  def 	lowerCAmelCase__   (self				)  ->    Tuple:

      """simple docstring"""






      lowerCAmelCase__  :       Optional[Any]				=  self.prepare_config_and_inputs()
      (
          lowerCAmelCase__	
      )  :       str				=  config_and_inputs
      lowerCAmelCase__  :       int				=  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths}
      return config, inputs_dict



@require_torch
class  lowerCamelCase__    (							lowerCamelCase__   ,       lowerCamelCase__   ,       lowerCamelCase__   ,       unittest.TestCase):




  '''simple docstring'''



  snake_case_			=(
      (
          XLMModel,
          XLMWithLMHeadModel,
          XLMForQuestionAnswering,
          XLMForSequenceClassification,
          XLMForQuestionAnsweringSimple,
          XLMForTokenClassification,
          XLMForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  snake_case_			=(
      (XLMWithLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Check other models whether language generation is also applicable
  snake_case_			=(
      {
          """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 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase				)  ->    Optional[Any]:

      """simple docstring"""






      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 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase=False				)  ->    List[Any]:

      """simple docstring"""






      lowerCAmelCase__  :       Optional[Any]				=  super()._prepare_for_class(__lowerCamelCase	,__lowerCamelCase	,return_labels=__lowerCamelCase				)

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

      return inputs_dict



  def 	lowerCAmelCase__   (self				)  ->    Any:

      """simple docstring"""






      lowerCAmelCase__  :       str				=  XLMModelTester(self				)
      lowerCAmelCase__  :       Tuple				=  ConfigTester(self	,config_class=__lowerCamelCase	,emb_dim=37				)



  def 	lowerCAmelCase__   (self				)  ->    Union[str, Any]:

      """simple docstring"""






      self.config_tester.run_common_tests()



  def 	lowerCAmelCase__   (self				)  ->    Any:

      """simple docstring"""






      lowerCAmelCase__  :       Union[str, Any]				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_model(*__lowerCamelCase				)



  def 	lowerCAmelCase__   (self				)  ->    List[str]:

      """simple docstring"""






      lowerCAmelCase__  :       Tuple				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_lm_head(*__lowerCamelCase				)



  def 	lowerCAmelCase__   (self				)  ->    str:

      """simple docstring"""






      lowerCAmelCase__  :       List[str]				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_simple_qa(*__lowerCamelCase				)



  def 	lowerCAmelCase__   (self				)  ->    Optional[Any]:

      """simple docstring"""






      lowerCAmelCase__  :       str				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_qa(*__lowerCamelCase				)



  def 	lowerCAmelCase__   (self				)  ->    Any:

      """simple docstring"""






      lowerCAmelCase__  :       Tuple				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_sequence_classif(*__lowerCamelCase				)



  def 	lowerCAmelCase__   (self				)  ->    Tuple:

      """simple docstring"""






      lowerCAmelCase__  :       List[str]				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_token_classif(*__lowerCamelCase				)



  def 	lowerCAmelCase__   (self				)  ->    int:

      """simple docstring"""






      lowerCAmelCase__  :       Union[str, Any]				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_for_multiple_choice(*__lowerCamelCase				)



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase=False	,__lowerCamelCase=1				)  ->    Dict:

      """simple docstring"""






      self.assertIsInstance(__lowerCamelCase	,__lowerCamelCase				)
      self.assertListEqual(
          [isinstance(__lowerCamelCase	,__lowerCamelCase				) for iter_attentions in attentions]	,[True] * len(__lowerCamelCase				)				)
      self.assertEqual(len(__lowerCamelCase				)	,(max_length - min_length) * num_beam_groups				)

      for idx, iter_attentions in enumerate(__lowerCamelCase				):
          # adds PAD dummy token
          lowerCAmelCase__  :       Tuple				=  min_length + idx + 1
          lowerCAmelCase__  :       Tuple				=  min_length + idx + 1

          lowerCAmelCase__  :       List[Any]				=  (
              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(__lowerCamelCase				)				)



  def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase=False	,__lowerCamelCase=1				)  ->    Union[str, Any]:

      """simple docstring"""






      self.assertIsInstance(__lowerCamelCase	,__lowerCamelCase				)
      self.assertListEqual(
          [isinstance(__lowerCamelCase	,__lowerCamelCase				) for iter_hidden_states in hidden_states]	,[True] * len(__lowerCamelCase				)	,)
      self.assertEqual(len(__lowerCamelCase				)	,(max_length - min_length) * num_beam_groups				)

      for idx, iter_hidden_states in enumerate(__lowerCamelCase				):
          # 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(__lowerCamelCase				)	,)
      pass



  @slow
  def 	lowerCAmelCase__   (self				)  ->    Optional[Any]:

      """simple docstring"""






      for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase__  :       Optional[int]				=  XLMModel.from_pretrained(__lowerCamelCase				)
          self.assertIsNotNone(__lowerCamelCase				)



@require_torch
class  lowerCamelCase__    (							unittest.TestCase):




  '''simple docstring'''



  @slow
  def 	lowerCAmelCase__   (self				)  ->    Union[str, Any]:

      """simple docstring"""






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






def 			lowerCAmelCase__	(					lowerCamelCase_	:	Any ,lowerCamelCase_	:	Optional[Any]):





				'''simple docstring'''

				lowerCAmelCase__  :       str				=  [0 for i in range(r + 1)]
				# nc0 = 1
				lowerCAmelCase__  :       Tuple				=  1
				for i in range(1 ,n + 1):
								# to compute current row from previous row.
								lowerCAmelCase__  :       Optional[int]				=  min(lowerCamelCase_ ,lowerCamelCase_)
								while j > 0:
												c[j] += c[j - 1]
												j -= 1
				return c[r]

print(binomial_coefficient(n=1_0, r=5))

