



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


snake_case_								=			logging.get_logger(__name__)







class  A_     (	nn.Module							):



						"""simple docstring"""
						__UpperCamelCase													=				42
						__UpperCamelCase													=				jnp.floataa






						def       UpperCAmelCase__	(   self					:Optional[Any]   )							->     List[Any]:
												UpperCAmelCase			 =					self.config.text_config
												UpperCAmelCase			 =					self.config.vision_config

												UpperCAmelCase			 =					self.config.projection_dim
												UpperCAmelCase			 =					text_config.hidden_size
												UpperCAmelCase			 =					vision_config.hidden_size

												UpperCAmelCase			 =					FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
												UpperCAmelCase			 =					FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__  ,    lowercase_   ).module_class

												UpperCAmelCase			 =					text_module(lowercase_  ,    dtype=self.dtype   )
												UpperCAmelCase			 =					vision_module(lowercase_  ,    dtype=self.dtype   )

												UpperCAmelCase			 =					nn.Dense(
												    self.projection_dim  ,    dtype=self.dtype  ,    kernel_init=jax.nn.initializers.normal(0.02   )  ,    use_bias=lowercase_  ,    )
												UpperCAmelCase			 =					nn.Dense(
												    self.projection_dim  ,    dtype=self.dtype  ,    kernel_init=jax.nn.initializers.normal(0.02   )  ,    use_bias=lowercase_  ,    )
												UpperCAmelCase			 =					self.param('logit_scale'  ,    jax.nn.initializers.ones  ,    []   )






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

												UpperCAmelCase			 =					self.vision_model(
												    pixel_values=lowercase_  ,    deterministic=lowercase_  ,    output_attentions=lowercase_  ,    output_hidden_states=lowercase_  ,    return_dict=lowercase_  ,    )

												UpperCAmelCase			 =					self.text_model(
												    input_ids=lowercase_  ,    attention_mask=lowercase_  ,    token_type_ids=lowercase_  ,    position_ids=lowercase_  ,    deterministic=lowercase_  ,    output_attentions=lowercase_  ,    output_hidden_states=lowercase_  ,    return_dict=lowercase_  ,    )

												UpperCAmelCase			 =					vision_outputs[1]
												UpperCAmelCase			 =					self.visual_projection(lowercase_   )

												UpperCAmelCase			 =					text_outputs[1]
												UpperCAmelCase			 =					self.text_projection(lowercase_   )

												# normalized features
												UpperCAmelCase			 =					image_embeds / jnp.linalg.norm(lowercase_  ,    axis=-1  ,    keepdims=lowercase_   )
												UpperCAmelCase			 =					text_embeds / jnp.linalg.norm(lowercase_  ,    axis=-1  ,    keepdims=lowercase_   )

												# cosine similarity as logits
												UpperCAmelCase			 =					jnp.exp(self.logit_scale   )
												UpperCAmelCase			 =					jnp.matmul(lowercase_  ,    image_embeds.T   ) * logit_scale
												UpperCAmelCase			 =					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=lowercase_  ,    logits_per_text=lowercase_  ,    text_embeds=lowercase_  ,    image_embeds=lowercase_  ,    text_model_output=lowercase_  ,    vision_model_output=lowercase_  ,    )







class  A_     (	SCREAMING_SNAKE_CASE_							):



						"""simple docstring"""
						__UpperCamelCase													=				HybridCLIPConfig
						__UpperCamelCase													=				FlaxHybridCLIPModule






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

												UpperCAmelCase			 =					self.module_class(config=lowercase_  ,    dtype=lowercase_  ,    **lowercase_   )
												super().__init__(lowercase_  ,    lowercase_  ,    input_shape=lowercase_  ,    seed=lowercase_  ,    dtype=lowercase_   )






						def       UpperCAmelCase__	(   self					:Tuple  ,    lowercase_					:jax.random.PRNGKey  ,    lowercase_					:Tuple  ,    lowercase_					:FrozenDict = None   )							->     FrozenDict:
												# init input tensor
												UpperCAmelCase			 =					jnp.zeros(input_shape[0]  ,    dtype='i4'   )
												UpperCAmelCase			 =					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_   ).shape[-1]   )  ,    input_shape[0]   )
												UpperCAmelCase			 =					jnp.ones_like(lowercase_   )
												UpperCAmelCase			 =					jnp.ones_like(lowercase_   )

												UpperCAmelCase			 =					jax.random.normal(lowercase_  ,    input_shape[1]   )

												UpperCAmelCase ,       UpperCAmelCase			 =					jax.random.split(lowercase_   )
												UpperCAmelCase			 =					{'params': params_rng, 'dropout': dropout_rng}

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






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

												if position_ids is None:
																		UpperCAmelCase			 =					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_   ).shape[-1]   )  ,    input_ids.shape   )

												if token_type_ids is None:
																		UpperCAmelCase			 =					jnp.zeros_like(lowercase_   )

												if attention_mask is None:
																		UpperCAmelCase			 =					jnp.ones_like(lowercase_   )

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

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






						def       UpperCAmelCase__	(   self					:Tuple  ,    lowercase_					:Tuple  ,    lowercase_					:Optional[int]=None  ,    lowercase_					:Union[str, Any]=None  ,    lowercase_					:Tuple=None  ,    lowercase_					:dict = None  ,    lowercase_					:jax.random.PRNGKey = None  ,    lowercase_					:Optional[Any]=False  ,    )							->     str:
												if position_ids is None:
																		UpperCAmelCase			 =					jnp.broadcast_to(jnp.arange(jnp.atleast_ad(lowercase_   ).shape[-1]   )  ,    input_ids.shape   )

												if token_type_ids is None:
																		UpperCAmelCase			 =					jnp.zeros_like(lowercase_   )

												if attention_mask is None:
																		UpperCAmelCase			 =					jnp.ones_like(lowercase_   )

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

												def _get_features(lowercase_					:Dict  ,    lowercase_					:List[Any]  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Any  ,    lowercase_					:List[Any]   ):
																		UpperCAmelCase			 =					module.text_model(
																		    input_ids=lowercase_  ,    attention_mask=lowercase_  ,    position_ids=lowercase_  ,    token_type_ids=lowercase_  ,    deterministic=lowercase_  ,    )
																		UpperCAmelCase			 =					text_outputs[1]
																		UpperCAmelCase			 =					module.text_projection(lowercase_   )
																		return text_features

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






						def       UpperCAmelCase__	(   self					:Optional[Any]  ,    lowercase_					:Optional[int]  ,    lowercase_					:dict = None  ,    lowercase_					:jax.random.PRNGKey = None  ,    lowercase_					:int=False   )							->     List[Any]:
												UpperCAmelCase			 =					{}
												if dropout_rng is not None:
																		UpperCAmelCase			 =					dropout_rng

												def _get_features(lowercase_					:Any  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:int   ):
																		UpperCAmelCase			 =					module.vision_model(pixel_values=lowercase_  ,    deterministic=lowercase_   )
																		UpperCAmelCase			 =					vision_outputs[1]  # pooled_output
																		UpperCAmelCase			 =					module.visual_projection(lowercase_   )
																		return image_features

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






						@classmethod
						def       UpperCAmelCase__	(   cls					:str  ,    lowercase_					:str = None  ,    lowercase_					:str = None  ,    *lowercase_					:Tuple  ,    **lowercase_					:str  ,    )							->     FlaxPreTrainedModel:
												UpperCAmelCase			 =					{
												    argument[len('text_'   ) :]: value for argument, value in kwargs.items() if argument.startswith('text_'   )
												}

												UpperCAmelCase			 =					{
												    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			 =					kwargs_text.pop('model'  ,    lowercase_   )
												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			 =					AutoConfig.from_pretrained(lowercase_   )
																								UpperCAmelCase			 =					text_config

																		UpperCAmelCase			 =					FlaxAutoModel.from_pretrained(lowercase_  ,    *lowercase_  ,    **lowercase_   )

												UpperCAmelCase			 =					kwargs_vision.pop('model'  ,    lowercase_   )
												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			 =					AutoConfig.from_pretrained(lowercase_   )
																								UpperCAmelCase			 =					vision_config

																		UpperCAmelCase			 =					FlaxAutoModel.from_pretrained(lowercase_  ,    *lowercase_  ,    **lowercase_   )

												# instantiate config with corresponding kwargs
												UpperCAmelCase			 =					kwargs.pop('dtype'  ,    jnp.floataa   )
												UpperCAmelCase			 =					HybridCLIPConfig.from_text_vision_configs(text_model.config  ,    vision_model.config  ,    **lowercase_   )

												# init model
												UpperCAmelCase			 =					cls(lowercase_  ,    *lowercase_  ,    dtype=lowercase_  ,    **lowercase_   )

												if vision_config.model_type == "clip":
																		UpperCAmelCase			 =					vision_model.params['vision_model']
																		UpperCAmelCase			 =					vision_model.params['visual_projection']['kernel']
												else:
																		UpperCAmelCase			 =					vision_model.params

												UpperCAmelCase			 =					text_model.params

												return model









import datasets

from .evaluate import (
    apply_no_ans_threshold,
    find_all_best_thresh,
    get_raw_scores,
    make_eval_dict,
    make_qid_to_has_ans,
    merge_eval,
)


__lowerCAmelCase									=				'''\
@inproceedings{Rajpurkar2016SQuAD10,
  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},
  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},
  booktitle={EMNLP},
  year={2016}
}
'''

__lowerCAmelCase									=				'''
This metric wrap the official scoring script for version 2 of the Stanford Question
Answering Dataset (SQuAD).

Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by
crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,
from the corresponding reading passage, or the question might be unanswerable.

SQuAD2.0 combines the 100,000 questions in SQuAD1.1 with over 50,000 unanswerable questions
written adversarially by crowdworkers to look similar to answerable ones.
To do well on SQuAD2.0, systems must not only answer questions when possible, but also
determine when no answer is supported by the paragraph and abstain from answering.
'''

__lowerCAmelCase									=				'''
Computes SQuAD v2 scores (F1 and EM).
Args:
    predictions: List of triple for question-answers to score with the following elements:
        - the question-answer \'id\' field as given in the references (see below)
        - the text of the answer
        - the probability that the question has no answer
    references: List of question-answers dictionaries with the following key-values:
            - \'id\': id of the question-answer pair (see above),
            - \'answers\': a list of Dict {\'text\': text of the answer as a string}
    no_answer_threshold: float
        Probability threshold to decide that a question has no answer.
Returns:
    \'exact\': Exact match (the normalized answer exactly match the gold answer)
    \'f1\': The F-score of predicted tokens versus the gold answer
    \'total\': Number of score considered
    \'HasAns_exact\': Exact match (the normalized answer exactly match the gold answer)
    \'HasAns_f1\': The F-score of predicted tokens versus the gold answer
    \'HasAns_total\': Number of score considered
    \'NoAns_exact\': Exact match (the normalized answer exactly match the gold answer)
    \'NoAns_f1\': The F-score of predicted tokens versus the gold answer
    \'NoAns_total\': Number of score considered
    \'best_exact\': Best exact match (with varying threshold)
    \'best_exact_thresh\': No-answer probability threshold associated to the best exact match
    \'best_f1\': Best F1 (with varying threshold)
    \'best_f1_thresh\': No-answer probability threshold associated to the best F1
Examples:

    >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\', \'no_answer_probability\': 0.}]
    >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}]
    >>> squad_v2_metric = datasets.load_metric("squad_v2")
    >>> results = squad_v2_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'exact\': 100.0, \'f1\': 100.0, \'total\': 1, \'HasAns_exact\': 100.0, \'HasAns_f1\': 100.0, \'HasAns_total\': 1, \'best_exact\': 100.0, \'best_exact_thresh\': 0.0, \'best_f1\': 100.0, \'best_f1_thresh\': 0.0}
'''





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION			,			_KWARGS_DESCRIPTION )
class     __a     (       datasets.Metric ):
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Dict:




        '''simple docstring'''
        return datasets.MetricInfo(
            description=_DESCRIPTION							,					citation=_CITATION							,					inputs_description=_KWARGS_DESCRIPTION							,					features=datasets.Features(
                {
                    'predictions': {
                        'id': datasets.Value('string'					),
                        'prediction_text': datasets.Value('string'					),
                        'no_answer_probability': datasets.Value('float32'					),
                    },
                    'references': {
                        'id': datasets.Value('string'					),
                        'answers': datasets.features.Sequence(
                            {'text': datasets.Value('string'					), 'answer_start': datasets.Value('int32'					)}					),
                    },
                }					)							,					codebase_urls=['https://rajpurkar.github.io/SQuAD-explorer/']							,					reference_urls=['https://rajpurkar.github.io/SQuAD-explorer/']							,					)
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__=1.0					)   ->							str:




        '''simple docstring'''
        lowercase__:  int         =  {p['id']: p['no_answer_probability'] for p in predictions}
        lowercase__:  int         =  [{'paragraphs': [{'qas': references}]}]
        lowercase__:  Optional[int]         =  {p['id']: p['prediction_text'] for p in predictions}

        lowercase__:  Any         =  make_qid_to_has_ans(lowerCAmelCase__					)  # maps qid to True/False
        lowercase__:  Optional[Any]         =  [k for k, v in qid_to_has_ans.items() if v]
        lowercase__:  Dict         =  [k for k, v in qid_to_has_ans.items() if not v]

        lowercase__     ,		lowercase__:  Dict         =  get_raw_scores(lowerCAmelCase__							,					lowerCAmelCase__					)
        lowercase__:  Optional[int]         =  apply_no_ans_threshold(lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)
        lowercase__:  List[Any]         =  apply_no_ans_threshold(lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)
        lowercase__:  Dict         =  make_eval_dict(lowerCAmelCase__							,					lowerCAmelCase__					)

        if has_ans_qids:
               lowercase__:  List[Any]         =  make_eval_dict(lowerCAmelCase__							,					lowerCAmelCase__							,					qid_list=lowerCAmelCase__					)
               merge_eval(lowerCAmelCase__							,					lowerCAmelCase__							,					'HasAns'					)
        if no_ans_qids:
               lowercase__:  int         =  make_eval_dict(lowerCAmelCase__							,					lowerCAmelCase__							,					qid_list=lowerCAmelCase__					)
               merge_eval(lowerCAmelCase__							,					lowerCAmelCase__							,					'NoAns'					)
        find_all_best_thresh(lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)
        return dict(lowerCAmelCase__					)


