




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


a_										=       logging.get_logger(__name__)

class 	_lowerCamelCase  (     nn.Module				):

      """simple docstring"""

      lowerCAmelCase__     :	HybridCLIPConfig
      lowerCAmelCase__     :	jnp.dtype      =   jnp.floataa







      def  snake_case		(       self   :	Optional[int]			):
          __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__	,    snake_case			).module_class

          __UpperCamelCase											=  text_module(snake_case	,    dtype=self.dtype			)
          __UpperCamelCase											=  vision_module(snake_case	,    dtype=self.dtype			)

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







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

          __UpperCamelCase											=  self.vision_model(
              pixel_values=snake_case	,    deterministic=snake_case	,    output_attentions=snake_case	,    output_hidden_states=snake_case	,    return_dict=snake_case	,    )

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

          __UpperCamelCase											=  vision_outputs[1]
          __UpperCamelCase											=  self.visual_projection(snake_case			)

          __UpperCamelCase											=  text_outputs[1]
          __UpperCamelCase											=  self.text_projection(snake_case			)

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

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







class 	_lowerCamelCase  (     _SCREAMING_SNAKE_CASE				):

      """simple docstring"""

      lowerCAmelCase__     :	List[Any]      =   HybridCLIPConfig
      lowerCAmelCase__     :	Union[str, Any]      =   FlaxHybridCLIPModule







      def __init__(       self   :	Any	,    snake_case   :	HybridCLIPConfig	,    snake_case   :	Optional[Tuple] = None	,    snake_case   :	int = 0	,    snake_case   :	jnp.dtype = jnp.floataa	,    **snake_case   :	Dict	,    ):
          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=snake_case	,    dtype=snake_case	,    **snake_case			)
          super().__init__(snake_case	,    snake_case	,    input_shape=snake_case	,    seed=snake_case	,    dtype=snake_case			)







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

          __UpperCamelCase											=  jax.random.normal(snake_case	,    input_shape[1]			)

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

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







      def __call__(       self   :	Optional[Any]	,    snake_case   :	List[str]	,    snake_case   :	Optional[Any]	,    snake_case   :	Optional[int]=None	,    snake_case   :	List[Any]=None	,    snake_case   :	List[Any]=None	,    snake_case   :	dict = None	,    snake_case   :	jax.random.PRNGKey = None	,    snake_case   :	bool = False	,    snake_case   :	Optional[bool] = None	,    snake_case   :	Optional[bool] = None	,    snake_case   :	Optional[bool] = None	,    ):
          __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(snake_case			).shape[-1]			)	,    input_ids.shape			)

          if token_type_ids is None:
              __UpperCamelCase											=  jnp.zeros_like(snake_case			)

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

          # 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(snake_case	,    dtype='''i4'''			)	,    jnp.array(snake_case	,    dtype=jnp.floataa			)	,    jnp.array(snake_case	,    dtype='''i4'''			)	,    jnp.array(snake_case	,    dtype='''i4'''			)	,    jnp.array(snake_case	,    dtype='''i4'''			)	,    not train	,    snake_case	,    snake_case	,    snake_case	,    rngs=snake_case	,    )







      def  snake_case		(       self   :	Dict	,    snake_case   :	Optional[Any]	,    snake_case   :	Optional[int]=None	,    snake_case   :	Union[str, Any]=None	,    snake_case   :	List[Any]=None	,    snake_case   :	dict = None	,    snake_case   :	jax.random.PRNGKey = None	,    snake_case   :	Union[str, Any]=False	,    ):
          if position_ids is None:
              __UpperCamelCase											=  jnp.broadcast_to(jnp.arange(jnp.atleast_ad(snake_case			).shape[-1]			)	,    input_ids.shape			)

          if token_type_ids is None:
              __UpperCamelCase											=  jnp.zeros_like(snake_case			)

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

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

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

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







      def  snake_case		(       self   :	List[str]	,    snake_case   :	Optional[int]	,    snake_case   :	dict = None	,    snake_case   :	jax.random.PRNGKey = None	,    snake_case   :	Any=False			):
          __UpperCamelCase											=  {}
          if dropout_rng is not None:
              __UpperCamelCase											=  dropout_rng

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

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







      @classmethod
      def  snake_case		(       cls   :	Optional[Any]	,    snake_case   :	str = None	,    snake_case   :	str = None	,    *snake_case   :	List[Any]	,    **snake_case   :	Union[str, Any]	,    ):
          __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'''	,    snake_case			)
          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(snake_case			)
                  __UpperCamelCase											=  text_config

              __UpperCamelCase											=  FlaxAutoModel.from_pretrained(snake_case	,    *snake_case	,    **snake_case			)

          __UpperCamelCase											=  kwargs_vision.pop('''model'''	,    snake_case			)
          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(snake_case			)
                  __UpperCamelCase											=  vision_config

              __UpperCamelCase											=  FlaxAutoModel.from_pretrained(snake_case	,    *snake_case	,    **snake_case			)

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

          # init model
          __UpperCamelCase											=  cls(snake_case	,    *snake_case	,    dtype=snake_case	,    **snake_case			)

          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 argparse
import json
import re
import string
import sys
from collections import Counter




def    __SCREAMING_SNAKE_CASE				(					lowercase_ )    ->		Dict:

    """simple docstring"""


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

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

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

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

    return white_space_fix(remove_articles(remove_punc(lower(lowercase_ ) ) ) )




def    __SCREAMING_SNAKE_CASE				(					lowercase_   ,       lowercase_ )    ->		List[str]:

    """simple docstring"""


    __UpperCamelCase											=  normalize_answer(lowercase_ ).split()
    __UpperCamelCase											=  normalize_answer(lowercase_ ).split()
    __UpperCamelCase											=  Counter(lowercase_ ) & Counter(lowercase_ )
    __UpperCamelCase											=  sum(common.values() )
    if num_same == 0:
        return 0
    __UpperCamelCase											=  1.0 * num_same / len(lowercase_ )
    __UpperCamelCase											=  1.0 * num_same / len(lowercase_ )
    __UpperCamelCase											=  (2 * precision * recall) / (precision + recall)
    return fa




def    __SCREAMING_SNAKE_CASE				(					lowercase_   ,       lowercase_ )    ->		Any:

    """simple docstring"""


    return normalize_answer(lowercase_ ) == normalize_answer(lowercase_ )




def    __SCREAMING_SNAKE_CASE				(					lowercase_   ,       lowercase_   ,       lowercase_ )    ->		int:

    """simple docstring"""


    __UpperCamelCase											=  []
    for ground_truth in ground_truths:
        __UpperCamelCase											=  metric_fn(lowercase_   ,       lowercase_ )
        scores_for_ground_truths.append(lowercase_ )
    return max(lowercase_ )




def    __SCREAMING_SNAKE_CASE				(					lowercase_   ,       lowercase_ )    ->		Optional[int]:

    """simple docstring"""


    __UpperCamelCase											=  __UpperCamelCase											=  __UpperCamelCase											=  0
    __UpperCamelCase											=  []
    for passage in dataset:
        for qa in passage["qas"]:
            total += 1
            if qa["id"] not in predictions:
                __UpperCamelCase											=  F"Unanswered question {qa['id']} will receive score 0."
                print(lowercase_   ,       file=sys.stderr )
                continue

            __UpperCamelCase											=  [x['''text'''] for x in qa['''answers''']]
            __UpperCamelCase											=  predictions[qa['''id''']]

            __UpperCamelCase											=  metric_max_over_ground_truths(lowercase_   ,       lowercase_   ,       lowercase_ )
            if int(_exact_match ) == 1:
                correct_ids.append(qa['''id'''] )
            exact_match += _exact_match

            fa += metric_max_over_ground_truths(lowercase_   ,       lowercase_   ,       lowercase_ )

    __UpperCamelCase											=  exact_match / total
    __UpperCamelCase											=  fa / total

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


if __name__ == "__main__":
 a_										=       "1.0"
 a_										=       argparse.ArgumentParser("Official evaluation script for ReCoRD v1.0.")
 parser.add_argument("data_file", help="The dataset file in JSON format.")
 parser.add_argument("pred_file", help="The model prediction file in JSON format.")
 parser.add_argument("--output_correct_ids", action="store_true", help="Output the correctly answered query IDs.")
 a_										=       parser.parse_args()

 with open(args.data_file) as data_file:
  a_										=       json.load(data_file)
  if dataset_json["version"] != expected_version:
   print(
       f"""Evaluation expects v-{expected_version}, but got dataset with v-{dataset_json['version']}""",
       file=sys.stderr,
   )
  a_										=       dataset_json["data"]

 with open(args.pred_file) as pred_file:
  a_										=       json.load(pred_file)

 a_						,      a_										=       evaluate(dataset, predictions)

 if args.output_correct_ids:
  print(f"""Output {len(correct_ids)} correctly answered question IDs.""")
  with open("correct_ids.json", "w") as f:
   json.dump(correct_ids, f)






