
"""simple docstring"""




import inspect
from typing import Callable, List, Optional, Set, Tuple, Union

import torch
from packaging import version
from safetensors.torch import storage_ptr, storage_size
from torch import nn

from .utils import logging


UpperCAmelCase__				:   str   							=			[nn.LayerNorm]

UpperCAmelCase__				:   int   							=			logging.get_logger(__name__)

UpperCAmelCase__				:   int   							=			version.parse(version.parse(torch.__version__).base_version)

UpperCAmelCase__				:   Optional[int]   							=			parsed_torch_version_base >= version.parse('2.0')
UpperCAmelCase__				:   str   							=			parsed_torch_version_base >= version.parse('1.12')
UpperCAmelCase__				:   int   							=			parsed_torch_version_base >= version.parse('1.11')
UpperCAmelCase__				:   Union[str, Any]   							=			parsed_torch_version_base < version.parse('1.11')

def lowercase_  (       _snake_case       ,_snake_case       ,_snake_case       ,_snake_case       ,self						):
							from torch import _softmax_backward_data

							if is_torch_less_than_1_11:
														return _softmax_backward_data(_snake_case       ,_snake_case       ,parent.dim       ,self						)
							else:
														return _softmax_backward_data(_snake_case       ,_snake_case       ,parent.dim       ,self.dtype						)

def lowercase_  (       _snake_case       ,_snake_case       ,_snake_case = 0						):
							SCREAMING_SNAKE_CASE__						:	str									=       index.to(layer.weight.device						)
							SCREAMING_SNAKE_CASE__						:	Dict									=       layer.weight.index_select(_snake_case       ,_snake_case						).clone().detach()
							if layer.bias is not None:
														if dim == 1:
																					SCREAMING_SNAKE_CASE__						:	List[Any]									=       layer.bias.clone().detach()
														else:
																					SCREAMING_SNAKE_CASE__						:	List[Any]									=       layer.bias[index].clone().detach()
							SCREAMING_SNAKE_CASE__						:	Any									=       list(layer.weight.size()						)
							SCREAMING_SNAKE_CASE__						:	List[str]									=       len(_snake_case						)
							SCREAMING_SNAKE_CASE__						:	Optional[Any]									=       nn.Linear(new_size[1]       ,new_size[0]       ,bias=layer.bias is not None						).to(layer.weight.device						)
							SCREAMING_SNAKE_CASE__						:	str									=       False
							new_layer.weight.copy_(W.contiguous()						)
							SCREAMING_SNAKE_CASE__						:	Optional[Any]									=       True
							if layer.bias is not None:
														SCREAMING_SNAKE_CASE__						:	List[Any]									=       False
														new_layer.bias.copy_(b.contiguous()						)
														SCREAMING_SNAKE_CASE__						:	Tuple									=       True
							return new_layer



class      lowerCAmelCase_						(nn.Module  ):


				"""simple docstring"""



				def __init__(self       ,	SCREAMING_SNAKE_CASE__       ,	SCREAMING_SNAKE_CASE__   ) ->		Optional[int]:



											"""simple docstring"""




											super().__init__()
											SCREAMING_SNAKE_CASE__						:	Tuple									=       nf
											SCREAMING_SNAKE_CASE__						:	Dict									=       nn.Parameter(torch.empty(SCREAMING_SNAKE_CASE__       ,	SCREAMING_SNAKE_CASE__   )   )
											SCREAMING_SNAKE_CASE__						:	str									=       nn.Parameter(torch.zeros(SCREAMING_SNAKE_CASE__   )   )
											nn.init.normal_(self.weight       ,	std=0.02   )



				def      __magic_name__   (self       ,	SCREAMING_SNAKE_CASE__   ) ->		Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	Tuple									=       x.size()[:-1] + (self.nf,)
											SCREAMING_SNAKE_CASE__						:	Optional[int]									=       torch.addmm(self.bias       ,	x.view(-1       ,	x.size(-1   )   )       ,	self.weight   )
											SCREAMING_SNAKE_CASE__						:	Optional[Any]									=       x.view(SCREAMING_SNAKE_CASE__   )
											return x






def lowercase_  (       _snake_case       ,_snake_case       ,_snake_case = 1						):
							SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       index.to(layer.weight.device						)
							SCREAMING_SNAKE_CASE__						:	str									=       layer.weight.index_select(_snake_case       ,_snake_case						).clone().detach()
							if dim == 0:
														SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       layer.bias.clone().detach()
							else:
														SCREAMING_SNAKE_CASE__						:	Optional[Any]									=       layer.bias[index].clone().detach()
							SCREAMING_SNAKE_CASE__						:	Any									=       list(layer.weight.size()						)
							SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       len(_snake_case						)
							SCREAMING_SNAKE_CASE__						:	Any									=       ConvaD(new_size[1]       ,new_size[0]						).to(layer.weight.device						)
							SCREAMING_SNAKE_CASE__						:	int									=       False
							new_layer.weight.copy_(W.contiguous()						)
							SCREAMING_SNAKE_CASE__						:	Dict									=       True
							SCREAMING_SNAKE_CASE__						:	Tuple									=       False
							new_layer.bias.copy_(b.contiguous()						)
							SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       True
							return new_layer

def lowercase_  (       _snake_case       ,_snake_case       ,_snake_case = None						):
							if isinstance(_snake_case       ,nn.Linear						):
														return prune_linear_layer(_snake_case       ,_snake_case       ,dim=0 if dim is None else dim						)
							elif isinstance(_snake_case       ,_snake_case						):
														return prune_convad_layer(_snake_case       ,_snake_case       ,dim=1 if dim is None else dim						)
							else:
														raise ValueError(f'''Can\'t prune layer of class {layer.__class__}'''						)

def lowercase_  (       _snake_case       ,_snake_case       ,_snake_case       ,*_snake_case						):
							assert len(_snake_case						) > 0, f'''{input_tensors} has to be a tuple/list of tensors'''

							# inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
							SCREAMING_SNAKE_CASE__						:	str									=       len(inspect.signature(_snake_case						).parameters						)
							if num_args_in_forward_chunk_fn != len(_snake_case						):
														raise ValueError(
														    f'''forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(_snake_case						)} input '''
														    """tensors are given"""						)

							if chunk_size > 0:
														SCREAMING_SNAKE_CASE__						:	List[str]									=       input_tensors[0].shape[chunk_dim]
														for input_tensor in input_tensors:
																					if input_tensor.shape[chunk_dim] != tensor_shape:
																												raise ValueError(
																												    f'''All input tenors have to be of the same shape: {tensor_shape}, '''
																												    f'''found shape {input_tensor.shape[chunk_dim]}'''						)

														if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
																					raise ValueError(
																					    f'''The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk '''
																					    f'''size {chunk_size}'''						)

														SCREAMING_SNAKE_CASE__						:	Tuple									=       input_tensors[0].shape[chunk_dim] // chunk_size

														# chunk input tensor into tuples
														SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       tuple(input_tensor.chunk(_snake_case       ,dim=_snake_case						) for input_tensor in input_tensors						)
														# apply forward fn to every tuple
														SCREAMING_SNAKE_CASE__						:	int									=       tuple(forward_fn(*_snake_case						) for input_tensors_chunk in zip(*_snake_case						)						)
														# concatenate output at same dimension
														return torch.cat(_snake_case       ,dim=_snake_case						)

							return forward_fn(*_snake_case						)

def lowercase_  (       _snake_case       ,_snake_case       ,_snake_case       ,_snake_case						):
							SCREAMING_SNAKE_CASE__						:	Dict									=       torch.ones(_snake_case       ,_snake_case						)
							SCREAMING_SNAKE_CASE__						:	Optional[int]									=       set(_snake_case						) - already_pruned_heads  # Convert to set and remove already pruned heads
							for head in heads:
														# Compute how many pruned heads are before the head and move the index accordingly
														SCREAMING_SNAKE_CASE__						:	Dict									=       head - sum(1 if h < head else 0 for h in already_pruned_heads						)
														SCREAMING_SNAKE_CASE__						:	Union[str, Any]									=       0
							SCREAMING_SNAKE_CASE__						:	int									=       mask.view(-1						).contiguous().eq(1						)
							SCREAMING_SNAKE_CASE__						:	torch.LongTensor									=       torch.arange(len(_snake_case						)						)[mask].long()
							return heads, index

def lowercase_  (       *_snake_case       ,_snake_case = None						):
							return torch.meshgrid(*_snake_case       ,indexing=_snake_case						)

def lowercase_  (       _snake_case						):
							return tensor.device, storage_ptr(_snake_case						), storage_size(_snake_case						)






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


UpperCAmelCase__				:   Union[str, Any]   							=			datasets.utils.logging.get_logger(__name__)



@dataclass
class      lowerCAmelCase_						(datasets.BuilderConfig  ):


				"""simple docstring"""



				__UpperCamelCase				:						Union[str, "sqlalchemy.sql.Selectable"] 	=       None
				__UpperCamelCase				:						Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] 	=       None
				__UpperCamelCase				:						Optional[Union[str, List[str]]] 	=       None
				__UpperCamelCase				:						bool 	=       True
				__UpperCamelCase				:						Optional[Union[List, Tuple, Dict]] 	=       None
				__UpperCamelCase				:						Optional[Union[List, Dict]] 	=       None
				__UpperCamelCase				:						Optional[List[str]] 	=       None
				__UpperCamelCase				:						Optional[int] 	=       10000
				__UpperCamelCase				:						Optional[datasets.Features] 	=       None



				def      __magic_name__   (self   ) ->		Dict:



											"""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      __magic_name__   (self       ,	SCREAMING_SNAKE_CASE__       ,	SCREAMING_SNAKE_CASE__ = None       ,	) ->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	List[Any]									=       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
											SCREAMING_SNAKE_CASE__						:	Dict									=       config_kwargs["""sql"""]
											if not isinstance(SCREAMING_SNAKE_CASE__       ,	SCREAMING_SNAKE_CASE__   ):
																		if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																									import sqlalchemy

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

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



				@property
				def      __magic_name__   (self   ) ->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:	Optional[int]									=       {
											    """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      lowerCAmelCase_						(datasets.ArrowBasedBuilder  ):


				"""simple docstring"""



				__UpperCamelCase				:						List[str] 	=       SqlConfig



				def      __magic_name__   (self   ) ->		List[Any]:



											"""simple docstring"""




											return datasets.DatasetInfo(features=self.config.features   )



				def      __magic_name__   (self       ,	SCREAMING_SNAKE_CASE__   ) ->		List[str]:



											"""simple docstring"""




											return [datasets.SplitGenerator(name=datasets.Split.TRAIN       ,	gen_kwargs={}   )]



				def      __magic_name__   (self       ,	SCREAMING_SNAKE_CASE__   ) ->		pa.Table:



											"""simple docstring"""




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



				def      __magic_name__   (self   ) ->		Optional[int]:



											"""simple docstring"""




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




