
import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    YosoForMaskedLM,
					    YosoForMultipleChoice,
					    YosoForQuestionAnswering,
					    YosoForSequenceClassification,
					    YosoForTokenClassification,
					    YosoModel,
					)
					from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST





class 							UpperCAmelCase   :




	'''simple docstring'''





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






	def        UpperCamelCase_						( self      :				Optional[int]				):
					__A         =				ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size				)

					__A         =				None
					if self.use_input_mask:
									__A         =				random_attention_mask([self.batch_size, self.seq_length]				)

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

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

					__A         =				self.get_config()

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






	def        UpperCamelCase_						( self      :				str				):
					return YosoConfig(
					    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							,is_decoder=A							,initializer_range=self.initializer_range							,)






	def        UpperCamelCase_						( self      :				List[Any]				):
					__A         =				self.get_config()
					__A         =				3_00
					return config






	def        UpperCamelCase_						( self      :				Union[str, Any]				):
					(
					    (
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							
					)         =				self.prepare_config_and_inputs()

					__A         =				True
					__A         =				floats_tensor([self.batch_size, self.seq_length, self.hidden_size]				)
					__A         =				ids_tensor([self.batch_size, self.seq_length]							,vocab_size=2				)

					return (
					    config,
					    input_ids,
					    token_type_ids,
					    input_mask,
					    sequence_labels,
					    token_labels,
					    choice_labels,
					    encoder_hidden_states,
					    encoder_attention_mask,
					)






	def        UpperCamelCase_						( self      :				Any							,A      :				Tuple							,A      :				Any							,A      :				int							,A      :				Optional[int]							,A      :				Tuple							,A      :				Union[str, Any]							,A      :				List[str]				):
					__A         =				YosoModel(config=A				)
					model.to(A				)
					model.eval()
					__A         =				model(A							,attention_mask=A							,token_type_ids=A				)
					__A         =				model(A							,token_type_ids=A				)
					__A         =				model(A				)
					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)				)






	def        UpperCamelCase_						( self      :				Optional[int]							,A      :				Dict							,A      :				Union[str, Any]							,A      :				Optional[Any]							,A      :				Optional[int]							,A      :				List[Any]							,A      :				int							,A      :				Tuple							,A      :				int							,A      :				str							,):
					__A         =				True
					__A         =				YosoModel(A				)
					model.to(A				)
					model.eval()
					__A         =				model(
					    A							,attention_mask=A							,token_type_ids=A							,encoder_hidden_states=A							,encoder_attention_mask=A							,)
					__A         =				model(
					    A							,attention_mask=A							,token_type_ids=A							,encoder_hidden_states=A							,)
					__A         =				model(A							,attention_mask=A							,token_type_ids=A				)
					self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)				)






	def        UpperCamelCase_						( self      :				List[Any]							,A      :				Dict							,A      :				Union[str, Any]							,A      :				int							,A      :				Any							,A      :				str							,A      :				int							,A      :				Union[str, Any]				):
					__A         =				YosoForMaskedLM(config=A				)
					model.to(A				)
					model.eval()
					__A         =				model(A							,attention_mask=A							,token_type_ids=A							,labels=A				)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)				)






	def        UpperCamelCase_						( self      :				Any							,A      :				List[Any]							,A      :				str							,A      :				Union[str, Any]							,A      :				Dict							,A      :				Union[str, Any]							,A      :				Dict							,A      :				Optional[int]				):
					__A         =				YosoForQuestionAnswering(config=A				)
					model.to(A				)
					model.eval()
					__A         =				model(
					    A							,attention_mask=A							,token_type_ids=A							,start_positions=A							,end_positions=A							,)
					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      :				List[Any]							,A      :				Optional[int]							,A      :				Tuple							,A      :				List[str]							,A      :				int							,A      :				int							,A      :				Optional[Any]							,A      :				str				):
					__A         =				self.num_labels
					__A         =				YosoForSequenceClassification(A				)
					model.to(A				)
					model.eval()
					__A         =				model(A							,attention_mask=A							,token_type_ids=A							,labels=A				)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_labels)				)






	def        UpperCamelCase_						( self      :				str							,A      :				Any							,A      :				str							,A      :				Optional[Any]							,A      :				Dict							,A      :				Any							,A      :				List[str]							,A      :				Tuple				):
					__A         =				self.num_labels
					__A         =				YosoForTokenClassification(config=A				)
					model.to(A				)
					model.eval()
					__A         =				model(A							,attention_mask=A							,token_type_ids=A							,labels=A				)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.num_labels)				)






	def        UpperCamelCase_						( self      :				int							,A      :				Dict							,A      :				Union[str, Any]							,A      :				Optional[int]							,A      :				Union[str, Any]							,A      :				Any							,A      :				List[str]							,A      :				Union[str, Any]				):
					__A         =				self.num_choices
					__A         =				YosoForMultipleChoice(config=A				)
					model.to(A				)
					model.eval()
					__A         =				input_ids.unsqueeze(1				).expand(-1							,self.num_choices							,-1				).contiguous()
					__A         =				token_type_ids.unsqueeze(1				).expand(-1							,self.num_choices							,-1				).contiguous()
					__A         =				input_mask.unsqueeze(1				).expand(-1							,self.num_choices							,-1				).contiguous()
					__A         =				model(
					    A							,attention_mask=A							,token_type_ids=A							,labels=A							,)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_choices)				)







	def        UpperCamelCase_						( self      :				int				):
					__A         =				self.prepare_config_and_inputs()
					(
					    (
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							(
					    __A
					)    ,							
					)         =				config_and_inputs
					__A         =				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
					return config, inputs_dict





@require_torch
class 							UpperCAmelCase   (						__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    unittest.TestCase						):




	'''simple docstring'''





	snake_case_      					=  (
	    (
	        YosoModel,
	        YosoForMaskedLM,
	        YosoForMultipleChoice,
	        YosoForQuestionAnswering,
	        YosoForSequenceClassification,
	        YosoForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case_      					=  False
	snake_case_      					=  False
	snake_case_      					=  False

	snake_case_      					=  ()
	snake_case_      					=  (
	    {
	        "feature-extraction": YosoModel,
	        "fill-mask": YosoForMaskedLM,
	        "question-answering": YosoForQuestionAnswering,
	        "text-classification": YosoForSequenceClassification,
	        "token-classification": YosoForTokenClassification,
	        "zero-shot": YosoForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)






	def        UpperCamelCase_						( self      :				Any				):
					__A         =				YosoModelTester(self				)
					__A         =				ConfigTester(self							,config_class=A							,hidden_size=37				)






	def        UpperCamelCase_						( self      :				Optional[int]				):
					self.config_tester.run_common_tests()






	def        UpperCamelCase_						( self      :				Any				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*A				)






	def        UpperCamelCase_						( self      :				List[Any]				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									__A         =				type
									self.model_tester.create_and_check_model(*A				)






	def        UpperCamelCase_						( self      :				List[str]				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*A				)






	def        UpperCamelCase_						( self      :				Union[str, Any]				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*A				)






	def        UpperCamelCase_						( self      :				Dict				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*A				)






	def        UpperCamelCase_						( self      :				Union[str, Any]				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*A				)






	def        UpperCamelCase_						( self      :				Any				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*A				)






	@slow
	def        UpperCamelCase_						( self      :				str				):
					for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A         =				YosoModel.from_pretrained(A				)
									self.assertIsNotNone(A				)







	def        UpperCamelCase_						( self      :				str				):
					return





@require_torch
class 							UpperCAmelCase   (						unittest.TestCase						):




	'''simple docstring'''





	@slow
	def        UpperCamelCase_						( self      :				Optional[Any]				):
					__A         =				YosoModel.from_pretrained("uw-madison/yoso-4096"				)
					__A         =				torch.tensor([[0, 1, 2, 3, 4, 5]]				)

					with torch.no_grad():
									__A         =				model(A				)[0]

					__A         =				torch.Size((1, 6, 7_68)				)
					self.assertEqual(output.shape							,A				)

					__A         =				torch.tensor(
					    [[[-0.06_11, 0.12_42, 0.08_40], [0.02_80, -0.00_48, 0.11_25], [0.01_06, 0.02_26, 0.07_51]]]				)

					self.assertTrue(torch.allclose(output[:, :3, :3]							,A							,atol=1E-4				)				)






	@slow
	def        UpperCamelCase_						( self      :				Union[str, Any]				):
					__A         =				YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"				)
					__A         =				torch.tensor([[0, 1, 2, 3, 4, 5]]				)

					with torch.no_grad():
									__A         =				model(A				)[0]

					__A         =				5_02_65

					__A         =				torch.Size((1, 6, vocab_size)				)
					self.assertEqual(output.shape							,A				)

					__A         =				torch.tensor(
					    [[[-2.13_13, -3.72_85, -2.24_07], [-2.70_47, -3.33_14, -2.64_08], [0.06_29, -2.51_66, -0.33_56]]]				)

					self.assertTrue(torch.allclose(output[:, :3, :3]							,A							,atol=1E-4				)				)







	@slow
	def        UpperCamelCase_						( self      :				Any				):
					__A         =				YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"				)
					__A         =				torch.arange(40_96				).unsqueeze(0				)

					with torch.no_grad():
									__A         =				model(A				)[0]

					__A         =				5_02_65

					__A         =				torch.Size((1, 40_96, vocab_size)				)
					self.assertEqual(output.shape							,A				)

					__A         =				torch.tensor(
					    [[[-2.39_14, -4.37_42, -5.09_56], [-4.09_88, -4.23_84, -7.04_06], [-3.14_27, -3.71_92, -6.68_00]]]				)

					self.assertTrue(torch.allclose(output[:, :3, :3]							,A							,atol=1E-4				)				)


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE							:str											=       {
    'configuration_deberta_v2': ['DEBERTA_V2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DebertaV2Config', 'DebertaV2OnnxConfig'],
    'tokenization_deberta_v2': ['DebertaV2Tokenizer'],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE							:int											=       ['DebertaV2TokenizerFast']

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE							:Any											=       [
					    'TF_DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'TFDebertaV2ForMaskedLM',
					    'TFDebertaV2ForQuestionAnswering',
					    'TFDebertaV2ForSequenceClassification',
					    'TFDebertaV2ForTokenClassification',
					    'TFDebertaV2Model',
					    'TFDebertaV2PreTrainedModel',
					]

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE							:Any											=       [
					    'DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'DebertaV2ForMaskedLM',
					    'DebertaV2ForMultipleChoice',
					    'DebertaV2ForQuestionAnswering',
					    'DebertaV2ForSequenceClassification',
					    'DebertaV2ForTokenClassification',
					    'DebertaV2Model',
					    'DebertaV2PreTrainedModel',
					]


if TYPE_CHECKING:
					from .configuration_deberta_va import (
					    DEBERTA_V2_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    DebertaVaConfig,
					    DebertaVaOnnxConfig,
					)
					from .tokenization_deberta_va import DebertaVaTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_deberta_va_fast import DebertaVaTokenizerFast

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_deberta_va import (
										    TF_DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFDebertaVaForMaskedLM,
										    TFDebertaVaForQuestionAnswering,
										    TFDebertaVaForSequenceClassification,
										    TFDebertaVaForTokenClassification,
										    TFDebertaVaModel,
										    TFDebertaVaPreTrainedModel,
										)

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_deberta_va import (
										    DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST,
										    DebertaVaForMaskedLM,
										    DebertaVaForMultipleChoice,
										    DebertaVaForQuestionAnswering,
										    DebertaVaForSequenceClassification,
										    DebertaVaForTokenClassification,
										    DebertaVaModel,
										    DebertaVaPreTrainedModel,
										)

else:
					import sys

					SCREAMING_SNAKE_CASE							:List[Any]											=       _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
