


"""simple docstring"""







import unittest

from transformers import DebertaVaConfig, 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 (
		    DebertaVaForMaskedLM,
		    DebertaVaForMultipleChoice,
		    DebertaVaForQuestionAnswering,
		    DebertaVaForSequenceClassification,
		    DebertaVaForTokenClassification,
		    DebertaVaModel,
		)
		from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST




class  UpperCamelCase_	( UpperCamelCase):

	"""simple docstring"""





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




	def   UpperCAmelCase_      ( self			:				List[str]   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size   )

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

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

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

						__SCREAMING_SNAKE_CASE						   =				self.get_config()

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




	def   UpperCAmelCase_      ( self			:				List[str]   )       ->	Optional[int]:
						return DebertaVaConfig(
						    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   UpperCAmelCase_      ( self			:				Dict			,			UpperCAmelCase__			:				Dict   )       ->	List[Any]:
						self.parent.assertListEqual(list(result.loss.size()   )			,			[]   )




	def   UpperCAmelCase_      ( self			:				str			,			UpperCAmelCase__			:				List[Any]			,			UpperCAmelCase__			:				Optional[Any]			,			UpperCAmelCase__			:				str			,			UpperCAmelCase__			:				Tuple			,			UpperCAmelCase__			:				Tuple			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				List[Any]   )       ->	Tuple:
						__SCREAMING_SNAKE_CASE						   =				DebertaVaModel(config=UpperCAmelCase__   )
						model.to(UpperCAmelCase__   )
						model.eval()
						__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__			,			attention_mask=UpperCAmelCase__			,			token_type_ids=UpperCAmelCase__   )[0]
						__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__			,			token_type_ids=UpperCAmelCase__   )[0]
						__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__   )[0]

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




	def   UpperCAmelCase_      ( self			:				Tuple			,			UpperCAmelCase__			:				Union[str, Any]			,			UpperCAmelCase__			:				List[str]			,			UpperCAmelCase__			:				int			,			UpperCAmelCase__			:				List[str]			,			UpperCAmelCase__			:				Optional[int]			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				Tuple   )       ->	str:
						__SCREAMING_SNAKE_CASE						   =				DebertaVaForMaskedLM(config=UpperCAmelCase__   )
						model.to(UpperCAmelCase__   )
						model.eval()
						__SCREAMING_SNAKE_CASE						   =				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   UpperCAmelCase_      ( self			:				Tuple			,			UpperCAmelCase__			:				List[Any]			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				List[str]			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				Dict			,			UpperCAmelCase__			:				str			,			UpperCAmelCase__			:				Dict   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				self.num_labels
						__SCREAMING_SNAKE_CASE						   =				DebertaVaForSequenceClassification(UpperCAmelCase__   )
						model.to(UpperCAmelCase__   )
						model.eval()
						__SCREAMING_SNAKE_CASE						   =				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   UpperCAmelCase_      ( self			:				List[Any]			,			UpperCAmelCase__			:				int			,			UpperCAmelCase__			:				Union[str, Any]			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				List[str]			,			UpperCAmelCase__			:				List[Any]			,			UpperCAmelCase__			:				Optional[int]			,			UpperCAmelCase__			:				List[Any]   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				self.num_labels
						__SCREAMING_SNAKE_CASE						   =				DebertaVaForTokenClassification(config=UpperCAmelCase__   )
						model.to(UpperCAmelCase__   )
						model.eval()
						__SCREAMING_SNAKE_CASE						   =				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   UpperCAmelCase_      ( self			:				str			,			UpperCAmelCase__			:				List[str]			,			UpperCAmelCase__			:				Optional[Any]			,			UpperCAmelCase__			:				Dict			,			UpperCAmelCase__			:				Optional[Any]			,			UpperCAmelCase__			:				List[Any]			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				Optional[Any]   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				DebertaVaForQuestionAnswering(config=UpperCAmelCase__   )
						model.to(UpperCAmelCase__   )
						model.eval()
						__SCREAMING_SNAKE_CASE						   =				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   UpperCAmelCase_      ( self			:				str			,			UpperCAmelCase__			:				Tuple			,			UpperCAmelCase__			:				Tuple			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				Optional[Any]			,			UpperCAmelCase__			:				List[Any]			,			UpperCAmelCase__			:				Dict			,			UpperCAmelCase__			:				Optional[Any]   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				DebertaVaForMultipleChoice(config=UpperCAmelCase__   )
						model.to(UpperCAmelCase__   )
						model.eval()
						__SCREAMING_SNAKE_CASE						   =				input_ids.unsqueeze(1   ).expand(-1			,			self.num_choices			,			-1   ).contiguous()
						__SCREAMING_SNAKE_CASE						   =				token_type_ids.unsqueeze(1   ).expand(-1			,			self.num_choices			,			-1   ).contiguous()
						__SCREAMING_SNAKE_CASE						   =				input_mask.unsqueeze(1   ).expand(-1			,			self.num_choices			,			-1   ).contiguous()
						__SCREAMING_SNAKE_CASE						   =				model(
						    UpperCAmelCase__			,			attention_mask=UpperCAmelCase__			,			token_type_ids=UpperCAmelCase__			,			labels=UpperCAmelCase__			,			)
						self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_choices)   )




	def   UpperCAmelCase_      ( self			:				Optional[int]   )       ->	Union[str, Any]:
						__SCREAMING_SNAKE_CASE						   =				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
						)   ,  
						)						   =				config_and_inputs
						__SCREAMING_SNAKE_CASE						   =				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
						return config, inputs_dict




@require_torch
class  UpperCamelCase_	( UpperCamelCase			,  UpperCamelCase			,  unittest.TestCase):

	"""simple docstring"""





	snake_case__				:  Any    					=			(
	    (
	        DebertaVaModel,
	        DebertaVaForMaskedLM,
	        DebertaVaForSequenceClassification,
	        DebertaVaForTokenClassification,
	        DebertaVaForQuestionAnswering,
	        DebertaVaForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case__				:  Dict    					=			(
	    {
	        "feature-extraction": DebertaVaModel,
	        "fill-mask": DebertaVaForMaskedLM,
	        "question-answering": DebertaVaForQuestionAnswering,
	        "text-classification": DebertaVaForSequenceClassification,
	        "token-classification": DebertaVaForTokenClassification,
	        "zero-shot": DebertaVaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)

	snake_case__				:  Any    					=			True
	snake_case__				:  Any    					=			False
	snake_case__				:  Union[str, Any]    					=			False
	snake_case__				:  List[Any]    					=			False
	snake_case__				:  Any    					=			False




	def   UpperCAmelCase_      ( self			:				List[str]   )       ->	Dict:
						__SCREAMING_SNAKE_CASE						   =				DebertaVaModelTester(self   )
						__SCREAMING_SNAKE_CASE						   =				ConfigTester(self			,			config_class=UpperCAmelCase__			,			hidden_size=3_7   )




	def   UpperCAmelCase_      ( self			:				Dict   )       ->	List[str]:
						self.config_tester.run_common_tests()




	def   UpperCAmelCase_      ( self			:				str   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	Dict:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	Tuple:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				str   )       ->	str:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Union[str, Any]   )       ->	Union[str, Any]:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Optional[int]   )       ->	Union[str, Any]:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_multiple_choice(*UpperCAmelCase__   )




	@slow
	def   UpperCAmelCase_      ( self			:				Dict   )       ->	List[Any]:
						for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__SCREAMING_SNAKE_CASE						   =				DebertaVaModel.from_pretrained(UpperCAmelCase__   )
											self.assertIsNotNone(UpperCAmelCase__   )




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

	"""simple docstring"""





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




	@slow
	def   UpperCAmelCase_      ( self			:				Dict   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge"   )

						__SCREAMING_SNAKE_CASE						   =				torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]   )
						__SCREAMING_SNAKE_CASE						   =				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
						with torch.no_grad():
											__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__			,			attention_mask=UpperCAmelCase__   )[0]
						# compare the actual values for a slice.
						__SCREAMING_SNAKE_CASE						   =				torch.tensor(
						    [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]]   )
						self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			,			UpperCAmelCase__			,			atol=1E-4   )			,			F"""{output[:, 1:4, 1:4]}"""   )


import json
import os
import tempfile

import transformers

import datasets
from utils import generate_example_dataset, get_duration


_lowerCamelCase					   =5_0_0_0_0_0

_lowerCamelCase   ,  _lowerCamelCase					   =os.path.split(__file__)
_lowerCamelCase					   =os.path.join(RESULTS_BASEPATH, """results""", RESULTS_FILENAME.replace(""".py""", """.json"""))





@get_duration
def     _a					(						lowerCamelCase,	**lowerCamelCase							):
 lowerCamelCase		:	Optional[Any]							=      dataset.map(**lowerCamelCase							)





@get_duration
def     _a					(						lowerCamelCase,	**lowerCamelCase							):
 lowerCamelCase		:	Optional[Any]							=      dataset.filter(**lowerCamelCase							)





def     _a					(						):
 lowerCamelCase		:	Optional[Any]							=      {"""num examples""": SPEED_TEST_N_EXAMPLES}
 with tempfile.TemporaryDirectory() as tmp_dir:
  lowerCamelCase		:	Any							=      datasets.Features({"""text""": datasets.Value("""string"""							), """numbers""": datasets.Value("""float32"""							)}							)
  lowerCamelCase		:	Tuple							=      generate_example_dataset(
      os.path.join(lowerCamelCase,	"""dataset.arrow"""							),	lowerCamelCase,	num_examples=lowerCamelCase							)

  lowerCamelCase		:	Tuple							=      transformers.AutoTokenizer.from_pretrained("""bert-base-cased""",	use_fast=lowerCamelCase							)

  def tokenize(lowerCamelCase							):
   return tokenizer(examples["""text"""]							)

  lowerCamelCase		:	List[str]							=      map(lowerCamelCase							)

  lowerCamelCase		:	int							=      map(lowerCamelCase,	batched=lowerCamelCase							)

  lowerCamelCase		:	int							=      map(lowerCamelCase,	function=lambda lowerCamelCase							: None,	batched=lowerCamelCase							)

  with dataset.formatted_as(type="""numpy"""							):
   lowerCamelCase		:	Optional[int]							=      map(lowerCamelCase,	function=lambda lowerCamelCase							: None,	batched=lowerCamelCase							)

  with dataset.formatted_as(type="""pandas"""							):
   lowerCamelCase		:	List[str]							=      map(lowerCamelCase,	function=lambda lowerCamelCase							: None,	batched=lowerCamelCase							)

  with dataset.formatted_as(type="""torch""",	columns="""numbers"""							):
   lowerCamelCase		:	List[str]							=      map(lowerCamelCase,	function=lambda lowerCamelCase							: None,	batched=lowerCamelCase							)

  with dataset.formatted_as(type="""tensorflow""",	columns="""numbers"""							):
   lowerCamelCase		:	Optional[int]							=      map(lowerCamelCase,	function=lambda lowerCamelCase							: None,	batched=lowerCamelCase							)

  lowerCamelCase		:	int							=      map(lowerCamelCase,	function=lowerCamelCase,	batched=lowerCamelCase							)

  lowerCamelCase		:	Union[str, Any]							=      filter(lowerCamelCase							)

  # Activate later when tokenizer support batched inputs
  # with dataset.formatted_as(type='numpy'):
  #     times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True)

 with open(lowerCamelCase,	"""wb"""							) as f:
  f.write(json.dumps(lowerCamelCase							).encode("""utf-8"""							)							)


if __name__ == "__main__":  # useful to run the profiler
      benchmark_map_filter()




