



"""simple docstring"""




from typing import TYPE_CHECKING

from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available
from ...utils import OptionalDependencyNotAvailable


lowerCAmelCase__		:					Optional[int]							={'''configuration_gpt_neox''': ['''GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTNeoXConfig''']}

try:
			if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase__		:					Dict							=['''GPTNeoXTokenizerFast''']

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase__		:					Optional[int]							=[
			    '''GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''GPTNeoXForCausalLM''',
			    '''GPTNeoXForQuestionAnswering''',
			    '''GPTNeoXForSequenceClassification''',
			    '''GPTNeoXForTokenClassification''',
			    '''GPTNeoXLayer''',
			    '''GPTNeoXModel''',
			    '''GPTNeoXPreTrainedModel''',
			]


if TYPE_CHECKING:
			from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig

			try:
						if not is_tokenizers_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_gpt_neox import (
						    GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST,
						    GPTNeoXForCausalLM,
						    GPTNeoXForQuestionAnswering,
						    GPTNeoXForSequenceClassification,
						    GPTNeoXForTokenClassification,
						    GPTNeoXLayer,
						    GPTNeoXModel,
						    GPTNeoXPreTrainedModel,
						)


else:
			import sys

			lowerCAmelCase__		:					List[Any]							=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)











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        UpperCAmelCase_  (       UpperCamelCase_	):

						'''simple docstring'''




						def __init__(   self				,							_A				,							_A				,							_A = None				,							_A = None				,							_A = False				,							**_A				,							):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''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=_A				,							download_mode=_A				,							verification_mode=_A				,							base_path=_A				,							)

													# Build dataset for splits
													__SCREAMING_SNAKE_CASE									= self.builder.as_dataset(
													    split='train'				,							verification_mode=_A				,							in_memory=self.keep_in_memory		)
													return dataset







class        UpperCAmelCase_  :

						'''simple docstring'''




						def __init__(   self				,							_A				,							_A				,							_A				,							_A = None				,							_A = None				,							**_A				,							):







													'''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 	_A     (   self		):







													'''simple docstring'''

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

													__SCREAMING_SNAKE_CASE									= self._write(index=_A				,							**self.to_sql_kwargs		)
													return written






						def 	_A     (   self				,							_A		):







													'''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(_A				,							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=_A				,							**_A		)
													return num_rows or len(_A		)






						def 	_A     (   self				,							_A				,							**_A		):







													'''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				,							_A				,							_A		)]				,							)				,							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






