



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


_lowercase       :						Union[str, Any]						=logging.get_logger(__name__)






class 			_SCREAMING_SNAKE_CASE				(nn.Module      ):
			A__								=  42
			A__								=  jnp.floataa



			def 				lowerCAmelCase (     self		:						Any			)      ->      Tuple:
							"""simple docstring"""





							snake_case__      :   List[str]      =    self.config.text_config
							snake_case__      :   List[str]      =    self.config.vision_config

							snake_case__      :   Tuple      =    self.config.projection_dim
							snake_case__      :   Optional[int]      =    text_config.hidden_size
							snake_case__      :   Tuple      =    vision_config.hidden_size

							snake_case__      :   Tuple      =    FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
							snake_case__      :   Optional[int]      =    FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__    ,			__UpperCamelCase			).module_class

							snake_case__      :   List[str]      =    text_module(__UpperCamelCase    ,			dtype=self.dtype			)
							snake_case__      :   Optional[Any]      =    vision_module(__UpperCamelCase    ,			dtype=self.dtype			)

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







			def __call__(     self		:						List[str]    ,			__UpperCamelCase		:						Union[str, Any]=None    ,			__UpperCamelCase		:						int=None    ,			__UpperCamelCase		:						Union[str, Any]=None    ,			__UpperCamelCase		:						Union[str, Any]=None    ,			__UpperCamelCase		:						List[Any]=None    ,			__UpperCamelCase		:						bool = True    ,			__UpperCamelCase		:						Any=None    ,			__UpperCamelCase		:						int=None    ,			__UpperCamelCase		:						Dict=None    ,			)      ->      int:
							"""simple docstring"""





							snake_case__      :   Optional[int]      =    return_dict if return_dict is not None else self.config.return_dict

							snake_case__      :   List[Any]      =    self.vision_model(
							    pixel_values=__UpperCamelCase    ,			deterministic=__UpperCamelCase    ,			output_attentions=__UpperCamelCase    ,			output_hidden_states=__UpperCamelCase    ,			return_dict=__UpperCamelCase    ,			)

							snake_case__      :   Dict      =    self.text_model(
							    input_ids=__UpperCamelCase    ,			attention_mask=__UpperCamelCase    ,			token_type_ids=__UpperCamelCase    ,			position_ids=__UpperCamelCase    ,			deterministic=__UpperCamelCase    ,			output_attentions=__UpperCamelCase    ,			output_hidden_states=__UpperCamelCase    ,			return_dict=__UpperCamelCase    ,			)

							snake_case__      :   List[Any]      =    vision_outputs[1]
							snake_case__      :   List[Any]      =    self.visual_projection(__UpperCamelCase			)

							snake_case__      :   str      =    text_outputs[1]
							snake_case__      :   Union[str, Any]      =    self.text_projection(__UpperCamelCase			)

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

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






class 			_SCREAMING_SNAKE_CASE				(lowercase__      ):
			A__								=  HybridCLIPConfig
			A__								=  FlaxHybridCLIPModule



			def __init__(     self		:						List[str]    ,			__UpperCamelCase		:						HybridCLIPConfig    ,			__UpperCamelCase		:						Optional[Tuple] = None    ,			__UpperCamelCase		:						int = 0    ,			__UpperCamelCase		:						jnp.dtype = jnp.floataa    ,			**__UpperCamelCase		:						Optional[Any]    ,			)      ->      Optional[Any]:
							"""simple docstring"""





							if input_shape is None:
											snake_case__      :   Any      =    ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

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



			def 				lowerCAmelCase (     self		:						int    ,			__UpperCamelCase		:						jax.random.PRNGKey    ,			__UpperCamelCase		:						Tuple    ,			__UpperCamelCase		:						FrozenDict = None			)      ->      FrozenDict:
							"""simple docstring"""





							snake_case__      :   Dict      =    jnp.zeros(input_shape[0]    ,			dtype='''i4'''			)
							snake_case__      :   Any      =    jnp.broadcast_to(jnp.arange(jnp.atleast_ad(__UpperCamelCase			).shape[-1]			)    ,			input_shape[0]			)
							snake_case__      :   str      =    jnp.ones_like(__UpperCamelCase			)
							snake_case__      :   List[str]      =    jnp.ones_like(__UpperCamelCase			)

							snake_case__      :   List[Any]      =    jax.random.normal(__UpperCamelCase    ,			input_shape[1]			)

							snake_case__  ,      snake_case__      :   Dict      =    jax.random.split(__UpperCamelCase			)
							snake_case__      :   Optional[Any]      =    {'''params''': params_rng, '''dropout''': dropout_rng}

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



			def __call__(     self		:						Optional[Any]    ,			__UpperCamelCase		:						Union[str, Any]    ,			__UpperCamelCase		:						Optional[int]    ,			__UpperCamelCase		:						List[str]=None    ,			__UpperCamelCase		:						Optional[Any]=None    ,			__UpperCamelCase		:						Union[str, Any]=None    ,			__UpperCamelCase		:						dict = None    ,			__UpperCamelCase		:						jax.random.PRNGKey = None    ,			__UpperCamelCase		:						bool = False    ,			__UpperCamelCase		:						Optional[bool] = None    ,			__UpperCamelCase		:						Optional[bool] = None    ,			__UpperCamelCase		:						Optional[bool] = None    ,			)      ->      List[str]:
							"""simple docstring"""





							snake_case__      :   str      =    output_attentions if output_attentions is not None else self.config.output_attentions
							snake_case__      :   List[str]      =    (
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							snake_case__      :   Dict      =    return_dict if return_dict is not None else self.config.return_dict

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

							if token_type_ids is None:
											snake_case__      :   List[Any]      =    jnp.zeros_like(__UpperCamelCase			)

							if attention_mask is None:
											snake_case__      :   Tuple      =    jnp.ones_like(__UpperCamelCase			)

							# Handle any PRNG if needed
							snake_case__      :   Tuple      =    {}
							if dropout_rng is not None:
											snake_case__      :   Optional[int]      =    dropout_rng

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



			def 				lowerCAmelCase (     self		:						List[Any]    ,			__UpperCamelCase		:						Optional[int]    ,			__UpperCamelCase		:						List[Any]=None    ,			__UpperCamelCase		:						int=None    ,			__UpperCamelCase		:						List[str]=None    ,			__UpperCamelCase		:						dict = None    ,			__UpperCamelCase		:						jax.random.PRNGKey = None    ,			__UpperCamelCase		:						str=False    ,			)      ->      Union[str, Any]:
							"""simple docstring"""





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

							if token_type_ids is None:
											snake_case__      :   Optional[Any]      =    jnp.zeros_like(__UpperCamelCase			)

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

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

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

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



			def 				lowerCAmelCase (     self		:						List[str]    ,			__UpperCamelCase		:						Any    ,			__UpperCamelCase		:						dict = None    ,			__UpperCamelCase		:						jax.random.PRNGKey = None    ,			__UpperCamelCase		:						Optional[int]=False			)      ->      int:
							"""simple docstring"""





							snake_case__      :   List[str]      =    {}
							if dropout_rng is not None:
											snake_case__      :   Optional[Any]      =    dropout_rng

							def _get_features(__UpperCamelCase		:						Any    ,			__UpperCamelCase		:						int    ,			__UpperCamelCase		:						Any			):
											snake_case__      :   List[Any]      =    module.vision_model(pixel_values=__UpperCamelCase    ,			deterministic=__UpperCamelCase			)
											snake_case__      :   List[Any]      =    vision_outputs[1]  # pooled_output
											snake_case__      :   int      =    module.visual_projection(__UpperCamelCase			)
											return image_features

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







			@classmethod
			def 				lowerCAmelCase (     cls		:						str    ,			__UpperCamelCase		:						str = None    ,			__UpperCamelCase		:						str = None    ,			*__UpperCamelCase		:						Tuple    ,			**__UpperCamelCase		:						List[Any]    ,			)      ->      FlaxPreTrainedModel:
							"""simple docstring"""





							snake_case__      :   Optional[Any]      =    {
							    argument[len('''text_'''			) :]: value for argument, value in kwargs.items() if argument.startswith('''text_'''			)
							}

							snake_case__      :   str      =    {
							    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
							snake_case__      :   Tuple      =    kwargs_text.pop('''model'''    ,			__UpperCamelCase			)
							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

															snake_case__      :   str      =    AutoConfig.from_pretrained(__UpperCamelCase			)
															snake_case__      :   Optional[int]      =    text_config

											snake_case__      :   Any      =    FlaxAutoModel.from_pretrained(__UpperCamelCase    ,			*__UpperCamelCase    ,			**__UpperCamelCase			)

							snake_case__      :   Optional[int]      =    kwargs_vision.pop('''model'''    ,			__UpperCamelCase			)
							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

															snake_case__      :   Optional[int]      =    AutoConfig.from_pretrained(__UpperCamelCase			)
															snake_case__      :   Any      =    vision_config

											snake_case__      :   List[str]      =    FlaxAutoModel.from_pretrained(__UpperCamelCase    ,			*__UpperCamelCase    ,			**__UpperCamelCase			)

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

							# init model
							snake_case__      :   Optional[int]      =    cls(__UpperCamelCase    ,			*__UpperCamelCase    ,			dtype=__UpperCamelCase    ,			**__UpperCamelCase			)

							if vision_config.model_type == "clip":
											snake_case__      :   Tuple      =    vision_model.params['''vision_model''']
											snake_case__      :   int      =    vision_model.params['''visual_projection''']['''kernel''']
							else:
											snake_case__      :   str      =    vision_model.params

							snake_case__      :   Any      =    text_model.params

							return model








"""simple docstring"""
import argparse
import json
import re
import string
import sys
from collections import Counter







def     _a					(   _SCREAMING_SNAKE_CASE					)     ->					Any:

							def remove_articles(_SCREAMING_SNAKE_CASE					):
														return re.sub(r"""\b(a|an|the)\b"""					,			""" """					,			_SCREAMING_SNAKE_CASE					)

							def white_space_fix(_SCREAMING_SNAKE_CASE					):
														return " ".join(text.split()					)

							def remove_punc(_SCREAMING_SNAKE_CASE					):
														snake_case_     							=		set(string.punctuation					)
														return "".join(ch for ch in text if ch not in exclude					)

							def lower(_SCREAMING_SNAKE_CASE					):
														return text.lower()

							return white_space_fix(remove_articles(remove_punc(lower(_SCREAMING_SNAKE_CASE					)					)					)					)







def     _a					(   _SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					)     ->					Optional[int]:
							snake_case_     							=		normalize_answer(_SCREAMING_SNAKE_CASE					).split()
							snake_case_     							=		normalize_answer(_SCREAMING_SNAKE_CASE					).split()
							snake_case_     							=		Counter(_SCREAMING_SNAKE_CASE					) & Counter(_SCREAMING_SNAKE_CASE					)
							snake_case_     							=		sum(common.values()					)
							if num_same == 0:
														return 0
							snake_case_     							=		1.0 * num_same / len(_SCREAMING_SNAKE_CASE					)
							snake_case_     							=		1.0 * num_same / len(_SCREAMING_SNAKE_CASE					)
							snake_case_     							=		(2 * precision * recall) / (precision + recall)
							return fa







def     _a					(   _SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					)     ->					Union[str, Any]:
							return normalize_answer(_SCREAMING_SNAKE_CASE					) == normalize_answer(_SCREAMING_SNAKE_CASE					)







def     _a					(   _SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					)     ->					Optional[int]:
							snake_case_     							=		[]
							for ground_truth in ground_truths:
														snake_case_     							=		metric_fn(_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					)
														scores_for_ground_truths.append(_SCREAMING_SNAKE_CASE					)
							return max(_SCREAMING_SNAKE_CASE					)







def     _a					(   _SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					)     ->					Optional[int]:
							snake_case_     							=		snake_case_     							=		snake_case_     							=		0
							for article in dataset:
														for paragraph in article["paragraphs"]:
																					for qa in paragraph["qas"]:
																												total += 1
																												if qa["id"] not in predictions:
																																			snake_case_     							=		"""Unanswered question """ + qa["""id"""] + """ will receive score 0."""
																																			print(_SCREAMING_SNAKE_CASE					,			file=sys.stderr					)
																																			continue
																												snake_case_     							=		[x["""text"""] for x in qa["""answers"""]]
																												snake_case_     							=		predictions[qa["""id"""]]
																												exact_match += metric_max_over_ground_truths(_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					)
																												fa += metric_max_over_ground_truths(_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					,			_SCREAMING_SNAKE_CASE					)

							snake_case_     							=		100.0 * exact_match / total
							snake_case_     							=		100.0 * fa / total

							return {"exact_match": exact_match, "f1": fa}


if __name__ == "__main__":
				__SCREAMING_SNAKE_CASE				:     Tuple		=     '1.1'
				__SCREAMING_SNAKE_CASE				:     Union[str, Any]		=     argparse.ArgumentParser(description='Evaluation for SQuAD ' + expected_version)
				parser.add_argument('dataset_file', help='Dataset file')
				parser.add_argument('prediction_file', help='Prediction File')
				__SCREAMING_SNAKE_CASE				:     Union[str, Any]		=     parser.parse_args()
				with open(args.dataset_file) as dataset_file:
								__SCREAMING_SNAKE_CASE				:     Optional[Any]		=     json.load(dataset_file)
								if dataset_json["version"] != expected_version:
												print(
												    'Evaluation expects v-' + expected_version + ', but got dataset with v-' + dataset_json['version'],
												    file=sys.stderr,
												)
								__SCREAMING_SNAKE_CASE				:     str		=     dataset_json['data']
				with open(args.prediction_file) as prediction_file:
								__SCREAMING_SNAKE_CASE				:     int		=     json.load(prediction_file)
				print(json.dumps(evaluate(dataset, predictions)))



