






'''simple docstring'''


import unittest

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

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 (
      ErnieMForInformationExtraction,
      ErnieMForMultipleChoice,
      ErnieMForQuestionAnswering,
      ErnieMForSequenceClassification,
      ErnieMForTokenClassification,
      ErnieMModel,
  )
  from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST




class 							lowerCAmelCase					:







   """simple docstring"""







   def __init__( self   ,   _A   ,   _A=13   ,   _A=7   ,   _A=True   ,   _A=True   ,   _A=True   ,   _A=99   ,   _A=32   ,   _A=5   ,   _A=4   ,   _A=37   ,   _A="gelu"   ,   _A=0.1   ,   _A=0.1   ,   _A=512   ,   _A=16   ,   _A=2   ,   _A=0.02   ,   _A=3   ,   _A=4   ,   _A=None   ,   )    ->   Any:
    __a    :	Optional[Any]									=						parent
    __a    :	List[str]									=						batch_size
    __a    :	Tuple									=						seq_length
    __a    :	Tuple									=						is_training
    __a    :	Union[str, Any]									=						use_input_mask
    __a    :	List[str]									=						use_labels
    __a    :	int									=						vocab_size
    __a    :	Tuple									=						hidden_size
    __a    :	Any									=						num_hidden_layers
    __a    :	List[str]									=						num_attention_heads
    __a    :	Optional[Any]									=						intermediate_size
    __a    :	List[Any]									=						hidden_act
    __a    :	int									=						hidden_dropout_prob
    __a    :	int									=						attention_probs_dropout_prob
    __a    :	List[Any]									=						max_position_embeddings
    __a    :	List[str]									=						type_vocab_size
    __a    :	List[str]									=						type_sequence_label_size
    __a    :	Tuple									=						initializer_range
    __a    :	int									=						num_labels
    __a    :	Tuple									=						num_choices
    __a    :	Optional[int]									=						scope



   def    __magic_name__    ( self			)    ->   Any:
    __a    :	Dict									=						ids_tensor([self.batch_size, self.seq_length]   ,   self.vocab_size			)

    __a    :	Dict									=						None
    if self.use_input_mask:
     __a    :	int									=						random_attention_mask([self.batch_size, self.seq_length]			)

    __a    :	List[str]									=						None
    __a    :	Tuple									=						None
    __a    :	str									=						None
    if self.use_labels:
     __a    :	str									=						ids_tensor([self.batch_size]   ,   self.type_sequence_label_size			)
     __a    :	Any									=						ids_tensor([self.batch_size, self.seq_length]   ,   self.num_labels			)
     __a    :	Union[str, Any]									=						ids_tensor([self.batch_size]   ,   self.num_choices			)

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

    return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



   def    __magic_name__    ( self			)    ->   List[Any]:
    __a    :	List[str]									=						ids_tensor([self.batch_size, self.seq_length]   ,   self.vocab_size			)

    __a    :	Tuple									=						None
    if self.use_input_mask:
     __a    :	List[Any]									=						random_attention_mask([self.batch_size, self.seq_length]			)
    __a    :	Tuple									=						self.get_config()

    return config, input_ids, input_mask



   def    __magic_name__    ( self			)    ->   Tuple:
    return ErnieMConfig(
        vocab_size=self.vocab_size   ,   hidden_size=self.hidden_size   ,   num_hidden_layers=self.num_hidden_layers   ,   num_attention_heads=self.num_attention_heads   ,   intermediate_size=self.intermediate_size   ,   hidden_act=self.hidden_act   ,   hidden_dropout_prob=self.hidden_dropout_prob   ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,   max_position_embeddings=self.max_position_embeddings   ,   type_vocab_size=self.type_vocab_size   ,   initializer_range=self.initializer_range   ,   )



   def    __magic_name__    ( self   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A			)    ->   Dict:
    __a    :	Union[str, Any]									=						ErnieMModel(config=_A			)
    model.to(_A			)
    model.eval()
    __a    :	Any									=						model(_A   ,   return_dict=_A			)
    self.parent.assertEqual(result.last_hidden_state.shape   ,   (self.batch_size, self.seq_length, self.hidden_size)			)



   def    __magic_name__    ( self   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A			)    ->   Any:
    __a    :	str									=						ErnieMForQuestionAnswering(config=_A			)
    model.to(_A			)
    model.eval()
    __a    :	Optional[Any]									=						model(
        _A   ,   attention_mask=_A   ,   start_positions=_A   ,   end_positions=_A   ,   )
    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    __magic_name__    ( self   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A			)    ->   str:
    __a    :	int									=						ErnieMForInformationExtraction(config=_A			)
    model.to(_A			)
    model.eval()
    __a    :	str									=						torch.ones_like(_A   ,   dtype=torch.floataa			)
    __a    :	Union[str, Any]									=						model(
        _A   ,   attention_mask=_A   ,   start_positions=_A   ,   end_positions=_A   ,   )
    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    __magic_name__    ( self   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A			)    ->   int:
    __a    :	Optional[int]									=						self.num_labels
    __a    :	Dict									=						ErnieMForSequenceClassification(_A			)
    model.to(_A			)
    model.eval()
    __a    :	Union[str, Any]									=						model(_A   ,   attention_mask=_A   ,   labels=_A			)
    self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.num_labels)			)



   def    __magic_name__    ( self   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A			)    ->   Optional[int]:
    __a    :	Optional[int]									=						self.num_labels
    __a    :	List[Any]									=						ErnieMForTokenClassification(config=_A			)
    model.to(_A			)
    model.eval()
    input_ids.to(_A			)
    input_mask.to(_A			)
    token_labels.to(_A			)

    __a    :	Dict									=						model(_A   ,   attention_mask=_A   ,   labels=_A			)

    self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.seq_length, self.num_labels)			)



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



   def    __magic_name__    ( self			)    ->   int:
    __a    :	List[str]									=						self.prepare_config_and_inputs()
    (
        __a    
    )    :	int									=						config_and_inputs
    __a    :	Dict									=						{'input_ids': input_ids, 'attention_mask': input_mask}
    return config, inputs_dict




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







   """simple docstring"""







   _A						=   (
       (
           ErnieMModel,
           ErnieMForMultipleChoice,
           ErnieMForQuestionAnswering,
           ErnieMForSequenceClassification,
           ErnieMForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   _A						=   ()
   _A						=   (
       {
           'feature-extraction': ErnieMModel,
           'question-answering': ErnieMForQuestionAnswering,
           'text-classification': ErnieMForSequenceClassification,
           'token-classification': ErnieMForTokenClassification,
           'zero-shot': ErnieMForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _A						=   False



   def    __magic_name__    ( self   ,   _A   ,   _A   ,   _A   ,   _A   ,   _A			)    ->   List[str]:
    if pipeline_test_casse_name == "QAPipelineTests":
     return True

    return False



   def    __magic_name__    ( self			)    ->   List[Any]:
    __a    :	Tuple									=						ErnieMModelTester(self			)
    __a    :	int									=						ConfigTester(self   ,   config_class=_A   ,   hidden_size=37			)



   def    __magic_name__    ( self			)    ->   Optional[Any]:
    self.config_tester.run_common_tests()



   def    __magic_name__    ( self			)    ->   Any:
    __a    :	int									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_A			)



   def    __magic_name__    ( self			)    ->   List[Any]:
    __a    :	Union[str, Any]									=						self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
     __a    :	Optional[int]									=						type
     self.model_tester.create_and_check_model(*_A			)



   def    __magic_name__    ( self			)    ->   Union[str, Any]:
    __a    :	Optional[int]									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*_A			)



   def    __magic_name__    ( self			)    ->   List[str]:
    __a    :	Any									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*_A			)



   def    __magic_name__    ( self			)    ->   Optional[int]:
    __a    :	Optional[Any]									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*_A			)



   def    __magic_name__    ( self			)    ->   List[Any]:
    __a    :	List[str]									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_information_extraction(*_A			)



   def    __magic_name__    ( self			)    ->   str:
    __a    :	Union[str, Any]									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*_A			)



   @slow
   def    __magic_name__    ( self			)    ->   List[str]:
    for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __a    :	List[Any]									=						ErnieMModel.from_pretrained(_A			)
     self.assertIsNotNone(_A			)




@require_torch
class 							lowerCAmelCase					(							unittest.TestCase   ):







   """simple docstring"""







   @slow
   def    __magic_name__    ( self			)    ->   Tuple:
    __a    :	int									=						ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'			)
    model.eval()
    __a    :	str									=						torch.tensor([[0, 1, 2, 3, 4, 5]]			)
    __a    :	int									=						model(_A			)[0]

    # TODO Replace vocab size
    __a    :	Any									=						768

    __a    :	int									=						torch.Size((1, 6, hidden_size)			)
    self.assertEqual(output.shape   ,   _A			)

    __a    :	int									=						torch.tensor(
        [[[-0.0_012, 0.1_245, -0.0_214], [-0.0_742, 0.0_244, -0.0_771], [-0.0_333, 0.1_164, -0.1_554]]]			)

    self.assertTrue(torch.allclose(output[:, :3, :3]   ,   _A   ,   atol=1E-3			)			)












'''simple docstring'''


def   lowerCAmelCase__			(					SCREAMING_SNAKE_CASE__ = 30		):
 __a    :	Tuple									=						exponent + 2
 __a    :	str									=						(1 + 5**0.5) / 2
 __a    :	Any									=						(phi**fibonacci_index - (phi - 1) ** fibonacci_index) / 5**0.5

 return int(SCREAMING_SNAKE_CASE__		)


if __name__ == "__main__":
  print(F"{solution() = }")




