





"""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 							lowercase__			(   __SCREAMING_SNAKE_CASE	):

			"""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    )
									UpperCamelCase				:		List[Any]				   =			Sql(
									    cache_dir=_A						,   features=_A						,   sql=_A						,   con=_A						,   **_A						,   )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			None
									UpperCamelCase				:		Optional[int]				   =			None
									UpperCamelCase				:		Dict				   =			None
									UpperCamelCase				:		Union[str, Any]				   =			None

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

									# Build dataset for splits
									UpperCamelCase				:		Optional[int]				   =			self.builder.as_dataset(
									    split="""train"""						,   verification_mode=_A						,   in_memory=self.keep_in_memory    )
									return dataset






class 							lowercase__			:

			"""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."""    )

									UpperCamelCase				:		Tuple				   =			dataset
									UpperCamelCase				:		Dict				   =			name
									UpperCamelCase				:		Optional[int]				   =			con
									UpperCamelCase				:		str				   =			batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
									UpperCamelCase				:		Optional[int]				   =			num_proc
									UpperCamelCase				:		Tuple				   =			to_sql_kwargs







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		int				   =			self.to_sql_kwargs.pop("""sql"""						,   _A    )
									UpperCamelCase				:		Optional[Any]				   =			self.to_sql_kwargs.pop("""con"""						,   _A    )
									UpperCamelCase				:		List[Any]				   =			self.to_sql_kwargs.pop("""index"""						,   _A    )

									UpperCamelCase				:		int				   =			self._write(index=_A						,   **self.to_sql_kwargs    )
									return written







			def   _a      (      self						,   _A    ):






									'''simple docstring'''







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







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






									'''simple docstring'''







									UpperCamelCase				:		List[Any]				   =			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:
															UpperCamelCase   ,						UpperCamelCase				:		Optional[int]				   =			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






from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_tf_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_tf_available():
    import tensorflow as tf


__A			:					Dict						=			logging.get_logger(__name__)





@dataclass
class 			_SCREAMING_SNAKE_CASE (   __snake_case		):







   '''simple docstring'''




   lowerCamelCase__				=      [
       "no_inference",
       "no_cuda",
       "no_tpu",
       "no_speed",
       "no_memory",
       "no_env_print",
       "no_multi_process",
   ]





   def __init__(       self					:			List[Any]     , **__lowerCamelCase					:			Any			):
       for deprecated_arg in self.deprecated_args:
           if deprecated_arg in kwargs:
               SCREAMING_SNAKE_CASE										=   deprecated_arg[3:]
               SCREAMING_SNAKE_CASE										=   not kwargs.pop(__lowerCamelCase			)
               logger.warning(
                   f"{deprecated_arg} is depreciated. Please use --no-{positive_arg} or"
                   f" {positive_arg}={kwargs[positive_arg]}"			)
       SCREAMING_SNAKE_CASE										=   kwargs.pop("tpu_name"     , self.tpu_name			)
       SCREAMING_SNAKE_CASE										=   kwargs.pop("device_idx"     , self.device_idx			)
       SCREAMING_SNAKE_CASE										=   kwargs.pop("eager_mode"     , self.eager_mode			)
       SCREAMING_SNAKE_CASE										=   kwargs.pop("use_xla"     , self.use_xla			)
       super().__init__(**__lowerCamelCase			)

   lowerCamelCase__				=      field(
       default=__snake_case					,       metadata={"help": "Name of TPU"}					,       )
   lowerCamelCase__				=      field(
       default=0					,       metadata={"help": "CPU / GPU device index. Defaults to 0."}					,       )
   lowerCamelCase__				=      field(default=__snake_case					,       metadata={"help": "Benchmark models in eager model."}		)
   lowerCamelCase__				=      field(
       default=__snake_case					,       metadata={
           "help": "Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`."
       }					,       )





   @cached_property
   def      _snake_case		(       self					:			Optional[int]			):
       requires_backends(self     , ["tf"]			)
       SCREAMING_SNAKE_CASE										=   None
       if self.tpu:
           try:
               if self.tpu_name:
                   SCREAMING_SNAKE_CASE										=   tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name			)
               else:
                   SCREAMING_SNAKE_CASE										=   tf.distribute.cluster_resolver.TPUClusterResolver()
           except ValueError:
               SCREAMING_SNAKE_CASE										=   None
       return tpu





   @cached_property
   def      _snake_case		(       self					:			Any			):
       requires_backends(self     , ["tf"]			)
       if self.is_tpu:
           tf.config.experimental_connect_to_cluster(self._setup_tpu			)
           tf.tpu.experimental.initialize_tpu_system(self._setup_tpu			)

           SCREAMING_SNAKE_CASE										=   tf.distribute.TPUStrategy(self._setup_tpu			)
       else:
           # currently no multi gpu is allowed
           if self.is_gpu:
               # TODO: Currently only single GPU is supported
               tf.config.set_visible_devices(self.gpu_list[self.device_idx]     , "GPU"			)
               SCREAMING_SNAKE_CASE										=   tf.distribute.OneDeviceStrategy(device=f"/gpu:{self.device_idx}"			)
           else:
               tf.config.set_visible_devices([]     , "GPU"			)  # disable GPU
               SCREAMING_SNAKE_CASE										=   tf.distribute.OneDeviceStrategy(device=f"/cpu:{self.device_idx}"			)

       return strategy





   @property
   def      _snake_case		(       self					:			Any			):
       requires_backends(self     , ["tf"]			)
       return self._setup_tpu is not None





   @property
   def      _snake_case		(       self					:			Optional[Any]			):
       requires_backends(self     , ["tf"]			)
       return self._setup_strategy





   @property
   def      _snake_case		(       self					:			List[str]			):
       requires_backends(self     , ["tf"]			)
       return tf.config.list_physical_devices("GPU"			)





   @property
   def      _snake_case		(       self					:			Any			):
       requires_backends(self     , ["tf"]			)
       if self.cuda:
           return len(self.gpu_list			)
       return 0





   @property
   def      _snake_case		(       self					:			Dict			):
       return self.n_gpu > 0