
"""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    _UpperCAmelCase(   lowerCamelCase     ):



		def __init__( self    ,		__a    ,		__a=13    ,		__a=7    ,		__a=True    ,		__a=True    ,		__a=True    ,		__a=True    ,		__a=99    ,		__a=32    ,		__a=5    ,		__a=4    ,		__a=37    ,		__a="gelu"    ,		__a=0.1    ,		__a=0.1    ,		__a=5_12    ,		__a=16    ,		__a=2    ,		__a=0.02    ,		__a=False    ,		__a=True    ,		__a="None"    ,		__a=3    ,		__a=4    ,		__a=None    ,		)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  parent
							_UpperCamelCase										=  batch_size
							_UpperCamelCase										=  seq_length
							_UpperCamelCase										=  is_training
							_UpperCamelCase										=  use_input_mask
							_UpperCamelCase										=  use_token_type_ids
							_UpperCamelCase										=  use_labels
							_UpperCamelCase										=  vocab_size
							_UpperCamelCase										=  hidden_size
							_UpperCamelCase										=  num_hidden_layers
							_UpperCamelCase										=  num_attention_heads
							_UpperCamelCase										=  intermediate_size
							_UpperCamelCase										=  hidden_act
							_UpperCamelCase										=  hidden_dropout_prob
							_UpperCamelCase										=  attention_probs_dropout_prob
							_UpperCamelCase										=  max_position_embeddings
							_UpperCamelCase										=  type_vocab_size
							_UpperCamelCase										=  type_sequence_label_size
							_UpperCamelCase										=  initializer_range
							_UpperCamelCase										=  num_labels
							_UpperCamelCase										=  num_choices
							_UpperCamelCase										=  relative_attention
							_UpperCamelCase										=  position_biased_input
							_UpperCamelCase										=  pos_att_type
							_UpperCamelCase										=  scope



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size)

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

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

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

							_UpperCamelCase										=  self.get_config()

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



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							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)	->	List[str]:






							'''simple docstring'''
							_UpperCamelCase										=  self.get_config()
							_UpperCamelCase										=  3_00
							return config



		def 						UpperCAmelCase							( self    ,		__a)	->	Tuple:






							'''simple docstring'''
							self.parent.assertListEqual(list(result.loss.size())    ,		[])



		def 						UpperCAmelCase							( self    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  DebertaModel(config=__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  model(__a    ,		attention_mask=__a    ,		token_type_ids=__a)[0]
							_UpperCamelCase										=  model(__a    ,		token_type_ids=__a)[0]
							_UpperCamelCase										=  model(__a)[0]

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



		def 						UpperCAmelCase							( self    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a)	->	Optional[int]:






							'''simple docstring'''
							_UpperCamelCase										=  DebertaForMaskedLM(config=__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  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    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  self.num_labels
							_UpperCamelCase										=  DebertaForSequenceClassification(__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  model(__a    ,		attention_mask=__a    ,		token_type_ids=__a    ,		labels=__a)
							self.parent.assertListEqual(list(result.logits.size())    ,		[self.batch_size, self.num_labels])
							self.check_loss_output(__a)



		def 						UpperCAmelCase							( self    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a)	->	Union[str, Any]:






							'''simple docstring'''
							_UpperCamelCase										=  self.num_labels
							_UpperCamelCase										=  DebertaForTokenClassification(config=__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  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    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a    ,		__a)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  DebertaForQuestionAnswering(config=__a)
							model.to(__a)
							model.eval()
							_UpperCamelCase										=  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)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  self.prepare_config_and_inputs()
							(
							    (
							    _UpperCamelCase
							)			,  (
							    _UpperCamelCase
							)			,  (
							    _UpperCamelCase
							)			,  (
							    _UpperCamelCase
							)			,  (
							    _UpperCamelCase
							)			,  (
							    _UpperCamelCase
							)			,  (
							    _UpperCamelCase
							)			,  
							)										=  config_and_inputs
							_UpperCamelCase										=  {'''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     ):
		lowercase__      =				(
		    (
		        DebertaModel,
		        DebertaForMaskedLM,
		        DebertaForSequenceClassification,
		        DebertaForTokenClassification,
		        DebertaForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		lowercase__      =				(
		    {
		        'feature-extraction': DebertaModel,
		        'fill-mask': DebertaForMaskedLM,
		        'question-answering': DebertaForQuestionAnswering,
		        'text-classification': DebertaForSequenceClassification,
		        'token-classification': DebertaForTokenClassification,
		        'zero-shot': DebertaForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)

		lowercase__      =				True
		lowercase__      =				False
		lowercase__      =				False
		lowercase__      =				False
		lowercase__      =				False



		def 						UpperCAmelCase							( self)	->	List[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  DebertaModelTester(self)
							_UpperCamelCase										=  ConfigTester(self    ,		config_class=__a    ,		hidden_size=37)



		def 						UpperCAmelCase							( self)	->	Dict:






							'''simple docstring'''
							self.config_tester.run_common_tests()



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_model(*__a)



		def 						UpperCAmelCase							( self)	->	List[str]:






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_sequence_classification(*__a)



		def 						UpperCAmelCase							( self)	->	Dict:






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_masked_lm(*__a)



		def 						UpperCAmelCase							( self)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_question_answering(*__a)



		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_token_classification(*__a)




		@slow
		def 						UpperCAmelCase							( self)	->	Optional[int]:






							'''simple docstring'''
							for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_UpperCamelCase										=  DebertaModel.from_pretrained(__a)
												self.assertIsNotNone(__a)






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



		@unittest.skip(reason='''Model not available yet''')
		def 						UpperCAmelCase							( self)	->	Tuple:






							'''simple docstring'''
							pass




		@slow
		def 						UpperCAmelCase							( self)	->	Union[str, Any]:






							'''simple docstring'''
							_UpperCamelCase										=  DebertaModel.from_pretrained('''microsoft/deberta-base''')

							_UpperCamelCase										=  torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]])
							_UpperCamelCase										=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
							with torch.no_grad():
												_UpperCamelCase										=  model(__a    ,		attention_mask=__a)[0]
							# compare the actual values for a slice.
							_UpperCamelCase										=  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]    ,		__a    ,		atol=1e-4)    ,		F'''{output[:, 1:4, 1:4]}''')








"""simple docstring"""





from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_a														=		logging.get_logger(__name__)

_a														=		{
    """facebook/levit-128S""": """https://huggingface.co/facebook/levit-128S/resolve/main/config.json""",
    # See all LeViT models at https://huggingface.co/models?filter=levit
}






class    _UpperCAmelCase(   lowerCamelCase     ):
		lowercase__      =				'levit'



		def __init__( self    ,		__a=2_24    ,		__a=3    ,		__a=3    ,		__a=2    ,		__a=1    ,		__a=16    ,		__a=[1_28, 2_56, 3_84]    ,		__a=[4, 8, 12]    ,		__a=[4, 4, 4]    ,		__a=[16, 16, 16]    ,		__a=0    ,		__a=[2, 2, 2]    ,		__a=[2, 2, 2]    ,		__a=0.02    ,		**__a    ,		)	->	List[str]:






							'''simple docstring'''
							super().__init__(**__a)
							_UpperCamelCase										=  image_size
							_UpperCamelCase										=  num_channels
							_UpperCamelCase										=  kernel_size
							_UpperCamelCase										=  stride
							_UpperCamelCase										=  padding
							_UpperCamelCase										=  hidden_sizes
							_UpperCamelCase										=  num_attention_heads
							_UpperCamelCase										=  depths
							_UpperCamelCase										=  key_dim
							_UpperCamelCase										=  drop_path_rate
							_UpperCamelCase										=  patch_size
							_UpperCamelCase										=  attention_ratio
							_UpperCamelCase										=  mlp_ratio
							_UpperCamelCase										=  initializer_range
							_UpperCamelCase										=  [
							    ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
							    ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
							]






class    _UpperCAmelCase(   lowerCamelCase     ):
		lowercase__      =				version.parse('1.11'     )



		@property
		def 						UpperCAmelCase							( self)	->	Mapping[str, Mapping[int, str]]:






							'''simple docstring'''
							return OrderedDict(
							    [
							        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
							    ])




		@property
		def 						UpperCAmelCase							( self)	->	float:






							'''simple docstring'''
							return 1e-4






