



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


_UpperCamelCase							    =						logging.get_logger(__name__)






class  lowerCamelCase_   (    nn.Module					):
  """simple docstring"""




  a_							 =42
  a_							 =jnp.floataa




  def 						_lowercase					(			self				:     Dict       ) -> List[str]:
    __lowerCamelCase	:	Any   	=			self.config.text_config
    __lowerCamelCase	:	Union[str, Any]   	=			self.config.vision_config

    __lowerCamelCase	:	Dict   	=			self.config.projection_dim
    __lowerCamelCase	:	Any   	=			text_config.hidden_size
    __lowerCamelCase	:	Union[str, Any]   	=			vision_config.hidden_size

    __lowerCamelCase	:	Any   	=			FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
    __lowerCamelCase	:	List[Any]   	=			FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__      ,     _a       ).module_class

    __lowerCamelCase	:	Dict   	=			text_module(_a      ,     dtype=self.dtype       )
    __lowerCamelCase	:	str   	=			vision_module(_a      ,     dtype=self.dtype       )

    __lowerCamelCase	:	List[Any]   	=			nn.Dense(
        self.projection_dim      ,     dtype=self.dtype      ,     kernel_init=jax.nn.initializers.normal(0.02       )      ,     use_bias=_a      ,     )
    __lowerCamelCase	:	str   	=			nn.Dense(
        self.projection_dim      ,     dtype=self.dtype      ,     kernel_init=jax.nn.initializers.normal(0.02       )      ,     use_bias=_a      ,     )
    __lowerCamelCase	:	Tuple   	=			self.param('logit_scale'      ,     jax.nn.initializers.ones      ,     []       )




  def __call__(			self				:     Any      ,     _a				:     List[str]=None      ,     _a				:     List[Any]=None      ,     _a				:     Any=None      ,     _a				:     Optional[Any]=None      ,     _a				:     List[Any]=None      ,     _a				:     bool = True      ,     _a				:     Union[str, Any]=None      ,     _a				:     Tuple=None      ,     _a				:     List[str]=None      ,     ) -> Any:
    __lowerCamelCase	:	Union[str, Any]   	=			return_dict if return_dict is not None else self.config.return_dict

    __lowerCamelCase	:	List[str]   	=			self.vision_model(
        pixel_values=_a      ,     deterministic=_a      ,     output_attentions=_a      ,     output_hidden_states=_a      ,     return_dict=_a      ,     )

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

    __lowerCamelCase	:	Dict   	=			vision_outputs[1]
    __lowerCamelCase	:	Any   	=			self.visual_projection(_a       )

    __lowerCamelCase	:	int   	=			text_outputs[1]
    __lowerCamelCase	:	Any   	=			self.text_projection(_a       )

    # normalized features
    __lowerCamelCase	:	List[str]   	=			image_embeds / jnp.linalg.norm(_a      ,     axis=-1      ,     keepdims=_a       )
    __lowerCamelCase	:	Union[str, Any]   	=			text_embeds / jnp.linalg.norm(_a      ,     axis=-1      ,     keepdims=_a       )

    # cosine similarity as logits
    __lowerCamelCase	:	Optional[int]   	=			jnp.exp(self.logit_scale       )
    __lowerCamelCase	:	Union[str, Any]   	=			jnp.matmul(_a      ,     image_embeds.T       ) * logit_scale
    __lowerCamelCase	:	Optional[int]   	=			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=_a      ,     logits_per_text=_a      ,     text_embeds=_a      ,     image_embeds=_a      ,     text_model_output=_a      ,     vision_model_output=_a      ,     )






class  lowerCamelCase_   (    SCREAMING_SNAKE_CASE_					):
  """simple docstring"""




  a_							 =HybridCLIPConfig
  a_							 =FlaxHybridCLIPModule




  def __init__(			self				:     Tuple      ,     _a				:     HybridCLIPConfig      ,     _a				:     Optional[Tuple] = None      ,     _a				:     int = 0      ,     _a				:     jnp.dtype = jnp.floataa      ,     **_a				:     str      ,     ) -> Optional[int]:
    if input_shape is None:
      __lowerCamelCase	:	List[str]   	=			((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

    __lowerCamelCase	:	Optional[Any]   	=			self.module_class(config=_a      ,     dtype=_a      ,     **_a       )
    super().__init__(_a      ,     _a      ,     input_shape=_a      ,     seed=_a      ,     dtype=_a       )




  def 						_lowercase					(			self				:     Optional[Any]      ,     _a				:     jax.random.PRNGKey      ,     _a				:     Tuple      ,     _a				:     FrozenDict = None       ) -> FrozenDict:
    # init input tensor
    __lowerCamelCase	:	str   	=			jnp.zeros(input_shape[0]      ,     dtype='i4'       )
    __lowerCamelCase	:	Tuple   	=			jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_a       ).shape[-1]       )      ,     input_shape[0]       )
    __lowerCamelCase	:	str   	=			jnp.ones_like(_a       )
    __lowerCamelCase	:	str   	=			jnp.ones_like(_a       )

    __lowerCamelCase	:	List[str]   	=			jax.random.normal(_a      ,     input_shape[1]       )

    __lowerCamelCase	,__lowerCamelCase	:	int   	=			jax.random.split(_a       )
    __lowerCamelCase	:	Any   	=			{'params': params_rng, 'dropout': dropout_rng}

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




  def __call__(			self				:     Any      ,     _a				:     str      ,     _a				:     Union[str, Any]      ,     _a				:     List[str]=None      ,     _a				:     Optional[Any]=None      ,     _a				:     Union[str, Any]=None      ,     _a				:     dict = None      ,     _a				:     jax.random.PRNGKey = None      ,     _a				:     bool = False      ,     _a				:     Optional[bool] = None      ,     _a				:     Optional[bool] = None      ,     _a				:     Optional[bool] = None      ,     ) -> Any:
    __lowerCamelCase	:	str   	=			output_attentions if output_attentions is not None else self.config.output_attentions
    __lowerCamelCase	:	Tuple   	=			(
        output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    )
    __lowerCamelCase	:	Optional[int]   	=			return_dict if return_dict is not None else self.config.return_dict

    if position_ids is None:
      __lowerCamelCase	:	Optional[Any]   	=			jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_a       ).shape[-1]       )      ,     input_ids.shape       )

    if token_type_ids is None:
      __lowerCamelCase	:	Optional[int]   	=			jnp.zeros_like(_a       )

    if attention_mask is None:
      __lowerCamelCase	:	Union[str, Any]   	=			jnp.ones_like(_a       )

    # Handle any PRNG if needed
    __lowerCamelCase	:	Any   	=			{}
    if dropout_rng is not None:
      __lowerCamelCase	:	Any   	=			dropout_rng

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




  def 						_lowercase					(			self				:     int      ,     _a				:     Optional[Any]      ,     _a				:     Tuple=None      ,     _a				:     Optional[Any]=None      ,     _a				:     List[Any]=None      ,     _a				:     dict = None      ,     _a				:     jax.random.PRNGKey = None      ,     _a				:     Optional[int]=False      ,     ) -> List[Any]:
    if position_ids is None:
      __lowerCamelCase	:	List[Any]   	=			jnp.broadcast_to(jnp.arange(jnp.atleast_ad(_a       ).shape[-1]       )      ,     input_ids.shape       )

    if token_type_ids is None:
      __lowerCamelCase	:	List[Any]   	=			jnp.zeros_like(_a       )

    if attention_mask is None:
      __lowerCamelCase	:	Optional[Any]   	=			jnp.ones_like(_a       )

    # Handle any PRNG if needed
    __lowerCamelCase	:	Tuple   	=			{}
    if dropout_rng is not None:
      __lowerCamelCase	:	Any   	=			dropout_rng

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

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




  def 						_lowercase					(			self				:     Any      ,     _a				:     Tuple      ,     _a				:     dict = None      ,     _a				:     jax.random.PRNGKey = None      ,     _a				:     Tuple=False       ) -> List[Any]:
    __lowerCamelCase	:	Any   	=			{}
    if dropout_rng is not None:
      __lowerCamelCase	:	List[str]   	=			dropout_rng

    def _get_features(_a				:     Optional[Any]      ,     _a				:     Tuple      ,     _a				:     Dict       ):
      __lowerCamelCase	:	Optional[Any]   	=			module.vision_model(pixel_values=_a      ,     deterministic=_a       )
      __lowerCamelCase	:	List[Any]   	=			vision_outputs[1]  # pooled_output
      __lowerCamelCase	:	Dict   	=			module.visual_projection(_a       )
      return image_features

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




  @classmethod
  def 						_lowercase					(			cls				:     List[str]      ,     _a				:     str = None      ,     _a				:     str = None      ,     *_a				:     Dict      ,     **_a				:     Optional[Any]      ,     ) -> FlaxPreTrainedModel:
    __lowerCamelCase	:	List[str]   	=			{
        argument[len('text_'       ) :]: value for argument, value in kwargs.items() if argument.startswith('text_'       )
    }

    __lowerCamelCase	:	Dict   	=			{
        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
    __lowerCamelCase	:	Dict   	=			kwargs_text.pop('model'      ,     _a       )
    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

        __lowerCamelCase	:	List[Any]   	=			AutoConfig.from_pretrained(_a       )
        __lowerCamelCase	:	int   	=			text_config

      __lowerCamelCase	:	Optional[int]   	=			FlaxAutoModel.from_pretrained(_a      ,     *_a      ,     **_a       )

    __lowerCamelCase	:	List[Any]   	=			kwargs_vision.pop('model'      ,     _a       )
    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

        __lowerCamelCase	:	Union[str, Any]   	=			AutoConfig.from_pretrained(_a       )
        __lowerCamelCase	:	Tuple   	=			vision_config

      __lowerCamelCase	:	List[Any]   	=			FlaxAutoModel.from_pretrained(_a      ,     *_a      ,     **_a       )

    # instantiate config with corresponding kwargs
    __lowerCamelCase	:	str   	=			kwargs.pop('dtype'      ,     jnp.floataa       )
    __lowerCamelCase	:	List[Any]   	=			HybridCLIPConfig.from_text_vision_configs(text_model.config      ,     vision_model.config      ,     **_a       )

    # init model
    __lowerCamelCase	:	Optional[int]   	=			cls(_a      ,     *_a      ,     dtype=_a      ,     **_a       )

    if vision_config.model_type == "clip":
      __lowerCamelCase	:	Union[str, Any]   	=			vision_model.params['vision_model']
      __lowerCamelCase	:	str   	=			vision_model.params['visual_projection']['kernel']
    else:
      __lowerCamelCase	:	Tuple   	=			vision_model.params

    __lowerCamelCase	:	List[Any]   	=			text_model.params

    return model











'''simple docstring'''





from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

# Load iris file
_UpperCamelCase							    =						load_iris()
iris.keys()


print(f'''Target names: \n {iris.target_names} ''')
print(f'''\n Features: \n {iris.feature_names}''')

# Train set e Test set
_UpperCamelCase    ,    _UpperCamelCase    ,    _UpperCamelCase    ,    _UpperCamelCase							    =						train_test_split(
    iris['data'], iris['target'], random_state=4
)

# KNN

_UpperCamelCase							    =						KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)

# new array to test
_UpperCamelCase							    =						[[1, 2, 1, 4], [2, 3, 4, 5]]

_UpperCamelCase							    =						knn.predict(X_new)

print(
    f'''\nNew array: \n {X_new}\n\nTarget Names Prediction: \n'''
    f''' {iris["target_names"][prediction]}'''
)






