





'''simple docstring'''

import os
import tempfile
import unittest

from transformers import NezhaConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       MODEL_FOR_PRETRAINING_MAPPING,
       NezhaForMaskedLM,
       NezhaForMultipleChoice,
       NezhaForNextSentencePrediction,
       NezhaForPreTraining,
       NezhaForQuestionAnswering,
       NezhaForSequenceClassification,
       NezhaForTokenClassification,
       NezhaModel,
   )
   from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST

class       UpperCAmelCase :




  def __init__(self      :	str							,			A__      :	int							,			A__      :	Optional[int]=1_3							,			A__      :	int=7							,			A__      :	Optional[int]=True							,			A__      :	int=True							,			A__      :	Union[str, Any]=True							,			A__      :	List[str]=True							,			A__      :	int=9_9							,			A__      :	Dict=3_2							,			A__      :	Any=5							,			A__      :	Optional[Any]=4							,			A__      :	Tuple=3_7							,			A__      :	Tuple="gelu"							,			A__      :	Optional[Any]=0.1							,			A__      :	List[Any]=0.1							,			A__      :	Tuple=1_2_8							,			A__      :	Union[str, Any]=3_2							,			A__      :	Any=1_6							,			A__      :	Any=2							,			A__      :	Dict=0.0_2							,			A__      :	str=3							,			A__      :	Any=4							,			A__      :	int=None							,			)					-> str:
      lowercase         = parent
      lowercase         = batch_size
      lowercase         = seq_length
      lowercase         = is_training
      lowercase         = use_input_mask
      lowercase         = use_token_type_ids
      lowercase         = use_labels
      lowercase         = vocab_size
      lowercase         = hidden_size
      lowercase         = num_hidden_layers
      lowercase         = num_attention_heads
      lowercase         = intermediate_size
      lowercase         = hidden_act
      lowercase         = hidden_dropout_prob
      lowercase         = attention_probs_dropout_prob
      lowercase         = max_position_embeddings
      lowercase         = type_vocab_size
      lowercase         = type_sequence_label_size
      lowercase         = initializer_range
      lowercase         = num_labels
      lowercase         = num_choices
      lowercase         = scope




  def 				UpperCAmelCase__   (self      :	Dict				)					-> Dict:
      lowercase         = ids_tensor([self.batch_size, self.seq_length]							,			self.vocab_size				)

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

      lowercase         = None
      if self.use_token_type_ids:
          lowercase         = ids_tensor([self.batch_size, self.seq_length]							,			self.type_vocab_size				)

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

      lowercase         = self.get_config()

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




  def 				UpperCAmelCase__   (self      :	Any				)					-> Optional[int]:
      return NezhaConfig(
          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							,			is_decoder=A__							,			initializer_range=self.initializer_range							,			)




  def 				UpperCAmelCase__   (self      :	Any				)					-> Union[str, Any]:
      (
          (
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,														
      )         = self.prepare_config_and_inputs()

      lowercase         = True
      lowercase         = floats_tensor([self.batch_size, self.seq_length, self.hidden_size]				)
      lowercase         = ids_tensor([self.batch_size, self.seq_length]							,			vocab_size=2				)

      return (
          config,
          input_ids,
          token_type_ids,
          input_mask,
          sequence_labels,
          token_labels,
          choice_labels,
          encoder_hidden_states,
          encoder_attention_mask,
      )




  def 				UpperCAmelCase__   (self      :	Optional[int]							,			A__      :	Dict							,			A__      :	int							,			A__      :	Tuple							,			A__      :	Tuple							,			A__      :	List[Any]							,			A__      :	Union[str, Any]							,			A__      :	Dict				)					-> Tuple:
      lowercase         = NezhaModel(config=A__				)
      model.to(A__				)
      model.eval()
      lowercase         = model(A__							,			attention_mask=A__							,			token_type_ids=A__				)
      lowercase         = model(A__							,			token_type_ids=A__				)
      lowercase         = model(A__				)
      self.parent.assertEqual(result.last_hidden_state.shape							,			(self.batch_size, self.seq_length, self.hidden_size)				)
      self.parent.assertEqual(result.pooler_output.shape							,			(self.batch_size, self.hidden_size)				)




  def 				UpperCAmelCase__   (self      :	List[str]							,			A__      :	int							,			A__      :	int							,			A__      :	Optional[int]							,			A__      :	Any							,			A__      :	Any							,			A__      :	Optional[int]							,			A__      :	str							,			A__      :	Dict							,			A__      :	Tuple							,			)					-> Any:
      lowercase         = True
      lowercase         = NezhaModel(A__				)
      model.to(A__				)
      model.eval()
      lowercase         = model(
          A__							,			attention_mask=A__							,			token_type_ids=A__							,			encoder_hidden_states=A__							,			encoder_attention_mask=A__							,			)
      lowercase         = model(
          A__							,			attention_mask=A__							,			token_type_ids=A__							,			encoder_hidden_states=A__							,			)
      lowercase         = model(A__							,			attention_mask=A__							,			token_type_ids=A__				)
      self.parent.assertEqual(result.last_hidden_state.shape							,			(self.batch_size, self.seq_length, self.hidden_size)				)
      self.parent.assertEqual(result.pooler_output.shape							,			(self.batch_size, self.hidden_size)				)




  def 				UpperCAmelCase__   (self      :	Dict							,			A__      :	Union[str, Any]							,			A__      :	str							,			A__      :	Optional[int]							,			A__      :	Any							,			A__      :	Any							,			A__      :	Any							,			A__      :	List[str]				)					-> Optional[int]:
      lowercase         = NezhaForMaskedLM(config=A__				)
      model.to(A__				)
      model.eval()
      lowercase         = model(A__							,			attention_mask=A__							,			token_type_ids=A__							,			labels=A__				)
      self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length, self.vocab_size)				)




  def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	Any							,			A__      :	Union[str, Any]							,			A__      :	Optional[int]							,			A__      :	Optional[int]							,			A__      :	List[Any]							,			A__      :	Tuple							,			A__      :	List[Any]				)					-> Optional[int]:
      lowercase         = NezhaForNextSentencePrediction(config=A__				)
      model.to(A__				)
      model.eval()
      lowercase         = model(
          A__							,			attention_mask=A__							,			token_type_ids=A__							,			labels=A__							,			)
      self.parent.assertEqual(result.logits.shape							,			(self.batch_size, 2)				)




  def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	Dict							,			A__      :	Dict							,			A__      :	Any							,			A__      :	str							,			A__      :	int							,			A__      :	Optional[Any]							,			A__      :	str				)					-> Optional[int]:
      lowercase         = NezhaForPreTraining(config=A__				)
      model.to(A__				)
      model.eval()
      lowercase         = model(
          A__							,			attention_mask=A__							,			token_type_ids=A__							,			labels=A__							,			next_sentence_label=A__							,			)
      self.parent.assertEqual(result.prediction_logits.shape							,			(self.batch_size, self.seq_length, self.vocab_size)				)
      self.parent.assertEqual(result.seq_relationship_logits.shape							,			(self.batch_size, 2)				)




  def 				UpperCAmelCase__   (self      :	int							,			A__      :	List[str]							,			A__      :	Optional[Any]							,			A__      :	Union[str, Any]							,			A__      :	Tuple							,			A__      :	Tuple							,			A__      :	Union[str, Any]							,			A__      :	Union[str, Any]				)					-> List[str]:
      lowercase         = NezhaForQuestionAnswering(config=A__				)
      model.to(A__				)
      model.eval()
      lowercase         = model(
          A__							,			attention_mask=A__							,			token_type_ids=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      :	Tuple							,			A__      :	Union[str, Any]							,			A__      :	List[str]							,			A__      :	Any							,			A__      :	Dict							,			A__      :	Optional[int]							,			A__      :	str							,			A__      :	List[str]				)					-> Union[str, Any]:
      lowercase         = self.num_labels
      lowercase         = NezhaForSequenceClassification(A__				)
      model.to(A__				)
      model.eval()
      lowercase         = model(A__							,			attention_mask=A__							,			token_type_ids=A__							,			labels=A__				)
      self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_labels)				)




  def 				UpperCAmelCase__   (self      :	List[Any]							,			A__      :	Tuple							,			A__      :	str							,			A__      :	Optional[int]							,			A__      :	Optional[int]							,			A__      :	int							,			A__      :	Any							,			A__      :	Optional[Any]				)					-> Optional[Any]:
      lowercase         = self.num_labels
      lowercase         = NezhaForTokenClassification(config=A__				)
      model.to(A__				)
      model.eval()
      lowercase         = model(A__							,			attention_mask=A__							,			token_type_ids=A__							,			labels=A__				)
      self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length, self.num_labels)				)




  def 				UpperCAmelCase__   (self      :	Dict							,			A__      :	List[Any]							,			A__      :	int							,			A__      :	List[Any]							,			A__      :	Tuple							,			A__      :	List[Any]							,			A__      :	Optional[Any]							,			A__      :	List[str]				)					-> str:
      lowercase         = self.num_choices
      lowercase         = NezhaForMultipleChoice(config=A__				)
      model.to(A__				)
      model.eval()
      lowercase         = input_ids.unsqueeze(1				).expand(-1							,			self.num_choices							,			-1				).contiguous()
      lowercase         = token_type_ids.unsqueeze(1				).expand(-1							,			self.num_choices							,			-1				).contiguous()
      lowercase         = input_mask.unsqueeze(1				).expand(-1							,			self.num_choices							,			-1				).contiguous()
      lowercase         = model(
          A__							,			attention_mask=A__							,			token_type_ids=A__							,			labels=A__							,			)
      self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_choices)				)





  def 				UpperCAmelCase__   (self      :	Any				)					-> int:
      lowercase         = self.prepare_config_and_inputs()
      (
          (
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,														
      )         = config_and_inputs
      lowercase         = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
      return config, inputs_dict




@require_torch
class       UpperCAmelCase (						_lowercase			,       _lowercase			,       _lowercase			,       unittest.TestCase  ):
  UpperCAmelCase					:	Union[str, Any] 			=		(
      (
          NezhaModel,
          NezhaForMaskedLM,
          NezhaForMultipleChoice,
          NezhaForNextSentencePrediction,
          NezhaForPreTraining,
          NezhaForQuestionAnswering,
          NezhaForSequenceClassification,
          NezhaForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  UpperCAmelCase					:	Dict 			=		(
      {
          '''feature-extraction''': NezhaModel,
          '''fill-mask''': NezhaForMaskedLM,
          '''question-answering''': NezhaForQuestionAnswering,
          '''text-classification''': NezhaForSequenceClassification,
          '''token-classification''': NezhaForTokenClassification,
          '''zero-shot''': NezhaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  UpperCAmelCase					:	int 			=		True




  def 				UpperCAmelCase__   (self      :	str							,			A__      :	Tuple							,			A__      :	Union[str, Any]							,			A__      :	Union[str, Any]=False				)					-> Any:
      lowercase         = super()._prepare_for_class(A__							,			A__							,			return_labels=A__				)

      if return_labels:
          if model_class in get_values(A__				):
              lowercase         = torch.zeros(
                  (self.model_tester.batch_size, self.model_tester.seq_length)							,			dtype=torch.long							,			device=A__				)
              lowercase         = torch.zeros(
                  self.model_tester.batch_size							,			dtype=torch.long							,			device=A__				)
      return inputs_dict




  def 				UpperCAmelCase__   (self      :	int				)					-> List[str]:
      lowercase         = NezhaModelTester(self				)
      lowercase         = ConfigTester(self							,			config_class=A__							,			hidden_size=3_7				)




  def 				UpperCAmelCase__   (self      :	Union[str, Any]				)					-> List[str]:
      self.config_tester.run_common_tests()




  def 				UpperCAmelCase__   (self      :	List[Any]				)					-> Any:
      lowercase         = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A__				)




  def 				UpperCAmelCase__   (self      :	str				)					-> List[Any]:
      lowercase         = self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_model_as_decoder(*A__				)




  def 				UpperCAmelCase__   (self      :	int				)					-> List[Any]:
      # This regression test was failing with PyTorch < 1.3
      (
          (
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,							(
          lowercase							
      )   ,														
      )         = self.model_tester.prepare_config_and_inputs_for_decoder()

      lowercase         = None

      self.model_tester.create_and_check_model_as_decoder(
          A__							,			A__							,			A__							,			A__							,			A__							,			A__							,			A__							,			A__							,			A__							,			)




  def 				UpperCAmelCase__   (self      :	Dict				)					-> Optional[int]:
      lowercase         = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*A__				)




  def 				UpperCAmelCase__   (self      :	Optional[Any]				)					-> Any:
      lowercase         = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*A__				)




  def 				UpperCAmelCase__   (self      :	Union[str, Any]				)					-> str:
      lowercase         = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_next_sequence_prediction(*A__				)




  def 				UpperCAmelCase__   (self      :	Optional[int]				)					-> List[Any]:
      lowercase         = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*A__				)




  def 				UpperCAmelCase__   (self      :	int				)					-> Tuple:
      lowercase         = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*A__				)




  def 				UpperCAmelCase__   (self      :	List[Any]				)					-> Union[str, Any]:
      lowercase         = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*A__				)




  def 				UpperCAmelCase__   (self      :	Any				)					-> Tuple:
      lowercase         = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*A__				)




  @slow
  def 				UpperCAmelCase__   (self      :	Dict				)					-> Tuple:
      for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowercase         = NezhaModel.from_pretrained(A__				)
          self.assertIsNotNone(A__				)





  @slow
  @require_torch_gpu
  def 				UpperCAmelCase__   (self      :	Union[str, Any]				)					-> List[Any]:
      lowercase   ,							lowercase         = self.model_tester.prepare_config_and_inputs_for_common()
      for model_class in self.all_model_classes:
          # NezhaForMultipleChoice behaves incorrectly in JIT environments.
          if model_class == NezhaForMultipleChoice:
              return

          lowercase         = True
          lowercase         = model_class(config=A__				)

          lowercase         = self._prepare_for_class(A__							,			A__				)
          lowercase         = torch.jit.trace(
              A__							,			(inputs_dict["input_ids"].to("cpu"				), inputs_dict["attention_mask"].to("cpu"				))				)

          with tempfile.TemporaryDirectory() as tmp:
              torch.jit.save(A__							,			os.path.join(A__							,			"bert.pt"				)				)
              lowercase         = torch.jit.load(os.path.join(A__							,			"bert.pt"				)							,			map_location=A__				)
              loaded(inputs_dict["input_ids"].to(A__				)							,			inputs_dict["attention_mask"].to(A__				)				)




@require_torch
class       UpperCAmelCase (						unittest.TestCase  ):




  @slow
  def 				UpperCAmelCase__   (self      :	Union[str, Any]				)					-> List[Any]:
      lowercase         = NezhaModel.from_pretrained("sijunhe/nezha-cn-base"				)
      lowercase         = torch.tensor([[0, 1, 2, 3, 4, 5]]				)
      lowercase         = torch.tensor([[0, 1, 1, 1, 1, 1]]				)
      with torch.no_grad():
          lowercase         = model(A__							,			attention_mask=A__				)[0]
      lowercase         = torch.Size((1, 6, 7_6_8)				)
      self.assertEqual(output.shape							,			A__				)
      lowercase         = torch.tensor([[[0.0_6_8_5, 0.2_4_4_1, 0.1_1_0_2], [0.0_6_0_0, 0.1_9_0_6, 0.1_3_4_9], [0.0_2_2_1, 0.0_8_1_9, 0.0_5_8_6]]]				)

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





  @slow
  def 				UpperCAmelCase__   (self      :	Union[str, Any]				)					-> int:
      lowercase         = NezhaForMaskedLM.from_pretrained("sijunhe/nezha-cn-base"				)
      lowercase         = torch.tensor([[0, 1, 2, 3, 4, 5]]				)
      lowercase         = torch.tensor([[1, 1, 1, 1, 1, 1]]				)
      with torch.no_grad():
          lowercase         = model(A__							,			attention_mask=A__				)[0]
      lowercase         = torch.Size((1, 6, 2_1_1_2_8)				)
      self.assertEqual(output.shape							,			A__				)
      lowercase         = torch.tensor(
          [[-2.7_9_3_9, -1.7_9_0_2, -2.2_1_8_9], [-2.8_5_8_5, -1.8_9_0_8, -2.3_7_2_3], [-2.6_4_9_9, -1.7_7_5_0, -2.2_5_5_8]]				)

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











'''simple docstring'''

import datasets

from .evaluate import evaluate


__lowerCamelCase : Optional[Any]											=			"\\n@inproceedings{Rajpurkar2016SQuAD10,\n  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n  booktitle={EMNLP},\n  year={2016}\n}\n"

__lowerCamelCase : Any											=			"\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n"

__lowerCamelCase : Union[str, Any]											=			"\nComputes SQuAD scores (F1 and EM).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair as given in the references (see below)\n        - 'prediction_text': the text of the answer\n    references: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair (see above),\n        - 'answers': a Dict in the SQuAD dataset format\n            {\n                'text': list of possible texts for the answer, as a list of strings\n                'answer_start': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    'exact_match': Exact match (the normalized answer exactly match the gold answer)\n    'f1': The F-score of predicted tokens versus the gold answer\nExamples:\n\n    >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]\n    >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]\n    >>> squad_metric = datasets.load_metric(\"squad\")\n    >>> results = squad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 100.0, 'f1': 100.0}\n"

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION			,       _KWARGS_DESCRIPTION  )
class       UpperCAmelCase (						datasets.Metric  ):




		def 				UpperCAmelCase__   (self      :	Union[str, Any]				)					-> Union[str, Any]:
						return datasets.MetricInfo(
						    description=_DESCRIPTION							,			citation=_CITATION							,			inputs_description=_KWARGS_DESCRIPTION							,			features=datasets.Features(
						        {
						            "predictions": {"id": datasets.Value("string"				), "prediction_text": datasets.Value("string"				)},
						            "references": {
						                "id": datasets.Value("string"				),
						                "answers": datasets.features.Sequence(
						                    {
						                        "text": datasets.Value("string"				),
						                        "answer_start": datasets.Value("int32"				),
						                    }				),
						            },
						        }				)							,			codebase_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]							,			reference_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]							,			)





		def 				UpperCAmelCase__   (self      :	List[str]							,			A__      :	List[Any]							,			A__      :	List[str]				)					-> str:
						lowercase         = {prediction["id"]: prediction["prediction_text"] for prediction in predictions}
						lowercase         = [
						    {
						        "paragraphs": [
						            {
						                "qas": [
						                    {
						                        "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
						                        "id": ref["id"],
						                    }
						                    for ref in references
						                ]
						            }
						        ]
						    }
						]
						lowercase         = evaluate(dataset=A__							,			predictions=A__				)
						return score




