




'''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 						a				(   _lowerCamelCase     ):


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


				def   A_ (		self    :					Any					):
									snake_case_									=				ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size					)

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

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

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

									snake_case_									=				self.get_config()

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


				def   A_ (		self    :					Dict					):
									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   A_ (		self    :					List[str]					):
									snake_case_									=				self.get_config()
									snake_case_									=				300
									return config


				def   A_ (		self    :					Tuple       ,				lowercase_    :					List[str]					):
									self.parent.assertListEqual(list(result.loss.size()					)       ,				[]					)


				def   A_ (		self    :					Tuple       ,				lowercase_    :					List[Any]       ,				lowercase_    :					Tuple       ,				lowercase_    :					List[str]       ,				lowercase_    :					Any       ,				lowercase_    :					Dict       ,				lowercase_    :					Tuple       ,				lowercase_    :					Optional[Any]					):
									snake_case_									=				DebertaModel(config=lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				model(lowercase_       ,				attention_mask=lowercase_       ,				token_type_ids=lowercase_					)[0]
									snake_case_									=				model(lowercase_       ,				token_type_ids=lowercase_					)[0]
									snake_case_									=				model(lowercase_					)[0]

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


				def   A_ (		self    :					Any       ,				lowercase_    :					Optional[Any]       ,				lowercase_    :					Union[str, Any]       ,				lowercase_    :					Union[str, Any]       ,				lowercase_    :					List[str]       ,				lowercase_    :					List[str]       ,				lowercase_    :					List[Any]       ,				lowercase_    :					Any					):
									snake_case_									=				DebertaForMaskedLM(config=lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				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   A_ (		self    :					Optional[int]       ,				lowercase_    :					List[str]       ,				lowercase_    :					Tuple       ,				lowercase_    :					List[str]       ,				lowercase_    :					Dict       ,				lowercase_    :					Union[str, Any]       ,				lowercase_    :					Dict       ,				lowercase_    :					Optional[Any]					):
									snake_case_									=				self.num_labels
									snake_case_									=				DebertaForSequenceClassification(lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				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   A_ (		self    :					List[Any]       ,				lowercase_    :					List[str]       ,				lowercase_    :					Tuple       ,				lowercase_    :					Dict       ,				lowercase_    :					List[Any]       ,				lowercase_    :					List[Any]       ,				lowercase_    :					Dict       ,				lowercase_    :					List[str]					):
									snake_case_									=				self.num_labels
									snake_case_									=				DebertaForTokenClassification(config=lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				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   A_ (		self    :					List[str]       ,				lowercase_    :					List[str]       ,				lowercase_    :					int       ,				lowercase_    :					Optional[int]       ,				lowercase_    :					Tuple       ,				lowercase_    :					Dict       ,				lowercase_    :					Optional[int]       ,				lowercase_    :					Optional[Any]					):
									snake_case_									=				DebertaForQuestionAnswering(config=lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				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   A_ (		self    :					Tuple					):
									snake_case_									=				self.prepare_config_and_inputs()
									(
									    (
									    snake_case_
									)			,(
									    snake_case_
									)			,(
									    snake_case_
									)			,(
									    snake_case_
									)			,(
									    snake_case_
									)			,(
									    snake_case_
									)			,(
									    snake_case_
									)			,
									)									=				config_and_inputs
									snake_case_									=				{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
									return config, inputs_dict







@require_torch
class 						a				(   _lowerCamelCase   ,     _lowerCamelCase   ,     unittest.TestCase     ):
				snake_case_      =		(
				    (
				        DebertaModel,
				        DebertaForMaskedLM,
				        DebertaForSequenceClassification,
				        DebertaForTokenClassification,
				        DebertaForQuestionAnswering,
				    )
				    if is_torch_available()
				    else ()
				)
				snake_case_      =		(
				    {
				        "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_      =		True
				snake_case_      =		False
				snake_case_      =		False
				snake_case_      =		False
				snake_case_      =		False


				def   A_ (		self    :					Optional[Any]					):
									snake_case_									=				DebertaModelTester(self					)
									snake_case_									=				ConfigTester(self       ,				config_class=lowercase_       ,				hidden_size=37					)


				def   A_ (		self    :					List[Any]					):
									self.config_tester.run_common_tests()


				def   A_ (		self    :					int					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_model(*lowercase_					)


				def   A_ (		self    :					Tuple					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_sequence_classification(*lowercase_					)


				def   A_ (		self    :					int					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_masked_lm(*lowercase_					)


				def   A_ (		self    :					int					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_question_answering(*lowercase_					)


				def   A_ (		self    :					Union[str, Any]					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_token_classification(*lowercase_					)


				@slow
				def   A_ (		self    :					List[str]					):
									for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														snake_case_									=				DebertaModel.from_pretrained(lowercase_					)
														self.assertIsNotNone(lowercase_					)







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


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


				@slow
				def   A_ (		self    :					str					):
									snake_case_									=				DebertaModel.from_pretrained('''microsoft/deberta-base'''					)

									snake_case_									=				torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]					)
									snake_case_									=				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
									with torch.no_grad():
														snake_case_									=				model(lowercase_       ,				attention_mask=lowercase_					)[0]
									# compare the actual values for a slice.
									snake_case_									=				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"""



from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


a_ =			logging.get_logger(__name__)

a_ =			{
    """shi-labs/nat-mini-in1k-224""": """https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json""",
    # See all Nat models at https://huggingface.co/models?filter=nat
}






class 		__snake_case					(		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 _lowerCamelCase        =     """nat"""

 _lowerCamelCase        =     {
     """num_attention_heads""": """num_heads""",
     """num_hidden_layers""": """num_layers""",
 }

 def __init__(    self					,						__lowerCamelCase=4					,						__lowerCamelCase=3					,						__lowerCamelCase=64					,						__lowerCamelCase=[3, 4, 6, 5]					,						__lowerCamelCase=[2, 4, 8, 16]					,						__lowerCamelCase=7					,						__lowerCamelCase=3.0					,						__lowerCamelCase=True					,						__lowerCamelCase=0.0					,						__lowerCamelCase=0.0					,						__lowerCamelCase=0.1					,						__lowerCamelCase="gelu"					,						__lowerCamelCase=0.0_2					,						__lowerCamelCase=1e-5					,						__lowerCamelCase=0.0					,						__lowerCamelCase=None					,						__lowerCamelCase=None					,						**__lowerCamelCase					,						):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase  )

     __A    :						Union[str, Any]						 =     patch_size
     __A    :						Optional[Any]						 =     num_channels
     __A    :						Tuple						 =     embed_dim
     __A    :						Dict						 =     depths
     __A    :						str						 =     len(__lowerCamelCase  )
     __A    :						Optional[Any]						 =     num_heads
     __A    :						str						 =     kernel_size
     __A    :						Any						 =     mlp_ratio
     __A    :						Optional[int]						 =     qkv_bias
     __A    :						str						 =     hidden_dropout_prob
     __A    :						Any						 =     attention_probs_dropout_prob
     __A    :						int						 =     drop_path_rate
     __A    :						int						 =     hidden_act
     __A    :						Any						 =     layer_norm_eps
     __A    :						Tuple						 =     initializer_range
     # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
     # this indicates the channel dimension after the last stage of the model
     __A    :						int						 =     int(embed_dim * 2 ** (len(__lowerCamelCase  ) - 1)  )
     __A    :						Union[str, Any]						 =     layer_scale_init_value
     __A    :						List[str]						 =     ['''stem'''] + [F"""stage{idx}""" for idx in range(1					,						len(__lowerCamelCase  ) + 1  )]
     __A ,       __A    :						Any						 =     get_aligned_output_features_output_indices(
         out_features=__lowerCamelCase					,						out_indices=__lowerCamelCase					,						stage_names=self.stage_names  )




