


import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    DebertaForMaskedLM,
		    DebertaForQuestionAnswering,
		    DebertaForSequenceClassification,
		    DebertaForTokenClassification,
		    DebertaModel,
		)
		from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST



class 						_UpperCAmelCase      (     lowercase							):






		def __init__(						self				:     Dict   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     int=13   ,  UpperCAmelCase				:     Tuple=7   ,  UpperCAmelCase				:     Any=True   ,  UpperCAmelCase				:     List[Any]=True   ,  UpperCAmelCase				:     Tuple=True   ,  UpperCAmelCase				:     Any=True   ,  UpperCAmelCase				:     Optional[Any]=99   ,  UpperCAmelCase				:     Any=32   ,  UpperCAmelCase				:     int=5   ,  UpperCAmelCase				:     Any=4   ,  UpperCAmelCase				:     Union[str, Any]=37   ,  UpperCAmelCase				:     Optional[Any]="gelu"   ,  UpperCAmelCase				:     Tuple=0.1   ,  UpperCAmelCase				:     Union[str, Any]=0.1   ,  UpperCAmelCase				:     Any=5_12   ,  UpperCAmelCase				:     List[Any]=16   ,  UpperCAmelCase				:     Dict=2   ,  UpperCAmelCase				:     Tuple=0.02   ,  UpperCAmelCase				:     List[Any]=False   ,  UpperCAmelCase				:     Optional[Any]=True   ,  UpperCAmelCase				:     Any="None"   ,  UpperCAmelCase				:     Tuple=3   ,  UpperCAmelCase				:     Optional[int]=4   ,  UpperCAmelCase				:     List[Any]=None   ,  ):
				SCREAMING_SNAKE_CASE_				:Dict				     =						parent
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						batch_size
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						seq_length
				SCREAMING_SNAKE_CASE_				:Tuple				     =						is_training
				SCREAMING_SNAKE_CASE_				:int				     =						use_input_mask
				SCREAMING_SNAKE_CASE_				:Any				     =						use_token_type_ids
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						use_labels
				SCREAMING_SNAKE_CASE_				:str				     =						vocab_size
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						hidden_size
				SCREAMING_SNAKE_CASE_				:Dict				     =						num_hidden_layers
				SCREAMING_SNAKE_CASE_				:Tuple				     =						num_attention_heads
				SCREAMING_SNAKE_CASE_				:Any				     =						intermediate_size
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						hidden_act
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						hidden_dropout_prob
				SCREAMING_SNAKE_CASE_				:str				     =						attention_probs_dropout_prob
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						max_position_embeddings
				SCREAMING_SNAKE_CASE_				:str				     =						type_vocab_size
				SCREAMING_SNAKE_CASE_				:List[str]				     =						type_sequence_label_size
				SCREAMING_SNAKE_CASE_				:List[str]				     =						initializer_range
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						num_labels
				SCREAMING_SNAKE_CASE_				:Dict				     =						num_choices
				SCREAMING_SNAKE_CASE_				:Any				     =						relative_attention
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						position_biased_input
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						pos_att_type
				SCREAMING_SNAKE_CASE_				:int				     =						scope






		def _snake_case		(						self				:     Any):
				SCREAMING_SNAKE_CASE_				:Dict				     =						ids_tensor([self.batch_size, self.seq_length]   ,  self.vocab_size)

				SCREAMING_SNAKE_CASE_				:List[Any]				     =						None
				if self.use_input_mask:
						SCREAMING_SNAKE_CASE_				:List[Any]				     =						ids_tensor([self.batch_size, self.seq_length]   ,  vocab_size=2)

				SCREAMING_SNAKE_CASE_				:Dict				     =						None
				if self.use_token_type_ids:
						SCREAMING_SNAKE_CASE_				:Optional[int]				     =						ids_tensor([self.batch_size, self.seq_length]   ,  self.type_vocab_size)

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

				SCREAMING_SNAKE_CASE_				:Any				     =						self.get_config()

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






		def _snake_case		(						self				:     Union[str, Any]):
				return DebertaConfig(
				    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   ,  relative_attention=self.relative_attention   ,  position_biased_input=self.position_biased_input   ,  pos_att_type=self.pos_att_type   ,  )






		def _snake_case		(						self				:     Tuple):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						self.get_config()
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						3_00
				return config






		def _snake_case		(						self				:     str   ,  UpperCAmelCase				:     Union[str, Any]):
				self.parent.assertListEqual(list(result.loss.size())   ,  [])






		def _snake_case		(						self				:     Tuple   ,  UpperCAmelCase				:     Dict   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     Dict   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     List[Any]):
				SCREAMING_SNAKE_CASE_				:List[str]				     =						DebertaModel(config=UpperCAmelCase)
				model.to(UpperCAmelCase)
				model.eval()
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						model(UpperCAmelCase   ,  attention_mask=UpperCAmelCase   ,  token_type_ids=UpperCAmelCase)[0]
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						model(UpperCAmelCase   ,  token_type_ids=UpperCAmelCase)[0]
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						model(UpperCAmelCase)[0]

				self.parent.assertListEqual(list(sequence_output.size())   ,  [self.batch_size, self.seq_length, self.hidden_size])






		def _snake_case		(						self				:     List[str]   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     List[str]   ,  UpperCAmelCase				:     Optional[int]   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     List[Any]):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						DebertaForMaskedLM(config=UpperCAmelCase)
				model.to(UpperCAmelCase)
				model.eval()
				SCREAMING_SNAKE_CASE_				:int				     =						model(UpperCAmelCase   ,  attention_mask=UpperCAmelCase   ,  token_type_ids=UpperCAmelCase   ,  labels=UpperCAmelCase)

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






		def _snake_case		(						self				:     Tuple   ,  UpperCAmelCase				:     Dict   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     List[str]   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     List[Any]   ,  UpperCAmelCase				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:int				     =						self.num_labels
				SCREAMING_SNAKE_CASE_				:str				     =						DebertaForSequenceClassification(UpperCAmelCase)
				model.to(UpperCAmelCase)
				model.eval()
				SCREAMING_SNAKE_CASE_				:Tuple				     =						model(UpperCAmelCase   ,  attention_mask=UpperCAmelCase   ,  token_type_ids=UpperCAmelCase   ,  labels=UpperCAmelCase)
				self.parent.assertListEqual(list(result.logits.size())   ,  [self.batch_size, self.num_labels])
				self.check_loss_output(UpperCAmelCase)






		def _snake_case		(						self				:     List[str]   ,  UpperCAmelCase				:     Dict   ,  UpperCAmelCase				:     Dict   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     int   ,  UpperCAmelCase				:     Optional[int]   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     int):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						self.num_labels
				SCREAMING_SNAKE_CASE_				:List[str]				     =						DebertaForTokenClassification(config=UpperCAmelCase)
				model.to(UpperCAmelCase)
				model.eval()
				SCREAMING_SNAKE_CASE_				:Any				     =						model(UpperCAmelCase   ,  attention_mask=UpperCAmelCase   ,  token_type_ids=UpperCAmelCase   ,  labels=UpperCAmelCase)
				self.parent.assertEqual(result.logits.shape   ,  (self.batch_size, self.seq_length, self.num_labels))






		def _snake_case		(						self				:     Optional[Any]   ,  UpperCAmelCase				:     str   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     Tuple   ,  UpperCAmelCase				:     Dict   ,  UpperCAmelCase				:     Tuple):
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						DebertaForQuestionAnswering(config=UpperCAmelCase)
				model.to(UpperCAmelCase)
				model.eval()
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						model(
				    UpperCAmelCase   ,  attention_mask=UpperCAmelCase   ,  token_type_ids=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				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						self.prepare_config_and_inputs()
				(
				    (
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				(
				    SCREAMING_SNAKE_CASE_
				)     ,				
				)				:int				     =						config_and_inputs
				SCREAMING_SNAKE_CASE_				:Any				     =						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
				return config, inputs_dict




@require_torch
class 						_UpperCAmelCase      (     lowercase		,		lowercase		,		unittest.TestCase							):
		lowerCamelCase_	:			Dict       			=  (
		    (
		        DebertaModel,
		        DebertaForMaskedLM,
		        DebertaForSequenceClassification,
		        DebertaForTokenClassification,
		        DebertaForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		lowerCamelCase_	:			int       			=  (
		    {
		        """feature-extraction""": DebertaModel,
		        """fill-mask""": DebertaForMaskedLM,
		        """question-answering""": DebertaForQuestionAnswering,
		        """text-classification""": DebertaForSequenceClassification,
		        """token-classification""": DebertaForTokenClassification,
		        """zero-shot""": DebertaForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)

		lowerCamelCase_	:			Optional[int]       			=  True
		lowerCamelCase_	:			Any       			=  False
		lowerCamelCase_	:			Dict       			=  False
		lowerCamelCase_	:			Dict       			=  False
		lowerCamelCase_	:			Tuple       			=  False






		def _snake_case		(						self				:     Optional[int]):
				SCREAMING_SNAKE_CASE_				:Any				     =						DebertaModelTester(self)
				SCREAMING_SNAKE_CASE_				:Tuple				     =						ConfigTester(self   ,  config_class=UpperCAmelCase   ,  hidden_size=37)






		def _snake_case		(						self				:     Tuple):
				self.config_tester.run_common_tests()






		def _snake_case		(						self				:     str):
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_model(*UpperCAmelCase)






		def _snake_case		(						self				:     Optional[int]):
				SCREAMING_SNAKE_CASE_				:Dict				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCAmelCase)






		def _snake_case		(						self				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCAmelCase)






		def _snake_case		(						self				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_question_answering(*UpperCAmelCase)






		def _snake_case		(						self				:     Optional[Any]):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_token_classification(*UpperCAmelCase)







		@slow
		def _snake_case		(						self				:     Any):
				for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						SCREAMING_SNAKE_CASE_				:str				     =						DebertaModel.from_pretrained(UpperCAmelCase)
						self.assertIsNotNone(UpperCAmelCase)




@require_torch
@require_sentencepiece
@require_tokenizers
class 						_UpperCAmelCase      (     unittest.TestCase							):






		@unittest.skip(reason="Model not available yet")
		def _snake_case		(						self				:     Union[str, Any]):
				pass







		@slow
		def _snake_case		(						self				:     int):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						DebertaModel.from_pretrained("microsoft/deberta-base")

				SCREAMING_SNAKE_CASE_				:Dict				     =						torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]])
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
				with torch.no_grad():
						SCREAMING_SNAKE_CASE_				:int				     =						model(UpperCAmelCase   ,  attention_mask=UpperCAmelCase)[0]
				# compare the actual values for a slice.
				SCREAMING_SNAKE_CASE_				:str				     =						torch.tensor(
				    [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]])
				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]   ,  UpperCAmelCase   ,  atol=1E-4)   ,  F"{output[:, 1:4, 1:4]}")










from collections import Counter
from pathlib import Path
from typing import Optional, Tuple

import yaml



class 						_UpperCAmelCase      (     yaml.SafeLoader							):






		def _snake_case		(						self				:     Dict   ,  UpperCAmelCase				:     Union[str, Any]):
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						[self.constructed_objects[key_node] for key_node, _ in node.value]
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						[tuple(UpperCAmelCase) if isinstance(UpperCAmelCase   ,  UpperCAmelCase) else key for key in keys]
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						Counter(UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:str				     =						[key for key in counter if counter[key] > 1]
				if duplicate_keys:
						raise TypeError(F"Got duplicate yaml keys: {duplicate_keys}")







		def _snake_case		(						self				:     int   ,  UpperCAmelCase				:     Union[str, Any]   ,  UpperCAmelCase				:     Union[str, Any]=False):
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						super().construct_mapping(UpperCAmelCase   ,  deep=UpperCAmelCase)
				self._check_no_duplicates_on_constructed_node(UpperCAmelCase)
				return mapping




def     lowercase	(    a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Any				     =						list(readme_content.splitlines() )
		if full_content and full_content[0] == "---" and "---" in full_content[1:]:
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						full_content[1:].index("---" ) + 1
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						"\n".join(full_content[1:sep_idx] )
				return yamlblock, "\n".join(full_content[sep_idx + 1 :] )

		return None, "\n".join(a )



class 						_UpperCAmelCase      (     lowercase							):
		# class attributes
		lowerCamelCase_	:			List[Any]       			=  {"""train_eval_index"""}  # train-eval-index in the YAML metadata






		@classmethod
		def _snake_case		(						cls				:     Optional[int]   ,  UpperCAmelCase				:     Path):
				with open(UpperCAmelCase   ,  encoding="utf-8") as readme_file:
						SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						_split_yaml_from_readme(readme_file.read())
				if yaml_string is not None:
						return cls.from_yaml_string(UpperCAmelCase)
				else:
						return cls()






		def _snake_case		(						self				:     Dict   ,  UpperCAmelCase				:     Path):
				if path.exists():
						with open(UpperCAmelCase   ,  encoding="utf-8") as readme_file:
								SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						readme_file.read()
				else:
						SCREAMING_SNAKE_CASE_				:List[str]				     =						None
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						self._to_readme(UpperCAmelCase)
				with open(UpperCAmelCase   ,  "w"   ,  encoding="utf-8") as readme_file:
						readme_file.write(UpperCAmelCase)






		def _snake_case		(						self				:     Union[str, Any]   ,  UpperCAmelCase				:     Optional[str] = None):
				if readme_content is not None:
						SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:List[str]				     =						_split_yaml_from_readme(UpperCAmelCase)
						SCREAMING_SNAKE_CASE_				:int				     =						"---\n" + self.to_yaml_string() + "---\n" + content
				else:
						SCREAMING_SNAKE_CASE_				:Any				     =						"---\n" + self.to_yaml_string() + "---\n"
				return full_content






		@classmethod
		def _snake_case		(						cls				:     List[str]   ,  UpperCAmelCase				:     str):
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						yaml.load(UpperCAmelCase   ,  Loader=_NoDuplicateSafeLoader) or {}

				# Convert the YAML keys to DatasetMetadata fields
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						{
				    (key.replace("-"   ,  "_") if key.replace("-"   ,  "_") in cls._FIELDS_WITH_DASHES else key): value
				    for key, value in metadata_dict.items()
				}
				return cls(**UpperCAmelCase)







		def _snake_case		(						self				:     str):
				return yaml.safe_dump(
				    {
				        (key.replace("_"   ,  "-") if key in self._FIELDS_WITH_DASHES else key): value
				        for key, value in self.items()
				    }   ,  sort_keys=UpperCAmelCase   ,  allow_unicode=UpperCAmelCase   ,  encoding="utf-8"   ,  ).decode("utf-8")




SCREAMING_SNAKE_CASE__     					=			{
    "image-classification": [],
    "translation": [],
    "image-segmentation": [],
    "fill-mask": [],
    "automatic-speech-recognition": [],
    "token-classification": [],
    "sentence-similarity": [],
    "audio-classification": [],
    "question-answering": [],
    "summarization": [],
    "zero-shot-classification": [],
    "table-to-text": [],
    "feature-extraction": [],
    "other": [],
    "multiple-choice": [],
    "text-classification": [],
    "text-to-image": [],
    "text2text-generation": [],
    "zero-shot-image-classification": [],
    "tabular-classification": [],
    "tabular-regression": [],
    "image-to-image": [],
    "tabular-to-text": [],
    "unconditional-image-generation": [],
    "text-retrieval": [],
    "text-to-speech": [],
    "object-detection": [],
    "audio-to-audio": [],
    "text-generation": [],
    "conversational": [],
    "table-question-answering": [],
    "visual-question-answering": [],
    "image-to-text": [],
    "reinforcement-learning": [],
    "voice-activity-detection": [],
    "time-series-forecasting": [],
    "document-question-answering": [],
}


if __name__ == "__main__":
		from argparse import ArgumentParser

		SCREAMING_SNAKE_CASE__     					=			ArgumentParser(usage="Validate the yaml metadata block of a README.md file.")
		ap.add_argument("readme_filepath")
		SCREAMING_SNAKE_CASE__     					=			ap.parse_args()

		SCREAMING_SNAKE_CASE__     					=			Path(args.readme_filepath)
		SCREAMING_SNAKE_CASE__     					=			DatasetMetadata.from_readme(readme_filepath)
		print(dataset_metadata)
		dataset_metadata.to_readme(readme_filepath)






