




'''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   _snake_case:






 def __init__(self					:     Union[str, Any]						,		a					:     Any						,		a					:     Optional[Any]=13						,		a					:     List[str]=7						,		a					:     Union[str, Any]=True						,		a					:     Union[str, Any]=True						,		a					:     List[Any]=True						,		a					:     List[Any]=99						,		a					:     Optional[int]=32						,		a					:     List[str]=5						,		a					:     Dict=4						,		a					:     int=37						,		a					:     List[str]="gelu"						,		a					:     Optional[Any]=0.1						,		a					:     int=0.1						,		a					:     Any=5_12						,		a					:     int=16						,		a					:     Optional[Any]=2						,		a					:     Tuple=0.02						,		a					:     Any=3						,		a					:     Dict=4						,		a					:     Dict=None						,		)					->      str:






    """simple docstring"""


    A__         =    parent
    A__         =    batch_size
    A__         =    seq_length
    A__         =    is_training
    A__         =    use_input_mask
    A__         =    use_labels
    A__         =    vocab_size
    A__         =    hidden_size
    A__         =    num_hidden_layers
    A__         =    num_attention_heads
    A__         =    intermediate_size
    A__         =    hidden_act
    A__         =    hidden_dropout_prob
    A__         =    attention_probs_dropout_prob
    A__         =    max_position_embeddings
    A__         =    type_vocab_size
    A__         =    type_sequence_label_size
    A__         =    initializer_range
    A__         =    num_labels
    A__         =    num_choices
    A__         =    scope






 def 	_UpperCamelCase   (self					:     Any						)					->      str:






    """simple docstring"""


    A__         =    ids_tensor([self.batch_size, self.seq_length]						,		self.vocab_size						)

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

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

    A__         =    self.get_config()

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






 def 	_UpperCamelCase   (self					:     int						)					->      List[Any]:






    """simple docstring"""


    A__         =    ids_tensor([self.batch_size, self.seq_length]						,		self.vocab_size						)

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

    return config, input_ids, input_mask






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






    """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					:     Optional[int]						,		a					:     str						,		a					:     Optional[Any]						,		a					:     List[str]						,		a					:     Dict						,		a					:     Any						,		a					:     Optional[int]						)					->      Dict:






    """simple docstring"""


    A__         =    ErnieMModel(config=a						)
    model.to(a						)
    model.eval()
    A__         =    model(a						,		return_dict=a						)
    self.parent.assertEqual(result.last_hidden_state.shape						,		(self.batch_size, self.seq_length, self.hidden_size)						)






 def 	_UpperCamelCase   (self					:     Union[str, Any]						,		a					:     List[Any]						,		a					:     Optional[Any]						,		a					:     Dict						,		a					:     List[str]						,		a					:     Optional[Any]						,		a					:     List[str]						)					->      Optional[int]:






    """simple docstring"""


    A__         =    ErnieMForQuestionAnswering(config=a						)
    model.to(a						)
    model.eval()
    A__         =    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 	_UpperCamelCase   (self					:     Optional[Any]						,		a					:     Any						,		a					:     Tuple						,		a					:     Dict						,		a					:     Optional[Any]						,		a					:     Tuple						,		a					:     Optional[Any]						)					->      int:






    """simple docstring"""


    A__         =    ErnieMForInformationExtraction(config=a						)
    model.to(a						)
    model.eval()
    A__         =    torch.ones_like(a						,		dtype=torch.floataa						)
    A__         =    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 	_UpperCamelCase   (self					:     Any						,		a					:     List[str]						,		a					:     List[str]						,		a					:     List[str]						,		a					:     Tuple						,		a					:     Tuple						,		a					:     str						)					->      Any:






    """simple docstring"""


    A__         =    self.num_labels
    A__         =    ErnieMForSequenceClassification(a						)
    model.to(a						)
    model.eval()
    A__         =    model(a						,		attention_mask=a						,		labels=a						)
    self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_labels)						)






 def 	_UpperCamelCase   (self					:     List[Any]						,		a					:     Tuple						,		a					:     List[Any]						,		a					:     Union[str, Any]						,		a					:     int						,		a					:     Optional[Any]						,		a					:     List[Any]						)					->      str:






    """simple docstring"""


    A__         =    self.num_labels
    A__         =    ErnieMForTokenClassification(config=a						)
    model.to(a						)
    model.eval()
    input_ids.to(a						)
    input_mask.to(a						)
    token_labels.to(a						)

    A__         =    model(a						,		attention_mask=a						,		labels=a						)

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






 def 	_UpperCamelCase   (self					:     Optional[int]						,		a					:     int						,		a					:     List[Any]						,		a					:     int						,		a					:     Any						,		a					:     Dict						,		a					:     Union[str, Any]						)					->      Any:






    """simple docstring"""


    A__         =    self.num_choices
    A__         =    ErnieMForMultipleChoice(config=a						)
    model.to(a						)
    model.eval()
    A__         =    input_ids.unsqueeze(1						).expand(-1						,		self.num_choices						,		-1						).contiguous()
    A__         =    input_mask.unsqueeze(1						).expand(-1						,		self.num_choices						,		-1						).contiguous()
    A__         =    model(
        a						,		attention_mask=a						,		labels=a						,		)
    self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_choices)						)






 def 	_UpperCamelCase   (self					:     List[Any]						)					->      Optional[int]:






    """simple docstring"""


    A__         =    self.prepare_config_and_inputs()
    (
        (
        A__
    )     , (
        A__
    )     , (
        A__
    )     , (
        A__
    )     , (
        A__
    )     , (
        A__
    )     , 
    )         =    config_and_inputs
    A__         =    {'input_ids': input_ids, 'attention_mask': input_mask}
    return config, inputs_dict




@require_torch
class   _snake_case( UpperCAmelCase					,   UpperCAmelCase					,   unittest.TestCase			):
 __snake_case:       Dict									=				(
     (
         ErnieMModel,
         ErnieMForMultipleChoice,
         ErnieMForQuestionAnswering,
         ErnieMForSequenceClassification,
         ErnieMForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 __snake_case:       Dict									=				()
 __snake_case:       List[Any]									=				(
     {
         '''feature-extraction''': ErnieMModel,
         '''question-answering''': ErnieMForQuestionAnswering,
         '''text-classification''': ErnieMForSequenceClassification,
         '''token-classification''': ErnieMForTokenClassification,
         '''zero-shot''': ErnieMForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __snake_case:       Tuple									=				False






 def 	_UpperCamelCase   (self					:     Union[str, Any]						,		a					:     Tuple						,		a					:     Optional[Any]						,		a					:     List[Any]						,		a					:     Tuple						,		a					:     Tuple						)					->      Any:






    """simple docstring"""


    if pipeline_test_casse_name == "QAPipelineTests":
       return True

    return False






 def 	_UpperCamelCase   (self					:     List[Any]						)					->      Optional[int]:






    """simple docstring"""


    A__         =    ErnieMModelTester(self						)
    A__         =    ConfigTester(self						,		config_class=a						,		hidden_size=37						)






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






    """simple docstring"""


    self.config_tester.run_common_tests()






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






    """simple docstring"""


    A__         =    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*a						)






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






    """simple docstring"""


    A__         =    self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
       A__         =    type
       self.model_tester.create_and_check_model(*a						)






 def 	_UpperCamelCase   (self					:     Optional[Any]						)					->      Tuple:






    """simple docstring"""


    A__         =    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*a						)






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






    """simple docstring"""


    A__         =    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*a						)






 def 	_UpperCamelCase   (self					:     Any						)					->      Optional[int]:






    """simple docstring"""


    A__         =    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*a						)






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






    """simple docstring"""


    A__         =    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_information_extraction(*a						)






 def 	_UpperCamelCase   (self					:     int						)					->      List[Any]:






    """simple docstring"""


    A__         =    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*a						)






 @slow
 def 	_UpperCamelCase   (self					:     List[str]						)					->      Tuple:






    """simple docstring"""


    for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       A__         =    ErnieMModel.from_pretrained(a						)
       self.assertIsNotNone(a						)




@require_torch
class   _snake_case( unittest.TestCase			):






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






    """simple docstring"""


    A__         =    ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'						)
    model.eval()
    A__         =    torch.tensor([[0, 1, 2, 3, 4, 5]]						)
    A__         =    model(a						)[0]

    # TODO Replace vocab size
    A__         =    7_68

    A__         =    torch.Size((1, 6, hidden_size)						)
    self.assertEqual(output.shape						,		a						)

    A__         =    torch.tensor(
        [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]						)

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








'''simple docstring'''







def 	_A			(   UpperCAmelCase	):






   '''simple docstring'''







   if not isinstance(UpperCAmelCase			,UpperCAmelCase	) or not all(
       isinstance(UpperCAmelCase			,UpperCAmelCase	) and item >= 0 for item in citations	):
      raise ValueError('The citations should be a list of non negative integers.'	)

   citations.sort()
   A__         =    len(UpperCAmelCase	)

   for i in range(UpperCAmelCase	):
      if citations[len_citations - 1 - i] <= i:
         return i

   return len_citations


if __name__ == "__main__":
    import doctest

    doctest.testmod()


