

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

import numpy as np


snake_case_        =			re.compile(r'''\b(a|an|the)\b''', re.UNICODE)

snake_case_        =			None

def      snake_case__      (   ):



     '''simple docstring'''





     lowercase__   :       Dict				=	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=_a     ,					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=_a     ,					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      snake_case__      (   SCREAMING_SNAKE_CASE_		:   str      ):



     '''simple docstring'''





     lowercase__   :       Any				=	{}
     for article in dataset:
          for p in article["paragraphs"]:
               for qa in p["qas"]:
                    lowercase__   :       Tuple				=	bool(qa['answers']['text']      )
     return qid_to_has_ans

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Optional[Any]      ):



     '''simple docstring'''





     def remove_articles(SCREAMING_SNAKE_CASE_		:   Union[str, Any]      ):
          return ARTICLES_REGEX.sub(' '     ,					_a      )

     def white_space_fix(SCREAMING_SNAKE_CASE_		:   str      ):
          return " ".join(text.split()      )

     def remove_punc(SCREAMING_SNAKE_CASE_		:   str      ):
          lowercase__   :       List[str]				=	set(string.punctuation      )
          return "".join(ch for ch in text if ch not in exclude      )

     def lower(SCREAMING_SNAKE_CASE_		:   Union[str, Any]      ):
          return text.lower()

     return white_space_fix(remove_articles(remove_punc(lower(_a      )      )      )      )

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Tuple      ):



     '''simple docstring'''





     if not s:
          return []
     return normalize_answer(_a      ).split()

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Dict     ,					SCREAMING_SNAKE_CASE_		:   Dict      ):



     '''simple docstring'''





     return int(normalize_answer(_a      ) == normalize_answer(_a      )      )

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Union[str, Any]     ,					SCREAMING_SNAKE_CASE_		:   str      ):



     '''simple docstring'''





     lowercase__   :       Optional[Any]				=	get_tokens(_a      )
     lowercase__   :       int				=	get_tokens(_a      )
     lowercase__   :       Dict				=	collections.Counter(_a      ) & collections.Counter(_a      )
     lowercase__   :       Optional[int]				=	sum(common.values()      )
     if len(_a      ) == 0 or len(_a      ) == 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
     lowercase__   :       Union[str, Any]				=	1.0 * num_same / len(_a      )
     lowercase__   :       Optional[Any]				=	1.0 * num_same / len(_a      )
     lowercase__   :       int				=	(2 * precision * recall) / (precision + recall)
     return fa

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   List[Any]     ,					SCREAMING_SNAKE_CASE_		:   Union[str, Any]      ):



     '''simple docstring'''





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

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   int     ,					SCREAMING_SNAKE_CASE_		:   List[Any]     ,					SCREAMING_SNAKE_CASE_		:   Dict     ,					SCREAMING_SNAKE_CASE_		:   List[Any]      ):



     '''simple docstring'''





     lowercase__   :       Optional[int]				=	{}
     for qid, s in scores.items():
          lowercase__   :       Tuple				=	na_probs[qid] > na_prob_thresh
          if pred_na:
               lowercase__   :       Union[str, Any]				=	float(not qid_to_has_ans[qid]      )
          else:
               lowercase__   :       str				=	s
     return new_scores

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   str     ,					SCREAMING_SNAKE_CASE_		:   Union[str, Any]     ,					SCREAMING_SNAKE_CASE_		:   Tuple=None      ):



     '''simple docstring'''





     if not qid_list:
          lowercase__   :       Optional[Any]				=	len(_a      )
          return collections.OrderedDict(
              [
                  ('exact', 100.0 * sum(exact_scores.values()      ) / total),
                  ('f1', 100.0 * sum(fa_scores.values()      ) / total),
                  ('total', total),
              ]      )
     else:
          lowercase__   :       int				=	len(_a      )
          return collections.OrderedDict(
              [
                  ('exact', 100.0 * sum(exact_scores[k] for k in qid_list      ) / total),
                  ('f1', 100.0 * sum(fa_scores[k] for k in qid_list      ) / total),
                  ('total', total),
              ]      )

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Any     ,					SCREAMING_SNAKE_CASE_		:   List[Any]     ,					SCREAMING_SNAKE_CASE_		:   Tuple      ):



     '''simple docstring'''





     for k in new_eval:
          lowercase__   :       List[str]				=	new_eval[k]

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Dict     ,					SCREAMING_SNAKE_CASE_		:   int     ,					SCREAMING_SNAKE_CASE_		:   int     ,					SCREAMING_SNAKE_CASE_		:   Union[str, Any]      ):



     '''simple docstring'''





     plt.step(_a     ,					_a     ,					color='b'     ,					alpha=0.2     ,					where='post'      )
     plt.fill_between(_a     ,					_a     ,					step='post'     ,					alpha=0.2     ,					color='b'      )
     plt.xlabel('Recall'      )
     plt.ylabel('Precision'      )
     plt.xlim([0.0, 1.05]      )
     plt.ylim([0.0, 1.05]      )
     plt.title(_a      )
     plt.savefig(_a      )
     plt.clf()

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   List[str]     ,					SCREAMING_SNAKE_CASE_		:   Tuple     ,					SCREAMING_SNAKE_CASE_		:   Dict     ,					SCREAMING_SNAKE_CASE_		:   Optional[int]     ,					SCREAMING_SNAKE_CASE_		:   Optional[int]=None     ,					SCREAMING_SNAKE_CASE_		:   List[Any]=None      ):



     '''simple docstring'''





     lowercase__   :       Any				=	sorted(_a     ,					key=lambda SCREAMING_SNAKE_CASE_      : na_probs[k]      )
     lowercase__   :       Union[str, Any]				=	0.0
     lowercase__   :       Optional[Any]				=	1.0
     lowercase__   :       List[Any]				=	0.0
     lowercase__   :       int				=	[1.0]
     lowercase__   :       List[str]				=	[0.0]
     lowercase__   :       Optional[int]				=	0.0
     for i, qid in enumerate(_a      ):
          if qid_to_has_ans[qid]:
               true_pos += scores[qid]
          lowercase__   :       Union[str, Any]				=	true_pos / float(i + 1      )
          lowercase__   :       Optional[Any]				=	true_pos / float(_a      )
          if i == len(_a      ) - 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(_a      )
               recalls.append(_a      )
     if out_image:
          plot_pr_curve(_a     ,					_a     ,					_a     ,					_a      )
     return {"ap": 100.0 * avg_prec}

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   str     ,					SCREAMING_SNAKE_CASE_		:   Union[str, Any]     ,					SCREAMING_SNAKE_CASE_		:   List[Any]     ,					SCREAMING_SNAKE_CASE_		:   Dict     ,					SCREAMING_SNAKE_CASE_		:   Union[str, Any]     ,					SCREAMING_SNAKE_CASE_		:   Optional[Any]      ):



     '''simple docstring'''





     if out_image_dir and not os.path.exists(_a      ):
          os.makedirs(_a      )
     lowercase__   :       int				=	sum(1 for v in qid_to_has_ans.values() if v      )
     if num_true_pos == 0:
          return
     lowercase__   :       List[Any]				=	make_precision_recall_eval(
         _a     ,					_a     ,					_a     ,					_a     ,					out_image=os.path.join(_a     ,					'pr_exact.png'      )     ,					title='Precision-Recall curve for Exact Match score'     ,					)
     lowercase__   :       List[Any]				=	make_precision_recall_eval(
         _a     ,					_a     ,					_a     ,					_a     ,					out_image=os.path.join(_a     ,					'pr_f1.png'      )     ,					title='Precision-Recall curve for F1 score'     ,					)
     lowercase__   :       str				=	{k: float(_a      ) for k, v in qid_to_has_ans.items()}
     lowercase__   :       List[str]				=	make_precision_recall_eval(
         _a     ,					_a     ,					_a     ,					_a     ,					out_image=os.path.join(_a     ,					'pr_oracle.png'      )     ,					title='Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)'     ,					)
     merge_eval(_a     ,					_a     ,					'pr_exact'      )
     merge_eval(_a     ,					_a     ,					'pr_f1'      )
     merge_eval(_a     ,					_a     ,					'pr_oracle'      )

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Dict     ,					SCREAMING_SNAKE_CASE_		:   List[Any]     ,					SCREAMING_SNAKE_CASE_		:   str     ,					SCREAMING_SNAKE_CASE_		:   str      ):



     '''simple docstring'''





     if not qid_list:
          return
     lowercase__   :       Optional[int]				=	[na_probs[k] for k in qid_list]
     lowercase__   :       Dict				=	np.ones_like(_a      ) / float(len(_a      )      )
     plt.hist(_a     ,					weights=_a     ,					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(_a     ,					f"""na_prob_hist_{name}.png"""      )      )
     plt.clf()

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Union[str, Any]     ,					SCREAMING_SNAKE_CASE_		:   str     ,					SCREAMING_SNAKE_CASE_		:   str     ,					SCREAMING_SNAKE_CASE_		:   Union[str, Any]      ):



     '''simple docstring'''





     lowercase__   :       Union[str, Any]				=	sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]      )
     lowercase__   :       List[str]				=	num_no_ans
     lowercase__   :       Union[str, Any]				=	cur_score
     lowercase__   :       List[Any]				=	0.0
     lowercase__   :       Union[str, Any]				=	sorted(_a     ,					key=lambda SCREAMING_SNAKE_CASE_      : na_probs[k]      )
     for i, qid in enumerate(_a      ):
          if qid not in scores:
               continue
          if qid_to_has_ans[qid]:
               lowercase__   :       int				=	scores[qid]
          else:
               if preds[qid]:
                    lowercase__   :       List[Any]				=	-1
               else:
                    lowercase__   :       Union[str, Any]				=	0
          cur_score += diff
          if cur_score > best_score:
               lowercase__   :       Optional[int]				=	cur_score
               lowercase__   :       Any				=	na_probs[qid]
     return 100.0 * best_score / len(_a      ), best_thresh

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Optional[Any]     ,					SCREAMING_SNAKE_CASE_		:   Optional[Any]     ,					SCREAMING_SNAKE_CASE_		:   Union[str, Any]     ,					SCREAMING_SNAKE_CASE_		:   Dict     ,					SCREAMING_SNAKE_CASE_		:   Dict     ,					SCREAMING_SNAKE_CASE_		:   Optional[int]      ):



     '''simple docstring'''





     lowercase__				,			lowercase__   :       str				=	find_best_thresh(_a     ,					_a     ,					_a     ,					_a      )
     lowercase__				,			lowercase__   :       List[Any]				=	find_best_thresh(_a     ,					_a     ,					_a     ,					_a      )
     lowercase__   :       Union[str, Any]				=	best_exact
     lowercase__   :       Union[str, Any]				=	exact_thresh
     lowercase__   :       Optional[Any]				=	best_fa
     lowercase__   :       Tuple				=	fa_thresh

def      snake_case__      (   ):



     '''simple docstring'''





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


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

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




import argparse
import json
import os

import torch
from transformers.file_utils import has_file

from diffusers import UNetaDConditionModel, UNetaDModel


snake_case_        =			False
snake_case_        =			True
snake_case_        =			False


if __name__ == "__main__":
       snake_case_        =			argparse.ArgumentParser()

       parser.add_argument(
           '''--repo_path''',
           default=None,
           type=str,
           required=True,
           help='''The config json file corresponding to the architecture.''',
       )

       parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the output model.''')

       snake_case_        =			parser.parse_args()

       snake_case_        =			{
           '''image_size''': '''sample_size''',
           '''num_res_blocks''': '''layers_per_block''',
           '''block_channels''': '''block_out_channels''',
           '''down_blocks''': '''down_block_types''',
           '''up_blocks''': '''up_block_types''',
           '''downscale_freq_shift''': '''freq_shift''',
           '''resnet_num_groups''': '''norm_num_groups''',
           '''resnet_act_fn''': '''act_fn''',
           '''resnet_eps''': '''norm_eps''',
           '''num_head_channels''': '''attention_head_dim''',
       }

       snake_case_        =			{
           '''time_steps''': '''time_proj''',
           '''mid''': '''mid_block''',
           '''downsample_blocks''': '''down_blocks''',
           '''upsample_blocks''': '''up_blocks''',
       }

       snake_case_        =			'''''' if has_file(args.repo_path, '''config.json''') else '''unet'''

       with open(os.path.join(args.repo_path, subfolder, '''config.json'''), '''r''', encoding='''utf-8''') as reader:
              snake_case_        =			reader.read()
              snake_case_        =			json.loads(text)

       if do_only_config:
              for key in config_parameters_to_change.keys():
                     config.pop(key, None)

       if has_file(args.repo_path, '''config.json'''):
              snake_case_        =			UNetaDModel(**config)
       else:
              snake_case_        =			UNetaDConditionModel if '''ldm-text2im-large-256''' in args.repo_path else UNetaDModel
              snake_case_        =			class_name(**config)

       if do_only_config:
              model.save_config(os.path.join(args.repo_path, subfolder))

       snake_case_        =			dict(model.config)

       if do_only_renaming:
              for key, value in config_parameters_to_change.items():
                     if key in config:
                            snake_case_        =			config[key]
                            del config[key]

              snake_case_        =			[k.replace('''UNetRes''', '''''') for k in config['''down_block_types''']]
              snake_case_        =			[k.replace('''UNetRes''', '''''') for k in config['''up_block_types''']]

       if do_only_weights:
              snake_case_        =			torch.load(os.path.join(args.repo_path, subfolder, '''diffusion_pytorch_model.bin'''))

              snake_case_        =			{}
              for param_key, param_value in state_dict.items():
                     if param_key.endswith('''.op.bias''') or param_key.endswith('''.op.weight'''):
                            continue
                     snake_case_        =			False
                     for key, new_key in key_parameters_to_change.items():
                            if not has_changed and param_key.split('''.''')[0] == key:
                                   snake_case_        =			param_value
                                   snake_case_        =			True
                     if not has_changed:
                            snake_case_        =			param_value

              model.load_state_dict(new_state_dict)
              model.save_pretrained(os.path.join(args.repo_path, subfolder))

