
'''simple docstring'''

from typing import Optional, Tuple

import flax.linen as nn
import jax
import jax.numpy as jnp
from configuration_hybrid_clip import HybridCLIPConfig
from flax.core.frozen_dict import FrozenDict

from transformers import FLAX_MODEL_MAPPING, FlaxCLIPVisionModel
from transformers.modeling_flax_utils import FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPOutput
from transformers.utils import logging


lowerCAmelCase_			:			List[str]			   =     logging.get_logger(__name__)







class 	__lowerCAmelCase    (  nn.Module				):
     snake_case   :    HybridCLIPConfig
     snake_case   :    jnp.dtype							      =      jnp.floataa



     def     snake_case_ (self	):
          _UpperCAmelCase					:  int         =    self.config.text_config
          _UpperCAmelCase					:  Dict         =    self.config.vision_config

          _UpperCAmelCase					:  str         =    self.config.projection_dim
          _UpperCAmelCase					:  int         =    text_config.hidden_size
          _UpperCAmelCase					:  int         =    vision_config.hidden_size

          _UpperCAmelCase					:  Optional[int]         =    FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
          _UpperCAmelCase					:  List[Any]         =    FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__						,	lowerCAmelCase__	).module_class

          _UpperCAmelCase					:  List[Any]         =    text_module(lowerCAmelCase__						,	dtype=self.dtype	)
          _UpperCAmelCase					:  Union[str, Any]         =    vision_module(lowerCAmelCase__						,	dtype=self.dtype	)

          _UpperCAmelCase					:  Optional[int]         =    nn.Dense(
              self.projection_dim						,	dtype=self.dtype						,	kernel_init=jax.nn.initializers.normal(0.0_2	)						,	use_bias=lowerCAmelCase__						,	)
          _UpperCAmelCase					:  List[str]         =    nn.Dense(
              self.projection_dim						,	dtype=self.dtype						,	kernel_init=jax.nn.initializers.normal(0.0_2	)						,	use_bias=lowerCAmelCase__						,	)
          _UpperCAmelCase					:  List[Any]         =    self.param("""logit_scale"""						,	jax.nn.initializers.ones						,	[]	)



     def __call__(self						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__ = True						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	):
          _UpperCAmelCase					:  int         =    return_dict if return_dict is not None else self.config.return_dict

          _UpperCAmelCase					:  Dict         =    self.vision_model(
              pixel_values=lowerCAmelCase__						,	deterministic=lowerCAmelCase__						,	output_attentions=lowerCAmelCase__						,	output_hidden_states=lowerCAmelCase__						,	return_dict=lowerCAmelCase__						,	)

          _UpperCAmelCase					:  int         =    self.text_model(
              input_ids=lowerCAmelCase__						,	attention_mask=lowerCAmelCase__						,	token_type_ids=lowerCAmelCase__						,	position_ids=lowerCAmelCase__						,	deterministic=lowerCAmelCase__						,	output_attentions=lowerCAmelCase__						,	output_hidden_states=lowerCAmelCase__						,	return_dict=lowerCAmelCase__						,	)

          _UpperCAmelCase					:  int         =    vision_outputs[1]
          _UpperCAmelCase					:  Dict         =    self.visual_projection(lowerCAmelCase__	)

          _UpperCAmelCase					:  List[str]         =    text_outputs[1]
          _UpperCAmelCase					:  List[Any]         =    self.text_projection(lowerCAmelCase__	)

          # normalized features
          _UpperCAmelCase					:  List[Any]         =    image_embeds / jnp.linalg.norm(lowerCAmelCase__						,	axis=-1						,	keepdims=lowerCAmelCase__	)
          _UpperCAmelCase					:  Optional[Any]         =    text_embeds / jnp.linalg.norm(lowerCAmelCase__						,	axis=-1						,	keepdims=lowerCAmelCase__	)

          # cosine similarity as logits
          _UpperCAmelCase					:  str         =    jnp.exp(self.logit_scale	)
          _UpperCAmelCase					:  str         =    jnp.matmul(lowerCAmelCase__						,	image_embeds.T	) * logit_scale
          _UpperCAmelCase					:  Any         =    logits_per_text.T

          if not return_dict:
               return (logits_per_image, logits_per_text, text_embeds, image_embeds, text_outputs, vision_outputs)

          return FlaxCLIPOutput(
              logits_per_image=lowerCAmelCase__						,	logits_per_text=lowerCAmelCase__						,	text_embeds=lowerCAmelCase__						,	image_embeds=lowerCAmelCase__						,	text_model_output=lowerCAmelCase__						,	vision_model_output=lowerCAmelCase__						,	)







class 	__lowerCAmelCase    (  __a				):
     snake_case   :    List[Any]							      =      HybridCLIPConfig
     snake_case   :    int							      =      FlaxHybridCLIPModule



     def __init__(self						,	lowerCAmelCase__						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = 0						,	lowerCAmelCase__ = jnp.floataa						,	**lowerCAmelCase__						,	):
          if input_shape is None:
               _UpperCAmelCase					:  Optional[Any]         =    ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

          _UpperCAmelCase					:  Union[str, Any]         =    self.module_class(config=lowerCAmelCase__						,	dtype=lowerCAmelCase__						,	**lowerCAmelCase__	)
          super().__init__(lowerCAmelCase__						,	lowerCAmelCase__						,	input_shape=lowerCAmelCase__						,	seed=lowerCAmelCase__						,	dtype=lowerCAmelCase__	)



     def     snake_case_ (self						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__ = None	):
          # init input tensor
          _UpperCAmelCase					:  List[Any]         =    jnp.zeros(input_shape[0]						,	dtype="""i4"""	)
          _UpperCAmelCase					:  str         =    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowerCAmelCase__	).shape[-1]	)						,	input_shape[0]	)
          _UpperCAmelCase					:  List[Any]         =    jnp.ones_like(lowerCAmelCase__	)
          _UpperCAmelCase					:  List[str]         =    jnp.ones_like(lowerCAmelCase__	)

          _UpperCAmelCase					:  Any         =    jax.random.normal(lowerCAmelCase__						,	input_shape[1]	)

          _UpperCAmelCase  ,     _UpperCAmelCase					:  int         =    jax.random.split(lowerCAmelCase__	)
          _UpperCAmelCase					:  Any         =    {"""params""": params_rng, """dropout""": dropout_rng}

          return self.module.init(lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__	)["params"]



     def __call__(self						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = False						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	):
          _UpperCAmelCase					:  Any         =    output_attentions if output_attentions is not None else self.config.output_attentions
          _UpperCAmelCase					:  int         =    (
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          _UpperCAmelCase					:  int         =    return_dict if return_dict is not None else self.config.return_dict

          if position_ids is None:
               _UpperCAmelCase					:  List[Any]         =    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowerCAmelCase__	).shape[-1]	)						,	input_ids.shape	)

          if token_type_ids is None:
               _UpperCAmelCase					:  List[str]         =    jnp.zeros_like(lowerCAmelCase__	)

          if attention_mask is None:
               _UpperCAmelCase					:  Any         =    jnp.ones_like(lowerCAmelCase__	)

          # Handle any PRNG if needed
          _UpperCAmelCase					:  List[str]         =    {}
          if dropout_rng is not None:
               _UpperCAmelCase					:  Union[str, Any]         =    dropout_rng

          return self.module.apply(
              {"""params""": params or self.params}						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype=jnp.floataa	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	not train						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	rngs=lowerCAmelCase__						,	)



     def     snake_case_ (self						,	lowerCAmelCase__						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__=None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__=False						,	):
          if position_ids is None:
               _UpperCAmelCase					:  str         =    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowerCAmelCase__	).shape[-1]	)						,	input_ids.shape	)

          if token_type_ids is None:
               _UpperCAmelCase					:  str         =    jnp.zeros_like(lowerCAmelCase__	)

          if attention_mask is None:
               _UpperCAmelCase					:  Optional[Any]         =    jnp.ones_like(lowerCAmelCase__	)

          # Handle any PRNG if needed
          _UpperCAmelCase					:  str         =    {}
          if dropout_rng is not None:
               _UpperCAmelCase					:  List[Any]         =    dropout_rng

          def _get_features(lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__	):
               _UpperCAmelCase					:  Union[str, Any]         =    module.text_model(
                   input_ids=lowerCAmelCase__						,	attention_mask=lowerCAmelCase__						,	position_ids=lowerCAmelCase__						,	token_type_ids=lowerCAmelCase__						,	deterministic=lowerCAmelCase__						,	)
               _UpperCAmelCase					:  Optional[int]         =    text_outputs[1]
               _UpperCAmelCase					:  List[Any]         =    module.text_projection(lowerCAmelCase__	)
               return text_features

          return self.module.apply(
              {"""params""": params or self.params}						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	jnp.array(lowerCAmelCase__						,	dtype="""i4"""	)						,	not train						,	method=_get_features						,	rngs=lowerCAmelCase__						,	)



     def     snake_case_ (self						,	lowerCAmelCase__						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	lowerCAmelCase__=False	):
          _UpperCAmelCase					:  Any         =    {}
          if dropout_rng is not None:
               _UpperCAmelCase					:  int         =    dropout_rng

          def _get_features(lowerCAmelCase__						,	lowerCAmelCase__						,	lowerCAmelCase__	):
               _UpperCAmelCase					:  Optional[Any]         =    module.vision_model(pixel_values=lowerCAmelCase__						,	deterministic=lowerCAmelCase__	)
               _UpperCAmelCase					:  int         =    vision_outputs[1]  # pooled_output
               _UpperCAmelCase					:  Optional[int]         =    module.visual_projection(lowerCAmelCase__	)
               return image_features

          return self.module.apply(
              {"""params""": params or self.params}						,	jnp.array(lowerCAmelCase__						,	dtype=jnp.floataa	)						,	not train						,	method=_get_features						,	rngs=lowerCAmelCase__						,	)



     @classmethod
     def     snake_case_ (cls						,	lowerCAmelCase__ = None						,	lowerCAmelCase__ = None						,	*lowerCAmelCase__						,	**lowerCAmelCase__						,	):
          _UpperCAmelCase					:  List[Any]         =    {
              argument[len("""text_"""	) :]: value for argument, value in kwargs.items() if argument.startswith("""text_"""	)
          }

          _UpperCAmelCase					:  Tuple         =    {
              argument[len("""vision_"""	) :]: value for argument, value in kwargs.items() if argument.startswith("""vision_"""	)
          }

          # remove text, vision kwargs from kwargs
          for key in kwargs_text.keys():
               del kwargs["text_" + key]
          for key in kwargs_vision.keys():
               del kwargs["vision_" + key]

          # Load and initialize the text and vision model
          _UpperCAmelCase					:  List[Any]         =    kwargs_text.pop("""model"""						,	lowerCAmelCase__	)
          if text_model is None:
               assert (
                   text_model_name_or_path is not None
               ), "If `model` is not defined as an argument, a `text_model_name_or_path` has to be defined"
               from transformers import FlaxAutoModel

               if "config" not in kwargs_text:
                    from transformers import AutoConfig

                    _UpperCAmelCase					:  Any         =    AutoConfig.from_pretrained(lowerCAmelCase__	)
                    _UpperCAmelCase					:  int         =    text_config

               _UpperCAmelCase					:  Any         =    FlaxAutoModel.from_pretrained(lowerCAmelCase__						,	*lowerCAmelCase__						,	**lowerCAmelCase__	)

          _UpperCAmelCase					:  List[Any]         =    kwargs_vision.pop("""model"""						,	lowerCAmelCase__	)
          if vision_model is None:
               assert (
                   vision_model_name_or_path is not None
               ), "If `model` is not defined as an argument, a `vision_model_name_or_path` has to be defined"
               from transformers import FlaxAutoModel

               if "config" not in kwargs_vision:
                    from transformers import AutoConfig

                    _UpperCAmelCase					:  Union[str, Any]         =    AutoConfig.from_pretrained(lowerCAmelCase__	)
                    _UpperCAmelCase					:  Any         =    vision_config

               _UpperCAmelCase					:  Tuple         =    FlaxAutoModel.from_pretrained(lowerCAmelCase__						,	*lowerCAmelCase__						,	**lowerCAmelCase__	)

          # instantiate config with corresponding kwargs
          _UpperCAmelCase					:  List[Any]         =    kwargs.pop("""dtype"""						,	jnp.floataa	)
          _UpperCAmelCase					:  List[Any]         =    HybridCLIPConfig.from_text_vision_configs(text_model.config						,	vision_model.config						,	**lowerCAmelCase__	)

          # init model
          _UpperCAmelCase					:  Optional[Any]         =    cls(lowerCAmelCase__						,	*lowerCAmelCase__						,	dtype=lowerCAmelCase__						,	**lowerCAmelCase__	)

          if vision_config.model_type == "clip":
               _UpperCAmelCase					:  str         =    vision_model.params["""vision_model"""]
               _UpperCAmelCase					:  int         =    vision_model.params["""visual_projection"""]["""kernel"""]
          else:
               _UpperCAmelCase					:  List[str]         =    vision_model.params

          _UpperCAmelCase					:  Any         =    text_model.params

          return model










"""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				:    Optional[Any]     =						datasets.utils.logging.get_logger(__name__)






@dataclass
class 						snake_case  (  datasets.BuilderConfig       ):
    __magic_name__         =     None
    __magic_name__         =     None
    __magic_name__         =     None
    __magic_name__         =     True
    __magic_name__         =     None
    __magic_name__         =     None
    __magic_name__         =     None
    __magic_name__         =     1_0000
    __magic_name__         =     None





    def 				lowerCamelCase__      (							self	:					Tuple			):


      '''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 				lowerCamelCase__      (							self	:					int				,		A	:					dict				,		A	:					Optional[datasets.Features] = None				,		):


      '''simple docstring'''




      a     :			str				=   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     :			List[str]				=   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			):
            a     :			Union[str, Any]				=   sqlalchemy.create_engine(config_kwargs['con'].split('://'			)[0] + '://'			)
            a     :			List[Any]				=   str(sql.compile(dialect=engine.dialect			)			)
            a     :			str				=   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}'''			)
      a     :			List[str]				=   config_kwargs['con']
      if not isinstance(A				,		A			):
        a     :			Optional[Any]				=   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 				lowerCamelCase__      (							self	:					Optional[int]			):


      '''simple docstring'''




      a     :			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 						snake_case  (  datasets.ArrowBasedBuilder       ):
    __magic_name__         =     SqlConfig





    def 				lowerCamelCase__      (							self	:					Any			):


      '''simple docstring'''




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





    def 				lowerCamelCase__      (							self	:					Dict				,		A	:					Optional[int]			):


      '''simple docstring'''




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





    def 				lowerCamelCase__      (							self	:					Union[str, Any]				,		A	:					pa.Table			):


      '''simple docstring'''




      if self.config.features is not None:
        a     :			List[Any]				=   self.config.features.arrow_schema
        if all(not require_storage_cast(A			) for feature in self.config.features.values()			):
          # cheaper cast
          a     :			Dict				=   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
          a     :			Union[str, Any]				=   table_cast(A				,		A			)
      return pa_table





    def 				lowerCamelCase__      (							self	:					Dict			):


      '''simple docstring'''




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




