





'''simple docstring'''




from typing import List, Optional, Union

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


lowercase__       : List[Any]         =       logging.get_logger(__name__)

lowercase__       : Optional[Any]         =       {
    '''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       (a__				):
     lowerCAmelCase						     =			'''informer'''
     lowerCAmelCase						     =			{
         '''hidden_size''': '''d_model''',
         '''num_attention_heads''': '''encoder_attention_heads''',
         '''num_hidden_layers''': '''encoder_layers''',
     }


     def __init__(		self		,      _UpperCAmelCase = None		,      _UpperCAmelCase = None		,      _UpperCAmelCase = "student_t"		,      _UpperCAmelCase = "nll"		,      _UpperCAmelCase = 1		,      _UpperCAmelCase = None		,      _UpperCAmelCase = "mean"		,      _UpperCAmelCase = 0		,      _UpperCAmelCase = 0		,      _UpperCAmelCase = 0		,      _UpperCAmelCase = 0		,      _UpperCAmelCase = None		,      _UpperCAmelCase = None		,      _UpperCAmelCase = 64		,      _UpperCAmelCase = 32		,      _UpperCAmelCase = 32		,      _UpperCAmelCase = 2		,      _UpperCAmelCase = 2		,      _UpperCAmelCase = 2		,      _UpperCAmelCase = 2		,      _UpperCAmelCase = True		,      _UpperCAmelCase = "gelu"		,      _UpperCAmelCase = 0.05		,      _UpperCAmelCase = 0.1		,      _UpperCAmelCase = 0.1		,      _UpperCAmelCase = 0.1		,      _UpperCAmelCase = 0.1		,      _UpperCAmelCase = 100		,      _UpperCAmelCase = 0.02		,      _UpperCAmelCase=True		,      _UpperCAmelCase = "prob"		,      _UpperCAmelCase = 5		,      _UpperCAmelCase = True		,      **_UpperCAmelCase		,      ):

          '''simple docstring'''






          __A		:					str						=			prediction_length
          __A		:					str						=			context_length or prediction_length
          __A		:					int						=			distribution_output
          __A		:					int						=			loss
          __A		:					Optional[Any]						=			input_size
          __A		:					Optional[int]						=			num_time_features
          __A		:					List[Any]						=			lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
          __A		:					int						=			scaling
          __A		:					int						=			num_dynamic_real_features
          __A		:					Tuple						=			num_static_real_features
          __A		:					Optional[Any]						=			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`')
               __A		:					Tuple						=			cardinality
          else:
               __A		:					Tuple						=			[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`')
               __A		:					Union[str, Any]						=			embedding_dimension
          else:
               __A		:					Optional[Any]						=			[min(50		,      (cat + 1) // 2) for cat in self.cardinality]

          __A		:					List[Any]						=			num_parallel_samples

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

          __A		:					Dict						=			dropout
          __A		:					List[Any]						=			attention_dropout
          __A		:					int						=			activation_dropout
          __A		:					List[str]						=			encoder_layerdrop
          __A		:					Union[str, Any]						=			decoder_layerdrop

          __A		:					str						=			activation_function
          __A		:					str						=			init_std

          __A		:					str						=			use_cache

          # Informer
          __A		:					Union[str, Any]						=			attention_type
          __A		:					int						=			sampling_factor
          __A		:					Union[str, Any]						=			distil

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


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          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
          )






'''simple docstring'''




import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


lowercase__       : Any         =       '''hf-internal-testing/tiny-random-bert'''
lowercase__       : Optional[Any]         =       os.path.join(TRANSFORMERS_CACHE, '''models--hf-internal-testing--tiny-random-bert''')
lowercase__       : List[Any]         =       '''9b8c223d42b2188cb49d29af482996f9d0f3e5a6'''


class 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					List[Any]						=			cached_file(_UpperCAmelCase		,      _UpperCAmelCase)
          # Should have downloaded the file in here
          self.assertTrue(os.path.isdir(_UpperCAmelCase))
          # Cache should contain at least those three subfolders:
          for subfolder in ["blobs", "refs", "snapshots"]:
               self.assertTrue(os.path.isdir(os.path.join(_UpperCAmelCase		,      _UpperCAmelCase)))
          with open(os.path.join(_UpperCAmelCase		,      'refs'		,      'main')) as f:
               __A		:					Any						=			f.read()
          self.assertEqual(_UpperCAmelCase		,      os.path.join(_UpperCAmelCase		,      'snapshots'		,      _UpperCAmelCase		,      _UpperCAmelCase))
          self.assertTrue(os.path.isfile(_UpperCAmelCase))

          # File is cached at the same place the second time.
          __A		:					Tuple						=			cached_file(_UpperCAmelCase		,      _UpperCAmelCase)
          self.assertEqual(_UpperCAmelCase		,      _UpperCAmelCase)

          # Using a specific revision to test the full commit hash.
          __A		:					List[Any]						=			cached_file(_UpperCAmelCase		,      _UpperCAmelCase		,      revision='9b8c223')
          self.assertEqual(_UpperCAmelCase		,      os.path.join(_UpperCAmelCase		,      'snapshots'		,      _UpperCAmelCase		,      _UpperCAmelCase))


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          with self.assertRaisesRegex(_UpperCAmelCase		,      'is not a valid model identifier'):
               __A		:					Dict						=			cached_file('tiny-random-bert'		,      _UpperCAmelCase)

          with self.assertRaisesRegex(_UpperCAmelCase		,      'is not a valid git identifier'):
               __A		:					Optional[int]						=			cached_file(_UpperCAmelCase		,      _UpperCAmelCase		,      revision='aaaa')

          with self.assertRaisesRegex(_UpperCAmelCase		,      'does not appear to have a file named'):
               __A		:					int						=			cached_file(_UpperCAmelCase		,      'conf')


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          with self.assertRaisesRegex(_UpperCAmelCase		,      'does not appear to have a file named'):
               __A		:					Any						=			cached_file(_UpperCAmelCase		,      'conf')

          with open(os.path.join(_UpperCAmelCase		,      'refs'		,      'main')) as f:
               __A		:					Dict						=			f.read()
          self.assertTrue(os.path.isfile(os.path.join(_UpperCAmelCase		,      '.no_exist'		,      _UpperCAmelCase		,      'conf')))

          __A		:					List[Any]						=			cached_file(_UpperCAmelCase		,      'conf'		,      _raise_exceptions_for_missing_entries=_UpperCAmelCase)
          self.assertIsNone(_UpperCAmelCase)

          __A		:					str						=			cached_file(_UpperCAmelCase		,      'conf'		,      local_files_only=_UpperCAmelCase		,      _raise_exceptions_for_missing_entries=_UpperCAmelCase)
          self.assertIsNone(_UpperCAmelCase)

          __A		:					List[str]						=			mock.Mock()
          __A		:					Dict						=			500
          __A		:					List[str]						=			{}
          __A		:					List[Any]						=			HTTPError
          __A		:					Optional[Any]						=			{}

          # Under the mock environment we get a 500 error when trying to reach the tokenizer.
          with mock.patch('requests.Session.request'		,      return_value=_UpperCAmelCase) as mock_head:
               __A		:					Dict						=			cached_file(_UpperCAmelCase		,      'conf'		,      _raise_exceptions_for_connection_errors=_UpperCAmelCase)
               self.assertIsNone(_UpperCAmelCase)
               # This check we did call the fake head request
               mock_head.assert_called()


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only'		,      _UpperCAmelCase))
          self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'		,      _UpperCAmelCase))
          self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'		,      _UpperCAmelCase))


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          self.assertIsNone(get_file_from_repo('bert-base-cased'		,      'ahah.txt'))

          # The function raises if the repository does not exist.
          with self.assertRaisesRegex(_UpperCAmelCase		,      'is not a valid model identifier'):
               get_file_from_repo('bert-base-case'		,      _UpperCAmelCase)

          # The function raises if the revision does not exist.
          with self.assertRaisesRegex(_UpperCAmelCase		,      'is not a valid git identifier'):
               get_file_from_repo('bert-base-cased'		,      _UpperCAmelCase		,      revision='ahaha')

          __A		:					List[str]						=			get_file_from_repo('bert-base-cased'		,      _UpperCAmelCase)
          # The name is the cached name which is not very easy to test, so instead we load the content.
          __A		:					List[str]						=			json.loads(open(_UpperCAmelCase		,      'r').read())
          self.assertEqual(config['hidden_size']		,      768)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          with tempfile.TemporaryDirectory() as tmp_dir:
               __A		:					Tuple						=			Path(_UpperCAmelCase) / 'a.txt'
               filename.touch()
               self.assertEqual(get_file_from_repo(_UpperCAmelCase		,      'a.txt')		,      str(_UpperCAmelCase))

               self.assertIsNone(get_file_from_repo(_UpperCAmelCase		,      'b.txt'))