





'''simple docstring'''






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


A__       :     Dict          =datasets.utils.logging.get_logger(__name__)




@dataclass
class 				UpperCAmelCase  (    datasets.BuilderConfig     ):
						_lowercase:    Tuple		  =     None
						_lowercase:    List[Any]		  =     None
						_lowercase:    int		  =     None
						_lowercase:    Any		  =     True
						_lowercase:    int		  =     None
						_lowercase:    List[Any]		  =     None
						_lowercase:    List[Any]		  =     None
						_lowercase:    List[str]		  =     10000
						_lowercase:    Tuple		  =     None




						def        lowercase__					(    self    :				List[str]						)       ->   List[str]:







												if self.sql is None:
																		raise ValueError("""sql must be specified"""						)
												if self.con is None:
																		raise ValueError("""con must be specified"""						)




						def        lowercase__					(    self    :				Union[str, Any]    ,    __snake_case    :				Any    ,    __snake_case    :				Optional[Any] = None    ,    )       ->   str:
												_lowerCAmelCase							  =							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
												_lowerCAmelCase							  =							config_kwargs["sql"]
												if not isinstance(__A    ,    __A						):
																		if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																								import sqlalchemy

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

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




						@property
						def        lowercase__					(    self    :				Union[str, Any]						)       ->   int:
												_lowerCAmelCase							  =							{
												    "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 				UpperCAmelCase  (    datasets.ArrowBasedBuilder     ):
						_lowercase:    List[str]		  =     SqlConfig




						def        lowercase__					(    self    :				str						)       ->   Union[str, Any]:
												return datasets.DatasetInfo(features=self.config.features						)




						def        lowercase__					(    self    :				Optional[Any]    ,    __snake_case    :				List[str]						)       ->   str:
												return [datasets.SplitGenerator(name=datasets.Split.TRAIN    ,    gen_kwargs={}						)]




						def        lowercase__					(    self    :				List[Any]    ,    __snake_case    :				List[Any]						)       ->   pa.Table:







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




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













'''simple docstring'''






import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
      from transformers.models.electra.modeling_flax_electra import (
          FlaxElectraForCausalLM,
          FlaxElectraForMaskedLM,
          FlaxElectraForMultipleChoice,
          FlaxElectraForPreTraining,
          FlaxElectraForQuestionAnswering,
          FlaxElectraForSequenceClassification,
          FlaxElectraForTokenClassification,
          FlaxElectraModel,
      )




class 				UpperCAmelCase  (    unittest.TestCase     ):




      def __init__(    self    :				str    ,    __snake_case    :				str    ,    __snake_case    :				str=13    ,    __snake_case    :				Optional[Any]=7    ,    __snake_case    :				str=True    ,    __snake_case    :				Any=True    ,    __snake_case    :				List[str]=True    ,    __snake_case    :				Any=True    ,    __snake_case    :				List[str]=99    ,    __snake_case    :				Dict=24    ,    __snake_case    :				Any=32    ,    __snake_case    :				List[str]=5    ,    __snake_case    :				Any=4    ,    __snake_case    :				Optional[Any]=37    ,    __snake_case    :				Union[str, Any]="gelu"    ,    __snake_case    :				Dict=0.1    ,    __snake_case    :				int=0.1    ,    __snake_case    :				Dict=5_12    ,    __snake_case    :				List[Any]=16    ,    __snake_case    :				Dict=2    ,    __snake_case    :				str=0.02    ,    __snake_case    :				Any=4    ,    )       ->   Union[str, Any]:
            _lowerCAmelCase							  =							parent
            _lowerCAmelCase							  =							batch_size
            _lowerCAmelCase							  =							seq_length
            _lowerCAmelCase							  =							is_training
            _lowerCAmelCase							  =							use_attention_mask
            _lowerCAmelCase							  =							use_token_type_ids
            _lowerCAmelCase							  =							use_labels
            _lowerCAmelCase							  =							vocab_size
            _lowerCAmelCase							  =							hidden_size
            _lowerCAmelCase							  =							embedding_size
            _lowerCAmelCase							  =							num_hidden_layers
            _lowerCAmelCase							  =							num_attention_heads
            _lowerCAmelCase							  =							intermediate_size
            _lowerCAmelCase							  =							hidden_act
            _lowerCAmelCase							  =							hidden_dropout_prob
            _lowerCAmelCase							  =							attention_probs_dropout_prob
            _lowerCAmelCase							  =							max_position_embeddings
            _lowerCAmelCase							  =							type_vocab_size
            _lowerCAmelCase							  =							type_sequence_label_size
            _lowerCAmelCase							  =							initializer_range
            _lowerCAmelCase							  =							num_choices




      def        lowercase__					(    self    :				Optional[Any]						)       ->   Optional[Any]:
            _lowerCAmelCase							  =							ids_tensor([self.batch_size, self.seq_length]    ,    self.vocab_size						)

            _lowerCAmelCase							  =							None
            if self.use_attention_mask:
                  _lowerCAmelCase							  =							random_attention_mask([self.batch_size, self.seq_length]						)

            _lowerCAmelCase							  =							None
            if self.use_token_type_ids:
                  _lowerCAmelCase							  =							ids_tensor([self.batch_size, self.seq_length]    ,    self.type_vocab_size						)

            _lowerCAmelCase							  =							ElectraConfig(
                vocab_size=self.vocab_size    ,    hidden_size=self.hidden_size    ,    embedding_size=self.embedding_size    ,    num_hidden_layers=self.num_hidden_layers    ,    num_attention_heads=self.num_attention_heads    ,    intermediate_size=self.intermediate_size    ,    hidden_act=self.hidden_act    ,    hidden_dropout_prob=self.hidden_dropout_prob    ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,    max_position_embeddings=self.max_position_embeddings    ,    type_vocab_size=self.type_vocab_size    ,    initializer_range=self.initializer_range    ,    )

            return config, input_ids, token_type_ids, attention_mask




      def        lowercase__					(    self    :				Any						)       ->   Optional[int]:
            _lowerCAmelCase							  =							self.prepare_config_and_inputs()
            _lowerCAmelCase  ,							_lowerCAmelCase  ,							_lowerCAmelCase  ,							_lowerCAmelCase							  =							config_and_inputs
            _lowerCAmelCase							  =							{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
            return config, inputs_dict




@require_flax
class 				UpperCAmelCase  (    snake_case_							,	unittest.TestCase     ):
      _lowercase:    str		  =     True

      _lowercase:    Optional[Any]		  =     (
          (
              FlaxElectraModel,
              FlaxElectraForCausalLM,
              FlaxElectraForMaskedLM,
              FlaxElectraForPreTraining,
              FlaxElectraForTokenClassification,
              FlaxElectraForQuestionAnswering,
              FlaxElectraForMultipleChoice,
              FlaxElectraForSequenceClassification,
          )
          if is_flax_available()
          else ()
      )




      def        lowercase__					(    self    :				Tuple						)       ->   Optional[int]:
            _lowerCAmelCase							  =							FlaxElectraModelTester(self						)




      @slow
      def        lowercase__					(    self    :				List[Any]						)       ->   str:
            for model_class_name in self.all_model_classes:
                  if model_class_name == FlaxElectraForMaskedLM:
                        _lowerCAmelCase							  =							model_class_name.from_pretrained("""google/electra-small-generator"""						)
                  else:
                        _lowerCAmelCase							  =							model_class_name.from_pretrained("""google/electra-small-discriminator"""						)
                  _lowerCAmelCase							  =							model(np.ones((1, 1)						)						)
                  self.assertIsNotNone(__snake_case						)






