


"""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 _lowerCAmelCase							:







   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.02		,      UpperCamelCase__=2		,      UpperCamelCase__=4		,      UpperCamelCase__="last"		,      UpperCamelCase__=True		,      UpperCamelCase__=None		,      UpperCamelCase__=0		,      )      ->						Any:

       '''simple docstring'''




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







   def 					lowerCamelCase						(      self		)      ->						Optional[int]:

       '''simple docstring'''




       snake_case       : List[Any]    =       ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size		)
       snake_case       : List[str]    =       random_attention_mask([self.batch_size, self.seq_length]		)

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

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

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

       snake_case       : 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		)      ->						Tuple:

       '''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		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      )      ->						str:

       '''simple docstring'''




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







   def 					lowerCamelCase						(      self		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      )      ->						str:

       '''simple docstring'''




       snake_case       : Optional[Any]    =       XLMWithLMHeadModel(__lowerCAmelCase		)
       model.to(__lowerCAmelCase		)
       model.eval()

       snake_case       : str    =       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		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      )      ->						List[str]:

       '''simple docstring'''




       snake_case       : Optional[int]    =       XLMForQuestionAnsweringSimple(__lowerCAmelCase		)
       model.to(__lowerCAmelCase		)
       model.eval()

       snake_case       : Any    =       model(__lowerCAmelCase		)

       snake_case       : Tuple    =       model(__lowerCAmelCase		,      start_positions=__lowerCAmelCase		,      end_positions=__lowerCAmelCase		)
       snake_case       : 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		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      )      ->						Tuple:

       '''simple docstring'''




       snake_case       : Union[str, Any]    =       XLMForQuestionAnswering(__lowerCAmelCase		)
       model.to(__lowerCAmelCase		)
       model.eval()

       snake_case       : List[Any]    =       model(__lowerCAmelCase		)

       snake_case       : Dict    =       model(
           __lowerCAmelCase		,      start_positions=__lowerCAmelCase		,      end_positions=__lowerCAmelCase		,      cls_index=__lowerCAmelCase		,      is_impossible=__lowerCAmelCase		,      p_mask=__lowerCAmelCase		,      )

       snake_case       : Optional[Any]    =       model(
           __lowerCAmelCase		,      start_positions=__lowerCAmelCase		,      end_positions=__lowerCAmelCase		,      cls_index=__lowerCAmelCase		,      is_impossible=__lowerCAmelCase		,      )

       (snake_case      )       : str    =       result_with_labels.to_tuple()

       snake_case       : int    =       model(__lowerCAmelCase		,      start_positions=__lowerCAmelCase		,      end_positions=__lowerCAmelCase		)

       (snake_case      )       : List[str]    =       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		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      )      ->						Optional[Any]:

       '''simple docstring'''




       snake_case       : List[Any]    =       XLMForSequenceClassification(__lowerCAmelCase		)
       model.to(__lowerCAmelCase		)
       model.eval()

       snake_case       : Any    =       model(__lowerCAmelCase		)
       snake_case       : Tuple    =       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		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      )      ->						Dict:

       '''simple docstring'''




       snake_case       : Tuple    =       self.num_labels
       snake_case       : Any    =       XLMForTokenClassification(__lowerCAmelCase		)
       model.to(__lowerCAmelCase		)
       model.eval()

       snake_case       : str    =       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		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      )      ->						Tuple:

       '''simple docstring'''




       snake_case       : Any    =       self.num_choices
       snake_case       : Optional[Any]    =       XLMForMultipleChoice(config=__lowerCAmelCase		)
       model.to(__lowerCAmelCase		)
       model.eval()
       snake_case       : str    =       input_ids.unsqueeze(1		).expand(-1		,      self.num_choices		,      -1		).contiguous()
       snake_case       : Tuple    =       token_type_ids.unsqueeze(1		).expand(-1		,      self.num_choices		,      -1		).contiguous()
       snake_case       : Optional[Any]    =       input_mask.unsqueeze(1		).expand(-1		,      self.num_choices		,      -1		).contiguous()
       snake_case       : int    =       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		)      ->						Optional[int]:

       '''simple docstring'''




       snake_case       : int    =       self.prepare_config_and_inputs()
       (
           snake_case      
       )       : List[Any]    =       config_and_inputs
       snake_case       : List[Any]    =       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """lengths""": input_lengths}
       return config, inputs_dict







@require_torch
class _lowerCAmelCase							( SCREAMING_SNAKE_CASE__       ,				SCREAMING_SNAKE_CASE__       ,				SCREAMING_SNAKE_CASE__       ,				unittest.TestCase      ):
   __UpperCAmelCase					:			str				    =    (
       (
           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					:			List[Any]				    =    (
       {
           '''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		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)      ->						List[str]:

       '''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		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__=False		)      ->						int:

       '''simple docstring'''




       snake_case       : Optional[int]    =       super()._prepare_for_class(__lowerCAmelCase		,      __lowerCAmelCase		,      return_labels=__lowerCAmelCase		)

       if return_labels:
           if model_class.__name__ == "XLMForQuestionAnswering":
               snake_case       : Any    =       torch.zeros(
                   self.model_tester.batch_size		,      dtype=torch.long		,      device=__lowerCAmelCase		)
               snake_case       : Dict    =       torch.zeros(
                   self.model_tester.batch_size		,      dtype=torch.long		,      device=__lowerCAmelCase		)

       return inputs_dict







   def 					lowerCamelCase						(      self		)      ->						List[Any]:

       '''simple docstring'''




       snake_case       : List[str]    =       XLMModelTester(self		)
       snake_case       : Optional[Any]    =       ConfigTester(self		,      config_class=__lowerCAmelCase		,      emb_dim=37		)







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

       '''simple docstring'''




       self.config_tester.run_common_tests()







   def 					lowerCamelCase						(      self		)      ->						Optional[int]:

       '''simple docstring'''




       snake_case       : List[Any]    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_model(*__lowerCAmelCase		)







   def 					lowerCamelCase						(      self		)      ->						int:

       '''simple docstring'''




       snake_case       : Tuple    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_lm_head(*__lowerCAmelCase		)







   def 					lowerCamelCase						(      self		)      ->						Dict:

       '''simple docstring'''




       snake_case       : int    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_simple_qa(*__lowerCAmelCase		)







   def 					lowerCamelCase						(      self		)      ->						Tuple:

       '''simple docstring'''




       snake_case       : Optional[int]    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_qa(*__lowerCAmelCase		)







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

       '''simple docstring'''




       snake_case       : int    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_sequence_classif(*__lowerCAmelCase		)







   def 					lowerCamelCase						(      self		)      ->						Tuple:

       '''simple docstring'''




       snake_case       : List[Any]    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_token_classif(*__lowerCAmelCase		)







   def 					lowerCamelCase						(      self		)      ->						Dict:

       '''simple docstring'''




       snake_case       : Any    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_for_multiple_choice(*__lowerCAmelCase		)







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

       '''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
           snake_case       : Tuple    =       min_length + idx + 1
           snake_case       : Optional[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(__lowerCAmelCase		)		)







   def 					lowerCamelCase						(      self		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__=False		,      UpperCamelCase__=1		)      ->						List[str]:

       '''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
           snake_case       : Optional[Any]    =       min_length + idx + 1
           snake_case       : List[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		)      ->						Union[str, Any]:

       '''simple docstring'''




       for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case       : Any    =       XLMModel.from_pretrained(__lowerCAmelCase		)
           self.assertIsNotNone(__lowerCAmelCase		)







@require_torch
class _lowerCAmelCase							( unittest.TestCase      ):







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

       '''simple docstring'''




       snake_case       : str    =       XLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048"		)
       model.to(__lowerCAmelCase		)
       snake_case       : str    =       torch.tensor([[14, 447]]		,      dtype=torch.long		,      device=__lowerCAmelCase		)  # the president
       snake_case       : 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
       snake_case       : Dict    =       model.generate(__lowerCAmelCase		,      do_sample=__lowerCAmelCase		)
       self.assertListEqual(output_ids[0].cpu().numpy().tolist()		,      __lowerCAmelCase		)













'''simple docstring'''

def 				snake_case       (  a_				:			list[int]     , a_				:			list[int]						)							->					tuple[float, float]:





      """simple docstring"""

      if not len(a_						) == len(a_						) == 3:
            raise ValueError("""Please enter a valid equation."""						)
      if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0:
            raise ValueError("""Both a & b of two equations can't be zero."""						)

      # Extract the coefficients
      UpperCamelCase_     ,      UpperCamelCase_     ,      UpperCamelCase_					: Optional[int]            =				equationa
      UpperCamelCase_     ,      UpperCamelCase_     ,      UpperCamelCase_					: List[str]            =				equationa

      # Calculate the determinants of the matrices
      UpperCamelCase_					: Union[str, Any]            =				aa * ba - aa * ba
      UpperCamelCase_					: Dict            =				ca * ba - ca * ba
      UpperCamelCase_					: Dict            =				aa * ca - aa * ca

      # Check if the system of linear equations has a solution (using Cramer's rule)
      if determinant == 0:
            if determinant_x == determinant_y == 0:
                  raise ValueError("""Infinite solutions. (Consistent system)"""						)
            else:
                  raise ValueError("""No solution. (Inconsistent system)"""						)
      else:
            if determinant_x == determinant_y == 0:
                  # Trivial solution (Inconsistent system)
                  return (0.0, 0.0)
            else:
                  UpperCamelCase_					: List[Any]            =				determinant_x / determinant
                  UpperCamelCase_					: List[str]            =				determinant_y / determinant
                  # Non-Trivial Solution (Consistent system)
                  return (x, y)






