

'''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,						lowercase_,						lowercase_=13,						lowercase_=7,						lowercase_=True,						lowercase_=True,						lowercase_=True,						lowercase_=99,						lowercase_=32,						lowercase_=5,						lowercase_=4,						lowercase_=37,						lowercase_="gelu",						lowercase_=0.1,						lowercase_=0.1,						lowercase_=512,						lowercase_=16,						lowercase_=2,						lowercase_=0.02,						lowercase_=3,						lowercase_=4,						lowercase_=None,						)    ->							Any:
					snake_case							=					parent
					snake_case							=					batch_size
					snake_case							=					seq_length
					snake_case							=					is_training
					snake_case							=					use_input_mask
					snake_case							=					use_labels
					snake_case							=					vocab_size
					snake_case							=					hidden_size
					snake_case							=					num_hidden_layers
					snake_case							=					num_attention_heads
					snake_case							=					intermediate_size
					snake_case							=					hidden_act
					snake_case							=					hidden_dropout_prob
					snake_case							=					attention_probs_dropout_prob
					snake_case							=					max_position_embeddings
					snake_case							=					type_vocab_size
					snake_case							=					type_sequence_label_size
					snake_case							=					initializer_range
					snake_case							=					num_labels
					snake_case							=					num_choices
					snake_case							=					scope

				def 					_lowerCamelCase    (       self )    ->							int:
					snake_case							=					ids_tensor([self.batch_size, self.seq_length],						self.vocab_size )

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

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

					snake_case							=					self.get_config()

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

				def 					_lowerCamelCase    (       self )    ->							Union[str, Any]:
					snake_case							=					ids_tensor([self.batch_size, self.seq_length],						self.vocab_size )

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

					return config, input_ids, input_mask

				def 					_lowerCamelCase    (       self )    ->							Dict:
					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,						lowercase_,						lowercase_,						lowercase_,						lowercase_,						lowercase_,						lowercase_ )    ->							List[str]:
					snake_case							=					ErnieMModel(config=lowercase_ )
					model.to(lowercase_ )
					model.eval()
					snake_case							=					model(lowercase_,						return_dict=lowercase_ )
					self.parent.assertEqual(result.last_hidden_state.shape,						(self.batch_size, self.seq_length, self.hidden_size) )

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

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_,						lowercase_,						lowercase_,						lowercase_,						lowercase_ )    ->							Optional[int]:
					snake_case							=					self.num_labels
					snake_case							=					ErnieMForTokenClassification(config=lowercase_ )
					model.to(lowercase_ )
					model.eval()
					input_ids.to(lowercase_ )
					input_mask.to(lowercase_ )
					token_labels.to(lowercase_ )

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

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

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_,						lowercase_,						lowercase_,						lowercase_,						lowercase_ )    ->							int:
					snake_case							=					self.num_choices
					snake_case							=					ErnieMForMultipleChoice(config=lowercase_ )
					model.to(lowercase_ )
					model.eval()
					snake_case							=					input_ids.unsqueeze(1 ).expand(-1,						self.num_choices,						-1 ).contiguous()
					snake_case							=					input_mask.unsqueeze(1 ).expand(-1,						self.num_choices,						-1 ).contiguous()
					snake_case							=					model(
					    lowercase_,						attention_mask=lowercase_,						labels=lowercase_,						)
					self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_choices) )






				def 					_lowerCamelCase    (       self )    ->							Tuple:
					snake_case							=					self.prepare_config_and_inputs()
					(
					    (
					    snake_case
					)			,			(
					    snake_case
					)			,			(
					    snake_case
					)			,			(
					    snake_case
					)			,			(
					    snake_case
					)			,			(
					    snake_case
					)			,			
					)							=					config_and_inputs
					snake_case							=					{'input_ids': input_ids, 'attention_mask': input_mask}
					return config, inputs_dict







@require_torch
class lowerCamelCase   (    __lowerCAmelCase				,				__lowerCAmelCase				,				unittest.TestCase				):
				snake_case_							     = (
				    (
				        ErnieMModel,
				        ErnieMForMultipleChoice,
				        ErnieMForQuestionAnswering,
				        ErnieMForSequenceClassification,
				        ErnieMForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				snake_case_							     = ()
				snake_case_							     = (
				    {
				        '''feature-extraction''': ErnieMModel,
				        '''question-answering''': ErnieMForQuestionAnswering,
				        '''text-classification''': ErnieMForSequenceClassification,
				        '''token-classification''': ErnieMForTokenClassification,
				        '''zero-shot''': ErnieMForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				snake_case_							     = False

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_,						lowercase_,						lowercase_,						lowercase_ )    ->							str:
					if pipeline_test_casse_name == "QAPipelineTests":
						return True

					return False

				def 					_lowerCamelCase    (       self )    ->							List[str]:
					snake_case							=					ErnieMModelTester(self )
					snake_case							=					ConfigTester(self,						config_class=lowercase_,						hidden_size=37 )

				def 					_lowerCamelCase    (       self )    ->							str:
					self.config_tester.run_common_tests()

				def 					_lowerCamelCase    (       self )    ->							Union[str, Any]:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							str:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
						snake_case							=					type
						self.model_tester.create_and_check_model(*lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							str:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							Optional[Any]:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							Optional[int]:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							Optional[Any]:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_information_extraction(*lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							int:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*lowercase_ )






				@slow
				def 					_lowerCamelCase    (       self )    ->							Dict:
					for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						snake_case							=					ErnieMModel.from_pretrained(lowercase_ )
						self.assertIsNotNone(lowercase_ )







@require_torch
class lowerCamelCase   (    unittest.TestCase				):

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

					# TODO Replace vocab size
					snake_case							=					768

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

					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],						lowercase_,						atol=1E-3 ) )













"""simple docstring"""





import doctest

import projectq
from projectq.ops import H, Measure






def 							A(    snake_case_						):
       """simple docstring"""





       lowercase__:							Dict         =     quantum_engine.allocate_qubit()
       H | qubit
       Measure | qubit
       return int(snake_case_						)


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

  # initialises a new quantum backend
  UpperCamelCase					=     projectq.MainEngine()

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

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

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






