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






 """simple docstring"""




 def __init__(    self ,       __magic_name__ ,       __magic_name__=13 ,       __magic_name__=7 ,       __magic_name__=True ,       __magic_name__=True ,       __magic_name__=True ,       __magic_name__=99 ,       __magic_name__=32 ,       __magic_name__=5 ,       __magic_name__=4 ,       __magic_name__=37 ,       __magic_name__="gelu" ,       __magic_name__=0.1 ,       __magic_name__=0.1 ,       __magic_name__=512 ,       __magic_name__=16 ,       __magic_name__=2 ,       __magic_name__=0.02 ,       __magic_name__=3 ,       __magic_name__=4 ,       __magic_name__=None ,       ):

        """simple docstring"""

        A_							:       Dict	      =				parent
        A_							:       Optional[int]	      =				batch_size
        A_							:       List[Any]	      =				seq_length
        A_							:       int	      =				is_training
        A_							:       str	      =				use_input_mask
        A_							:       Any	      =				use_labels
        A_							:       int	      =				vocab_size
        A_							:       int	      =				hidden_size
        A_							:       Union[str, Any]	      =				num_hidden_layers
        A_							:       List[Any]	      =				num_attention_heads
        A_							:       Union[str, Any]	      =				intermediate_size
        A_							:       Tuple	      =				hidden_act
        A_							:       Any	      =				hidden_dropout_prob
        A_							:       Tuple	      =				attention_probs_dropout_prob
        A_							:       List[str]	      =				max_position_embeddings
        A_							:       Optional[int]	      =				type_vocab_size
        A_							:       Any	      =				type_sequence_label_size
        A_							:       Tuple	      =				initializer_range
        A_							:       Tuple	      =				num_labels
        A_							:       int	      =				num_choices
        A_							:       Union[str, Any]	      =				scope
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[Any]	      =				ids_tensor([self.batch_size, self.seq_length] ,       self.vocab_size      )

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

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

        A_							:       int	      =				self.get_config()

        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Optional[int]	      =				ids_tensor([self.batch_size, self.seq_length] ,       self.vocab_size      )

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

        return config, input_ids, input_mask
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        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  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      ):

        """simple docstring"""

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

        """simple docstring"""

        A_							:       List[str]	      =				ErnieMForQuestionAnswering(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Union[str, Any]	      =				model(
            __magic_name__ ,       attention_mask=__magic_name__ ,       start_positions=__magic_name__ ,       end_positions=__magic_name__ ,       )
        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  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      ):

        """simple docstring"""

        A_							:       List[Any]	      =				ErnieMForInformationExtraction(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       int	      =				torch.ones_like(__magic_name__ ,       dtype=torch.floataa      )
        A_							:       Optional[int]	      =				model(
            __magic_name__ ,       attention_mask=__magic_name__ ,       start_positions=__magic_name__ ,       end_positions=__magic_name__ ,       )
        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  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      ):

        """simple docstring"""

        A_							:       Optional[Any]	      =				self.num_labels
        A_							:       Dict	      =				ErnieMForSequenceClassification(__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Optional[Any]	      =				model(__magic_name__ ,       attention_mask=__magic_name__ ,       labels=__magic_name__      )
        self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      ):

        """simple docstring"""

        A_							:       str	      =				self.num_labels
        A_							:       Dict	      =				ErnieMForTokenClassification(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        input_ids.to(__magic_name__      )
        input_mask.to(__magic_name__      )
        token_labels.to(__magic_name__      )

        A_							:       List[str]	      =				model(__magic_name__ ,       attention_mask=__magic_name__ ,       labels=__magic_name__      )

        self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.seq_length, self.num_labels)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      ):

        """simple docstring"""

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

        """simple docstring"""

        A_							:       Dict	      =				self.prepare_config_and_inputs()
        (
            (
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							
        )							:       List[Any]	      =				config_and_inputs
        A_							:       Optional[int]	      =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
        return config, inputs_dict






@require_torch
class     __UpperCAmelCase(    A__      ,	A__      ,	unittest.TestCase				):






 """simple docstring"""




 __magic_name__          =   (
     (
         ErnieMModel,
         ErnieMForMultipleChoice,
         ErnieMForQuestionAnswering,
         ErnieMForSequenceClassification,
         ErnieMForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 __magic_name__          =   ()
 __magic_name__          =   (
     {
         """feature-extraction""": ErnieMModel,
         """question-answering""": ErnieMForQuestionAnswering,
         """text-classification""": ErnieMForSequenceClassification,
         """token-classification""": ErnieMForTokenClassification,
         """zero-shot""": ErnieMForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __magic_name__          =   False
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__      ):

        """simple docstring"""

        if pipeline_test_casse_name == "QAPipelineTests":
               return True

        return False
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Optional[Any]	      =				ErnieMModelTester(self      )
        A_							:       Union[str, Any]	      =				ConfigTester(self ,       config_class=__magic_name__ ,       hidden_size=37      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        self.config_tester.run_common_tests()
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Tuple	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Any	      =				self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
               A_							:       List[str]	      =				type
               self.model_tester.create_and_check_model(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       int	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[Any]	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Optional[Any]	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       int	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_information_extraction(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[str]	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*__magic_name__      )
 @slow
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               A_							:       Tuple	      =				ErnieMModel.from_pretrained(__magic_name__      )
               self.assertIsNotNone(__magic_name__      )






@require_torch
class     __UpperCAmelCase(    unittest.TestCase				):






 """simple docstring"""




 @slow
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Union[str, Any]	      =				ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''      )
        model.eval()
        A_							:       Optional[int]	      =				torch.tensor([[0, 1, 2, 3, 4, 5]]      )
        A_							:       Union[str, Any]	      =				model(__magic_name__      )[0]

        # TODO Replace vocab size
        A_							:       Optional[int]	      =				768

        A_							:       Optional[int]	      =				torch.Size((1, 6, hidden_size)      )
        self.assertEqual(output.shape ,       __magic_name__      )

        A_							:       Optional[Any]	      =				torch.tensor(
            [[[-0.00_12, 0.12_45, -0.02_14], [-0.07_42, 0.02_44, -0.07_71], [-0.03_33, 0.11_64, -0.15_54]]]      )

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



import json
import os
import tempfile
import unittest

from transformers.modelcard import ModelCard






class     __UpperCAmelCase(    unittest.TestCase				):






 """simple docstring"""




 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Optional[int]	      =				{
            '''model_details''': {
                '''Organization''': '''testing''',
                '''Model date''': '''today''',
                '''Model version''': '''v2.1, Developed by Test Corp in 2019.''',
                '''Architecture''': '''Convolutional Neural Network.''',
            },
            '''metrics''': '''BLEU and ROUGE-1''',
            '''evaluation_data''': {
                '''Datasets''': {'''BLEU''': '''My-great-dataset-v1''', '''ROUGE-1''': '''My-short-dataset-v2.1'''},
                '''Preprocessing''': '''See details on https://arxiv.org/pdf/1810.03993.pdf''',
            },
            '''training_data''': {
                '''Dataset''': '''English Wikipedia dump dated 2018-12-01''',
                '''Preprocessing''': (
                    '''Using SentencePiece vocabulary of size 52k tokens. See details on'''
                    ''' https://arxiv.org/pdf/1810.03993.pdf'''
                ),
            },
            '''quantitative_analyses''': {'''BLEU''': 55.1, '''ROUGE-1''': 76},
        }
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Tuple	      =				ModelCard.from_dict(self.inputs_dict      )
        self.assertTrue(hasattr(__magic_name__ ,       '''model_details'''      )      )
        self.assertTrue(hasattr(__magic_name__ ,       '''intended_use'''      )      )
        self.assertTrue(hasattr(__magic_name__ ,       '''factors'''      )      )
        self.assertTrue(hasattr(__magic_name__ ,       '''metrics'''      )      )
        self.assertTrue(hasattr(__magic_name__ ,       '''evaluation_data'''      )      )
        self.assertTrue(hasattr(__magic_name__ ,       '''training_data'''      )      )
        self.assertTrue(hasattr(__magic_name__ ,       '''quantitative_analyses'''      )      )
        self.assertTrue(hasattr(__magic_name__ ,       '''ethical_considerations'''      )      )
        self.assertTrue(hasattr(__magic_name__ ,       '''caveats_and_recommendations'''      )      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Optional[Any]	      =				ModelCard.from_dict(self.inputs_dict      )
        A_							:       List[Any]	      =				json.loads(modelcard.to_json_string()      )
        for key, value in self.inputs_dict.items():
               self.assertEqual(obj[key] ,       __magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Dict	      =				ModelCard.from_dict(self.inputs_dict      )

        with tempfile.TemporaryDirectory() as tmpdirname:
               A_							:       str	      =				os.path.join(__magic_name__ ,       '''modelcard.json'''      )
               model_card_first.to_json_file(__magic_name__      )
               A_							:       str	      =				ModelCard.from_json_file(__magic_name__      )

        self.assertEqual(model_card_second.to_dict() ,       model_card_first.to_dict()      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[str]	      =				ModelCard.from_dict(self.inputs_dict      )

        with tempfile.TemporaryDirectory() as tmpdirname:
               model_card_first.save_pretrained(__magic_name__      )
               A_							:       Optional[int]	      =				ModelCard.from_pretrained(__magic_name__      )

        self.assertEqual(model_card_second.to_dict() ,       model_card_first.to_dict()      )


