





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


		"""simple docstring"""







		def __init__(				self    :					str							,	UpperCamelCase__    :					str							,	UpperCamelCase__    :					int=13							,	UpperCamelCase__    :					List[Any]=7							,	UpperCamelCase__    :					int=True							,	UpperCamelCase__    :					Optional[int]=True							,	UpperCamelCase__    :					List[Any]=True							,	UpperCamelCase__    :					List[Any]=True							,	UpperCamelCase__    :					Any=99							,	UpperCamelCase__    :					str=32							,	UpperCamelCase__    :					List[Any]=5							,	UpperCamelCase__    :					int=4							,	UpperCamelCase__    :					List[str]=37							,	UpperCamelCase__    :					List[Any]="gelu"							,	UpperCamelCase__    :					List[Any]=0.1							,	UpperCamelCase__    :					str=0.1							,	UpperCamelCase__    :					Dict=512							,	UpperCamelCase__    :					Any=16							,	UpperCamelCase__    :					str=2							,	UpperCamelCase__    :					List[Any]=0.02							,	UpperCamelCase__    :					List[str]=False							,	UpperCamelCase__    :					List[str]=True							,	UpperCamelCase__    :					Union[str, Any]="None"							,	UpperCamelCase__    :					Union[str, Any]=3							,	UpperCamelCase__    :					Optional[int]=4							,	UpperCamelCase__    :					List[str]=None							,	)     ->						str:
				_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    :					Dict  )     ->						str:
				_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    :					List[Any]  )     ->						Tuple:
				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    :					Optional[Any]  )     ->						int:
				_UpperCamelCase       			=self.get_config()
				_UpperCamelCase       			=300
				return config


		def   UpperCamelCase__      (				self    :					Optional[Any]							,	UpperCamelCase__    :					Any  )     ->						Tuple:
				self.parent.assertListEqual(list(result.loss.size()  )							,	[]  )


		def   UpperCamelCase__      (				self    :					Tuple							,	UpperCamelCase__    :					Dict							,	UpperCamelCase__    :					List[str]							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					List[Any]							,	UpperCamelCase__    :					List[Any]							,	UpperCamelCase__    :					Optional[int]							,	UpperCamelCase__    :					str  )     ->						Any:
				_UpperCamelCase       			=DebertaModel(config=UpperCamelCase_  )
				model.to(UpperCamelCase_  )
				model.eval()
				_UpperCamelCase       			=model(UpperCamelCase_							,	attention_mask=UpperCamelCase_							,	token_type_ids=UpperCamelCase_  )[0]
				_UpperCamelCase       			=model(UpperCamelCase_							,	token_type_ids=UpperCamelCase_  )[0]
				_UpperCamelCase       			=model(UpperCamelCase_  )[0]

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


		def   UpperCamelCase__      (				self    :					int							,	UpperCamelCase__    :					Optional[int]							,	UpperCamelCase__    :					Optional[Any]							,	UpperCamelCase__    :					str							,	UpperCamelCase__    :					List[Any]							,	UpperCamelCase__    :					Optional[Any]							,	UpperCamelCase__    :					Tuple							,	UpperCamelCase__    :					List[Any]  )     ->						str:
				_UpperCamelCase       			=DebertaForMaskedLM(config=UpperCamelCase_  )
				model.to(UpperCamelCase_  )
				model.eval()
				_UpperCamelCase       			=model(UpperCamelCase_							,	attention_mask=UpperCamelCase_							,	token_type_ids=UpperCamelCase_							,	labels=UpperCamelCase_  )

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


		def   UpperCamelCase__      (				self    :					Union[str, Any]							,	UpperCamelCase__    :					List[Any]							,	UpperCamelCase__    :					Optional[int]							,	UpperCamelCase__    :					Any							,	UpperCamelCase__    :					str							,	UpperCamelCase__    :					str							,	UpperCamelCase__    :					Any							,	UpperCamelCase__    :					List[Any]  )     ->						str:
				_UpperCamelCase       			=self.num_labels
				_UpperCamelCase       			=DebertaForSequenceClassification(UpperCamelCase_  )
				model.to(UpperCamelCase_  )
				model.eval()
				_UpperCamelCase       			=model(UpperCamelCase_							,	attention_mask=UpperCamelCase_							,	token_type_ids=UpperCamelCase_							,	labels=UpperCamelCase_  )
				self.parent.assertListEqual(list(result.logits.size()  )							,	[self.batch_size, self.num_labels]  )
				self.check_loss_output(UpperCamelCase_  )


		def   UpperCamelCase__      (				self    :					Union[str, Any]							,	UpperCamelCase__    :					Dict							,	UpperCamelCase__    :					Union[str, Any]							,	UpperCamelCase__    :					Dict							,	UpperCamelCase__    :					Tuple							,	UpperCamelCase__    :					Tuple							,	UpperCamelCase__    :					Any							,	UpperCamelCase__    :					str  )     ->						Dict:
				_UpperCamelCase       			=self.num_labels
				_UpperCamelCase       			=DebertaForTokenClassification(config=UpperCamelCase_  )
				model.to(UpperCamelCase_  )
				model.eval()
				_UpperCamelCase       			=model(UpperCamelCase_							,	attention_mask=UpperCamelCase_							,	token_type_ids=UpperCamelCase_							,	labels=UpperCamelCase_  )
				self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.seq_length, self.num_labels)  )


		def   UpperCamelCase__      (				self    :					Union[str, Any]							,	UpperCamelCase__    :					Any							,	UpperCamelCase__    :					Optional[int]							,	UpperCamelCase__    :					Dict							,	UpperCamelCase__    :					List[Any]							,	UpperCamelCase__    :					Any							,	UpperCamelCase__    :					Tuple							,	UpperCamelCase__    :					List[str]  )     ->						Dict:
				_UpperCamelCase       			=DebertaForQuestionAnswering(config=UpperCamelCase_  )
				model.to(UpperCamelCase_  )
				model.eval()
				_UpperCamelCase       			=model(
				    UpperCamelCase_							,	attention_mask=UpperCamelCase_							,	token_type_ids=UpperCamelCase_							,	start_positions=UpperCamelCase_							,	end_positions=UpperCamelCase_							,	)
				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]  )     ->						List[Any]:
				_UpperCamelCase       			=self.prepare_config_and_inputs()
				(
				    _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   (				lowercase_   ,     lowercase_   ,     unittest.TestCase):


		"""simple docstring"""







		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   UpperCamelCase__      (				self    :					List[Any]  )     ->						Dict:
				_UpperCamelCase       			=DebertaModelTester(self  )
				_UpperCamelCase       			=ConfigTester(self							,	config_class=UpperCamelCase_							,	hidden_size=37  )


		def   UpperCamelCase__      (				self    :					int  )     ->						Union[str, Any]:
				self.config_tester.run_common_tests()


		def   UpperCamelCase__      (				self    :					List[str]  )     ->						str:
				_UpperCamelCase       			=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_model(*UpperCamelCase_  )


		def   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						Optional[Any]:
				_UpperCamelCase       			=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCamelCase_  )


		def   UpperCamelCase__      (				self    :					int  )     ->						Union[str, Any]:
				_UpperCamelCase       			=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCamelCase_  )


		def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						Any:
				_UpperCamelCase       			=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_question_answering(*UpperCamelCase_  )


		def   UpperCamelCase__      (				self    :					List[Any]  )     ->						Tuple:
				_UpperCamelCase       			=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_token_classification(*UpperCamelCase_  )




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





				for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						_UpperCamelCase       			=DebertaModel.from_pretrained(UpperCamelCase_  )
						self.assertIsNotNone(UpperCamelCase_  )







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


		"""simple docstring"""







		@unittest.skip(reason='''Model not available yet'''  )
		def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						Union[str, Any]:
				pass




		@slow
		def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						Any:
				_UpperCamelCase       			=DebertaModel.from_pretrained('''microsoft/deberta-base'''  )

				_UpperCamelCase       			=torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]  )
				_UpperCamelCase       			=torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
				with torch.no_grad():
						_UpperCamelCase       			=model(UpperCamelCase_							,	attention_mask=UpperCamelCase_  )[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]							,	UpperCamelCase_							,	atol=1E-4  )							,	F'''{output[:, 1:4, 1:4]}'''  )













'''simple docstring'''






# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowerCamelCase						: Optional[int]		=   {
    'configuration_xmod': [
        'XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'XmodConfig',
        'XmodOnnxConfig',
    ],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __lowerCamelCase						: int		=   [
          'XMOD_PRETRAINED_MODEL_ARCHIVE_LIST',
          'XmodForCausalLM',
          'XmodForMaskedLM',
          'XmodForMultipleChoice',
          'XmodForQuestionAnswering',
          'XmodForSequenceClassification',
          'XmodForTokenClassification',
          'XmodModel',
          'XmodPreTrainedModel',
      ]

if TYPE_CHECKING:
      from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_xmod import (
                XMOD_PRETRAINED_MODEL_ARCHIVE_LIST,
                XmodForCausalLM,
                XmodForMaskedLM,
                XmodForMultipleChoice,
                XmodForQuestionAnswering,
                XmodForSequenceClassification,
                XmodForTokenClassification,
                XmodModel,
                XmodPreTrainedModel,
            )

else:
      import sys

      __lowerCamelCase						: Dict		=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






