






import os
import tempfile
import unittest

from transformers import NezhaConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    MODEL_FOR_PRETRAINING_MAPPING,
				    NezhaForMaskedLM,
				    NezhaForMultipleChoice,
				    NezhaForNextSentencePrediction,
				    NezhaForPreTraining,
				    NezhaForQuestionAnswering,
				    NezhaForSequenceClassification,
				    NezhaForTokenClassification,
				    NezhaModel,
				)
				from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST






class       _UpperCamelCase    :

				"""simple docstring"""







				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__=128				,   a__=32				,   a__=16				,   a__=2				,   a__=0.02				,   a__=3				,   a__=4				,   a__=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								=				scope

				def      _UpperCAmelCase					(						self   )	->				Tuple:
					A								=				ids_tensor([self.batch_size, self.seq_length]				,   self.vocab_size   )

					A								=				None
					if self.use_input_mask:
						A								=				random_attention_mask([self.batch_size, self.seq_length]   )

					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   )	->				Optional[int]:
					return NezhaConfig(
					    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				,   is_decoder=__UpperCamelCase				,   initializer_range=self.initializer_range				,   )

				def      _UpperCAmelCase					(						self   )	->				Union[str, Any]:
					(
					    (
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			
					)								=				self.prepare_config_and_inputs()

					A								=				True
					A								=				floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
					A								=				ids_tensor([self.batch_size, self.seq_length]				,   vocab_size=2   )

					return (
					    config,
					    input_ids,
					    token_type_ids,
					    input_mask,
					    sequence_labels,
					    token_labels,
					    choice_labels,
					    encoder_hidden_states,
					    encoder_attention_mask,
					)

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__   )	->				Any:
					A								=				NezhaModel(config=__UpperCamelCase   )
					model.to(__UpperCamelCase   )
					model.eval()
					A								=				model(__UpperCamelCase				,   attention_mask=__UpperCamelCase				,   token_type_ids=__UpperCamelCase   )
					A								=				model(__UpperCamelCase				,   token_type_ids=__UpperCamelCase   )
					A								=				model(__UpperCamelCase   )
					self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)   )
					self.parent.assertEqual(result.pooler_output.shape				,   (self.batch_size, self.hidden_size)   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   )	->				int:
					A								=				True
					A								=				NezhaModel(__UpperCamelCase   )
					model.to(__UpperCamelCase   )
					model.eval()
					A								=				model(
					    __UpperCamelCase				,   attention_mask=__UpperCamelCase				,   token_type_ids=__UpperCamelCase				,   encoder_hidden_states=__UpperCamelCase				,   encoder_attention_mask=__UpperCamelCase				,   )
					A								=				model(
					    __UpperCamelCase				,   attention_mask=__UpperCamelCase				,   token_type_ids=__UpperCamelCase				,   encoder_hidden_states=__UpperCamelCase				,   )
					A								=				model(__UpperCamelCase				,   attention_mask=__UpperCamelCase				,   token_type_ids=__UpperCamelCase   )
					self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)   )
					self.parent.assertEqual(result.pooler_output.shape				,   (self.batch_size, self.hidden_size)   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__   )	->				Optional[int]:
					A								=				NezhaForMaskedLM(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				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__   )	->				Any:
					A								=				NezhaForNextSentencePrediction(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, 2)   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__   )	->				List[Any]:
					A								=				NezhaForPreTraining(config=__UpperCamelCase   )
					model.to(__UpperCamelCase   )
					model.eval()
					A								=				model(
					    __UpperCamelCase				,   attention_mask=__UpperCamelCase				,   token_type_ids=__UpperCamelCase				,   labels=__UpperCamelCase				,   next_sentence_label=__UpperCamelCase				,   )
					self.parent.assertEqual(result.prediction_logits.shape				,   (self.batch_size, self.seq_length, self.vocab_size)   )
					self.parent.assertEqual(result.seq_relationship_logits.shape				,   (self.batch_size, 2)   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__   )	->				Any:
					A								=				NezhaForQuestionAnswering(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				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__   )	->				Tuple:
					A								=				self.num_labels
					A								=				NezhaForSequenceClassification(__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.num_labels)   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__   )	->				Tuple:
					A								=				self.num_labels
					A								=				NezhaForTokenClassification(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				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__				,   a__   )	->				int:
					A								=				self.num_choices
					A								=				NezhaForMultipleChoice(config=__UpperCamelCase   )
					model.to(__UpperCamelCase   )
					model.eval()
					A								=				input_ids.unsqueeze(1   ).expand(-1				,   self.num_choices				,   -1   ).contiguous()
					A								=				token_type_ids.unsqueeze(1   ).expand(-1				,   self.num_choices				,   -1   ).contiguous()
					A								=				input_mask.unsqueeze(1   ).expand(-1				,   self.num_choices				,   -1   ).contiguous()
					A								=				model(
					    __UpperCamelCase				,   attention_mask=__UpperCamelCase				,   token_type_ids=__UpperCamelCase				,   labels=__UpperCamelCase				,   )
					self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.num_choices)   )

				def      _UpperCAmelCase					(						self   )	->				Optional[int]:
					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       _UpperCamelCase    (					_UpperCAmelCase    ,		_UpperCAmelCase    ,		_UpperCAmelCase    ,		unittest.TestCase				):

				"""simple docstring"""







				lowerCAmelCase 		= (
				    (
				        NezhaModel,
				        NezhaForMaskedLM,
				        NezhaForMultipleChoice,
				        NezhaForNextSentencePrediction,
				        NezhaForPreTraining,
				        NezhaForQuestionAnswering,
				        NezhaForSequenceClassification,
				        NezhaForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				lowerCAmelCase 		= (
				    {
				        'feature-extraction': NezhaModel,
				        'fill-mask': NezhaForMaskedLM,
				        'question-answering': NezhaForQuestionAnswering,
				        'text-classification': NezhaForSequenceClassification,
				        'token-classification': NezhaForTokenClassification,
				        'zero-shot': NezhaForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				lowerCAmelCase 		= True

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__=False   )	->				Dict:
					A								=				super()._prepare_for_class(__UpperCamelCase				,   __UpperCamelCase				,   return_labels=__UpperCamelCase   )

					if return_labels:
						if model_class in get_values(__UpperCamelCase   ):
							A								=				torch.zeros(
							    (self.model_tester.batch_size, self.model_tester.seq_length)				,   dtype=torch.long				,   device=__UpperCamelCase   )
							A								=				torch.zeros(
							    self.model_tester.batch_size				,   dtype=torch.long				,   device=__UpperCamelCase   )
					return inputs_dict

				def      _UpperCAmelCase					(						self   )	->				Tuple:
					A								=				NezhaModelTester(self   )
					A								=				ConfigTester(self				,   config_class=__UpperCamelCase				,   hidden_size=37   )

				def      _UpperCAmelCase					(						self   )	->				int:
					self.config_tester.run_common_tests()

				def      _UpperCAmelCase					(						self   )	->				Union[str, Any]:
					A								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__UpperCamelCase   )

				def      _UpperCAmelCase					(						self   )	->				Optional[int]:
					A								=				self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_model_as_decoder(*__UpperCamelCase   )

				def      _UpperCAmelCase					(						self   )	->				List[str]:
					(
					    (
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			(
					    A
					)					,			
					)								=				self.model_tester.prepare_config_and_inputs_for_decoder()

					A								=				None

					self.model_tester.create_and_check_model_as_decoder(
					    __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   )

				def      _UpperCAmelCase					(						self   )	->				List[str]:
					A								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*__UpperCamelCase   )

				def      _UpperCAmelCase					(						self   )	->				str:
					A								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*__UpperCamelCase   )

				def      _UpperCAmelCase					(						self   )	->				Dict:
					A								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_next_sequence_prediction(*__UpperCamelCase   )

				def      _UpperCAmelCase					(						self   )	->				Optional[Any]:
					A								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*__UpperCamelCase   )

				def      _UpperCAmelCase					(						self   )	->				List[Any]:
					A								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*__UpperCamelCase   )

				def      _UpperCAmelCase					(						self   )	->				Optional[Any]:
					A								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*__UpperCamelCase   )

				def      _UpperCAmelCase					(						self   )	->				Tuple:
					A								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase   )

				@slow
				def      _UpperCAmelCase					(						self   )	->				Union[str, Any]:






					for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						A								=				NezhaModel.from_pretrained(__UpperCamelCase   )
						self.assertIsNotNone(__UpperCamelCase   )

				@slow
				@require_torch_gpu
				def      _UpperCAmelCase					(						self   )	->				Any:
					A					,			A								=				self.model_tester.prepare_config_and_inputs_for_common()
					for model_class in self.all_model_classes:
						# NezhaForMultipleChoice behaves incorrectly in JIT environments.
						if model_class == NezhaForMultipleChoice:
							return

						A								=				True
						A								=				model_class(config=__UpperCamelCase   )

						A								=				self._prepare_for_class(__UpperCamelCase				,   __UpperCamelCase   )
						A								=				torch.jit.trace(
						    __UpperCamelCase				,   (inputs_dict["""input_ids"""].to("""cpu"""   ), inputs_dict["""attention_mask"""].to("""cpu"""   ))   )

						with tempfile.TemporaryDirectory() as tmp:
							torch.jit.save(__UpperCamelCase				,   os.path.join(__UpperCamelCase				,   """bert.pt"""   )   )
							A								=				torch.jit.load(os.path.join(__UpperCamelCase				,   """bert.pt"""   )				,   map_location=__UpperCamelCase   )
							loaded(inputs_dict["""input_ids"""].to(__UpperCamelCase   )				,   inputs_dict["""attention_mask"""].to(__UpperCamelCase   )   )






@require_torch
class       _UpperCamelCase    (					unittest.TestCase				):

				"""simple docstring"""







				@slow
				def      _UpperCAmelCase					(						self   )	->				Any:
					A								=				NezhaModel.from_pretrained("""sijunhe/nezha-cn-base"""   )
					A								=				torch.tensor([[0, 1, 2, 3, 4, 5]]   )
					A								=				torch.tensor([[0, 1, 1, 1, 1, 1]]   )
					with torch.no_grad():
						A								=				model(__UpperCamelCase				,   attention_mask=__UpperCamelCase   )[0]
					A								=				torch.Size((1, 6, 768)   )
					self.assertEqual(output.shape				,   __UpperCamelCase   )
					A								=				torch.tensor([[[0.06_85, 0.24_41, 0.11_02], [0.06_00, 0.19_06, 0.13_49], [0.02_21, 0.08_19, 0.05_86]]]   )

					self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,   __UpperCamelCase				,   atol=1e-4   )   )

				@slow
				def      _UpperCAmelCase					(						self   )	->				Any:
					A								=				NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base"""   )
					A								=				torch.tensor([[0, 1, 2, 3, 4, 5]]   )
					A								=				torch.tensor([[1, 1, 1, 1, 1, 1]]   )
					with torch.no_grad():
						A								=				model(__UpperCamelCase				,   attention_mask=__UpperCamelCase   )[0]
					A								=				torch.Size((1, 6, 2_1128)   )
					self.assertEqual(output.shape				,   __UpperCamelCase   )
					A								=				torch.tensor(
					    [[-2.79_39, -1.79_02, -2.21_89], [-2.85_85, -1.89_08, -2.37_23], [-2.64_99, -1.77_50, -2.25_58]]   )

					self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,   __UpperCamelCase				,   atol=1e-4   )   )







from typing import List, Optional, Union

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


_lowercase       :      Optional[Any]	=	logging.get_logger(__name__)

_lowercase       :      List[str]	=	{
    "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       _UpperCamelCase    (					__snake_case				):

				"""simple docstring"""







				lowerCAmelCase 		= 'informer'
				lowerCAmelCase 		= {
				    'hidden_size': 'd_model',
				    'num_attention_heads': 'encoder_attention_heads',
				    'num_hidden_layers': 'encoder_layers',
				}

				def __init__(						self				,   a__ = None				,   a__ = None				,   a__ = "student_t"				,   a__ = "nll"				,   a__ = 1				,   a__ = None				,   a__ = "mean"				,   a__ = 0				,   a__ = 0				,   a__ = 0				,   a__ = 0				,   a__ = None				,   a__ = None				,   a__ = 64				,   a__ = 32				,   a__ = 32				,   a__ = 2				,   a__ = 2				,   a__ = 2				,   a__ = 2				,   a__ = True				,   a__ = "gelu"				,   a__ = 0.05				,   a__ = 0.1				,   a__ = 0.1				,   a__ = 0.1				,   a__ = 0.1				,   a__ = 100				,   a__ = 0.02				,   a__=True				,   a__ = "prob"				,   a__ = 5				,   a__ = True				,   **a__				,   )	->				Union[str, Any]:
					# time series specific configuration
					A								=				prediction_length
					A								=				context_length or prediction_length
					A								=				distribution_output
					A								=				loss
					A								=				input_size
					A								=				num_time_features
					A								=				lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
					A								=				scaling
					A								=				num_dynamic_real_features
					A								=				num_static_real_features
					A								=				num_static_categorical_features

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

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

					A								=				num_parallel_samples

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

					A								=				dropout
					A								=				attention_dropout
					A								=				activation_dropout
					A								=				encoder_layerdrop
					A								=				decoder_layerdrop

					A								=				activation_function
					A								=				init_std

					A								=				use_cache

					# Informer
					A								=				attention_type
					A								=				sampling_factor
					A								=				distil

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

				@property
				def      _UpperCAmelCase					(						self   )	->				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
					)



