



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





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



       '''simple docstring'''







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





    def 					__snake_case       (    self    :    Dict     ):



       '''simple docstring'''







       snake_case   :							Optional[Any]						  =ids_tensor([self.batch_size, self.seq_length],    self.vocab_size     )

       snake_case   :							Optional[int]						  =None
       if self.use_input_mask:
          snake_case   :							List[str]						  =random_attention_mask([self.batch_size, self.seq_length]     )

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

       snake_case   :							Any						  =self.get_config()

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





    def 					__snake_case       (    self    :    int     ):



       '''simple docstring'''







       snake_case   :							Tuple						  =ids_tensor([self.batch_size, self.seq_length],    self.vocab_size     )

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

       return config, input_ids, input_mask





    def 					__snake_case       (    self    :    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 					__snake_case       (    self    :    str,    _snake_case    :    Dict,    _snake_case    :    Optional[int],    _snake_case    :    List[Any],    _snake_case    :    Dict,    _snake_case    :    Optional[int],    _snake_case    :    Optional[int]     ):



       '''simple docstring'''







       snake_case   :							List[Any]						  =ErnieMModel(config=UpperCamelCase__     )
       model.to(UpperCamelCase__     )
       model.eval()
       snake_case   :							Optional[int]						  =model(UpperCamelCase__,    return_dict=UpperCamelCase__     )
       self.parent.assertEqual(result.last_hidden_state.shape,    (self.batch_size, self.seq_length, self.hidden_size)     )





    def 					__snake_case       (    self    :    Any,    _snake_case    :    Tuple,    _snake_case    :    Union[str, Any],    _snake_case    :    int,    _snake_case    :    Union[str, Any],    _snake_case    :    Any,    _snake_case    :    Optional[Any]     ):



       '''simple docstring'''







       snake_case   :							Optional[int]						  =ErnieMForQuestionAnswering(config=UpperCamelCase__     )
       model.to(UpperCamelCase__     )
       model.eval()
       snake_case   :							Optional[int]						  =model(
           UpperCamelCase__,    attention_mask=UpperCamelCase__,    start_positions=UpperCamelCase__,    end_positions=UpperCamelCase__,    )
       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 					__snake_case       (    self    :    str,    _snake_case    :    Optional[int],    _snake_case    :    int,    _snake_case    :    str,    _snake_case    :    List[str],    _snake_case    :    Optional[int],    _snake_case    :    Optional[Any]     ):



       '''simple docstring'''







       snake_case   :							Optional[int]						  =ErnieMForInformationExtraction(config=UpperCamelCase__     )
       model.to(UpperCamelCase__     )
       model.eval()
       snake_case   :							List[str]						  =torch.ones_like(UpperCamelCase__,    dtype=torch.floataa     )
       snake_case   :							int						  =model(
           UpperCamelCase__,    attention_mask=UpperCamelCase__,    start_positions=UpperCamelCase__,    end_positions=UpperCamelCase__,    )
       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 					__snake_case       (    self    :    Optional[Any],    _snake_case    :    Union[str, Any],    _snake_case    :    List[str],    _snake_case    :    Any,    _snake_case    :    Union[str, Any],    _snake_case    :    Dict,    _snake_case    :    Tuple     ):



       '''simple docstring'''







       snake_case   :							List[str]						  =self.num_labels
       snake_case   :							str						  =ErnieMForSequenceClassification(UpperCamelCase__     )
       model.to(UpperCamelCase__     )
       model.eval()
       snake_case   :							Union[str, Any]						  =model(UpperCamelCase__,    attention_mask=UpperCamelCase__,    labels=UpperCamelCase__     )
       self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.num_labels)     )





    def 					__snake_case       (    self    :    Union[str, Any],    _snake_case    :    Tuple,    _snake_case    :    int,    _snake_case    :    int,    _snake_case    :    Optional[Any],    _snake_case    :    Optional[Any],    _snake_case    :    str     ):



       '''simple docstring'''







       snake_case   :							List[Any]						  =self.num_labels
       snake_case   :							str						  =ErnieMForTokenClassification(config=UpperCamelCase__     )
       model.to(UpperCamelCase__     )
       model.eval()
       input_ids.to(UpperCamelCase__     )
       input_mask.to(UpperCamelCase__     )
       token_labels.to(UpperCamelCase__     )

       snake_case   :							Dict						  =model(UpperCamelCase__,    attention_mask=UpperCamelCase__,    labels=UpperCamelCase__     )

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





    def 					__snake_case       (    self    :    str,    _snake_case    :    List[str],    _snake_case    :    Union[str, Any],    _snake_case    :    Optional[Any],    _snake_case    :    Tuple,    _snake_case    :    List[str],    _snake_case    :    Tuple     ):



       '''simple docstring'''







       snake_case   :							Tuple						  =self.num_choices
       snake_case   :							Optional[Any]						  =ErnieMForMultipleChoice(config=UpperCamelCase__     )
       model.to(UpperCamelCase__     )
       model.eval()
       snake_case   :							Optional[Any]						  =input_ids.unsqueeze(1     ).expand(-1,    self.num_choices,    -1     ).contiguous()
       snake_case   :							Optional[int]						  =input_mask.unsqueeze(1     ).expand(-1,    self.num_choices,    -1     ).contiguous()
       snake_case   :							int						  =model(
           UpperCamelCase__,    attention_mask=UpperCamelCase__,    labels=UpperCamelCase__,    )
       self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.num_choices)     )






    def 					__snake_case       (    self    :    str     ):



       '''simple docstring'''







       snake_case   :							Optional[int]						  =self.prepare_config_and_inputs()
       (
           snake_case				
       )   :							int						  =config_and_inputs
       snake_case   :							str						  ={"input_ids": input_ids, "attention_mask": input_mask}
       return config, inputs_dict





@require_torch
class 							lowerCAmelCase_		(	lowerCAmelCase__  ,      lowerCAmelCase__  ,      unittest.TestCase						):
    __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 					__snake_case       (    self    :    List[str],    _snake_case    :    Optional[Any],    _snake_case    :    Dict,    _snake_case    :    List[str],    _snake_case    :    Tuple,    _snake_case    :    Any     ):



       '''simple docstring'''







       if pipeline_test_casse_name == "QAPipelineTests":
          return True

       return False





    def 					__snake_case       (    self    :    str     ):



       '''simple docstring'''







       snake_case   :							Tuple						  =ErnieMModelTester(self     )
       snake_case   :							List[Any]						  =ConfigTester(self,    config_class=UpperCamelCase__,    hidden_size=37     )





    def 					__snake_case       (    self    :    List[str]     ):



       '''simple docstring'''







       self.config_tester.run_common_tests()





    def 					__snake_case       (    self    :    Dict     ):



       '''simple docstring'''







       snake_case   :							Optional[Any]						  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCamelCase__     )





    def 					__snake_case       (    self    :    Optional[Any]     ):



       '''simple docstring'''







       snake_case   :							List[Any]						  =self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
          snake_case   :							int						  =type
          self.model_tester.create_and_check_model(*UpperCamelCase__     )





    def 					__snake_case       (    self    :    Any     ):



       '''simple docstring'''







       snake_case   :							int						  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__     )





    def 					__snake_case       (    self    :    Any     ):



       '''simple docstring'''







       snake_case   :							Optional[Any]						  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__     )





    def 					__snake_case       (    self    :    List[Any]     ):



       '''simple docstring'''







       snake_case   :							Tuple						  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__     )





    def 					__snake_case       (    self    :    Union[str, Any]     ):



       '''simple docstring'''







       snake_case   :							Union[str, Any]						  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_information_extraction(*UpperCamelCase__     )





    def 					__snake_case       (    self    :    Dict     ):



       '''simple docstring'''







       snake_case   :							Any						  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__     )






    @slow
    def 					__snake_case       (    self    :    Tuple     ):



       '''simple docstring'''







       for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          snake_case   :							Union[str, Any]						  =ErnieMModel.from_pretrained(UpperCamelCase__     )
          self.assertIsNotNone(UpperCamelCase__     )





@require_torch
class 							lowerCAmelCase_		(	unittest.TestCase						):





    @slow
    def 					__snake_case       (    self    :    Union[str, Any]     ):



       '''simple docstring'''







       snake_case   :							Tuple						  =ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''     )
       model.eval()
       snake_case   :							Optional[Any]						  =torch.tensor([[0, 1, 2, 3, 4, 5]]     )
       snake_case   :							Dict						  =model(UpperCamelCase__     )[0]

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

       snake_case   :							Optional[Any]						  =torch.Size((1, 6, hidden_size)     )
       self.assertEqual(output.shape,    UpperCamelCase__     )

       snake_case   :							Dict						  =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],    UpperCamelCase__,    atol=1E-3     )     )






'''simple docstring'''





import doctest

import projectq
from projectq.ops import H, Measure
def 		_a   (  lowerCamelCase_							):
   snake_case   :							str						  =quantum_engine.allocate_qubit()
   H | qubit
   Measure | qubit
   return int(lowerCamelCase_							)


if __name__ == "__main__":
      doctest.testmod()

      # initialises a new quantum backend
      A			:   Dict			=      projectq.MainEngine()

      # Generate a list of 10 random numbers
      A			:   Dict			=      [get_random_number(quantum_engine) for _ in range(10)]

      # Flushes the quantum engine from memory
      quantum_engine.flush()

      print("""Random numbers""", random_numbers_list)





