



"""simple docstring"""


import unittest

from transformers import DebertaVaConfig, 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 (
						    DebertaVaForMaskedLM,
						    DebertaVaForMultipleChoice,
						    DebertaVaForQuestionAnswering,
						    DebertaVaForSequenceClassification,
						    DebertaVaForTokenClassification,
						    DebertaVaModel,
						)
						from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST
class 			_a (					lowerCAmelCase):
			"""simple docstring"""



			def __init__(		self     :    Optional[int]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    List[str]=1_3				,   __UpperCamelCase     :    str=7				,   __UpperCamelCase     :    Any=True				,   __UpperCamelCase     :    Optional[Any]=True				,   __UpperCamelCase     :    Optional[Any]=True				,   __UpperCamelCase     :    Any=True				,   __UpperCamelCase     :    Optional[int]=9_9				,   __UpperCamelCase     :    Any=3_2				,   __UpperCamelCase     :    Union[str, Any]=5				,   __UpperCamelCase     :    Dict=4				,   __UpperCamelCase     :    Union[str, Any]=3_7				,   __UpperCamelCase     :    List[Any]="gelu"				,   __UpperCamelCase     :    str=0.1				,   __UpperCamelCase     :    List[Any]=0.1				,   __UpperCamelCase     :    List[Any]=5_1_2				,   __UpperCamelCase     :    str=1_6				,   __UpperCamelCase     :    Union[str, Any]=2				,   __UpperCamelCase     :    List[Any]=0.0_2				,   __UpperCamelCase     :    int=False				,   __UpperCamelCase     :    Union[str, Any]=True				,   __UpperCamelCase     :    Tuple="None"				,   __UpperCamelCase     :    Any=3				,   __UpperCamelCase     :    Any=4				,   __UpperCamelCase     :    int=None				,   )->Optional[int]:
							_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  lowercase__						(		self     :    Optional[int]   )->int:
							_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  lowercase__						(		self     :    Any   )->Dict:
							return DebertaVaConfig(
							    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  lowercase__						(		self     :    List[str]				,   __UpperCamelCase     :    Dict   )->Union[str, Any]:
							self.parent.assertListEqual(list(result.loss.size()   )				,   []   )



			def  lowercase__						(		self     :    Union[str, Any]				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    Optional[Any]   )->Tuple:
							_UpperCAmelCase					=    DebertaVaModel(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  lowercase__						(		self     :    List[Any]				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    Optional[Any]				,   __UpperCamelCase     :    Optional[Any]   )->Tuple:
							_UpperCAmelCase					=    DebertaVaForMaskedLM(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  lowercase__						(		self     :    str				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    List[str]   )->str:
							_UpperCAmelCase					=    self.num_labels
							_UpperCAmelCase					=    DebertaVaForSequenceClassification(__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  lowercase__						(		self     :    List[Any]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    List[Any]   )->Dict:
							_UpperCAmelCase					=    self.num_labels
							_UpperCAmelCase					=    DebertaVaForTokenClassification(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  lowercase__						(		self     :    int				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    str   )->Dict:
							_UpperCAmelCase					=    DebertaVaForQuestionAnswering(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  lowercase__						(		self     :    Union[str, Any]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Optional[Any]				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    Optional[int]   )->Tuple:
							_UpperCAmelCase					=    DebertaVaForMultipleChoice(config=__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							model.eval()
							_UpperCAmelCase					=    input_ids.unsqueeze(1   ).expand(-1				,   self.num_choices				,   -1   ).contiguous()
							_UpperCAmelCase					=    token_type_ids.unsqueeze(1   ).expand(-1				,   self.num_choices				,   -1   ).contiguous()
							_UpperCAmelCase					=    input_mask.unsqueeze(1   ).expand(-1				,   self.num_choices				,   -1   ).contiguous()
							_UpperCAmelCase					=    model(
							    __UpperCamelCase				,   attention_mask=__UpperCamelCase				,   token_type_ids=__UpperCamelCase				,   labels=__UpperCamelCase				,   )
							self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.num_choices)   )



			def  lowercase__						(		self     :    Any   )->List[str]:
							_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 			_a (					lowerCAmelCase			,							lowerCAmelCase			,							unittest.TestCase):
			"""simple docstring"""
			UpperCamelCase__			      =	(
			    (
			        DebertaVaModel,
			        DebertaVaForMaskedLM,
			        DebertaVaForSequenceClassification,
			        DebertaVaForTokenClassification,
			        DebertaVaForQuestionAnswering,
			        DebertaVaForMultipleChoice,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase__			      =	(
			    {
			        """feature-extraction""": DebertaVaModel,
			        """fill-mask""": DebertaVaForMaskedLM,
			        """question-answering""": DebertaVaForQuestionAnswering,
			        """text-classification""": DebertaVaForSequenceClassification,
			        """token-classification""": DebertaVaForTokenClassification,
			        """zero-shot""": DebertaVaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			UpperCamelCase__			      =	True
			UpperCamelCase__			      =	False
			UpperCamelCase__			      =	False
			UpperCamelCase__			      =	False
			UpperCamelCase__			      =	False



			def  lowercase__						(		self     :    str   )->str:
							_UpperCAmelCase					=    DebertaVaModelTester(self   )
							_UpperCAmelCase					=    ConfigTester(self				,   config_class=__UpperCamelCase				,   hidden_size=3_7   )



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



			def  lowercase__						(		self     :    List[Any]   )->Tuple:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_model(*__UpperCamelCase   )



			def  lowercase__						(		self     :    List[Any]   )->Optional[Any]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_sequence_classification(*__UpperCamelCase   )



			def  lowercase__						(		self     :    Tuple   )->Any:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_masked_lm(*__UpperCamelCase   )



			def  lowercase__						(		self     :    Dict   )->Optional[Any]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_question_answering(*__UpperCamelCase   )



			def  lowercase__						(		self     :    str   )->str:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_token_classification(*__UpperCamelCase   )



			def  lowercase__						(		self     :    Optional[int]   )->Any:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_multiple_choice(*__UpperCamelCase   )



			@slow
			def  lowercase__						(		self     :    Union[str, Any]   )->Union[str, Any]:
							for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											_UpperCAmelCase					=    DebertaVaModel.from_pretrained(__UpperCamelCase   )
											self.assertIsNotNone(__UpperCamelCase   )



@require_torch
@require_sentencepiece
@require_tokenizers
class 			_a (					unittest.TestCase):
			"""simple docstring"""



			@unittest.skip(reason='''Model not available yet'''   )
			def  lowercase__						(		self     :    int   )->Any:
							pass



			@slow
			def  lowercase__						(		self     :    Any   )->Union[str, Any]:
							_UpperCAmelCase					=    DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge'''   )

							_UpperCAmelCase					=    torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 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.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]   )
							self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,   __UpperCamelCase				,   atol=1e-4   )				,   F'{output[:, 1:4, 1:4]}'   )








"""simple docstring"""


from typing import List, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A   :							Any										=   logging.get_logger(__name__)

__A   :							Dict										=   {
    "huggingface/informer-tourism-monthly": (
        "https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json"
    ),
    # See all Informer models at https://huggingface.co/models?filter=informer
}
class 			_a (					lowerCAmelCase):
			"""simple docstring"""
			UpperCamelCase__			      =	"""informer"""
			UpperCamelCase__			      =	{
			    """hidden_size""": """d_model""",
			    """num_attention_heads""": """encoder_attention_heads""",
			    """num_hidden_layers""": """encoder_layers""",
			}



			def __init__(		self     :    List[str]				,   __UpperCamelCase     :    Optional[int] = None				,   __UpperCamelCase     :    Optional[int] = None				,   __UpperCamelCase     :    str = "student_t"				,   __UpperCamelCase     :    str = "nll"				,   __UpperCamelCase     :    int = 1				,   __UpperCamelCase     :    List[int] = None				,   __UpperCamelCase     :    Optional[Union[str, bool]] = "mean"				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    Optional[List[int]] = None				,   __UpperCamelCase     :    Optional[List[int]] = None				,   __UpperCamelCase     :    int = 6_4				,   __UpperCamelCase     :    int = 3_2				,   __UpperCamelCase     :    int = 3_2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    bool = True				,   __UpperCamelCase     :    str = "gelu"				,   __UpperCamelCase     :    float = 0.0_5				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    int = 1_0_0				,   __UpperCamelCase     :    float = 0.0_2				,   __UpperCamelCase     :    Optional[Any]=True				,   __UpperCamelCase     :    str = "prob"				,   __UpperCamelCase     :    int = 5				,   __UpperCamelCase     :    bool = True				,   **__UpperCamelCase     :    Union[str, Any]				,   )->Dict:
							# time series specific configuration
							_UpperCAmelCase					=    prediction_length
							_UpperCAmelCase					=    context_length or prediction_length
							_UpperCAmelCase					=    distribution_output
							_UpperCAmelCase					=    loss
							_UpperCAmelCase					=    input_size
							_UpperCAmelCase					=    num_time_features
							_UpperCAmelCase					=    lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
							_UpperCAmelCase					=    scaling
							_UpperCAmelCase					=    num_dynamic_real_features
							_UpperCAmelCase					=    num_static_real_features
							_UpperCAmelCase					=    num_static_categorical_features

							# set cardinality
							if cardinality and num_static_categorical_features > 0:
											if len(__UpperCamelCase   ) != num_static_categorical_features:
															raise ValueError(
															    '''The cardinality should be a list of the same length as `num_static_categorical_features`'''   )
											_UpperCAmelCase					=    cardinality
							else:
											_UpperCAmelCase					=    [0]

							# set embedding_dimension
							if embedding_dimension and num_static_categorical_features > 0:
											if len(__UpperCamelCase   ) != num_static_categorical_features:
															raise ValueError(
															    '''The embedding dimension should be a list of the same length as `num_static_categorical_features`'''   )
											_UpperCAmelCase					=    embedding_dimension
							else:
											_UpperCAmelCase					=    [min(5_0				,   (cat + 1) // 2   ) for cat in self.cardinality]

							_UpperCAmelCase					=    num_parallel_samples

							# Transformer architecture configuration
							_UpperCAmelCase					=    input_size * len(self.lags_sequence   ) + self._number_of_features
							_UpperCAmelCase					=    d_model
							_UpperCAmelCase					=    encoder_attention_heads
							_UpperCAmelCase					=    decoder_attention_heads
							_UpperCAmelCase					=    encoder_ffn_dim
							_UpperCAmelCase					=    decoder_ffn_dim
							_UpperCAmelCase					=    encoder_layers
							_UpperCAmelCase					=    decoder_layers

							_UpperCAmelCase					=    dropout
							_UpperCAmelCase					=    attention_dropout
							_UpperCAmelCase					=    activation_dropout
							_UpperCAmelCase					=    encoder_layerdrop
							_UpperCAmelCase					=    decoder_layerdrop

							_UpperCAmelCase					=    activation_function
							_UpperCAmelCase					=    init_std

							_UpperCAmelCase					=    use_cache

							# Informer
							_UpperCAmelCase					=    attention_type
							_UpperCAmelCase					=    sampling_factor
							_UpperCAmelCase					=    distil

							super().__init__(is_encoder_decoder=__UpperCamelCase				,   **__UpperCamelCase   )



			@property
			def  lowercase__						(		self     :    Optional[int]   )->int:
							return (
							    sum(self.embedding_dimension   )
							    + self.num_dynamic_real_features
							    + self.num_time_features
							    + self.num_static_real_features
							    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
							)



