
"""simple docstring"""

from sklearn.metrics import mean_absolute_error

import datasets


_A            = '\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n'

_A            = '\\nMean Absolute Error (MAE) is the mean of the magnitude of difference between the predicted and actual\nvalues.\n'


_A            = '\nArgs:\n    predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)\n        Estimated target values.\n    references: array-like of shape (n_samples,) or (n_samples, n_outputs)\n        Ground truth (correct) target values.\n    sample_weight: array-like of shape (n_samples,), default=None\n        Sample weights.\n    multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average"\n        Defines aggregating of multiple output values. Array-like value defines weights used to average errors.\n\n                 "raw_values" : Returns a full set of errors in case of multioutput input.\n\n                 "uniform_average" : Errors of all outputs are averaged with uniform weight.\n\nReturns:\n    mae : mean absolute error.\n        If multioutput is "raw_values", then mean absolute error is returned for each output separately. If multioutput is "uniform_average" or an ndarray of weights, then the weighted average of all output errors is returned.\n        MAE output is non-negative floating point. The best value is 0.0.\nExamples:\n\n    >>> mae_metric = datasets.load_metric("mae")\n    >>> predictions = [2.5, 0.0, 2, 8]\n    >>> references = [3, -0.5, 2, 7]\n    >>> results = mae_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'mae\': 0.5}\n\n    If you\'re using multi-dimensional lists, then set the config as follows :\n\n    >>> mae_metric = datasets.load_metric("mae", "multilist")\n    >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]\n    >>> references = [[0, 2], [-1, 2], [8, -5]]\n    >>> results = mae_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'mae\': 0.75}\n    >>> results = mae_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\')\n    >>> print(results)\n    {\'mae\': array([0.5, 1. ])}\n'






@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION				,       _KWARGS_DESCRIPTION		)
class        _lowercase   (				datasets.Metric		):




	def 			_UpperCamelCase    ( self					)		->							Optional[Any]:
			return datasets.MetricInfo(
			    description=_DESCRIPTION		, citation=_CITATION		, inputs_description=_KWARGS_DESCRIPTION		, features=datasets.Features(self._get_feature_types()					)		, reference_urls=[
			        'https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_absolute_error.html'
			    ]		, )




	def 			_UpperCamelCase    ( self					)		->							Tuple:
			if self.config_name == "multilist":
					return {
					    "predictions": datasets.Sequence(datasets.Value('float'					)					),
					    "references": datasets.Sequence(datasets.Value('float'					)					),
					}
			else:
					return {
					    "predictions": datasets.Value('float'					),
					    "references": datasets.Value('float'					),
					}




	def 			_UpperCamelCase    ( self		, UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_=None		, UpperCAmelCase_="uniform_average"					)		->							Union[str, Any]:
			lowerCamelCase     :			Any		=    mean_absolute_error(UpperCAmelCase_		, UpperCAmelCase_		, sample_weight=UpperCAmelCase_		, multioutput=UpperCAmelCase_					)

			return {"mae": mae_score}










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


lowercase_           =	logging.get_logger(__name__)


class        A_					(       nn.Module    ):




      '''simple docstring'''


      __snake_case					       =   42
      __snake_case					       =   jnp.floataa




      def      _snake_case	(				self:       List[Any]				):
       __lowerCamelCase		:							Union[str, Any]							=						self.config.text_config
       __lowerCamelCase		:							List[Any]							=						self.config.vision_config

       __lowerCamelCase		:							Any							=						self.config.projection_dim
       __lowerCamelCase		:							Dict							=						text_config.hidden_size
       __lowerCamelCase		:							str							=						vision_config.hidden_size

       __lowerCamelCase		:							Optional[Any]							=						FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
       __lowerCamelCase		:							int							=						FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__						,      a				).module_class

       __lowerCamelCase		:							Union[str, Any]							=						text_module(a						,      dtype=self.dtype				)
       __lowerCamelCase		:							int							=						vision_module(a						,      dtype=self.dtype				)

       __lowerCamelCase		:							Union[str, Any]							=						nn.Dense(
           self.projection_dim						,      dtype=self.dtype						,      kernel_init=jax.nn.initializers.normal(0.0_2				)						,      use_bias=a						,      )
       __lowerCamelCase		:							int							=						nn.Dense(
           self.projection_dim						,      dtype=self.dtype						,      kernel_init=jax.nn.initializers.normal(0.0_2				)						,      use_bias=a						,      )
       __lowerCamelCase		:							Dict							=						self.param('logit_scale'						,      jax.nn.initializers.ones						,      []				)




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

       __lowerCamelCase		:							Any							=						self.vision_model(
           pixel_values=a						,      deterministic=a						,      output_attentions=a						,      output_hidden_states=a						,      return_dict=a						,      )

       __lowerCamelCase		:							Union[str, Any]							=						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		:							Tuple							=						vision_outputs[1]
       __lowerCamelCase		:							List[Any]							=						self.visual_projection(a				)

       __lowerCamelCase		:							Any							=						text_outputs[1]
       __lowerCamelCase		:							Optional[Any]							=						self.text_projection(a				)

       # normalized features
       __lowerCamelCase		:							str							=						image_embeds / jnp.linalg.norm(a						,      axis=-1						,      keepdims=a				)
       __lowerCamelCase		:							Optional[int]							=						text_embeds / jnp.linalg.norm(a						,      axis=-1						,      keepdims=a				)

       # cosine similarity as logits
       __lowerCamelCase		:							Union[str, Any]							=						jnp.exp(self.logit_scale				)
       __lowerCamelCase		:							Union[str, Any]							=						jnp.matmul(a						,      image_embeds.T				) * logit_scale
       __lowerCamelCase		:							List[str]							=						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        A_					(       __UpperCamelCase    ):




      '''simple docstring'''


      __snake_case					       =   HybridCLIPConfig
      __snake_case					       =   FlaxHybridCLIPModule




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

       __lowerCamelCase		:							int							=						self.module_class(config=a						,      dtype=a						,      **a				)
       super().__init__(a						,      a						,      input_shape=a						,      seed=a						,      dtype=a				)




      def      _snake_case	(				self:       Dict						,      a:       jax.random.PRNGKey						,      a:       Tuple						,      a:       FrozenDict = None				):
       # init input tensor
       __lowerCamelCase		:							int							=						jnp.zeros(input_shape[0]						,      dtype='i4'				)
       __lowerCamelCase		:							List[Any]							=						jnp.broadcast_to(jnp.arange(jnp.atleast_ad(a				).shape[-1]				)						,      input_shape[0]				)
       __lowerCamelCase		:							Any							=						jnp.ones_like(a				)
       __lowerCamelCase		:							int							=						jnp.ones_like(a				)

       __lowerCamelCase		:							Optional[Any]							=						jax.random.normal(a						,      input_shape[1]				)

       __lowerCamelCase					,     __lowerCamelCase		:							List[Any]							=						jax.random.split(a				)
       __lowerCamelCase		:							Tuple							=						{'params': params_rng, 'dropout': dropout_rng}

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




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

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

       if token_type_ids is None:
        __lowerCamelCase		:							str							=						jnp.zeros_like(a				)

       if attention_mask is None:
        __lowerCamelCase		:							str							=						jnp.ones_like(a				)

       # Handle any PRNG if needed
       __lowerCamelCase		:							Union[str, 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      _snake_case	(				self:       Tuple						,      a:       Tuple						,      a:       List[Any]=None						,      a:       Dict=None						,      a:       int=None						,      a:       dict = None						,      a:       jax.random.PRNGKey = None						,      a:       Tuple=False						,      ):
       if position_ids is None:
        __lowerCamelCase		:							str							=						jnp.broadcast_to(jnp.arange(jnp.atleast_ad(a				).shape[-1]				)						,      input_ids.shape				)

       if token_type_ids is None:
        __lowerCamelCase		:							Union[str, Any]							=						jnp.zeros_like(a				)

       if attention_mask is None:
        __lowerCamelCase		:							int							=						jnp.ones_like(a				)

       # Handle any PRNG if needed
       __lowerCamelCase		:							Optional[int]							=						{}
       if dropout_rng is not None:
        __lowerCamelCase		:							List[str]							=						dropout_rng

       def _get_features(a:       str						,      a:       Any						,      a:       Optional[int]						,      a:       Optional[int]						,      a:       Union[str, Any]						,      a:       Optional[int]				):
        __lowerCamelCase		:							Tuple							=						module.text_model(
            input_ids=a						,      attention_mask=a						,      position_ids=a						,      token_type_ids=a						,      deterministic=a						,      )
        __lowerCamelCase		:							int							=						text_outputs[1]
        __lowerCamelCase		:							Optional[Any]							=						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      _snake_case	(				self:       int						,      a:       Optional[int]						,      a:       dict = None						,      a:       jax.random.PRNGKey = None						,      a:       str=False				):
       __lowerCamelCase		:							Dict							=						{}
       if dropout_rng is not None:
        __lowerCamelCase		:							List[str]							=						dropout_rng

       def _get_features(a:       str						,      a:       Tuple						,      a:       Any				):
        __lowerCamelCase		:							Dict							=						module.vision_model(pixel_values=a						,      deterministic=a				)
        __lowerCamelCase		:							int							=						vision_outputs[1]  # pooled_output
        __lowerCamelCase		:							int							=						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      _snake_case	(				cls:       str						,      a:       str = None						,      a:       str = None						,      *a:       Optional[Any]						,      **a:       List[str]						,      ):
       __lowerCamelCase		:							List[Any]							=						{
           argument[len('text_'				) :]: value for argument, value in kwargs.items() if argument.startswith('text_'				)
       }

       __lowerCamelCase		:							Optional[Any]							=						{
           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		:							Any							=						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		:							Optional[int]							=						AutoConfig.from_pretrained(a				)
         __lowerCamelCase		:							Any							=						text_config

        __lowerCamelCase		:							Dict							=						FlaxAutoModel.from_pretrained(a						,      *a						,      **a				)

       __lowerCamelCase		:							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		:							Any							=						AutoConfig.from_pretrained(a				)
         __lowerCamelCase		:							int							=						vision_config

        __lowerCamelCase		:							str							=						FlaxAutoModel.from_pretrained(a						,      *a						,      **a				)

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

       # init model
       __lowerCamelCase		:							List[Any]							=						cls(a						,      *a						,      dtype=a						,      **a				)

       if vision_config.model_type == "clip":
        __lowerCamelCase		:							Any							=						vision_model.params['vision_model']
        __lowerCamelCase		:							Optional[Any]							=						vision_model.params['visual_projection']['kernel']
       else:
        __lowerCamelCase		:							Optional[int]							=						vision_model.params

       __lowerCamelCase		:							List[str]							=						text_model.params

       return model

