'''simple docstring'''




import argparse
import collections
import json
import os
import re
import string
import sys

import numpy as np


__UpperCAmelCase												=re.compile(R"\b(a|an|the)\b", re.UNICODE)

__UpperCAmelCase												=None






def __lowerCAmelCase			(							)     ->   Union[str, Any]:
    __lowerCamelCase          =  argparse.ArgumentParser('''Official evaluation script for SQuAD version 2.0.'''    )
    parser.add_argument('''data_file'''    ,  metavar='''data.json'''    ,  help='''Input data JSON file.'''    )
    parser.add_argument('''pred_file'''    ,  metavar='''pred.json'''    ,  help='''Model predictions.'''    )
    parser.add_argument(
        '''--out-file'''    ,  '''-o'''    ,  metavar='''eval.json'''    ,  help='''Write accuracy metrics to file (default is stdout).'''    )
    parser.add_argument(
        '''--na-prob-file'''    ,  '''-n'''    ,  metavar='''na_prob.json'''    ,  help='''Model estimates of probability of no answer.'''    )
    parser.add_argument(
        '''--na-prob-thresh'''    ,  '''-t'''    ,  type=UpperCamelCase__    ,  default=1.0    ,  help='''Predict "" if no-answer probability exceeds this (default = 1.0).'''    ,  )
    parser.add_argument(
        '''--out-image-dir'''    ,  '''-p'''    ,  metavar='''out_images'''    ,  default=UpperCamelCase__    ,  help='''Save precision-recall curves to directory.'''    )
    parser.add_argument('''--verbose'''    ,  '''-v'''    ,  action='''store_true'''    )
    if len(sys.argv    ) == 1:
        parser.print_help()
        sys.exit(1    )
    return parser.parse_args()






def __lowerCAmelCase			(							UpperCamelCase__    )     ->   Any:
    __lowerCamelCase          =  {}
    for article in dataset:
        for p in article["paragraphs"]:
            for qa in p["qas"]:
                __lowerCamelCase          =  bool(qa['''answers''']['''text''']    )
    return qid_to_has_ans






def __lowerCAmelCase			(							UpperCamelCase__    )     ->   Tuple:

    def remove_articles(UpperCamelCase__    ):
        return ARTICLES_REGEX.sub(''' '''    ,  UpperCamelCase__    )

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

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

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

    return white_space_fix(remove_articles(remove_punc(lower(UpperCamelCase__    )    )    )    )






def __lowerCAmelCase			(							UpperCamelCase__    )     ->   Any:
    if not s:
        return []
    return normalize_answer(UpperCamelCase__    ).split()






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    )     ->   Any:
    return int(normalize_answer(UpperCamelCase__    ) == normalize_answer(UpperCamelCase__    )    )






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    )     ->   Any:
    __lowerCamelCase          =  get_tokens(UpperCamelCase__    )
    __lowerCamelCase          =  get_tokens(UpperCamelCase__    )
    __lowerCamelCase          =  collections.Counter(UpperCamelCase__    ) & collections.Counter(UpperCamelCase__    )
    __lowerCamelCase          =  sum(common.values()    )
    if len(UpperCamelCase__    ) == 0 or len(UpperCamelCase__    ) == 0:
        # If either is no-answer, then F1 is 1 if they agree, 0 otherwise
        return int(gold_toks == pred_toks    )
    if num_same == 0:
        return 0
    __lowerCamelCase          =  1.0 * num_same / len(UpperCamelCase__    )
    __lowerCamelCase          =  1.0 * num_same / len(UpperCamelCase__    )
    __lowerCamelCase          =  (2 * precision * recall) / (precision + recall)
    return fa






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    )     ->   Union[str, Any]:
    __lowerCamelCase          =  {}
    __lowerCamelCase          =  {}
    for article in dataset:
        for p in article["paragraphs"]:
            for qa in p["qas"]:
                __lowerCamelCase          =  qa['''id''']
                __lowerCamelCase          =  [t for t in qa['''answers''']['''text'''] if normalize_answer(UpperCamelCase__    )]
                if not gold_answers:
                    # For unanswerable questions, only correct answer is empty string
                    __lowerCamelCase          =  ['''''']
                if qid not in preds:
                    print(f"""Missing prediction for {qid}"""    )
                    continue
                __lowerCamelCase          =  preds[qid]
                # Take max over all gold answers
                __lowerCamelCase          =  max(compute_exact(UpperCamelCase__    ,  UpperCamelCase__    ) for a in gold_answers    )
                __lowerCamelCase          =  max(compute_fa(UpperCamelCase__    ,  UpperCamelCase__    ) for a in gold_answers    )
    return exact_scores, fa_scores






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )     ->   Optional[int]:
    __lowerCamelCase          =  {}
    for qid, s in scores.items():
        __lowerCamelCase          =  na_probs[qid] > na_prob_thresh
        if pred_na:
            __lowerCamelCase          =  float(not qid_to_has_ans[qid]    )
        else:
            __lowerCamelCase          =  s
    return new_scores






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__=None    )     ->   Dict:
    if not qid_list:
        __lowerCamelCase          =  len(UpperCamelCase__    )
        return collections.OrderedDict(
            [
                ('''exact''', 1_00.0 * sum(exact_scores.values()    ) / total),
                ('''f1''', 1_00.0 * sum(fa_scores.values()    ) / total),
                ('''total''', total),
            ]    )
    else:
        __lowerCamelCase          =  len(UpperCamelCase__    )
        return collections.OrderedDict(
            [
                ('''exact''', 1_00.0 * sum(exact_scores[k] for k in qid_list    ) / total),
                ('''f1''', 1_00.0 * sum(fa_scores[k] for k in qid_list    ) / total),
                ('''total''', total),
            ]    )






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )     ->   List[Any]:
    for k in new_eval:
        __lowerCamelCase          =  new_eval[k]






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )     ->   Any:
    plt.step(UpperCamelCase__    ,  UpperCamelCase__    ,  color='''b'''    ,  alpha=0.2    ,  where='''post'''    )
    plt.fill_between(UpperCamelCase__    ,  UpperCamelCase__    ,  step='''post'''    ,  alpha=0.2    ,  color='''b'''    )
    plt.xlabel('''Recall'''    )
    plt.ylabel('''Precision'''    )
    plt.xlim([0.0, 1.0_5]    )
    plt.ylim([0.0, 1.0_5]    )
    plt.title(UpperCamelCase__    )
    plt.savefig(UpperCamelCase__    )
    plt.clf()






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__=None    ,  UpperCamelCase__=None    )     ->   Optional[Any]:
    __lowerCamelCase          =  sorted(UpperCamelCase__    ,  key=lambda UpperCamelCase__    : na_probs[k]    )
    __lowerCamelCase          =  0.0
    __lowerCamelCase          =  1.0
    __lowerCamelCase          =  0.0
    __lowerCamelCase          =  [1.0]
    __lowerCamelCase          =  [0.0]
    __lowerCamelCase          =  0.0
    for i, qid in enumerate(UpperCamelCase__    ):
        if qid_to_has_ans[qid]:
            true_pos += scores[qid]
        __lowerCamelCase          =  true_pos / float(i + 1    )
        __lowerCamelCase          =  true_pos / float(UpperCamelCase__    )
        if i == len(UpperCamelCase__    ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
            # i.e., if we can put a threshold after this point
            avg_prec += cur_p * (cur_r - recalls[-1])
            precisions.append(UpperCamelCase__    )
            recalls.append(UpperCamelCase__    )
    if out_image:
        plot_pr_curve(UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )
    return {"ap": 1_00.0 * avg_prec}






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )     ->   Tuple:
    if out_image_dir and not os.path.exists(UpperCamelCase__    ):
        os.makedirs(UpperCamelCase__    )
    __lowerCamelCase          =  sum(1 for v in qid_to_has_ans.values() if v    )
    if num_true_pos == 0:
        return
    __lowerCamelCase          =  make_precision_recall_eval(
        UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  out_image=os.path.join(UpperCamelCase__    ,  '''pr_exact.png'''    )    ,  title='''Precision-Recall curve for Exact Match score'''    ,  )
    __lowerCamelCase          =  make_precision_recall_eval(
        UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  out_image=os.path.join(UpperCamelCase__    ,  '''pr_f1.png'''    )    ,  title='''Precision-Recall curve for F1 score'''    ,  )
    __lowerCamelCase          =  {k: float(UpperCamelCase__    ) for k, v in qid_to_has_ans.items()}
    __lowerCamelCase          =  make_precision_recall_eval(
        UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  out_image=os.path.join(UpperCamelCase__    ,  '''pr_oracle.png'''    )    ,  title='''Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)'''    ,  )
    merge_eval(UpperCamelCase__    ,  UpperCamelCase__    ,  '''pr_exact'''    )
    merge_eval(UpperCamelCase__    ,  UpperCamelCase__    ,  '''pr_f1'''    )
    merge_eval(UpperCamelCase__    ,  UpperCamelCase__    ,  '''pr_oracle'''    )






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )     ->   Tuple:
    if not qid_list:
        return
    __lowerCamelCase          =  [na_probs[k] for k in qid_list]
    __lowerCamelCase          =  np.ones_like(UpperCamelCase__    ) / float(len(UpperCamelCase__    )    )
    plt.hist(UpperCamelCase__    ,  weights=UpperCamelCase__    ,  bins=20    ,  range=(0.0, 1.0)    )
    plt.xlabel('''Model probability of no-answer'''    )
    plt.ylabel('''Proportion of dataset'''    )
    plt.title(f"""Histogram of no-answer probability: {name}"""    )
    plt.savefig(os.path.join(UpperCamelCase__    ,  f"""na_prob_hist_{name}.png"""    )    )
    plt.clf()






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )     ->   Any:
    __lowerCamelCase          =  sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]    )
    __lowerCamelCase          =  num_no_ans
    __lowerCamelCase          =  cur_score
    __lowerCamelCase          =  0.0
    __lowerCamelCase          =  sorted(UpperCamelCase__    ,  key=lambda UpperCamelCase__    : na_probs[k]    )
    for i, qid in enumerate(UpperCamelCase__    ):
        if qid not in scores:
            continue
        if qid_to_has_ans[qid]:
            __lowerCamelCase          =  scores[qid]
        else:
            if preds[qid]:
                __lowerCamelCase          =  -1
            else:
                __lowerCamelCase          =  0
        cur_score += diff
        if cur_score > best_score:
            __lowerCamelCase          =  cur_score
            __lowerCamelCase          =  na_probs[qid]
    return 1_00.0 * best_score / len(UpperCamelCase__    ), best_thresh






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )     ->   Optional[Any]:
    __lowerCamelCase     ,  __lowerCamelCase          =  find_best_thresh(UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )
    __lowerCamelCase     ,  __lowerCamelCase          =  find_best_thresh(UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )
    __lowerCamelCase          =  best_exact
    __lowerCamelCase          =  exact_thresh
    __lowerCamelCase          =  best_fa
    __lowerCamelCase          =  fa_thresh






def __lowerCAmelCase			(							)     ->   Tuple:
    with open(OPTS.data_file    ) as f:
        __lowerCamelCase          =  json.load(UpperCamelCase__    )
        __lowerCamelCase          =  dataset_json['''data''']
    with open(OPTS.pred_file    ) as f:
        __lowerCamelCase          =  json.load(UpperCamelCase__    )
    if OPTS.na_prob_file:
        with open(OPTS.na_prob_file    ) as f:
            __lowerCamelCase          =  json.load(UpperCamelCase__    )
    else:
        __lowerCamelCase          =  {k: 0.0 for k in preds}
    __lowerCamelCase          =  make_qid_to_has_ans(UpperCamelCase__    )  # maps qid to True/False
    __lowerCamelCase          =  [k for k, v in qid_to_has_ans.items() if v]
    __lowerCamelCase          =  [k for k, v in qid_to_has_ans.items() if not v]
    __lowerCamelCase     ,  __lowerCamelCase          =  get_raw_scores(UpperCamelCase__    ,  UpperCamelCase__    )
    __lowerCamelCase          =  apply_no_ans_threshold(UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  OPTS.na_prob_thresh    )
    __lowerCamelCase          =  apply_no_ans_threshold(UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  OPTS.na_prob_thresh    )
    __lowerCamelCase          =  make_eval_dict(UpperCamelCase__    ,  UpperCamelCase__    )
    if has_ans_qids:
        __lowerCamelCase          =  make_eval_dict(UpperCamelCase__    ,  UpperCamelCase__    ,  qid_list=UpperCamelCase__    )
        merge_eval(UpperCamelCase__    ,  UpperCamelCase__    ,  '''HasAns'''    )
    if no_ans_qids:
        __lowerCamelCase          =  make_eval_dict(UpperCamelCase__    ,  UpperCamelCase__    ,  qid_list=UpperCamelCase__    )
        merge_eval(UpperCamelCase__    ,  UpperCamelCase__    ,  '''NoAns'''    )
    if OPTS.na_prob_file:
        find_all_best_thresh(UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )
    if OPTS.na_prob_file and OPTS.out_image_dir:
        run_precision_recall_analysis(UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  OPTS.out_image_dir    )
        histogram_na_prob(UpperCamelCase__    ,  UpperCamelCase__    ,  OPTS.out_image_dir    ,  '''hasAns'''    )
        histogram_na_prob(UpperCamelCase__    ,  UpperCamelCase__    ,  OPTS.out_image_dir    ,  '''noAns'''    )
    if OPTS.out_file:
        with open(OPTS.out_file    ,  '''w'''    ) as f:
            json.dump(UpperCamelCase__    ,  UpperCamelCase__    )
    else:
        print(json.dumps(UpperCamelCase__    ,  indent=2    )    )


if __name__ == "__main__":
  __UpperCAmelCase												=parse_args()
  if OPTS.out_image_dir:
    import matplotlib

    matplotlib.use("Agg")
    import matplotlib.pyplot as plt
  main()







'''simple docstring'''




import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin




class    a__				(       UpperCAmelCase__							,       UpperCAmelCase__			):






     @register_to_config
     def __init__(			self		:		Any  ,    *,
         a		:		int = 4  ,    a		:		int = 7_68  ,    a		:		int  ,    a		:		Optional[int]  ,    ):





         """simple docstring"""



         super().__init__()

         __lowerCamelCase          =  nn.Parameter(torch.zeros(a   )   )

         # parameters for additional clip time embeddings
         __lowerCamelCase          =  nn.Linear(a  ,    a   )
         __lowerCamelCase          =  nn.Linear(a  ,    a   )

         # parameters for encoder hidden states
         __lowerCamelCase          =  clip_extra_context_tokens
         __lowerCamelCase          =  nn.Linear(
             a  ,    self.clip_extra_context_tokens * cross_attention_dim   )
         __lowerCamelCase          =  nn.Linear(a  ,    a   )
         __lowerCamelCase          =  nn.LayerNorm(a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		int  ,    *, a		:		Union[str, Any]  ,    a		:		Any  ,    a		:		str  ,    a		:		Any   ):





         """simple docstring"""



         if do_classifier_free_guidance:
             # Add the classifier free guidance embeddings to the image embeddings
             __lowerCamelCase          =  image_embeddings.shape[0]
             __lowerCamelCase          =  self.learned_classifier_free_guidance_embeddings.unsqueeze(0   )
             __lowerCamelCase          =  classifier_free_guidance_embeddings.expand(
                 a  ,    -1   )
             __lowerCamelCase          =  torch.cat([classifier_free_guidance_embeddings, image_embeddings]  ,    dim=0   )

         # The image embeddings batch size and the text embeddings batch size are equal
         assert image_embeddings.shape[0] == prompt_embeds.shape[0]

         __lowerCamelCase          =  prompt_embeds.shape[0]

         # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
         # adding CLIP embeddings to the existing timestep embedding, ...
         __lowerCamelCase          =  self.embedding_proj(a   )
         __lowerCamelCase          =  self.clip_image_embeddings_project_to_time_embeddings(a   )
         __lowerCamelCase          =  time_projected_image_embeddings + time_projected_prompt_embeds

         # ... and by projecting CLIP embeddings into four
         # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
         __lowerCamelCase          =  self.clip_extra_context_tokens_proj(a   )
         __lowerCamelCase          =  clip_extra_context_tokens.reshape(a  ,    -1  ,    self.clip_extra_context_tokens   )
         __lowerCamelCase          =  clip_extra_context_tokens.permute(0  ,    2  ,    1   )

         __lowerCamelCase          =  self.encoder_hidden_states_proj(a   )
         __lowerCamelCase          =  self.text_encoder_hidden_states_norm(a   )
         __lowerCamelCase          =  torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]  ,    dim=1   )

         return text_encoder_hidden_states, additive_clip_time_embeddings
