


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






       def __init__( self			,     _UpperCamelCase			,     _UpperCamelCase=13			,     _UpperCamelCase=7			,     _UpperCamelCase=True			,     _UpperCamelCase=True			,     _UpperCamelCase=True			,     _UpperCamelCase=True			,     _UpperCamelCase=99			,     _UpperCamelCase=32			,     _UpperCamelCase=5			,     _UpperCamelCase=4			,     _UpperCamelCase=37			,     _UpperCamelCase="gelu"			,     _UpperCamelCase=0.1			,     _UpperCamelCase=0.1			,     _UpperCamelCase=512			,     _UpperCamelCase=16			,     _UpperCamelCase=2			,     _UpperCamelCase=0.02			,     _UpperCamelCase=False			,     _UpperCamelCase=True			,     _UpperCamelCase="None"			,     _UpperCamelCase=3			,     _UpperCamelCase=4			,     _UpperCamelCase=None			,     )->      Any:
            _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				=							relative_attention
            _A				=							position_biased_input
            _A				=							pos_att_type
            _A				=							scope






       def   UpperCamelCase						( self   )->      Union[str, Any]:
            _A				=							ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size   )

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

            _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   )->      int:
            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   )->      str:
            _A				=							self.get_config()
            _A				=							300
            return config






       def   UpperCamelCase						( self			,     _UpperCamelCase   )->      List[Any]:
            self.parent.assertListEqual(list(result.loss.size()   )			,     []   )






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      Tuple:
            _A				=							DebertaModel(config=_UpperCamelCase   )
            model.to(_UpperCamelCase   )
            model.eval()
            _A				=							model(_UpperCamelCase			,     attention_mask=_UpperCamelCase			,     token_type_ids=_UpperCamelCase   )[0]
            _A				=							model(_UpperCamelCase			,     token_type_ids=_UpperCamelCase   )[0]
            _A				=							model(_UpperCamelCase   )[0]

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






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      Tuple:
            _A				=							DebertaForMaskedLM(config=_UpperCamelCase   )
            model.to(_UpperCamelCase   )
            model.eval()
            _A				=							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			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      List[str]:
            _A				=							self.num_labels
            _A				=							DebertaForSequenceClassification(_UpperCamelCase   )
            model.to(_UpperCamelCase   )
            model.eval()
            _A				=							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			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      str:
            _A				=							self.num_labels
            _A				=							DebertaForTokenClassification(config=_UpperCamelCase   )
            model.to(_UpperCamelCase   )
            model.eval()
            _A				=							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			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      Optional[Any]:
            _A				=							DebertaForQuestionAnswering(config=_UpperCamelCase   )
            model.to(_UpperCamelCase   )
            model.eval()
            _A				=							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   )->      Tuple:
            _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        lowerCAmelCase_  (     UpperCAmelCase      ,							UpperCAmelCase      ,							unittest.TestCase	):
       __UpperCAmelCase					=(
           (
               DebertaModel,
               DebertaForMaskedLM,
               DebertaForSequenceClassification,
               DebertaForTokenClassification,
               DebertaForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       __UpperCAmelCase					=(
           {
               'feature-extraction': DebertaModel,
               'fill-mask': DebertaForMaskedLM,
               'question-answering': DebertaForQuestionAnswering,
               'text-classification': DebertaForSequenceClassification,
               'token-classification': DebertaForTokenClassification,
               'zero-shot': DebertaForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )

       __UpperCAmelCase					=True
       __UpperCAmelCase					=False
       __UpperCAmelCase					=False
       __UpperCAmelCase					=False
       __UpperCAmelCase					=False






       def   UpperCamelCase						( self   )->      Optional[int]:
            _A				=							DebertaModelTester(self   )
            _A				=							ConfigTester(self			,     config_class=_UpperCamelCase			,     hidden_size=37   )






       def   UpperCamelCase						( self   )->      List[str]:
            self.config_tester.run_common_tests()






       def   UpperCamelCase						( self   )->      Any:
            _A				=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_deberta_model(*_UpperCamelCase   )






       def   UpperCamelCase						( self   )->      Tuple:
            _A				=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_deberta_for_sequence_classification(*_UpperCamelCase   )






       def   UpperCamelCase						( self   )->      List[Any]:
            _A				=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_deberta_for_masked_lm(*_UpperCamelCase   )






       def   UpperCamelCase						( self   )->      Tuple:
            _A				=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_deberta_for_question_answering(*_UpperCamelCase   )






       def   UpperCamelCase						( self   )->      Tuple:
            _A				=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_deberta_for_token_classification(*_UpperCamelCase   )






       @slow
       def   UpperCamelCase						( self   )->      str:
            for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _A				=							DebertaModel.from_pretrained(_UpperCamelCase   )
                 self.assertIsNotNone(_UpperCamelCase   )






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






       @unittest.skip(reason='Model not available yet'   )
       def   UpperCamelCase						( self   )->      List[Any]:
            pass






       @slow
       def   UpperCamelCase						( self   )->      int:
            _A				=							DebertaModel.from_pretrained('microsoft/deberta-base'   )

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

def 					lowerCamelCase_ ( __UpperCamelCase					:       list							,    __UpperCamelCase					:       int = 3		)					->	list:







     """simple docstring"""
     _A				=							min(__UpperCamelCase		)
     _A				=							max(__UpperCamelCase		)
     # normalize data
     return [round((x - x_min) / (x_max - x_min)							,    __UpperCamelCase		) for x in data]

def 					lowerCamelCase_ ( __UpperCamelCase					:       list							,    __UpperCamelCase					:       int = 3		)					->	list:







     """simple docstring"""
     _A				=							mean(__UpperCamelCase		)
     _A				=							stdev(__UpperCamelCase		)
     # standardize data
     return [round((x - mu) / (sigma)							,    __UpperCamelCase		) for x in data]





