





"""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 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
				"""simple docstring"""




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




				def        __lowercase	(  self	:Optional[Any]      ):
											__lowerCamelCase   :   Union[str, Any]       =ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size      )

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

											__lowerCamelCase   :   Optional[Any]       =None
											if self.use_token_type_ids:
																		__lowerCamelCase   :   List[str]       =ids_tensor([self.batch_size, self.seq_length]    ,     self.type_vocab_size      )

											__lowerCamelCase   :   Any       =None
											__lowerCamelCase   :   Union[str, Any]       =None
											__lowerCamelCase   :   Optional[Any]       =None
											if self.use_labels:
																		__lowerCamelCase   :   Any       =ids_tensor([self.batch_size]    ,     self.type_sequence_label_size      )
																		__lowerCamelCase   :   Union[str, Any]       =ids_tensor([self.batch_size, self.seq_length]    ,     self.num_labels      )
																		__lowerCamelCase   :   Tuple       =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        __lowercase	(  self	:Optional[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        __lowercase	(  self	:int      ):
											__lowerCamelCase   :   str       =self.get_config()
											__lowerCamelCase   :   Dict       =300
											return config




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




				def        __lowercase	(  self	:Tuple    ,     __lowercase	:List[Any]    ,     __lowercase	:List[str]    ,     __lowercase	:Dict    ,     __lowercase	:Tuple    ,     __lowercase	:List[Any]    ,     __lowercase	:Dict    ,     __lowercase	:str      ):
											__lowerCamelCase   :   int       =DebertaModel(config=__lowercase      )
											model.to(__lowercase      )
											model.eval()
											__lowerCamelCase   :   Dict       =model(__lowercase    ,     attention_mask=__lowercase    ,     token_type_ids=__lowercase      )[0]
											__lowerCamelCase   :   str       =model(__lowercase    ,     token_type_ids=__lowercase      )[0]
											__lowerCamelCase   :   str       =model(__lowercase      )[0]

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




				def        __lowercase	(  self	:Dict    ,     __lowercase	:int    ,     __lowercase	:int    ,     __lowercase	:Optional[Any]    ,     __lowercase	:Tuple    ,     __lowercase	:Union[str, Any]    ,     __lowercase	:Tuple    ,     __lowercase	:Optional[Any]      ):
											__lowerCamelCase   :   List[str]       =DebertaForMaskedLM(config=__lowercase      )
											model.to(__lowercase      )
											model.eval()
											__lowerCamelCase   :   Union[str, Any]       =model(__lowercase    ,     attention_mask=__lowercase    ,     token_type_ids=__lowercase    ,     labels=__lowercase      )

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




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




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




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

@require_torch
class 		SCREAMING_SNAKE_CASE_				(     snake_case__							,	snake_case__							,	unittest.TestCase ):
				"""simple docstring"""
				__snake_case :							List[str]									=		(
				    (
				        DebertaModel,
				        DebertaForMaskedLM,
				        DebertaForSequenceClassification,
				        DebertaForTokenClassification,
				        DebertaForQuestionAnswering,
				    )
				    if is_torch_available()
				    else ()
				)
				__snake_case :							str									=		(
				    {
				        """feature-extraction""": DebertaModel,
				        """fill-mask""": DebertaForMaskedLM,
				        """question-answering""": DebertaForQuestionAnswering,
				        """text-classification""": DebertaForSequenceClassification,
				        """token-classification""": DebertaForTokenClassification,
				        """zero-shot""": DebertaForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)

				__snake_case :							int									=		True
				__snake_case :							Tuple									=		False
				__snake_case :							Any									=		False
				__snake_case :							List[str]									=		False
				__snake_case :							Tuple									=		False




				def        __lowercase	(  self	:int      ):
											__lowerCamelCase   :   Union[str, Any]       =DebertaModelTester(self      )
											__lowerCamelCase   :   Optional[Any]       =ConfigTester(self    ,     config_class=__lowercase    ,     hidden_size=37      )




				def        __lowercase	(  self	:Any      ):
											self.config_tester.run_common_tests()




				def        __lowercase	(  self	:Tuple      ):
											__lowerCamelCase   :   Optional[Any]       =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_model(*__lowercase      )




				def        __lowercase	(  self	:Any      ):
											__lowerCamelCase   :   Optional[Any]       =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_sequence_classification(*__lowercase      )




				def        __lowercase	(  self	:Tuple      ):
											__lowerCamelCase   :   List[Any]       =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_masked_lm(*__lowercase      )




				def        __lowercase	(  self	:str      ):
											__lowerCamelCase   :   Optional[int]       =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_question_answering(*__lowercase      )




				def        __lowercase	(  self	:int      ):
											__lowerCamelCase   :   Dict       =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_token_classification(*__lowercase      )




				@slow
				def        __lowercase	(  self	:Dict      ):
											for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase   :   str       =DebertaModel.from_pretrained(__lowercase      )
																		self.assertIsNotNone(__lowercase      )

@require_torch
@require_sentencepiece
@require_tokenizers
class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				@unittest.skip(reason='''Model not available yet'''      )
				def        __lowercase	(  self	:Optional[Any]      ):
											pass




				@slow
				def        __lowercase	(  self	:Tuple      ):
											__lowerCamelCase   :   Union[str, Any]       =DebertaModel.from_pretrained('''microsoft/deberta-base'''      )

											__lowerCamelCase   :   Optional[Any]       =torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]      )
											__lowerCamelCase   :   str       =torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
											with torch.no_grad():
																		__lowerCamelCase   :   List[Any]       =model(__lowercase    ,     attention_mask=__lowercase      )[0]
											# compare the actual values for a slice.
											__lowerCamelCase   :   Dict       =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]    ,     __lowercase    ,     atol=1e-4      )    ,     f'{output[:, 1:4, 1:4]}'      )











"""simple docstring"""





import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
							from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
							from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
							from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


_UpperCamelCase					      =   DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

_UpperCamelCase					      =   'main'
# Default branch name
_UpperCamelCase					      =   'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'
# One particular commit (not the top of `main`)
_UpperCamelCase					      =   'aaaaaaa'
# This commit does not exist, so we should 404.

_UpperCamelCase					      =   'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684'
# Sha-1 of config.json on the top of `main`, for checking purposes
_UpperCamelCase					      =   '4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'



@contextlib.contextmanager
def 					lowerCAmelCase_      (   ):


							'''simple docstring'''







							print('''Welcome!''' )
							yield
							print('''Bye!''' )



@contextlib.contextmanager
def 					lowerCAmelCase_      (   ):


							'''simple docstring'''







							print('''Bonjour!''' )
							yield
							print('''Au revoir!''' )
class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				def        __lowercase	(  self	:str      ):
											# If the spec is missing, importlib would not be able to import the module dynamically.
											assert transformers.__spec__ is not None
											assert importlib.util.find_spec('''transformers'''      ) is not None

class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				@unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO      )
				def        __lowercase	(  self	:str    ,     __lowercase	:str      ):
											with ContextManagers([]      ):
																		print('''Transformers are awesome!'''      )
											# The print statement adds a new line at the end of the output
											self.assertEqual(mock_stdout.getvalue()    ,     '''Transformers are awesome!\n'''      )




				@unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO      )
				def        __lowercase	(  self	:Any    ,     __lowercase	:Tuple      ):
											with ContextManagers([context_en()]      ):
																		print('''Transformers are awesome!'''      )
											# The output should be wrapped with an English welcome and goodbye
											self.assertEqual(mock_stdout.getvalue()    ,     '''Welcome!\nTransformers are awesome!\nBye!\n'''      )




				@unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO      )
				def        __lowercase	(  self	:List[Any]    ,     __lowercase	:Optional[Any]      ):
											with ContextManagers([context_fr(), context_en()]      ):
																		print('''Transformers are awesome!'''      )
											# The output should be wrapped with an English and French welcome and goodbye
											self.assertEqual(mock_stdout.getvalue()    ,     '''Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n'''      )




				@require_torch
				def        __lowercase	(  self	:Tuple      ):
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''', '''next_sentence_label''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''start_positions''', '''end_positions''']      )
											class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
															"""simple docstring"""
															pass

											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )




				@require_tf
				def        __lowercase	(  self	:str      ):
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''', '''next_sentence_label''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''start_positions''', '''end_positions''']      )
											class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
															"""simple docstring"""
															pass

											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )




				@require_flax
				def        __lowercase	(  self	:str      ):
											# Flax models don't have labels
											self.assertEqual(find_labels(__lowercase      )    ,     []      )
											self.assertEqual(find_labels(__lowercase      )    ,     []      )
											self.assertEqual(find_labels(__lowercase      )    ,     []      )
											class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
															"""simple docstring"""
															pass

											self.assertEqual(find_labels(__lowercase      )    ,     []      )




