






"""simple docstring"""



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        lowercase__      (							snake_case__       ):


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


			def      UpperCAmelCase__						(				self					:  Tuple ):
										lowerCamelCase_	:							Any									=ids_tensor([self.batch_size, self.seq_length]       ,	self.vocab_size )

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

										lowerCamelCase_	:							Optional[int]									=None
										if self.use_token_type_ids:
																	lowerCamelCase_	:							Union[str, Any]									=ids_tensor([self.batch_size, self.seq_length]       ,	self.type_vocab_size )

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

										lowerCamelCase_	:							List[str]									=self.get_config()

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


			def      UpperCAmelCase__						(				self					:  int ):
										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      UpperCAmelCase__						(				self					:  Optional[int] ):
										lowerCamelCase_	:							List[Any]									=self.get_config()
										lowerCamelCase_	:							Optional[Any]									=300
										return config


			def      UpperCAmelCase__						(				self					:  Optional[int]       ,	snake_case__					:  List[Any] ):
										self.parent.assertListEqual(list(result.loss.size() )       ,	[] )


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

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


			def      UpperCAmelCase__						(				self					:  int       ,	snake_case__					:  Any       ,	snake_case__					:  int       ,	snake_case__					:  List[str]       ,	snake_case__					:  List[Any]       ,	snake_case__					:  List[str]       ,	snake_case__					:  int       ,	snake_case__					:  int ):
										lowerCamelCase_	:							Optional[int]									=DebertaForMaskedLM(config=snake_case__ )
										model.to(snake_case__ )
										model.eval()
										lowerCamelCase_	:							Union[str, Any]									=model(snake_case__       ,	attention_mask=snake_case__       ,	token_type_ids=snake_case__       ,	labels=snake_case__ )

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


			def      UpperCAmelCase__						(				self					:  str       ,	snake_case__					:  int       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Optional[Any]       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Optional[Any] ):
										lowerCamelCase_	:							Optional[int]									=self.num_labels
										lowerCamelCase_	:							List[str]									=DebertaForSequenceClassification(snake_case__ )
										model.to(snake_case__ )
										model.eval()
										lowerCamelCase_	:							List[str]									=model(snake_case__       ,	attention_mask=snake_case__       ,	token_type_ids=snake_case__       ,	labels=snake_case__ )
										self.parent.assertListEqual(list(result.logits.size() )       ,	[self.batch_size, self.num_labels] )
										self.check_loss_output(snake_case__ )


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


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







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

			_UpperCAmelCase		:Any             =			True
			_UpperCAmelCase		:Any             =			False
			_UpperCAmelCase		:Tuple             =			False
			_UpperCAmelCase		:Any             =			False
			_UpperCAmelCase		:Optional[Any]             =			False


			def      UpperCAmelCase__						(				self					:  Any ):
										lowerCamelCase_	:							str									=DebertaModelTester(self )
										lowerCamelCase_	:							int									=ConfigTester(self       ,	config_class=snake_case__       ,	hidden_size=37 )


			def      UpperCAmelCase__						(				self					:  List[str] ):
										self.config_tester.run_common_tests()


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							int									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_model(*snake_case__ )


			def      UpperCAmelCase__						(				self					:  Optional[int] ):
										lowerCamelCase_	:							Optional[int]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case__ )


			def      UpperCAmelCase__						(				self					:  str ):
										lowerCamelCase_	:							str									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case__ )


			def      UpperCAmelCase__						(				self					:  Any ):
										lowerCamelCase_	:							int									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_question_answering(*snake_case__ )


			def      UpperCAmelCase__						(				self					:  Optional[int] ):
										lowerCamelCase_	:							List[str]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_token_classification(*snake_case__ )


			@slow
			def      UpperCAmelCase__						(				self					:  List[Any] ):
										for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowerCamelCase_	:							Optional[int]									=DebertaModel.from_pretrained(snake_case__ )
																	self.assertIsNotNone(snake_case__ )







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


			@unittest.skip(reason="Model not available yet" )
			def      UpperCAmelCase__						(				self					:  Dict ):
										pass


			@slow
			def      UpperCAmelCase__						(				self					:  Union[str, Any] ):
										lowerCamelCase_	:							Any									=DebertaModel.from_pretrained("microsoft/deberta-base" )

										lowerCamelCase_	:							Dict									=torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] )
										lowerCamelCase_	:							Dict									=torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
										with torch.no_grad():
																	lowerCamelCase_	:							Dict									=model(snake_case__       ,	attention_mask=snake_case__ )[0]
										# compare the actual values for a slice.
										lowerCamelCase_	:							str									=torch.tensor(
										    [[[-0.5_986, -0.8_055, -0.8_462], [1.4_484, -0.9_348, -0.8_059], [0.3_123, 0.0_032, -1.4_131]]] )
										self.assertTrue(torch.allclose(output[:, 1:4, 1:4]       ,	snake_case__       ,	atol=1E-4 )       ,	F"""{output[:, 1:4, 1:4]}""" )











"""simple docstring"""



import torch

def 			_snake_case    (					)    ->     Union[str, Any]:
							if torch.cuda.is_available():
														lowerCamelCase_	:							int									=torch.cuda.device_count()
							else:
														lowerCamelCase_	:							List[str]									=0
							print(F"""Successfully ran on {num_gpus} GPUs"""  )


if __name__ == "__main__":
						main()



