






"""simple docstring"""
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
							    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    OpenAIGPTConfig,
							    OpenAIGPTDoubleHeadsModel,
							    OpenAIGPTForSequenceClassification,
							    OpenAIGPTLMHeadModel,
							    OpenAIGPTModel,
							)



class   SCREAMING_SNAKE_CASE	:





	"""simple docstring"""




	def __init__(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_=13  ,  lowerCAmelCase_=7  ,  lowerCAmelCase_=True  ,  lowerCAmelCase_=True  ,  lowerCAmelCase_=True  ,  lowerCAmelCase_=99  ,  lowerCAmelCase_=32  ,  lowerCAmelCase_=5  ,  lowerCAmelCase_=4  ,  lowerCAmelCase_=37  ,  lowerCAmelCase_="gelu"  ,  lowerCAmelCase_=0.1  ,  lowerCAmelCase_=0.1  ,  lowerCAmelCase_=512  ,  lowerCAmelCase_=16  ,  lowerCAmelCase_=2  ,  lowerCAmelCase_=0.02  ,  lowerCAmelCase_=3  ,  lowerCAmelCase_=4  ,  lowerCAmelCase_=None  ,  ):
		A_		:    List[Any]						     = parent
		A_		:    Any						     = batch_size
		A_		:    Any						     = seq_length
		A_		:    List[str]						     = is_training
		A_		:    Any						     = use_token_type_ids
		A_		:    Tuple						     = use_labels
		A_		:    Any						     = vocab_size
		A_		:    Any						     = hidden_size
		A_		:    Dict						     = num_hidden_layers
		A_		:    Tuple						     = num_attention_heads
		A_		:    Dict						     = intermediate_size
		A_		:    Optional[int]						     = hidden_act
		A_		:    Union[str, Any]						     = hidden_dropout_prob
		A_		:    Optional[int]						     = attention_probs_dropout_prob
		A_		:    Tuple						     = max_position_embeddings
		A_		:    Optional[int]						     = type_vocab_size
		A_		:    Optional[Any]						     = type_sequence_label_size
		A_		:    Optional[Any]						     = initializer_range
		A_		:    Optional[int]						     = num_labels
		A_		:    str						     = num_choices
		A_		:    int						     = scope
		A_		:    Tuple						     = self.vocab_size - 1





	def  lowerCamelCase(self				):
		A_		:    int						     = ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size				)

		A_		:    Optional[int]						     = None
		if self.use_token_type_ids:
			A_		:    Optional[int]						     = ids_tensor([self.batch_size, self.seq_length]  ,  self.type_vocab_size				)

		A_		:    Tuple						     = None
		A_		:    List[Any]						     = None
		A_		:    List[str]						     = None
		if self.use_labels:
			A_		:    str						     = ids_tensor([self.batch_size]  ,  self.type_sequence_label_size				)
			A_		:    Optional[Any]						     = ids_tensor([self.batch_size, self.seq_length]  ,  self.num_labels				)
			A_		:    Tuple						     = ids_tensor([self.batch_size]  ,  self.num_choices				)

		A_		:    Optional[Any]						     = OpenAIGPTConfig(
		    vocab_size=self.vocab_size  ,  n_embd=self.hidden_size  ,  n_layer=self.num_hidden_layers  ,  n_head=self.num_attention_heads  ,  n_positions=self.max_position_embeddings  ,  pad_token_id=self.pad_token_id  ,  )

		A_		:    List[str]						     = ids_tensor([self.num_hidden_layers, self.num_attention_heads]  ,  2				)

		return (
		    config,
		    input_ids,
		    head_mask,
		    token_type_ids,
		    sequence_labels,
		    token_labels,
		    choice_labels,
		)





	def  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  *lowerCAmelCase_				):
		A_		:    Optional[int]						     = OpenAIGPTModel(config=lowerCAmelCase_				)
		model.to(lowerCAmelCase_				)
		model.eval()

		A_		:    List[str]						     = model(lowerCAmelCase_  ,  token_type_ids=lowerCAmelCase_  ,  head_mask=lowerCAmelCase_				)
		A_		:    Any						     = model(lowerCAmelCase_  ,  token_type_ids=lowerCAmelCase_				)
		A_		:    int						     = model(lowerCAmelCase_				)

		self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)				)





	def  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  *lowerCAmelCase_				):
		A_		:    Union[str, Any]						     = OpenAIGPTLMHeadModel(lowerCAmelCase_				)
		model.to(lowerCAmelCase_				)
		model.eval()

		A_		:    Any						     = model(lowerCAmelCase_  ,  token_type_ids=lowerCAmelCase_  ,  labels=lowerCAmelCase_				)
		self.parent.assertEqual(result.loss.shape  ,  ()				)
		self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.vocab_size)				)





	def  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  *lowerCAmelCase_				):
		A_		:    Any						     = OpenAIGPTDoubleHeadsModel(lowerCAmelCase_				)
		model.to(lowerCAmelCase_				)
		model.eval()

		A_		:    str						     = model(lowerCAmelCase_  ,  token_type_ids=lowerCAmelCase_  ,  labels=lowerCAmelCase_				)
		self.parent.assertEqual(result.loss.shape  ,  ()				)
		self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.vocab_size)				)





	def  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  *lowerCAmelCase_				):
		A_		:    List[Any]						     = self.num_labels
		A_		:    List[str]						     = OpenAIGPTForSequenceClassification(lowerCAmelCase_				)
		model.to(lowerCAmelCase_				)
		model.eval()

		A_		:    Tuple						     = ids_tensor([self.batch_size]  ,  self.type_sequence_label_size				)
		A_		:    List[Any]						     = model(lowerCAmelCase_  ,  token_type_ids=lowerCAmelCase_  ,  labels=lowerCAmelCase_				)
		self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.num_labels)				)





	def  lowerCamelCase(self				):
		A_		:    int						     = self.prepare_config_and_inputs()
		(
		    (
		    A_
		)     ,  (
		    A_
		)     ,  (
		    A_
		)     ,  (
		    A_
		)     ,  (
		    A_
		)     ,  (
		    A_
		)     ,  (
		    A_
		)     ,  
		)		:    List[str]						     = config_and_inputs
		A_		:    int						     = {
		    """input_ids""": input_ids,
		    """token_type_ids""": token_type_ids,
		    """head_mask""": head_mask,
		}

		return config, inputs_dict






@require_torch
class   SCREAMING_SNAKE_CASE	(					_SCREAMING_SNAKE_CASE							,							_SCREAMING_SNAKE_CASE							,							_SCREAMING_SNAKE_CASE							,							unittest.TestCase    ):





	"""simple docstring"""




	_A							:					Optional[Any] 			=					(
	    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
	    if is_torch_available()
	    else ()
	)
	_A							:					Tuple 			=					(
	    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
	)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
	_A							:					int 			=					(
	    {
	        """feature-extraction""": OpenAIGPTModel,
	        """text-classification""": OpenAIGPTForSequenceClassification,
	        """text-generation""": OpenAIGPTLMHeadModel,
	        """zero-shot""": OpenAIGPTForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)





	def  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_				):
		if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
			# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
			# `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
			# tiny config could not be created.
			return True

		return False





	def  lowerCamelCase(self  ,  lowerCAmelCase_  ,  lowerCAmelCase_  ,  lowerCAmelCase_=False				):
		A_		:    Tuple						     = super()._prepare_for_class(lowerCAmelCase_  ,  lowerCAmelCase_  ,  return_labels=lowerCAmelCase_				)

		if return_labels:
			if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
				A_		:    Tuple						     = torch.zeros(
				    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)  ,  dtype=torch.long  ,  device=lowerCAmelCase_  ,  )
				A_		:    Union[str, Any]						     = inputs_dict["""labels"""]
				A_		:    List[str]						     = inputs_dict["""labels"""]
				A_		:    str						     = torch.zeros(
				    (self.model_tester.batch_size, self.model_tester.num_choices)  ,  dtype=torch.long  ,  device=lowerCAmelCase_  ,  )
				A_		:    Optional[Any]						     = torch.zeros(
				    self.model_tester.batch_size  ,  dtype=torch.long  ,  device=lowerCAmelCase_				)
		return inputs_dict





	def  lowerCamelCase(self				):
		A_		:    int						     = OpenAIGPTModelTester(self				)
		A_		:    Any						     = ConfigTester(self  ,  config_class=lowerCAmelCase_  ,  n_embd=37				)





	def  lowerCamelCase(self				):
		self.config_tester.run_common_tests()





	def  lowerCamelCase(self				):
		A_		:    int						     = self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase_				)





	def  lowerCamelCase(self				):
		A_		:    List[str]						     = self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase_				)





	def  lowerCamelCase(self				):
		A_		:    int						     = self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase_				)





	def  lowerCamelCase(self				):
		A_		:    Tuple						     = self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase_				)





	@slow
	def  lowerCamelCase(self				):
		for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			A_		:    Optional[int]						     = OpenAIGPTModel.from_pretrained(lowerCAmelCase_				)
			self.assertIsNotNone(lowerCAmelCase_				)






@require_torch
class   SCREAMING_SNAKE_CASE	(					unittest.TestCase    ):





	"""simple docstring"""




	@slow
	def  lowerCamelCase(self				):
		A_		:    Dict						     = OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""				)
		model.to(lowerCAmelCase_				)
		A_		:    Dict						     = torch.tensor([[481, 4735, 544]]  ,  dtype=torch.long  ,  device=lowerCAmelCase_				)  # the president is
		A_		:    Any						     = [
		    481,
		    4735,
		    544,
		    246,
		    963,
		    870,
		    762,
		    239,
		    244,
		    40477,
		    244,
		    249,
		    719,
		    881,
		    487,
		    544,
		    240,
		    244,
		    603,
		    481,
		]  # the president is a very good man. " \n " i\'m sure he is, " said the

		A_		:    List[Any]						     = model.generate(lowerCAmelCase_  ,  do_sample=lowerCAmelCase_				)
		self.assertListEqual(output_ids[0].tolist()  ,  lowerCAmelCase_				)









"""simple docstring"""
from typing import List, Optional, Union

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


_lowerCAmelCase       =				logging.get_logger(__name__)

_lowerCAmelCase       =				{
    "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   SCREAMING_SNAKE_CASE	(					_SCREAMING_SNAKE_CASE    ):





	"""simple docstring"""




	_A							:					Optional[Any] 			=					"""informer"""
	_A							:					Dict 			=					{
	    """hidden_size""": """d_model""",
	    """num_attention_heads""": """encoder_attention_heads""",
	    """num_hidden_layers""": """encoder_layers""",
	}





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

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

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

		A_		:    Optional[int]						     = num_parallel_samples

		# Transformer architecture configuration
		A_		:    Union[str, Any]						     = input_size * len(self.lags_sequence				) + self._number_of_features
		A_		:    Dict						     = d_model
		A_		:    Dict						     = encoder_attention_heads
		A_		:    Dict						     = decoder_attention_heads
		A_		:    Any						     = encoder_ffn_dim
		A_		:    Tuple						     = decoder_ffn_dim
		A_		:    Tuple						     = encoder_layers
		A_		:    Optional[int]						     = decoder_layers

		A_		:    List[str]						     = dropout
		A_		:    List[str]						     = attention_dropout
		A_		:    Any						     = activation_dropout
		A_		:    Any						     = encoder_layerdrop
		A_		:    List[Any]						     = decoder_layerdrop

		A_		:    str						     = activation_function
		A_		:    Optional[Any]						     = init_std

		A_		:    Optional[int]						     = use_cache

		# Informer
		A_		:    Dict						     = attention_type
		A_		:    List[Any]						     = sampling_factor
		A_		:    List[Any]						     = distil

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





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

