


import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
					import sqlitea

					import sqlalchemy


UpperCamelCase			:					str         =							datasets.utils.logging.get_logger(__name__)

@dataclass
class      __snake_case				(   datasets.BuilderConfig):
					SCREAMING_SNAKE_CASE_		:       Union[str, "sqlalchemy.sql.Selectable"]								=			None
					SCREAMING_SNAKE_CASE_		:       Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]								=			None
					SCREAMING_SNAKE_CASE_		:       Optional[Union[str, List[str]]]								=			None
					SCREAMING_SNAKE_CASE_		:       bool								=			True
					SCREAMING_SNAKE_CASE_		:       Optional[Union[List, Tuple, Dict]]								=			None
					SCREAMING_SNAKE_CASE_		:       Optional[Union[List, Dict]]								=			None
					SCREAMING_SNAKE_CASE_		:       Optional[List[str]]								=			None
					SCREAMING_SNAKE_CASE_		:       Optional[int]								=			1_00_00
					SCREAMING_SNAKE_CASE_		:       Optional[datasets.Features]								=			None
					def 	a			(						self       :						Union[str, Any]  )							->					Union[str, Any]:



											'''simple docstring'''





											if self.sql is None:
																	raise ValueError('sql must be specified'  )
											if self.con is None:
																	raise ValueError('con must be specified'  )
					def 	a			(						self       :						Tuple	,      _lowerCAmelCase       :						dict	,      _lowerCAmelCase       :						Optional[datasets.Features] = None	,      )							->					str:



											'''simple docstring'''





											__magic_name__					=      config_kwargs.copy()
											# We need to stringify the Selectable object to make its hash deterministic

											# The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
											__magic_name__					=      config_kwargs['sql']
											if not isinstance(_lowerCAmelCase	,      _lowerCAmelCase  ):
																	if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																							import sqlalchemy

																							if isinstance(_lowerCAmelCase	,      sqlalchemy.sql.Selectable  ):
																													__magic_name__					=      sqlalchemy.create_engine(config_kwargs['con'].split('://'  )[0] + '://'  )
																													__magic_name__					=      str(sql.compile(dialect=engine.dialect  )  )
																													__magic_name__					=      sql_str
																							else:
																													raise TypeError(
																													    F'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(_lowerCAmelCase  )}: {sql}'''  )
																	else:
																							raise TypeError(
																							    F'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(_lowerCAmelCase  )}: {sql}'''  )
											__magic_name__					=      config_kwargs['con']
											if not isinstance(_lowerCAmelCase	,      _lowerCAmelCase  ):
																	__magic_name__					=      id(_lowerCAmelCase  )
																	logger.info(
																	    F'''SQL connection \'con\' of type {type(_lowerCAmelCase  )} couldn\'t be hashed properly. To enable hashing, specify \'con\' as URI string instead.'''  )

											return super().create_config_id(_lowerCAmelCase	,      custom_features=_lowerCAmelCase  )







					@property
					def 	a			(						self       :						int  )							->					Optional[int]:



											'''simple docstring'''





											__magic_name__					=      {
											    'index_col': self.index_col,
											    'columns': self.columns,
											    'params': self.params,
											    'coerce_float': self.coerce_float,
											    'parse_dates': self.parse_dates,
											}
											return pd_read_sql_kwargs







class      __snake_case				(   datasets.ArrowBasedBuilder):
					SCREAMING_SNAKE_CASE_		:       str								=			SqlConfig
					def 	a			(						self       :						Tuple  )							->					str:



											'''simple docstring'''





											return datasets.DatasetInfo(features=self.config.features  )
					def 	a			(						self       :						int	,      _lowerCAmelCase       :						List[Any]  )							->					str:



											'''simple docstring'''





											return [datasets.SplitGenerator(name=datasets.Split.TRAIN	,      gen_kwargs={}  )]
					def 	a			(						self       :						int	,      _lowerCAmelCase       :						pa.Table  )							->					pa.Table:



											'''simple docstring'''





											if self.config.features is not None:
																	__magic_name__					=      self.config.features.arrow_schema
																	if all(not require_storage_cast(_lowerCAmelCase  ) for feature in self.config.features.values()  ):
																							# cheaper cast
																							__magic_name__					=      pa.Table.from_arrays([pa_table[field.name] for field in schema]	,      schema=_lowerCAmelCase  )
																	else:
																							# more expensive cast; allows str <-> int/float or str to Audio for example
																							__magic_name__					=      table_cast(_lowerCAmelCase	,      _lowerCAmelCase  )
											return pa_table







					def 	a			(						self       :						Optional[Any]  )							->					List[Any]:



											'''simple docstring'''





											__magic_name__					=      self.config.chunksize
											__magic_name__					=      pd.read_sql(
											    self.config.sql	,      self.config.con	,      chunksize=_lowerCAmelCase	,      **self.config.pd_read_sql_kwargs  )
											__magic_name__					=      [sql_reader] if chunksize is None else sql_reader
											for chunk_idx, df in enumerate(_lowerCAmelCase  ):
																	__magic_name__					=      pa.Table.from_pandas(_lowerCAmelCase  )
																	yield chunk_idx, self._cast_table(_lowerCAmelCase  )





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


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

UpperCamelCase			:					Tuple         =							{
    '''mnaylor/mega-base-wikitext''': '''https://huggingface.co/mnaylor/mega-base-wikitext/resolve/main/config.json''',
}

class      __snake_case				(   _UpperCAmelCase):
					SCREAMING_SNAKE_CASE_		:       str								=			"mega"
					def __init__(						self       :						Optional[int]	,      _lowerCAmelCase       :						List[Any]=3_0_5_2_2	,      _lowerCAmelCase       :						List[str]=1_2_8	,      _lowerCAmelCase       :						Dict=4	,      _lowerCAmelCase       :						Optional[Any]=2_5_6	,      _lowerCAmelCase       :						Optional[Any]=1_6	,      _lowerCAmelCase       :						Dict=True	,      _lowerCAmelCase       :						Optional[Any]=6_4	,      _lowerCAmelCase       :						Optional[Any]=False	,      _lowerCAmelCase       :						Optional[int]=-1	,      _lowerCAmelCase       :						Any=None	,      _lowerCAmelCase       :						List[Any]=True	,      _lowerCAmelCase       :						List[Any]="scalenorm"	,      _lowerCAmelCase       :						str=True	,      _lowerCAmelCase       :						Union[str, Any]="silu"	,      _lowerCAmelCase       :						Optional[Any]="softmax"	,      _lowerCAmelCase       :						Optional[Any]=0.1	,      _lowerCAmelCase       :						Dict=0.1	,      _lowerCAmelCase       :						Any=0.1	,      _lowerCAmelCase       :						Optional[Any]=False	,      _lowerCAmelCase       :						Any=True	,      _lowerCAmelCase       :						int=2_5_6	,      _lowerCAmelCase       :						List[str]=True	,      _lowerCAmelCase       :						str=0.1	,      _lowerCAmelCase       :						List[str]=2_0_4_8	,      _lowerCAmelCase       :						Optional[Any]=False	,      _lowerCAmelCase       :						str=2	,      _lowerCAmelCase       :						Dict=0.02	,      _lowerCAmelCase       :						str=0.2	,      _lowerCAmelCase       :						List[str]=0.02	,      _lowerCAmelCase       :						int=1.0	,      _lowerCAmelCase       :						Optional[int]=1	,      _lowerCAmelCase       :						List[str]=0	,      _lowerCAmelCase       :						str=2	,      _lowerCAmelCase       :						Optional[Any]="rotary"	,      _lowerCAmelCase       :						int=None	,      _lowerCAmelCase       :						int=True	,      _lowerCAmelCase       :						Optional[Any]=True	,      **_lowerCAmelCase       :						str	,      )							->					Any:



											'''simple docstring'''





											super().__init__(pad_token_id=_lowerCAmelCase	,      bos_token_id=_lowerCAmelCase	,      eos_token_id=_lowerCAmelCase	,      **_lowerCAmelCase  )

											__magic_name__					=      vocab_size
											__magic_name__					=      hidden_size
											__magic_name__					=      num_hidden_layers
											__magic_name__					=      activation
											__magic_name__					=      attention_activation
											__magic_name__					=      intermediate_size
											__magic_name__					=      ema_projection_size
											__magic_name__					=      bidirectional
											__magic_name__					=      shared_representation_size
											__magic_name__					=      use_chunking
											__magic_name__					=      chunk_size
											__magic_name__					=      truncation
											__magic_name__					=      normalize_before_mega
											__magic_name__					=      normalization_type
											__magic_name__					=      norm_affine
											__magic_name__					=      dropout_prob
											__magic_name__					=      hidden_dropout_prob
											__magic_name__					=      attention_probs_dropout_prob
											__magic_name__					=      use_feature_dropout
											__magic_name__					=      use_normalized_ffn
											__magic_name__					=      nffn_hidden_size
											__magic_name__					=      normalize_before_ffn
											__magic_name__					=      nffn_activation_dropout_prob
											__magic_name__					=      max_positions
											__magic_name__					=      add_token_type_embeddings
											__magic_name__					=      type_vocab_size
											__magic_name__					=      initializer_range
											__magic_name__					=      ema_delta_alpha_range
											__magic_name__					=      ema_beta_range
											__magic_name__					=      ema_gamma_omega_range
											__magic_name__					=      relative_positional_bias
											__magic_name__					=      use_cache
											__magic_name__					=      classifier_dropout
											__magic_name__					=      add_lm_hidden_dense_layer
											__magic_name__					=      1  # not used but required by Hugging Face







class      __snake_case				(   _UpperCAmelCase):
					@property
					def 	a			(						self       :						int  )							->					Mapping[str, Mapping[int, str]]:



											'''simple docstring'''





											if self.task == "multiple-choice":
																	__magic_name__					=      {0: 'batch', 1: 'choice', 2: 'sequence'}
											else:
																	__magic_name__					=      {0: 'batch', 1: 'sequence'}
											return OrderedDict(
											    [
											        ('input_ids', dynamic_axis),
											        ('attention_mask', dynamic_axis),
											    ]  )

