

'''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__	(		__lowerCamelCase ):



					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			,				)     ->			List[Any]:
						__lowerCAmelCase												=  parent
						__lowerCAmelCase												=  batch_size
						__lowerCAmelCase												=  seq_length
						__lowerCAmelCase												=  is_training
						__lowerCAmelCase												=  use_input_mask
						__lowerCAmelCase												=  use_token_type_ids
						__lowerCAmelCase												=  use_labels
						__lowerCAmelCase												=  vocab_size
						__lowerCAmelCase												=  hidden_size
						__lowerCAmelCase												=  num_hidden_layers
						__lowerCAmelCase												=  num_attention_heads
						__lowerCAmelCase												=  intermediate_size
						__lowerCAmelCase												=  hidden_act
						__lowerCAmelCase												=  hidden_dropout_prob
						__lowerCAmelCase												=  attention_probs_dropout_prob
						__lowerCAmelCase												=  max_position_embeddings
						__lowerCAmelCase												=  type_vocab_size
						__lowerCAmelCase												=  type_sequence_label_size
						__lowerCAmelCase												=  initializer_range
						__lowerCAmelCase												=  num_labels
						__lowerCAmelCase												=  num_choices
						__lowerCAmelCase												=  relative_attention
						__lowerCAmelCase												=  position_biased_input
						__lowerCAmelCase												=  pos_att_type
						__lowerCAmelCase												=  scope



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

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

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

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

						__lowerCAmelCase												=  self.get_config()

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



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						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			,				UpperCamelCase     )     ->			Any:
						self.parent.assertListEqual(list(result.loss.size()     )			,				[]     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			Dict:
						__lowerCAmelCase												=  DebertaVaModel(config=SCREAMING_SNAKE_CASE_     )
						model.to(SCREAMING_SNAKE_CASE_     )
						model.eval()
						__lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_     )[0]
						__lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_     )[0]
						__lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_     )[0]

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



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			Optional[Any]:
						__lowerCAmelCase												=  DebertaVaForMaskedLM(config=SCREAMING_SNAKE_CASE_     )
						model.to(SCREAMING_SNAKE_CASE_     )
						model.eval()
						__lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_     )

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



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			Optional[int]:
						__lowerCAmelCase												=  self.num_labels
						__lowerCAmelCase												=  DebertaVaForSequenceClassification(SCREAMING_SNAKE_CASE_     )
						model.to(SCREAMING_SNAKE_CASE_     )
						model.eval()
						__lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_     )
						self.parent.assertListEqual(list(result.logits.size()     )			,				[self.batch_size, self.num_labels]     )
						self.check_loss_output(SCREAMING_SNAKE_CASE_     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  self.num_labels
						__lowerCAmelCase												=  DebertaVaForTokenClassification(config=SCREAMING_SNAKE_CASE_     )
						model.to(SCREAMING_SNAKE_CASE_     )
						model.eval()
						__lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_     )
						self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.seq_length, self.num_labels)     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  DebertaVaForQuestionAnswering(config=SCREAMING_SNAKE_CASE_     )
						model.to(SCREAMING_SNAKE_CASE_     )
						model.eval()
						__lowerCAmelCase												=  model(
						    SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				start_positions=SCREAMING_SNAKE_CASE_			,				end_positions=SCREAMING_SNAKE_CASE_			,				)
						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			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[Any]:
						__lowerCAmelCase												=  DebertaVaForMultipleChoice(config=SCREAMING_SNAKE_CASE_     )
						model.to(SCREAMING_SNAKE_CASE_     )
						model.eval()
						__lowerCAmelCase												=  input_ids.unsqueeze(1     ).expand(-1			,				self.num_choices			,				-1     ).contiguous()
						__lowerCAmelCase												=  token_type_ids.unsqueeze(1     ).expand(-1			,				self.num_choices			,				-1     ).contiguous()
						__lowerCAmelCase												=  input_mask.unsqueeze(1     ).expand(-1			,				self.num_choices			,				-1     ).contiguous()
						__lowerCAmelCase												=  model(
						    SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_			,				)
						self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.num_choices)     )



					def        UpperCAmelCase_ (    self     )     ->			Optional[int]:
						__lowerCAmelCase												=  self.prepare_config_and_inputs()
						(
						    (
						    __lowerCAmelCase 
						)							,       (
						    __lowerCAmelCase 
						)							,       (
						    __lowerCAmelCase 
						)							,       (
						    __lowerCAmelCase 
						)							,       (
						    __lowerCAmelCase 
						)							,       (
						    __lowerCAmelCase 
						)							,       (
						    __lowerCAmelCase 
						)							,        
						)												=  config_and_inputs
						__lowerCAmelCase												=  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
						return config, inputs_dict






@require_torch
class 					UpperCAmelCase__	(		__lowerCamelCase							,       __lowerCamelCase							,       unittest.TestCase ):
					a      :     int        =							(
					    (
					        DebertaVaModel,
					        DebertaVaForMaskedLM,
					        DebertaVaForSequenceClassification,
					        DebertaVaForTokenClassification,
					        DebertaVaForQuestionAnswering,
					        DebertaVaForMultipleChoice,
					    )
					    if is_torch_available()
					    else ()
					)
					a      :     List[str]        =							(
					    {
					        """feature-extraction""": DebertaVaModel,
					        """fill-mask""": DebertaVaForMaskedLM,
					        """question-answering""": DebertaVaForQuestionAnswering,
					        """text-classification""": DebertaVaForSequenceClassification,
					        """token-classification""": DebertaVaForTokenClassification,
					        """zero-shot""": DebertaVaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					a      :     List[Any]        =							True
					a      :     Optional[Any]        =							False
					a      :     int        =							False
					a      :     List[str]        =							False
					a      :     Union[str, Any]        =							False



					def        UpperCAmelCase_ (    self     )     ->			int:
						__lowerCAmelCase												=  DebertaVaModelTester(self     )
						__lowerCAmelCase												=  ConfigTester(self			,				config_class=SCREAMING_SNAKE_CASE_			,				hidden_size=37     )



					def        UpperCAmelCase_ (    self     )     ->			Dict:
						self.config_tester.run_common_tests()



					def        UpperCAmelCase_ (    self     )     ->			List[Any]:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*SCREAMING_SNAKE_CASE_     )



					def        UpperCAmelCase_ (    self     )     ->			str:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*SCREAMING_SNAKE_CASE_     )



					def        UpperCAmelCase_ (    self     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*SCREAMING_SNAKE_CASE_     )



					def        UpperCAmelCase_ (    self     )     ->			List[str]:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*SCREAMING_SNAKE_CASE_     )



					def        UpperCAmelCase_ (    self     )     ->			Dict:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*SCREAMING_SNAKE_CASE_     )



					def        UpperCAmelCase_ (    self     )     ->			Any:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_multiple_choice(*SCREAMING_SNAKE_CASE_     )



					@slow
					def        UpperCAmelCase_ (    self     )     ->			int:
						for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__lowerCAmelCase												=  DebertaVaModel.from_pretrained(SCREAMING_SNAKE_CASE_     )
							self.assertIsNotNone(SCREAMING_SNAKE_CASE_     )






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



					@unittest.skip(reason="Model not available yet"     )
					def        UpperCAmelCase_ (    self     )     ->			Any:
						pass



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

						__lowerCAmelCase												=  torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]     )
						__lowerCAmelCase												=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
						with torch.no_grad():
							__lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_     )[0]
						# compare the actual values for a slice.
						__lowerCAmelCase												=  torch.tensor(
						    [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]     )
						self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			,				SCREAMING_SNAKE_CASE_			,				atol=1E-4     )			,				F'''{output[:, 1:4, 1:4]}'''     )




'''simple docstring'''



from argparse import ArgumentParser

from . import BaseTransformersCLICommand



def   __lowerCAmelCase					(							lowerCamelCase   :						List[str]					):






	'''simple docstring'''




	return DownloadCommand(args.model				,		args.cache_dir				,		args.force				,		args.trust_remote_code					)






class 					UpperCAmelCase__	(		UpperCamelCase__ ):



					@staticmethod
					def        UpperCAmelCase_ (    UpperCamelCase     )     ->			Tuple:
						__lowerCAmelCase												=  parser.add_parser("download"     )
						download_parser.add_argument(
						    "--cache-dir"			,				type=UpperCamelCase			,				default=UpperCamelCase			,				help="Path to location to store the models"     )
						download_parser.add_argument(
						    "--force"			,				action="store_true"			,				help="Force the model to be download even if already in cache-dir"     )
						download_parser.add_argument(
						    "--trust-remote-code"			,				action="store_true"			,				help="Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you've reviewed the code as it will execute on your local machine"			,				)
						download_parser.add_argument("model"			,				type=UpperCamelCase			,				help="Name of the model to download"     )
						download_parser.set_defaults(func=UpperCamelCase     )



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[str]:
						__lowerCAmelCase												=  model
						__lowerCAmelCase												=  cache
						__lowerCAmelCase												=  force
						__lowerCAmelCase												=  trust_remote_code



					def        UpperCAmelCase_ (    self     )     ->			Any:
						from ..models.auto import AutoModel, AutoTokenizer

						AutoModel.from_pretrained(
						    self._model			,				cache_dir=self._cache			,				force_download=self._force			,				trust_remote_code=self._trust_remote_code     )
						AutoTokenizer.from_pretrained(
						    self._model			,				cache_dir=self._cache			,				force_download=self._force			,				trust_remote_code=self._trust_remote_code     )