



'''simple docstring'''







import jax
import jax.numpy as jnp
from bigbird_flax import FlaxBigBirdForNaturalQuestions
from datasets import load_from_disk

from transformers import BigBirdTokenizerFast


_UpperCamelCase  						=			{0: """null""", 1: """short""", 2: """long""", 3: """yes""", 4: """no"""}
_UpperCamelCase  						=			set("""""".join(["""‘""", """’""", """´""", """`""", """.""", """,""", """-""", """\""""]))
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Dict					, UpperCAmelCase_      :	List[Any]=0					, UpperCAmelCase_      :	List[str]=None						) ->							List[str]:
							return [" ".join(x.split(""" """						)[begin:end]						) for x in answers if len(x.split(""" """						)						) > 1]
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Tuple					, UpperCAmelCase_      :	Optional[int]=False						) ->							Any:






							if make_sub_answers:
														# if answers are longer than one word, make sure a predictions is correct if it coresponds to the complete 1: or :-1 sub word
														# *e.g.* if the correct answer contains a prefix such as "the", or "a"
														lowerCAmelCase__  =   (
														    given_answers + get_sub_answers(a__					, begin=1						) + get_sub_answers(a__					, end=-1						)
														)
							lowerCAmelCase__  =   []
							for answer in given_answers:
														lowerCAmelCase__  =   answer.replace("""_"""					, """ """						).lower()
														lowerCAmelCase__  =   """""".join(c if c not in PUNCTUATION_SET_TO_EXCLUDE else """ """ for c in alias						)
														answers.append(""" """.join(alias.split()						).strip()						)
							return set(a__						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Union[str, Any]					, UpperCAmelCase_      :	Union[str, Any]					, UpperCAmelCase_      :	int=1					, UpperCAmelCase_      :	Optional[Any]=100						) ->							Optional[int]:
							lowerCAmelCase__  ,lowerCAmelCase__  =   jax.lax.top_k(a__					, a__						)
							lowerCAmelCase__  ,lowerCAmelCase__  =   jax.lax.top_k(a__					, a__						)

							lowerCAmelCase__  =   best_end_idx[:, None] - best_start_idx[None, :]
							lowerCAmelCase__  =   jnp.logical_or(widths < 0					, widths > max_size						)
							lowerCAmelCase__  =   (best_end_scores[:, None] + best_start_scores[None, :]) - (1E8 * mask)
							lowerCAmelCase__  =   jnp.argmax(a__						).item()

							return best_start_idx[best_score % top_k], best_end_idx[best_score // top_k]
def 			_lowerCAmelCase(      UpperCAmelCase_      :	str						) ->							str:
							lowerCAmelCase__  =   sample["""question"""]["""text"""]
							lowerCAmelCase__  =   sample["""document"""]["""tokens"""]["""token"""]
							lowerCAmelCase__  =   sample["""document"""]["""tokens"""]["""is_html"""]
							lowerCAmelCase__  =   sample["""annotations"""]["""long_answer"""]
							lowerCAmelCase__  =   sample["""annotations"""]["""short_answers"""]

							lowerCAmelCase__  =   """ """.join([context[i] for i in range(len(a__						)						) if not is_html[i]]						)

							# 0 - No ; 1 - Yes
							for answer in sample["annotations"]["yes_no_answer"]:
														if answer == 0 or answer == 1:
																					return {
																					    "question": question,
																					    "context": context_string,
																					    "short": [],
																					    "long": [],
																					    "category": "no" if answer == 0 else "yes",
																					}

							lowerCAmelCase__  =   []
							for s in short_answers:
														short_targets.extend(s["""text"""]						)
							lowerCAmelCase__  =   list(set(a__						)						)

							lowerCAmelCase__  =   []
							for s in long_answers:
														if s["start_token"] == -1:
																					continue
														lowerCAmelCase__  =   context[s["""start_token"""] : s["""end_token"""]]
														lowerCAmelCase__  =   is_html[s["""start_token"""] : s["""end_token"""]]
														lowerCAmelCase__  =   """ """.join([answer[i] for i in range(len(a__						)						) if not html[i]]						)
														if new_answer not in long_targets:
																					long_targets.append(a__						)

							lowerCAmelCase__  =   """long_short""" if len(short_targets + long_targets						) > 0 else """null"""

							return {
							    "question": question,
							    "context": context_string,
							    "short": short_targets,
							    "long": long_targets,
							    "category": category,
							}
def 			_lowerCAmelCase(      ) ->							str:
							lowerCAmelCase__  =   load_from_disk("""natural-questions-validation"""						)
							lowerCAmelCase__  =   dataset.map(a__						).remove_columns(["""annotations""", """document""", """id"""]						)
							print(a__						)

							lowerCAmelCase__  =   dataset.filter(lambda UpperCAmelCase_						: (len(x["""question"""]						) + len(x["""context"""]						)) < 4 * 4096						)
							lowerCAmelCase__  =   short_validation_dataset.filter(lambda UpperCAmelCase_						: x["category"] != "null"						)
							short_validation_dataset

							lowerCAmelCase__  =   """vasudevgupta/flax-bigbird-natural-questions"""
							lowerCAmelCase__  =   FlaxBigBirdForNaturalQuestions.from_pretrained(a__						)
							lowerCAmelCase__  =   BigBirdTokenizerFast.from_pretrained(a__						)

							@jax.jit
							def forward(*UpperCAmelCase_      :	Dict					, **UpperCAmelCase_      :	Optional[Any]						):
														lowerCAmelCase__  ,lowerCAmelCase__  ,lowerCAmelCase__  =   model(*a__					, **a__						)
														return start_logits, end_logits, jnp.argmax(a__					, axis=-1						)

							def evaluate(UpperCAmelCase_      :	Optional[int]						):
														# encode question and context so that they are separated by a tokenizer.sep_token and cut at max_length
														lowerCAmelCase__  =   tokenizer(
														    example["""question"""]					, example["""context"""]					, return_tensors="""np"""					, max_length=4096					, padding="""max_length"""					, truncation=a__					, )

														lowerCAmelCase__  ,lowerCAmelCase__  ,lowerCAmelCase__  =   forward(**a__						)

														lowerCAmelCase__  =   CATEGORY_MAPPING[category.item()]

														lowerCAmelCase__  =   example["""long"""] + example["""short"""]
														if example["category"] in ["yes", "no", "null"]:
																					lowerCAmelCase__  =   [example["""category"""]]
														lowerCAmelCase__  =   example["""category"""] != """null"""
														# Now target can be: "yes", "no", "null", "list of long & short answers"

														if predicted_category in ["yes", "no", "null"]:
																					lowerCAmelCase__  =   [predicted_category]
																					lowerCAmelCase__  =   example["""output"""] == example["""targets"""]
																					lowerCAmelCase__  =   predicted_category != """null"""
																					return example

														lowerCAmelCase__  =   38 if predicted_category == """short""" else 1024
														lowerCAmelCase__  ,lowerCAmelCase__  =   get_best_valid_start_end_idx(
														    start_scores[0]					, end_scores[0]					, top_k=8					, max_size=a__						)

														lowerCAmelCase__  =   inputs["""input_ids"""][0].tolist()
														lowerCAmelCase__  =   [tokenizer.decode(input_ids[start_score : end_score + 1]						)]

														lowerCAmelCase__  =   expand_to_aliases(example["""targets"""]					, make_sub_answers=a__						)
														lowerCAmelCase__  =   expand_to_aliases(example["""output"""]						)

														# some preprocessing to both prediction and answer
														lowerCAmelCase__  =   {"""""".join(a.split()						) for a in answers}
														lowerCAmelCase__  =   {"""""".join(p.split()						) for p in predictions}
														lowerCAmelCase__  =   {s for s in predictions if s not in ["""``""", """\'\'""", """`""", """\'"""]}

														# if there is a common element, it's a exact match
														lowerCAmelCase__  =   len(list(answers & predictions						)						) > 0
														lowerCAmelCase__  =   predicted_category != """null""" and len(a__						) > 0

														return example

							lowerCAmelCase__  =   short_validation_dataset.map(a__						)

							lowerCAmelCase__  =   len(a__						)
							lowerCAmelCase__  =   len(short_validation_dataset.filter(lambda UpperCAmelCase_						: x["match"] == 1						)						)
							print("""EM score:"""					, (matched / total) * 100					, """%"""						)


if __name__ == "__main__":
		main()






'''simple docstring'''

from itertools import compress, repeat
from math import ceil, sqrt



def 				UpperCamelCase__      (      a__   ):


						'''simple docstring'''






						if num <= 2:
												return []
						if num == 3:
												return [2]

						# Odd sieve for numbers in range [3, num - 1]
						_lowerCAmelCase					    =bytearray(B'\x01'   ) * ((num >> 1) - 1)

						for i in range(3      ,   int(sqrt(a__   )   ) + 1      ,   2   ):
												if sieve[(i >> 1) - 1]:
																		_lowerCAmelCase					    =i**2
																		_lowerCAmelCase					    =repeat(
																		    0      ,   ceil((num - i_squared) / (i << 1)   )   )

						return [2] + list(compress(range(3      ,   a__      ,   2   )      ,   a__   )   )


if __name__ == "__main__":
							import doctest

							doctest.testmod()





