

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



      """simple docstring"""







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

           SCREAMING_SNAKE_CASE										=None
           if self.use_input_mask:
                SCREAMING_SNAKE_CASE										=random_attention_mask([self.batch_size, self.seq_length]    )

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

           SCREAMING_SNAKE_CASE										=self.get_config()

           return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
      def 	_lowerCAmelCase					(				self					:							Union[str, Any]    ):
           SCREAMING_SNAKE_CASE										=ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size    )

           SCREAMING_SNAKE_CASE										=None
           if self.use_input_mask:
                SCREAMING_SNAKE_CASE										=random_attention_mask([self.batch_size, self.seq_length]    )
           SCREAMING_SNAKE_CASE										=self.get_config()

           return config, input_ids, input_mask
      def 	_lowerCAmelCase					(				self					:							Any    ):
           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 	_lowerCAmelCase					(				self					:							str      ,snake_case					:							Union[str, Any]      ,snake_case					:							int      ,snake_case					:							str      ,snake_case					:							Tuple      ,snake_case					:							List[str]      ,snake_case					:							str    ):
           SCREAMING_SNAKE_CASE										=ErnieMModel(config=snake_case    )
           model.to(snake_case    )
           model.eval()
           SCREAMING_SNAKE_CASE										=model(snake_case      ,return_dict=snake_case    )
           self.parent.assertEqual(result.last_hidden_state.shape      ,(self.batch_size, self.seq_length, self.hidden_size)    )
      def 	_lowerCAmelCase					(				self					:							Any      ,snake_case					:							int      ,snake_case					:							Union[str, Any]      ,snake_case					:							str      ,snake_case					:							Union[str, Any]      ,snake_case					:							int      ,snake_case					:							int    ):
           SCREAMING_SNAKE_CASE										=ErnieMForQuestionAnswering(config=snake_case    )
           model.to(snake_case    )
           model.eval()
           SCREAMING_SNAKE_CASE										=model(
               snake_case      ,attention_mask=snake_case      ,start_positions=snake_case      ,end_positions=snake_case      ,)
           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					:							Optional[int]      ,snake_case					:							Optional[Any]      ,snake_case					:							List[Any]      ,snake_case					:							List[Any]      ,snake_case					:							Any      ,snake_case					:							Any      ,snake_case					:							List[str]    ):
           SCREAMING_SNAKE_CASE										=ErnieMForInformationExtraction(config=snake_case    )
           model.to(snake_case    )
           model.eval()
           SCREAMING_SNAKE_CASE										=torch.ones_like(snake_case      ,dtype=torch.floataa    )
           SCREAMING_SNAKE_CASE										=model(
               snake_case      ,attention_mask=snake_case      ,start_positions=snake_case      ,end_positions=snake_case      ,)
           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					:							Optional[Any]      ,snake_case					:							Optional[Any]      ,snake_case					:							Union[str, Any]      ,snake_case					:							Optional[int]      ,snake_case					:							Tuple      ,snake_case					:							List[str]      ,snake_case					:							str    ):
           SCREAMING_SNAKE_CASE										=self.num_labels
           SCREAMING_SNAKE_CASE										=ErnieMForSequenceClassification(snake_case    )
           model.to(snake_case    )
           model.eval()
           SCREAMING_SNAKE_CASE										=model(snake_case      ,attention_mask=snake_case      ,labels=snake_case    )
           self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_labels)    )
      def 	_lowerCAmelCase					(				self					:							Optional[Any]      ,snake_case					:							List[Any]      ,snake_case					:							Optional[Any]      ,snake_case					:							Any      ,snake_case					:							Any      ,snake_case					:							Any      ,snake_case					:							Any    ):
           SCREAMING_SNAKE_CASE										=self.num_labels
           SCREAMING_SNAKE_CASE										=ErnieMForTokenClassification(config=snake_case    )
           model.to(snake_case    )
           model.eval()
           input_ids.to(snake_case    )
           input_mask.to(snake_case    )
           token_labels.to(snake_case    )

           SCREAMING_SNAKE_CASE										=model(snake_case      ,attention_mask=snake_case      ,labels=snake_case    )

           self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.num_labels)    )
      def 	_lowerCAmelCase					(				self					:							Dict      ,snake_case					:							Optional[Any]      ,snake_case					:							Union[str, Any]      ,snake_case					:							Any      ,snake_case					:							Tuple      ,snake_case					:							int      ,snake_case					:							Union[str, Any]    ):
           SCREAMING_SNAKE_CASE										=self.num_choices
           SCREAMING_SNAKE_CASE										=ErnieMForMultipleChoice(config=snake_case    )
           model.to(snake_case    )
           model.eval()
           SCREAMING_SNAKE_CASE										=input_ids.unsqueeze(1    ).expand(-1      ,self.num_choices      ,-1    ).contiguous()
           SCREAMING_SNAKE_CASE										=input_mask.unsqueeze(1    ).expand(-1      ,self.num_choices      ,-1    ).contiguous()
           SCREAMING_SNAKE_CASE										=model(
               snake_case      ,attention_mask=snake_case      ,labels=snake_case      ,)
           self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_choices)    )


      def 	_lowerCAmelCase					(				self					:							Any    ):
           SCREAMING_SNAKE_CASE										=self.prepare_config_and_inputs()
           (
               (
               SCREAMING_SNAKE_CASE
           )						,  (
               SCREAMING_SNAKE_CASE
           )						,  (
               SCREAMING_SNAKE_CASE
           )						,  (
               SCREAMING_SNAKE_CASE
           )						,  (
               SCREAMING_SNAKE_CASE
           )						,  (
               SCREAMING_SNAKE_CASE
           )						,  
           )										=config_and_inputs
           SCREAMING_SNAKE_CASE										={'input_ids': input_ids, 'attention_mask': input_mask}
           return config, inputs_dict







@require_torch
class 		a_	(							lowerCamelCase_			,      lowerCamelCase_			,      unittest.TestCase					):



      """simple docstring"""







      __UpperCAmelCase      		=				(
          (
              ErnieMModel,
              ErnieMForMultipleChoice,
              ErnieMForQuestionAnswering,
              ErnieMForSequenceClassification,
              ErnieMForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      __UpperCAmelCase      		=				()
      __UpperCAmelCase      		=				(
          {
              'feature-extraction': ErnieMModel,
              'question-answering': ErnieMForQuestionAnswering,
              'text-classification': ErnieMForSequenceClassification,
              'token-classification': ErnieMForTokenClassification,
              'zero-shot': ErnieMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      __UpperCAmelCase      		=				False
      def 	_lowerCAmelCase					(				self					:							List[str]      ,snake_case					:							List[Any]      ,snake_case					:							int      ,snake_case					:							Optional[int]      ,snake_case					:							str      ,snake_case					:							Dict    ):
           if pipeline_test_casse_name == "QAPipelineTests":
                return True

           return False
      def 	_lowerCAmelCase					(				self					:							List[str]    ):
           SCREAMING_SNAKE_CASE										=ErnieMModelTester(self    )
           SCREAMING_SNAKE_CASE										=ConfigTester(self      ,config_class=snake_case      ,hidden_size=37    )
      def 	_lowerCAmelCase					(				self					:							Optional[int]    ):
           self.config_tester.run_common_tests()
      def 	_lowerCAmelCase					(				self					:							Any    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							Tuple    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                SCREAMING_SNAKE_CASE										=type
                self.model_tester.create_and_check_model(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							List[Any]    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							Optional[int]    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							Any    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							int    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_information_extraction(*snake_case    )
      def 	_lowerCAmelCase					(				self					:							Optional[int]    ):
           SCREAMING_SNAKE_CASE										=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*snake_case    )


      @slow
      def 	_lowerCAmelCase					(				self					:							str    ):
           for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                SCREAMING_SNAKE_CASE										=ErnieMModel.from_pretrained(snake_case    )
                self.assertIsNotNone(snake_case    )







@require_torch
class 		a_	(							unittest.TestCase					):



      """simple docstring"""







      @slow
      def 	_lowerCAmelCase					(				self					:							List[Any]    ):
           SCREAMING_SNAKE_CASE										=ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'    )
           model.eval()
           SCREAMING_SNAKE_CASE										=torch.tensor([[0, 1, 2, 3, 4, 5]]    )
           SCREAMING_SNAKE_CASE										=model(snake_case    )[0]

           # TODO Replace vocab size
           SCREAMING_SNAKE_CASE										=768

           SCREAMING_SNAKE_CASE										=torch.Size((1, 6, hidden_size)    )
           self.assertEqual(output.shape      ,snake_case    )

           SCREAMING_SNAKE_CASE										=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]      ,snake_case      ,atol=1e-3    )    )




import decimal







def       snake_case__    (       ):





     """simple docstring"""







     SCREAMING_SNAKE_CASE										=0
     SCREAMING_SNAKE_CASE										=decimal.Context(prec=105      )
     for i in range(2,   100      ):
          SCREAMING_SNAKE_CASE										=decimal.Decimal(lowerCAmelCase_      )
          SCREAMING_SNAKE_CASE										=number.sqrt(lowerCAmelCase_      )
          if len(str(lowerCAmelCase_      )      ) > 1:
               answer += int(str(lowerCAmelCase_      )[0]      )
               SCREAMING_SNAKE_CASE										=str(lowerCAmelCase_      )[2:101]
               answer += sum(int(lowerCAmelCase_      ) for x in sqrt_number_str      )
     return answer


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(f'{solution() = }')

