

'''simple docstring'''



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__  ( a   ):



 """simple docstring"""



 def __init__(						self   :			Dict   ,				__SCREAMING_SNAKE_CASE   :			Union[str, "sqlalchemy.sql.Selectable"]   ,				__SCREAMING_SNAKE_CASE   :			Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]   ,				__SCREAMING_SNAKE_CASE   :			Optional[Features] = None   ,				__SCREAMING_SNAKE_CASE   :			str = None   ,				__SCREAMING_SNAKE_CASE   :			bool = False   ,				**__SCREAMING_SNAKE_CASE   :			List[str]   ,				)    ->					Tuple:


      """simple docstring"""




      super().__init__(features=__SCREAMING_SNAKE_CASE   ,				cache_dir=__SCREAMING_SNAKE_CASE   ,				keep_in_memory=__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )
      __SCREAMING_SNAKE_CASE      = Sql(
          cache_dir=__SCREAMING_SNAKE_CASE   ,				features=__SCREAMING_SNAKE_CASE   ,				sql=__SCREAMING_SNAKE_CASE   ,				con=__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE   ,				)


 def  UpperCAmelCase__						(						self   :			Tuple     )    ->					int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = None
      __SCREAMING_SNAKE_CASE      = None
      __SCREAMING_SNAKE_CASE      = None
      __SCREAMING_SNAKE_CASE      = 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
      __SCREAMING_SNAKE_CASE      = self.builder.as_dataset(
          split="""train"""   ,				verification_mode=__SCREAMING_SNAKE_CASE   ,				in_memory=self.keep_in_memory     )
      return dataset







class 		lowerCAmelCase__  :



 """simple docstring"""



 def __init__(						self   :			str   ,				__SCREAMING_SNAKE_CASE   :			Dataset   ,				__SCREAMING_SNAKE_CASE   :			str   ,				__SCREAMING_SNAKE_CASE   :			Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]   ,				__SCREAMING_SNAKE_CASE   :			Optional[int] = None   ,				__SCREAMING_SNAKE_CASE   :			Optional[int] = None   ,				**__SCREAMING_SNAKE_CASE   :			str   ,				)    ->					Tuple:


      """simple docstring"""




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

      __SCREAMING_SNAKE_CASE      = dataset
      __SCREAMING_SNAKE_CASE      = name
      __SCREAMING_SNAKE_CASE      = con
      __SCREAMING_SNAKE_CASE      = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
      __SCREAMING_SNAKE_CASE      = num_proc
      __SCREAMING_SNAKE_CASE      = to_sql_kwargs
 def  UpperCAmelCase__						(						self   :			Optional[int]     )    ->					int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.to_sql_kwargs.pop("""sql"""   ,				__SCREAMING_SNAKE_CASE     )
      __SCREAMING_SNAKE_CASE      = self.to_sql_kwargs.pop("""con"""   ,				__SCREAMING_SNAKE_CASE     )
      __SCREAMING_SNAKE_CASE      = self.to_sql_kwargs.pop("""index"""   ,				__SCREAMING_SNAKE_CASE     )

      __SCREAMING_SNAKE_CASE      = self._write(index=__SCREAMING_SNAKE_CASE   ,				**self.to_sql_kwargs     )
      return written
 def  UpperCAmelCase__						(						self   :			int   ,				__SCREAMING_SNAKE_CASE   :			str     )    ->					Optional[int]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE      = args
      __SCREAMING_SNAKE_CASE      = {**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs
      __SCREAMING_SNAKE_CASE      = query_table(
          table=self.dataset.data   ,				key=slice(__SCREAMING_SNAKE_CASE   ,				offset + self.batch_size     )   ,				indices=self.dataset._indices   ,				)
      __SCREAMING_SNAKE_CASE      = batch.to_pandas()
      __SCREAMING_SNAKE_CASE      = df.to_sql(self.name   ,				self.con   ,				index=__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )
      return num_rows or len(__SCREAMING_SNAKE_CASE     )


 def  UpperCAmelCase__						(						self   :			Optional[int]   ,				__SCREAMING_SNAKE_CASE   :			str   ,				**__SCREAMING_SNAKE_CASE   :			Union[str, Any]     )    ->					int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = 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:
           __SCREAMING_SNAKE_CASE  ,     __SCREAMING_SNAKE_CASE      = 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









'''simple docstring'''



from typing import Optional, Union

import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models.modeling_utils import ModelMixin







class 		lowerCAmelCase__  ( a					,   a   ):



 """simple docstring"""



 @register_to_config
 def __init__(						self   :			int   ,				__SCREAMING_SNAKE_CASE   :			int = 768   ,				)    ->					int:


      """simple docstring"""




      super().__init__()

      __SCREAMING_SNAKE_CASE      = nn.Parameter(torch.zeros(1   ,				__SCREAMING_SNAKE_CASE     )     )
      __SCREAMING_SNAKE_CASE      = nn.Parameter(torch.ones(1   ,				__SCREAMING_SNAKE_CASE     )     )
 def  UpperCAmelCase__						(						self   :			Any   ,				__SCREAMING_SNAKE_CASE   :			Optional[Union[str, torch.device]] = None   ,				__SCREAMING_SNAKE_CASE   :			Optional[torch.dtype] = None   ,				)    ->					List[str]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = nn.Parameter(self.mean.to(__SCREAMING_SNAKE_CASE     ).to(__SCREAMING_SNAKE_CASE     )     )
      __SCREAMING_SNAKE_CASE      = nn.Parameter(self.std.to(__SCREAMING_SNAKE_CASE     ).to(__SCREAMING_SNAKE_CASE     )     )
      return self
 def  UpperCAmelCase__						(						self   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			Optional[int]     )    ->					Optional[int]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = (embeds - self.mean) * 1.0 / self.std
      return embeds


 def  UpperCAmelCase__						(						self   :			Tuple   ,				__SCREAMING_SNAKE_CASE   :			Any     )    ->					Optional[int]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = (embeds * self.std) + self.mean
      return embeds






