

import logging

import torch
from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.albert.modeling_albert import (
    ALBERT_INPUTS_DOCSTRING,
    ALBERT_START_DOCSTRING,
    AlbertModel,
    AlbertPreTrainedModel,
    AlbertTransformer,
)


lowercase_									=    logging.getLogger(__name__)


class 					A		(				_UpperCAmelCase					):







    """simple docstring"""





    def 		snake_case__	( self					:			Optional[Any],lowercase_					:			List[Any],lowercase_					:			int,lowercase_					:			List[Any]=None,lowercase_					:			str=None						)->       str:





        '''simple docstring'''







        if current_layer == 0:
            A__        =  self.embedding_hidden_mapping_in(lowercase_						)
        else:
            A__        =  hidden_states[0]

        A__        =  int(self.config.num_hidden_layers / self.config.num_hidden_groups						)

        # Index of the hidden group
        A__        =  int(current_layer / (self.config.num_hidden_layers / self.config.num_hidden_groups)						)

        A__        =  self.albert_layer_groups[group_idx](
            lowercase_,lowercase_,head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group],)
        A__        =  layer_group_output[0]

        return (hidden_states,)




@add_start_docstrings(
    'The bare ALBERT Model transformer with PABEE outputting raw hidden-states without any specific head on top.'       ,				_UpperCAmelCase       ,				)
class 					A		(				_UpperCAmelCase					):







    """simple docstring"""





    def __init__( self					:			List[str],lowercase_					:			Optional[Any]						)->       List[str]:





        '''simple docstring'''







        super().__init__(lowercase_						)

        A__        =  AlbertTransformerWithPabee(lowercase_						)

        self.init_weights()
        A__        =  0
        A__        =  0
        A__        =  0

        A__        =  0


    def 		snake_case__	( self					:			int,lowercase_					:			List[Any]						)->       List[Any]:





        '''simple docstring'''







        A__        =  threshold


    def 		snake_case__	( self					:			List[str],lowercase_					:			str						)->       int:





        '''simple docstring'''







        A__        =  patience


    def 		snake_case__	( self					:			Tuple						)->       Union[str, Any]:





        '''simple docstring'''







        A__        =  0
        A__        =  0


    def 		snake_case__	( self					:			str						)->       Tuple:





        '''simple docstring'''







        A__        =  self.inference_layers_num / self.inference_instances_num
        A__        =  (
            F'*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up ='
            F' {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***'
        )
        print(lowercase_						)


    @add_start_docstrings_to_model_forward(lowercase_						)
    def 		snake_case__	( self					:			Union[str, Any],lowercase_					:			int=None,lowercase_					:			Dict=None,lowercase_					:			int=None,lowercase_					:			int=None,lowercase_					:			int=None,lowercase_					:			str=None,lowercase_					:			str=None,lowercase_					:			Optional[Any]=None,lowercase_					:			str=False,)->       Dict:





        '''simple docstring'''







        if input_ids is not None and inputs_embeds is not None:
            raise ValueError('You cannot specify both input_ids and inputs_embeds at the same time'						)
        elif input_ids is not None:
            A__        =  input_ids.size()
        elif inputs_embeds is not None:
            A__        =  inputs_embeds.size()[:-1]
        else:
            raise ValueError('You have to specify either input_ids or inputs_embeds'						)

        A__        =  input_ids.device if input_ids is not None else inputs_embeds.device

        if attention_mask is None:
            A__        =  torch.ones(lowercase_,device=lowercase_						)
        if token_type_ids is None:
            A__        =  torch.zeros(lowercase_,dtype=torch.long,device=lowercase_						)

        A__        =  attention_mask.unsqueeze(1						).unsqueeze(2						)
        A__        =  extended_attention_mask.to(dtype=self.dtype						)  # fp16 compatibility
        A__        =  (1.0 - extended_attention_mask) * -10_000.0
        A__        =  self.get_head_mask(lowercase_,self.config.num_hidden_layers						)

        A__        =  self.embeddings(
            lowercase_,position_ids=lowercase_,token_type_ids=lowercase_,inputs_embeds=lowercase_						)
        A__        =  embedding_output

        if self.training:
            A__        =  []
            for i in range(self.config.num_hidden_layers						):
                A__        =  self.encoder.adaptive_forward(
                    lowercase_,current_layer=lowercase_,attention_mask=lowercase_,head_mask=lowercase_,)

                A__        =  self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]						)						)
                A__        =  output_layers[i](output_dropout(lowercase_						)						)
                res.append(lowercase_						)
        elif self.patience == 0:  # Use all layers for inference
            A__        =  self.encoder(lowercase_,lowercase_,head_mask=lowercase_						)
            A__        =  self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]						)						)
            A__        =  [output_layers[self.config.num_hidden_layers - 1](lowercase_						)]
        else:
            A__        =  0
            A__        =  None
            A__        =  0
            for i in range(self.config.num_hidden_layers						):
                calculated_layer_num += 1
                A__        =  self.encoder.adaptive_forward(
                    lowercase_,current_layer=lowercase_,attention_mask=lowercase_,head_mask=lowercase_,)

                A__        =  self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]						)						)
                A__        =  output_layers[i](lowercase_						)
                if regression:
                    A__        =  logits.detach()
                    if patient_result is not None:
                        A__        =  patient_result.detach()
                    if (patient_result is not None) and torch.abs(patient_result - labels						) < self.regression_threshold:
                        patient_counter += 1
                    else:
                        A__        =  0
                else:
                    A__        =  logits.detach().argmax(dim=1						)
                    if patient_result is not None:
                        A__        =  patient_result.detach().argmax(dim=1						)
                    if (patient_result is not None) and torch.all(labels.eq(lowercase_						)						):
                        patient_counter += 1
                    else:
                        A__        =  0

                A__        =  logits
                if patient_counter == self.patience:
                    break
            A__        =  [patient_result]
            self.inference_layers_num += calculated_layer_num
            self.inference_instances_num += 1

        return res




@add_start_docstrings(
    'Albert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of\n    the pooled output) e.g. for GLUE tasks. '       ,				_UpperCAmelCase       ,				)
class 					A		(				_UpperCAmelCase					):







    """simple docstring"""





    def __init__( self					:			Tuple,lowercase_					:			str						)->       int:





        '''simple docstring'''







        super().__init__(lowercase_						)
        A__        =  config.num_labels

        A__        =  AlbertModelWithPabee(lowercase_						)
        A__        =  nn.Dropout(config.classifier_dropout_prob						)
        A__        =  nn.ModuleList(
            [nn.Linear(config.hidden_size,self.config.num_labels						) for _ in range(config.num_hidden_layers						)]						)

        self.init_weights()


    @add_start_docstrings_to_model_forward(lowercase_						)
    def 		snake_case__	( self					:			Any,lowercase_					:			Tuple=None,lowercase_					:			List[Any]=None,lowercase_					:			Optional[Any]=None,lowercase_					:			Union[str, Any]=None,lowercase_					:			str=None,lowercase_					:			List[str]=None,lowercase_					:			Optional[Any]=None,)->       Any:





        '''simple docstring'''







        A__        =  self.albert(
            input_ids=lowercase_,attention_mask=lowercase_,token_type_ids=lowercase_,position_ids=lowercase_,head_mask=lowercase_,inputs_embeds=lowercase_,output_dropout=self.dropout,output_layers=self.classifiers,regression=self.num_labels == 1,)

        A__        =  (logits[-1],)

        if labels is not None:
            A__        =  None
            A__        =  0
            for ix, logits_item in enumerate(lowercase_						):
                if self.num_labels == 1:
                    #  We are doing regression
                    A__        =  MSELoss()
                    A__        =  loss_fct(logits_item.view(-1						),labels.view(-1						)						)
                else:
                    A__        =  CrossEntropyLoss()
                    A__        =  loss_fct(logits_item.view(-1,self.num_labels						),labels.view(-1						)						)
                if total_loss is None:
                    A__        =  loss
                else:
                    total_loss += loss * (ix + 1)
                total_weights += ix + 1
            A__        =  (total_loss / total_weights,) + outputs

        return outputs




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


lowercase_									=    datasets.utils.logging.get_logger(__name__)


@dataclass
class 					A		(				datasets.BuilderConfig					):







    """simple docstring"""





    lowerCamelCase						      =				None
    lowerCamelCase						      =				None
    lowerCamelCase						      =				None
    lowerCamelCase						      =				True
    lowerCamelCase						      =				None
    lowerCamelCase						      =				None
    lowerCamelCase						      =				None
    lowerCamelCase						      =				1_00_00
    lowerCamelCase						      =				None


    def 		snake_case__	( self					:			Tuple						)->       Optional[int]:





        '''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 		snake_case__	( self					:			Dict,lowercase_					:			dict,lowercase_					:			Optional[datasets.Features] = None,)->       str:





        '''simple docstring'''







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

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

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


    @property
    def 		snake_case__	( self					:			Union[str, Any]						)->       Any:





        '''simple docstring'''







        A__        =  {
            '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 					A		(				datasets.ArrowBasedBuilder					):







    """simple docstring"""





    lowerCamelCase						      =				SqlConfig


    def 		snake_case__	( self					:			Any						)->       Any:





        '''simple docstring'''







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


    def 		snake_case__	( self					:			Tuple,lowercase_					:			List[Any]						)->       Union[str, Any]:





        '''simple docstring'''







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


    def 		snake_case__	( self					:			Any,lowercase_					:			pa.Table						)->       pa.Table:





        '''simple docstring'''







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


    def 		snake_case__	( self					:			List[str]						)->       Union[str, Any]:





        '''simple docstring'''







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

