

from dataclasses import dataclass
from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..models.embeddings import ImagePositionalEmbeddings
from ..utils import BaseOutput, deprecate
from .attention import BasicTransformerBlock
from .embeddings import PatchEmbed
from .modeling_utils import ModelMixin


@dataclass
class 					A		(				_UpperCAmelCase					):







    """simple docstring"""





    lowerCamelCase						      =				42




class 					A		(				_UpperCAmelCase       ,				_UpperCAmelCase					):







    """simple docstring"""





    @register_to_config
    def __init__( self					:			int,lowercase_					:			int = 1_6,lowercase_					:			int = 8_8,lowercase_					:			Optional[int] = None,lowercase_					:			Optional[int] = None,lowercase_					:			int = 1,lowercase_					:			float = 0.0,lowercase_					:			int = 3_2,lowercase_					:			Optional[int] = None,lowercase_					:			bool = False,lowercase_					:			Optional[int] = None,lowercase_					:			Optional[int] = None,lowercase_					:			Optional[int] = None,lowercase_					:			str = "geglu",lowercase_					:			Optional[int] = None,lowercase_					:			bool = False,lowercase_					:			bool = False,lowercase_					:			bool = False,lowercase_					:			str = "layer_norm",lowercase_					:			bool = True,)->       int:





        '''simple docstring'''







        super().__init__()
        A__        =  use_linear_projection
        A__        =  num_attention_heads
        A__        =  attention_head_dim
        A__        =  num_attention_heads * attention_head_dim

        # 1. Transformer2DModel can process both standard continuous images of shape `(batch_size, num_channels, width, height)` as well as quantized image embeddings of shape `(batch_size, num_image_vectors)`
        # Define whether input is continuous or discrete depending on configuration
        A__        =  (in_channels is not None) and (patch_size is None)
        A__        =  num_vector_embeds is not None
        A__        =  in_channels is not None and patch_size is not None

        if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
            A__        =  (
                F'The configuration file of this model: {self.__class__} is outdated. `norm_type` is either not set or'
                ' incorrectly set to `\'layer_norm\'`.Make sure to set `norm_type` to `\'ada_norm\'` in the config.'
                ' Please make sure to update the config accordingly as leaving `norm_type` might led to incorrect'
                ' results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it'
                ' would be very nice if you could open a Pull request for the `transformer/config.json` file'
            )
            deprecate('norm_type!=num_embeds_ada_norm','1.0.0',lowercase_,standard_warn=lowercase_						)
            A__        =  'ada_norm'

        if self.is_input_continuous and self.is_input_vectorized:
            raise ValueError(
                F'Cannot define both `in_channels`: {in_channels} and `num_vector_embeds`: {num_vector_embeds}. Make'
                ' sure that either `in_channels` or `num_vector_embeds` is None.'						)
        elif self.is_input_vectorized and self.is_input_patches:
            raise ValueError(
                F'Cannot define both `num_vector_embeds`: {num_vector_embeds} and `patch_size`: {patch_size}. Make'
                ' sure that either `num_vector_embeds` or `num_patches` is None.'						)
        elif not self.is_input_continuous and not self.is_input_vectorized and not self.is_input_patches:
            raise ValueError(
                F'Has to define `in_channels`: {in_channels}, `num_vector_embeds`: {num_vector_embeds}, or patch_size:'
                F' {patch_size}. Make sure that `in_channels`, `num_vector_embeds` or `num_patches` is not None.'						)

        # 2. Define input layers
        if self.is_input_continuous:
            A__        =  in_channels

            A__        =  torch.nn.GroupNorm(num_groups=lowercase_,num_channels=lowercase_,eps=1E-6,affine=lowercase_						)
            if use_linear_projection:
                A__        =  nn.Linear(lowercase_,lowercase_						)
            else:
                A__        =  nn.Convad(lowercase_,lowercase_,kernel_size=1,stride=1,padding=0						)
        elif self.is_input_vectorized:
            assert sample_size is not None, "Transformer2DModel over discrete input must provide sample_size"
            assert num_vector_embeds is not None, "Transformer2DModel over discrete input must provide num_embed"

            A__        =  sample_size
            A__        =  sample_size
            A__        =  num_vector_embeds
            A__        =  self.height * self.width

            A__        =  ImagePositionalEmbeddings(
                num_embed=lowercase_,embed_dim=lowercase_,height=self.height,width=self.width						)
        elif self.is_input_patches:
            assert sample_size is not None, "Transformer2DModel over patched input must provide sample_size"

            A__        =  sample_size
            A__        =  sample_size

            A__        =  patch_size
            A__        =  PatchEmbed(
                height=lowercase_,width=lowercase_,patch_size=lowercase_,in_channels=lowercase_,embed_dim=lowercase_,)

        # 3. Define transformers blocks
        A__        =  nn.ModuleList(
            [
                BasicTransformerBlock(
                    lowercase_,lowercase_,lowercase_,dropout=lowercase_,cross_attention_dim=lowercase_,activation_fn=lowercase_,num_embeds_ada_norm=lowercase_,attention_bias=lowercase_,only_cross_attention=lowercase_,upcast_attention=lowercase_,norm_type=lowercase_,norm_elementwise_affine=lowercase_,)
                for d in range(lowercase_						)
            ]						)

        # 4. Define output layers
        A__        =  in_channels if out_channels is None else out_channels
        if self.is_input_continuous:
            # TODO: should use out_channels for continuous projections
            if use_linear_projection:
                A__        =  nn.Linear(lowercase_,lowercase_						)
            else:
                A__        =  nn.Convad(lowercase_,lowercase_,kernel_size=1,stride=1,padding=0						)
        elif self.is_input_vectorized:
            A__        =  nn.LayerNorm(lowercase_						)
            A__        =  nn.Linear(lowercase_,self.num_vector_embeds - 1						)
        elif self.is_input_patches:
            A__        =  nn.LayerNorm(lowercase_,elementwise_affine=lowercase_,eps=1E-6						)
            A__        =  nn.Linear(lowercase_,2 * inner_dim						)
            A__        =  nn.Linear(lowercase_,patch_size * patch_size * self.out_channels						)


    def 		snake_case__	( self					:			Union[str, Any],lowercase_					:			torch.Tensor,lowercase_					:			Optional[torch.Tensor] = None,lowercase_					:			Optional[torch.LongTensor] = None,lowercase_					:			Optional[torch.LongTensor] = None,lowercase_					:			Dict[str, Any] = None,lowercase_					:			Optional[torch.Tensor] = None,lowercase_					:			Optional[torch.Tensor] = None,lowercase_					:			bool = True,)->       Dict:





        '''simple docstring'''







        if attention_mask is not None and attention_mask.ndim == 2:
            # assume that mask is expressed as:
            #   (1 = keep,      0 = discard)
            # convert mask into a bias that can be added to attention scores:
            #       (keep = +0,     discard = -10000.0)
            A__        =  (1 - attention_mask.to(hidden_states.dtype						)) * -10_000.0
            A__        =  attention_mask.unsqueeze(1						)

        # convert encoder_attention_mask to a bias the same way we do for attention_mask
        if encoder_attention_mask is not None and encoder_attention_mask.ndim == 2:
            A__        =  (1 - encoder_attention_mask.to(hidden_states.dtype						)) * -10_000.0
            A__        =  encoder_attention_mask.unsqueeze(1						)

        # 1. Input
        if self.is_input_continuous:
            A__ ,		A__ ,		A__ ,		A__        =  hidden_states.shape
            A__        =  hidden_states

            A__        =  self.norm(lowercase_						)
            if not self.use_linear_projection:
                A__        =  self.proj_in(lowercase_						)
                A__        =  hidden_states.shape[1]
                A__        =  hidden_states.permute(0,2,3,1						).reshape(lowercase_,height * width,lowercase_						)
            else:
                A__        =  hidden_states.shape[1]
                A__        =  hidden_states.permute(0,2,3,1						).reshape(lowercase_,height * width,lowercase_						)
                A__        =  self.proj_in(lowercase_						)
        elif self.is_input_vectorized:
            A__        =  self.latent_image_embedding(lowercase_						)
        elif self.is_input_patches:
            A__        =  self.pos_embed(lowercase_						)

        # 2. Blocks
        for block in self.transformer_blocks:
            A__        =  block(
                lowercase_,attention_mask=lowercase_,encoder_hidden_states=lowercase_,encoder_attention_mask=lowercase_,timestep=lowercase_,cross_attention_kwargs=lowercase_,class_labels=lowercase_,)

        # 3. Output
        if self.is_input_continuous:
            if not self.use_linear_projection:
                A__        =  hidden_states.reshape(lowercase_,lowercase_,lowercase_,lowercase_						).permute(0,3,1,2						).contiguous()
                A__        =  self.proj_out(lowercase_						)
            else:
                A__        =  self.proj_out(lowercase_						)
                A__        =  hidden_states.reshape(lowercase_,lowercase_,lowercase_,lowercase_						).permute(0,3,1,2						).contiguous()

            A__        =  hidden_states + residual
        elif self.is_input_vectorized:
            A__        =  self.norm_out(lowercase_						)
            A__        =  self.out(lowercase_						)
            # (batch, self.num_vector_embeds - 1, self.num_latent_pixels)
            A__        =  logits.permute(0,2,1						)

            # log(p(x_0))
            A__        =  F.log_softmax(logits.double(),dim=1						).float()
        elif self.is_input_patches:
            # TODO: cleanup!
            A__        =  self.transformer_blocks[0].norma.emb(
                lowercase_,lowercase_,hidden_dtype=hidden_states.dtype						)
            A__ ,		A__        =  self.proj_out_a(F.silu(lowercase_						)						).chunk(2,dim=1						)
            A__        =  self.norm_out(lowercase_						) * (1 + scale[:, None]) + shift[:, None]
            A__        =  self.proj_out_a(lowercase_						)

            # unpatchify
            A__        =  A__        =  int(hidden_states.shape[1] ** 0.5						)
            A__        =  hidden_states.reshape(
                shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels)						)
            A__        =  torch.einsum('nhwpqc->nchpwq',lowercase_						)
            A__        =  hidden_states.reshape(
                shape=(-1, self.out_channels, height * self.patch_size, width * self.patch_size)						)

        if not return_dict:
            return (output,)

        return TransformeraDModelOutput(sample=lowercase_						)




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_						)

