



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







							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=512,    __a=16,    __a=2,    __a=0.02,    __a=False,    __a=True,    __a="None",    __a=3,    __a=4,    __a=None,    ):

														'''simple docstring'''

														_lowerCAmelCase  :							Tuple				     =					parent
														_lowerCAmelCase  :							Dict				     =					batch_size
														_lowerCAmelCase  :							str				     =					seq_length
														_lowerCAmelCase  :							Dict				     =					is_training
														_lowerCAmelCase  :							int				     =					use_input_mask
														_lowerCAmelCase  :							Union[str, Any]				     =					use_token_type_ids
														_lowerCAmelCase  :							List[Any]				     =					use_labels
														_lowerCAmelCase  :							List[str]				     =					vocab_size
														_lowerCAmelCase  :							int				     =					hidden_size
														_lowerCAmelCase  :							Tuple				     =					num_hidden_layers
														_lowerCAmelCase  :							List[str]				     =					num_attention_heads
														_lowerCAmelCase  :							Any				     =					intermediate_size
														_lowerCAmelCase  :							Dict				     =					hidden_act
														_lowerCAmelCase  :							str				     =					hidden_dropout_prob
														_lowerCAmelCase  :							Optional[Any]				     =					attention_probs_dropout_prob
														_lowerCAmelCase  :							Any				     =					max_position_embeddings
														_lowerCAmelCase  :							List[str]				     =					type_vocab_size
														_lowerCAmelCase  :							str				     =					type_sequence_label_size
														_lowerCAmelCase  :							List[Any]				     =					initializer_range
														_lowerCAmelCase  :							str				     =					num_labels
														_lowerCAmelCase  :							List[str]				     =					num_choices
														_lowerCAmelCase  :							Optional[int]				     =					relative_attention
														_lowerCAmelCase  :							int				     =					position_biased_input
														_lowerCAmelCase  :							Dict				     =					pos_att_type
														_lowerCAmelCase  :							Optional[Any]				     =					scope







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Union[str, Any]				     =					ids_tensor([self.batch_size, self.seq_length],    self.vocab_size)

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

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

														_lowerCAmelCase  :							Tuple				     =					None
														_lowerCAmelCase  :							int				     =					None
														_lowerCAmelCase  :							Optional[Any]				     =					None
														if self.use_labels:
																					_lowerCAmelCase  :							List[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  :							List[str]				     =					self.get_config()

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







							def      snake_case__   (     self):

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

														'''simple docstring'''

														_lowerCAmelCase  :							List[str]				     =					self.get_config()
														_lowerCAmelCase  :							int				     =					300
														return config







							def      snake_case__   (     self,    __a):

														'''simple docstring'''

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







							def      snake_case__   (     self,    __a,    __a,    __a,    __a,    __a,    __a,    __a):

														'''simple docstring'''

														_lowerCAmelCase  :							List[Any]				     =					DebertaModel(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase  :							Dict				     =					model(__a,    attention_mask=__a,    token_type_ids=__a)[0]
														_lowerCAmelCase  :							Tuple				     =					model(__a,    token_type_ids=__a)[0]
														_lowerCAmelCase  :							str				     =					model(__a)[0]

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







							def      snake_case__   (     self,    __a,    __a,    __a,    __a,    __a,    __a,    __a):

														'''simple docstring'''

														_lowerCAmelCase  :							Dict				     =					DebertaForMaskedLM(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase  :							int				     =					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      snake_case__   (     self,    __a,    __a,    __a,    __a,    __a,    __a,    __a):

														'''simple docstring'''

														_lowerCAmelCase  :							Dict				     =					self.num_labels
														_lowerCAmelCase  :							Tuple				     =					DebertaForSequenceClassification(__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase  :							str				     =					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      snake_case__   (     self,    __a,    __a,    __a,    __a,    __a,    __a,    __a):

														'''simple docstring'''

														_lowerCAmelCase  :							int				     =					self.num_labels
														_lowerCAmelCase  :							Tuple				     =					DebertaForTokenClassification(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase  :							Optional[Any]				     =					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      snake_case__   (     self,    __a,    __a,    __a,    __a,    __a,    __a,    __a):

														'''simple docstring'''

														_lowerCAmelCase  :							str				     =					DebertaForQuestionAnswering(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase  :							Tuple				     =					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      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Optional[Any]				     =					self.prepare_config_and_inputs()
														(
														    (
														    _lowerCAmelCase
														)     ,					(
														    _lowerCAmelCase
														)     ,					(
														    _lowerCAmelCase
														)     ,					(
														    _lowerCAmelCase
														)     ,					(
														    _lowerCAmelCase
														)     ,					(
														    _lowerCAmelCase
														)     ,					(
														    _lowerCAmelCase
														)     ,					
														)  :							Optional[Any]				     =					config_and_inputs
														_lowerCAmelCase  :							Union[str, Any]				     =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
														return config, inputs_dict



@require_torch
class 						UpperCAmelCase_					( a     ,      a     ,      unittest.TestCase):
							lowerCamelCase__         =	(
							    (
							        DebertaModel,
							        DebertaForMaskedLM,
							        DebertaForSequenceClassification,
							        DebertaForTokenClassification,
							        DebertaForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							lowerCamelCase__         =	(
							    {
							        'feature-extraction': DebertaModel,
							        'fill-mask': DebertaForMaskedLM,
							        'question-answering': DebertaForQuestionAnswering,
							        'text-classification': DebertaForSequenceClassification,
							        'token-classification': DebertaForTokenClassification,
							        'zero-shot': DebertaForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)

							lowerCamelCase__         =	True
							lowerCamelCase__         =	False
							lowerCamelCase__         =	False
							lowerCamelCase__         =	False
							lowerCamelCase__         =	False







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Union[str, Any]				     =					DebertaModelTester(self)
														_lowerCAmelCase  :							Optional[int]				     =					ConfigTester(self,    config_class=__a,    hidden_size=37)







							def      snake_case__   (     self):

														'''simple docstring'''

														self.config_tester.run_common_tests()







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Union[str, Any]				     =					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_model(*__a)







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Optional[Any]				     =					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_for_sequence_classification(*__a)







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Tuple				     =					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_for_masked_lm(*__a)







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Union[str, Any]				     =					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_for_question_answering(*__a)







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							int				     =					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_for_token_classification(*__a)







							@slow
							def      snake_case__   (     self):

														'''simple docstring'''

														for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_lowerCAmelCase  :							Optional[Any]				     =					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      snake_case__   (     self):

														'''simple docstring'''

														pass







							@slow
							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							int				     =					DebertaModel.from_pretrained("microsoft/deberta-base")

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





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


_snake_case	=						logging.get_logger(__name__)

_snake_case	=						{
    "microsoft/focalnet-tiny": "https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json",
}



class 						UpperCAmelCase_					( a     ,      a):
							lowerCamelCase__         =	'focalnet'







							def __init__(     self,    __a=224,    __a=4,    __a=3,    __a=96,    __a=False,    __a=[192, 384, 768, 768],    __a=[2, 2, 6, 2],    __a=[2, 2, 2, 2],    __a=[3, 3, 3, 3],    __a="gelu",    __a=4.0,    __a=0.0,    __a=0.1,    __a=False,    __a=1E-4,    __a=False,    __a=False,    __a=False,    __a=0.02,    __a=1E-5,    __a=32,    __a=None,    __a=None,    **__a,    ):

														'''simple docstring'''

														super().__init__(**__a)

														_lowerCAmelCase  :							str				     =					image_size
														_lowerCAmelCase  :							List[str]				     =					patch_size
														_lowerCAmelCase  :							List[Any]				     =					num_channels
														_lowerCAmelCase  :							Tuple				     =					embed_dim
														_lowerCAmelCase  :							List[Any]				     =					use_conv_embed
														_lowerCAmelCase  :							Any				     =					hidden_sizes
														_lowerCAmelCase  :							Tuple				     =					depths
														_lowerCAmelCase  :							Dict				     =					focal_levels
														_lowerCAmelCase  :							Optional[Any]				     =					focal_windows
														_lowerCAmelCase  :							str				     =					hidden_act
														_lowerCAmelCase  :							Union[str, Any]				     =					mlp_ratio
														_lowerCAmelCase  :							Any				     =					hidden_dropout_prob
														_lowerCAmelCase  :							Dict				     =					drop_path_rate
														_lowerCAmelCase  :							str				     =					use_layerscale
														_lowerCAmelCase  :							str				     =					layerscale_value
														_lowerCAmelCase  :							Union[str, Any]				     =					use_post_layernorm
														_lowerCAmelCase  :							Optional[int]				     =					use_post_layernorm_in_modulation
														_lowerCAmelCase  :							str				     =					normalize_modulator
														_lowerCAmelCase  :							Any				     =					initializer_range
														_lowerCAmelCase  :							Union[str, Any]				     =					layer_norm_eps
														_lowerCAmelCase  :							Any				     =					encoder_stride
														_lowerCAmelCase  :							List[str]				     =					["stem"] + [f"stage{idx}" for idx in range(1,    len(self.depths) + 1)]
														_lowerCAmelCase     ,					_lowerCAmelCase  :							List[str]				     =					get_aligned_output_features_output_indices(
														    out_features=__a,    out_indices=__a,    stage_names=self.stage_names)
