




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  _lowerCAmelCase     (  _lowercase	):

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

						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  Union[str, Any]		       =     ids_tensor([self.batch_size, self.seq_length]      ,				self.vocab_size		)

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

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

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

								lowerCAmelCase__				:  Optional[Any]		       =     self.get_config()

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

						def 	__magic_name__(   self		):
								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 	__magic_name__(   self      ,				__UpperCAmelCase		):
								self.parent.assertListEqual(list(result.loss.size()		)      ,				[]		)

						def 	__magic_name__(   self      ,				__UpperCAmelCase      ,				__UpperCAmelCase      ,				__UpperCAmelCase      ,				__UpperCAmelCase      ,				__UpperCAmelCase      ,				__UpperCAmelCase      ,				__UpperCAmelCase		):
								lowerCAmelCase__				:  Optional[int]		       =     DebertaVaModel(config=__UpperCAmelCase		)
								model.to(__UpperCAmelCase		)
								model.eval()
								lowerCAmelCase__				:  Optional[Any]		       =     model(__UpperCAmelCase      ,				attention_mask=__UpperCAmelCase      ,				token_type_ids=__UpperCAmelCase		)[0]
								lowerCAmelCase__				:  List[Any]		       =     model(__UpperCAmelCase      ,				token_type_ids=__UpperCAmelCase		)[0]
								lowerCAmelCase__				:  int		       =     model(__UpperCAmelCase		)[0]

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

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







						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  Union[str, Any]		       =     self.prepare_config_and_inputs()
								(
								    lowerCAmelCase__			
								)				:  Tuple		       =     config_and_inputs
								lowerCAmelCase__				:  Optional[int]		       =     {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
								return config, inputs_dict






@require_torch
class  _lowerCAmelCase     (  _lowercase    ,			_lowercase    ,			unittest.TestCase	):
						A__		     =		(
						    (
						        DebertaVaModel,
						        DebertaVaForMaskedLM,
						        DebertaVaForSequenceClassification,
						        DebertaVaForTokenClassification,
						        DebertaVaForQuestionAnswering,
						        DebertaVaForMultipleChoice,
						    )
						    if is_torch_available()
						    else ()
						)
						A__		     =		(
						    {
						        'feature-extraction': DebertaVaModel,
						        'fill-mask': DebertaVaForMaskedLM,
						        'question-answering': DebertaVaForQuestionAnswering,
						        'text-classification': DebertaVaForSequenceClassification,
						        'token-classification': DebertaVaForTokenClassification,
						        'zero-shot': DebertaVaForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)

						A__		     =		True
						A__		     =		False
						A__		     =		False
						A__		     =		False
						A__		     =		False

						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  str		       =     DebertaVaModelTester(self		)
								lowerCAmelCase__				:  Union[str, Any]		       =     ConfigTester(self      ,				config_class=__UpperCAmelCase      ,				hidden_size=37		)

						def 	__magic_name__(   self		):
								self.config_tester.run_common_tests()

						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  Dict		       =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*__UpperCAmelCase		)

						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  Optional[Any]		       =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*__UpperCAmelCase		)

						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  List[Any]		       =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*__UpperCAmelCase		)

						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  List[Any]		       =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*__UpperCAmelCase		)

						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  Tuple		       =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*__UpperCAmelCase		)

						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  str		       =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_multiple_choice(*__UpperCAmelCase		)







						@slow
						def 	__magic_name__(   self		):
								for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase__				:  List[Any]		       =     DebertaVaModel.from_pretrained(__UpperCAmelCase		)
										self.assertIsNotNone(__UpperCAmelCase		)






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

						@unittest.skip(reason='''Model not available yet'''		)
						def 	__magic_name__(   self		):
								pass







						@slow
						def 	__magic_name__(   self		):
								lowerCAmelCase__				:  str		       =     DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge'''		)

								lowerCAmelCase__				:  List[Any]		       =     torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]		)
								lowerCAmelCase__				:  int		       =     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
								with torch.no_grad():
										lowerCAmelCase__				:  str		       =     model(__UpperCAmelCase      ,				attention_mask=__UpperCAmelCase		)[0]
								# compare the actual values for a slice.
								lowerCAmelCase__				:  Tuple		       =     torch.tensor(
								    [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]		)
								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]      ,				__UpperCAmelCase      ,				atol=1e-4		)      ,				f"""{output[:, 1:4, 1:4]}"""		)







from datetime import datetime

import matplotlib.pyplot as plt
import torch



def      __lowerCAmelCase			(					UpperCamelCase      )     ->	str:
  for param in module.parameters():
    lowerCAmelCase__				:  int		       =     False



def      __lowerCAmelCase			(					)     ->	Optional[Any]:
  lowerCAmelCase__				:  List[Any]		       =     '''cuda''' if torch.cuda.is_available() else '''cpu'''
  if torch.backends.mps.is_available() and torch.backends.mps.is_built():
    lowerCAmelCase__				:  Optional[Any]		       =     '''mps'''
  if device == "mps":
    print(
        '''WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch'''
        ''' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues'''
        ''' with generations.'''      )
  return device



def      __lowerCAmelCase			(					UpperCamelCase      )     ->	List[Any]:
  lowerCAmelCase__				:  str		       =     plt.imshow(UpperCamelCase      )
  fig.axes.get_xaxis().set_visible(UpperCamelCase      )
  fig.axes.get_yaxis().set_visible(UpperCamelCase      )
  plt.show()



def      __lowerCAmelCase			(					)     ->	str:
  lowerCAmelCase__				:  Dict		       =     datetime.now()
  lowerCAmelCase__				:  Optional[int]		       =     current_time.strftime('''%H:%M:%S'''      )
  return timestamp

