





import multiprocessing
from typing import TYPE_CHECKING, Optional, Union

from .. import Dataset, Features, config
from ..formatting import query_table
from ..packaged_modules.sql.sql import Sql
from ..utils import logging
from .abc import AbstractDatasetInputStream


if TYPE_CHECKING:
  import sqlitea

  import sqlalchemy







class   __lowerCAmelCase			(					lowerCAmelCase__  ):





   def __init__( self					,			snake_case					,			snake_case					,			snake_case = None					,			snake_case = None					,			snake_case = False					,			**snake_case					,			)				->  List[Any]:
      """simple docstring"""





      super().__init__(features=_SCREAMING_SNAKE_CASE					,			cache_dir=_SCREAMING_SNAKE_CASE					,			keep_in_memory=_SCREAMING_SNAKE_CASE					,			**_SCREAMING_SNAKE_CASE      )
      a__	:						Dict      		=  Sql(
          cache_dir=_SCREAMING_SNAKE_CASE					,			features=_SCREAMING_SNAKE_CASE					,			sql=_SCREAMING_SNAKE_CASE					,			con=_SCREAMING_SNAKE_CASE					,			**_SCREAMING_SNAKE_CASE					,			)





   def 	_snake_case ( self      )				->  Optional[Any]:
      """simple docstring"""





      a__	:						List[Any]      		=  None
      a__	:						Dict      		=  None
      a__	:						Any      		=  None
      a__	:						Dict      		=  None

      self.builder.download_and_prepare(
          download_config=_SCREAMING_SNAKE_CASE					,			download_mode=_SCREAMING_SNAKE_CASE					,			verification_mode=_SCREAMING_SNAKE_CASE					,			base_path=_SCREAMING_SNAKE_CASE					,			)

      # Build dataset for splits
      a__	:						Tuple      		=  self.builder.as_dataset(
          split="train"					,			verification_mode=_SCREAMING_SNAKE_CASE					,			in_memory=self.keep_in_memory      )
      return dataset







class   __lowerCAmelCase			:





   def __init__( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case = None					,			snake_case = None					,			**snake_case					,			)				->  List[Any]:
      """simple docstring"""





      if num_proc is not None and num_proc <= 0:
         raise ValueError(F"""num_proc {num_proc} must be an integer > 0."""      )

      a__	:						List[Any]      		=  dataset
      a__	:						Dict      		=  name
      a__	:						Union[str, Any]      		=  con
      a__	:						List[Any]      		=  batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
      a__	:						Union[str, Any]      		=  num_proc
      a__	:						List[str]      		=  to_sql_kwargs





   def 	_snake_case ( self      )				->  int:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  self.to_sql_kwargs.pop("sql"					,			_SCREAMING_SNAKE_CASE      )
      a__	:						Tuple      		=  self.to_sql_kwargs.pop("con"					,			_SCREAMING_SNAKE_CASE      )
      a__	:						str      		=  self.to_sql_kwargs.pop("index"					,			_SCREAMING_SNAKE_CASE      )

      a__	:						Union[str, Any]      		=  self._write(index=_SCREAMING_SNAKE_CASE					,			**self.to_sql_kwargs      )
      return written





   def 	_snake_case ( self					,			snake_case      )				->  Optional[int]:
      """simple docstring"""





      a__		,     a__		,     a__	:						Union[str, Any]      		=  args
      a__	:						Optional[Any]      		=  {**to_sql_kwargs, "if_exists": "append"} if offset > 0 else to_sql_kwargs
      a__	:						int      		=  query_table(
          table=self.dataset.data					,			key=slice(_SCREAMING_SNAKE_CASE					,			offset + self.batch_size      )					,			indices=self.dataset._indices					,			)
      a__	:						Union[str, Any]      		=  batch.to_pandas()
      a__	:						Tuple      		=  df.to_sql(self.name					,			self.con					,			index=_SCREAMING_SNAKE_CASE					,			**_SCREAMING_SNAKE_CASE      )
      return num_rows or len(_SCREAMING_SNAKE_CASE      )





   def 	_snake_case ( self					,			snake_case					,			**snake_case      )				->  int:
      """simple docstring"""





      a__	:						str      		=  0

      if self.num_proc is None or self.num_proc == 1:
         for offset in logging.tqdm(
             range(0					,			len(self.dataset      )					,			self.batch_size      )					,			unit="ba"					,			disable=not logging.is_progress_bar_enabled()					,			desc="Creating SQL from Arrow format"					,			):
            written += self._batch_sql((offset, index, to_sql_kwargs)      )
      else:
         a__		,     a__	:						List[str]      		=  len(self.dataset      ), self.batch_size
         with multiprocessing.Pool(self.num_proc      ) as pool:
            for num_rows in logging.tqdm(
                pool.imap(
                    self._batch_sql					,			[(offset, index, to_sql_kwargs) for offset in range(0					,			_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE      )]					,			)					,			total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size					,			unit="ba"					,			disable=not logging.is_progress_bar_enabled()					,			desc="Creating SQL from Arrow format"					,			):
               written += num_rows

      return written








from typing import List, Optional, Union

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


SCREAMING_SNAKE_CASE__       :			Any			      =						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__       :			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   __lowerCAmelCase			(					_UpperCamelCase  ):
   _UpperCamelCase :						Optional[Any]     		=				"""informer"""
   _UpperCamelCase :						Any     		=				{
       """hidden_size""": """d_model""",
       """num_attention_heads""": """encoder_attention_heads""",
       """num_hidden_layers""": """encoder_layers""",
   }





   def __init__( self					,			snake_case = None					,			snake_case = None					,			snake_case = "student_t"					,			snake_case = "nll"					,			snake_case = 1					,			snake_case = None					,			snake_case = "mean"					,			snake_case = 0					,			snake_case = 0					,			snake_case = 0					,			snake_case = 0					,			snake_case = None					,			snake_case = None					,			snake_case = 64					,			snake_case = 32					,			snake_case = 32					,			snake_case = 2					,			snake_case = 2					,			snake_case = 2					,			snake_case = 2					,			snake_case = True					,			snake_case = "gelu"					,			snake_case = 0.05					,			snake_case = 0.1					,			snake_case = 0.1					,			snake_case = 0.1					,			snake_case = 0.1					,			snake_case = 100					,			snake_case = 0.02					,			snake_case=True					,			snake_case = "prob"					,			snake_case = 5					,			snake_case = True					,			**snake_case					,			)				->  Union[str, Any]:
      """simple docstring"""





      a__	:						Optional[Any]      		=  prediction_length
      a__	:						Optional[int]      		=  context_length or prediction_length
      a__	:						Optional[int]      		=  distribution_output
      a__	:						str      		=  loss
      a__	:						Optional[Any]      		=  input_size
      a__	:						int      		=  num_time_features
      a__	:						Optional[int]      		=  lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
      a__	:						Optional[int]      		=  scaling
      a__	:						List[str]      		=  num_dynamic_real_features
      a__	:						Optional[int]      		=  num_static_real_features
      a__	:						Optional[int]      		=  num_static_categorical_features

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

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

      a__	:						Optional[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__	:						Any      		=  encoder_attention_heads
      a__	:						Optional[Any]      		=  decoder_attention_heads
      a__	:						int      		=  encoder_ffn_dim
      a__	:						List[Any]      		=  decoder_ffn_dim
      a__	:						List[str]      		=  encoder_layers
      a__	:						Any      		=  decoder_layers

      a__	:						List[str]      		=  dropout
      a__	:						int      		=  attention_dropout
      a__	:						List[Any]      		=  activation_dropout
      a__	:						Optional[int]      		=  encoder_layerdrop
      a__	:						Tuple      		=  decoder_layerdrop

      a__	:						Any      		=  activation_function
      a__	:						Tuple      		=  init_std

      a__	:						Optional[int]      		=  use_cache

      # Informer
      a__	:						Union[str, Any]      		=  attention_type
      a__	:						List[str]      		=  sampling_factor
      a__	:						Optional[int]      		=  distil

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





   @property
   def 	_snake_case ( self      )				->  int:
      """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
      )

