
"""simple docstring"""

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

import numpy as np


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

_A						=			None
def 		UpperCAmelCase_ (		):





		'''simple docstring'''


		lowerCamelCase					:      Optional[Any]          =   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=_lowerCAmelCase,						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=_lowerCAmelCase,						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 		UpperCAmelCase_ (		a_      ):





		'''simple docstring'''


		lowerCamelCase					:      Optional[int]          =   {}
		for article in dataset:
				for p in article["paragraphs"]:
						for qa in p["qas"]:
								lowerCamelCase					:      Tuple          =   bool(qa['answers']['text']      )
		return qid_to_has_ans
def 		UpperCAmelCase_ (		a_      ):





		'''simple docstring'''


		def remove_articles(a_      ):
				return ARTICLES_REGEX.sub(' ',						_lowerCAmelCase      )

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

		def remove_punc(a_      ):
				lowerCamelCase					:      Optional[int]          =   set(string.punctuation      )
				return "".join(ch for ch in text if ch not in exclude      )

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

		return white_space_fix(remove_articles(remove_punc(lower(_lowerCAmelCase      )      )      )      )
def 		UpperCAmelCase_ (		a_      ):





		'''simple docstring'''


		if not s:
				return []
		return normalize_answer(_lowerCAmelCase      ).split()
def 		UpperCAmelCase_ (		a_,						a_      ):





		'''simple docstring'''


		return int(normalize_answer(_lowerCAmelCase      ) == normalize_answer(_lowerCAmelCase      )      )
def 		UpperCAmelCase_ (		a_,						a_      ):





		'''simple docstring'''


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





		'''simple docstring'''


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





		'''simple docstring'''


		lowerCamelCase					:      List[Any]          =   {}
		for qid, s in scores.items():
				lowerCamelCase					:      Optional[Any]          =   na_probs[qid] > na_prob_thresh
				if pred_na:
						lowerCamelCase					:      int          =   float(not qid_to_has_ans[qid]      )
				else:
						lowerCamelCase					:      Dict          =   s
		return new_scores
def 		UpperCAmelCase_ (		a_,						a_,						a_=None      ):





		'''simple docstring'''


		if not qid_list:
				lowerCamelCase					:      List[str]          =   len(_lowerCAmelCase      )
				return collections.OrderedDict(
				    [
				        ('exact', 100.0 * sum(exact_scores.values()      ) / total),
				        ('f1', 100.0 * sum(fa_scores.values()      ) / total),
				        ('total', total),
				    ]      )
		else:
				lowerCamelCase					:      Optional[Any]          =   len(_lowerCAmelCase      )
				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 		UpperCAmelCase_ (		a_,						a_,						a_      ):





		'''simple docstring'''


		for k in new_eval:
				lowerCamelCase					:      str          =   new_eval[k]
def 		UpperCAmelCase_ (		a_,						a_,						a_,						a_      ):





		'''simple docstring'''


		plt.step(_lowerCAmelCase,						_lowerCAmelCase,						color='b',						alpha=0.2,						where='post'      )
		plt.fill_between(_lowerCAmelCase,						_lowerCAmelCase,						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(_lowerCAmelCase      )
		plt.savefig(_lowerCAmelCase      )
		plt.clf()
def 		UpperCAmelCase_ (		a_,						a_,						a_,						a_,						a_=None,						a_=None      ):





		'''simple docstring'''


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





		'''simple docstring'''


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





		'''simple docstring'''


		if not qid_list:
				return
		lowerCamelCase					:      List[Any]          =   [na_probs[k] for k in qid_list]
		lowerCamelCase					:      List[str]          =   np.ones_like(_lowerCAmelCase      ) / float(len(_lowerCAmelCase      )      )
		plt.hist(_lowerCAmelCase,						weights=_lowerCAmelCase,						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(_lowerCAmelCase,						F"""na_prob_hist_{name}.png"""      )      )
		plt.clf()
def 		UpperCAmelCase_ (		a_,						a_,						a_,						a_      ):





		'''simple docstring'''


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





		'''simple docstring'''


		lowerCamelCase     ,   lowerCamelCase					:      int          =   find_best_thresh(_lowerCAmelCase,						_lowerCAmelCase,						_lowerCAmelCase,						_lowerCAmelCase      )
		lowerCamelCase     ,   lowerCamelCase					:      int          =   find_best_thresh(_lowerCAmelCase,						_lowerCAmelCase,						_lowerCAmelCase,						_lowerCAmelCase      )
		lowerCamelCase					:      List[str]          =   best_exact
		lowerCamelCase					:      Any          =   exact_thresh
		lowerCamelCase					:      Optional[Any]          =   best_fa
		lowerCamelCase					:      List[Any]          =   fa_thresh
def 		UpperCAmelCase_ (		):





		'''simple docstring'''


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


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

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








"""simple docstring"""

import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_A						=			logging.get_logger(__name__)

_A						=			{
    'asapp/sew-d-tiny-100k': 'https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json',
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}






class 	_lowercase       (    __UpperCAmelCase		):
	lowercase_				=     'sew-d'




	def __init__(			self		,			UpperCAmelCase_=32		,			UpperCAmelCase_=768		,			UpperCAmelCase_=12		,			UpperCAmelCase_=12		,			UpperCAmelCase_=3072		,			UpperCAmelCase_=2		,			UpperCAmelCase_=512		,			UpperCAmelCase_=256		,			UpperCAmelCase_=True		,			UpperCAmelCase_=True		,			UpperCAmelCase_=("p2c", "c2p")		,			UpperCAmelCase_="layer_norm"		,			UpperCAmelCase_="gelu_python"		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=0.0		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=0.02		,			UpperCAmelCase_=1E-7		,			UpperCAmelCase_=1E-5		,			UpperCAmelCase_="group"		,			UpperCAmelCase_="gelu"		,			UpperCAmelCase_=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)		,			UpperCAmelCase_=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)		,			UpperCAmelCase_=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)		,			UpperCAmelCase_=False		,			UpperCAmelCase_=128		,			UpperCAmelCase_=16		,			UpperCAmelCase_=True		,			UpperCAmelCase_=0.05		,			UpperCAmelCase_=10		,			UpperCAmelCase_=2		,			UpperCAmelCase_=0.0		,			UpperCAmelCase_=10		,			UpperCAmelCase_=0		,			UpperCAmelCase_="mean"		,			UpperCAmelCase_=False		,			UpperCAmelCase_=False		,			UpperCAmelCase_=256		,			UpperCAmelCase_=0		,			UpperCAmelCase_=1		,			UpperCAmelCase_=2		,			**UpperCAmelCase_		,			)		->							Optional[Any]:
			super().__init__(**UpperCAmelCase_		,			pad_token_id=UpperCAmelCase_		,			bos_token_id=UpperCAmelCase_		,			eos_token_id=UpperCAmelCase_ )
			lowerCamelCase					:      Any          =   hidden_size
			lowerCamelCase					:      Any          =   feat_extract_norm
			lowerCamelCase					:      List[str]          =   feat_extract_activation
			lowerCamelCase					:      str          =   list(UpperCAmelCase_ )
			lowerCamelCase					:      Any          =   list(UpperCAmelCase_ )
			lowerCamelCase					:      str          =   list(UpperCAmelCase_ )
			lowerCamelCase					:      List[Any]          =   conv_bias
			lowerCamelCase					:      Optional[int]          =   num_conv_pos_embeddings
			lowerCamelCase					:      str          =   num_conv_pos_embedding_groups
			lowerCamelCase					:      Optional[int]          =   len(self.conv_dim )
			lowerCamelCase					:      Optional[int]          =   num_hidden_layers
			lowerCamelCase					:      Union[str, Any]          =   intermediate_size
			lowerCamelCase					:      str          =   squeeze_factor
			lowerCamelCase					:      Any          =   max_position_embeddings
			lowerCamelCase					:      List[Any]          =   position_buckets
			lowerCamelCase					:      Union[str, Any]          =   share_att_key
			lowerCamelCase					:      Optional[int]          =   relative_attention
			lowerCamelCase					:      Tuple          =   norm_rel_ebd
			lowerCamelCase					:      Union[str, Any]          =   list(UpperCAmelCase_ )
			lowerCamelCase					:      List[Any]          =   hidden_act
			lowerCamelCase					:      Optional[Any]          =   num_attention_heads
			lowerCamelCase					:      Tuple          =   hidden_dropout
			lowerCamelCase					:      List[Any]          =   attention_dropout
			lowerCamelCase					:      Optional[Any]          =   activation_dropout
			lowerCamelCase					:      List[str]          =   feat_proj_dropout
			lowerCamelCase					:      List[str]          =   final_dropout
			lowerCamelCase					:      str          =   layer_norm_eps
			lowerCamelCase					:      int          =   feature_layer_norm_eps
			lowerCamelCase					:      Optional[Any]          =   initializer_range
			lowerCamelCase					:      int          =   vocab_size

			if (
			    (len(self.conv_stride ) != self.num_feat_extract_layers)
			    or (len(self.conv_kernel ) != self.num_feat_extract_layers)
			    or (len(self.conv_dim ) != self.num_feat_extract_layers)
			):
					raise ValueError(
					    'Configuration for convolutional layers is incorrect.'
					    'It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,'
					    F"""but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)"""
					    F"""= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" )

			# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
			lowerCamelCase					:      Any          =   apply_spec_augment
			lowerCamelCase					:      Optional[int]          =   mask_time_prob
			lowerCamelCase					:      Optional[Any]          =   mask_time_length
			lowerCamelCase					:      str          =   mask_time_min_masks
			lowerCamelCase					:      List[Any]          =   mask_feature_prob
			lowerCamelCase					:      int          =   mask_feature_length
			lowerCamelCase					:      List[Any]          =   mask_feature_min_masks

			# ctc loss
			lowerCamelCase					:      Optional[Any]          =   ctc_loss_reduction
			lowerCamelCase					:      Union[str, Any]          =   ctc_zero_infinity

			# sequence classification
			lowerCamelCase					:      Optional[Any]          =   use_weighted_layer_sum
			lowerCamelCase					:      Dict          =   classifier_proj_size




	@property
	def       _UpperCamelCase   (			self )		->							Optional[Any]:
			return functools.reduce(operator.mul		,			self.conv_stride		,			1 )






