




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

from transformers import BigBirdTokenizerFast


__lowerCAmelCase						:							List[Any]			     =						{0: 'null', 1: 'short', 2: 'long', 3: 'yes', 4: 'no'}
__lowerCAmelCase						:							List[str]			     =						set(''.join(['‘', '’', '´', '`', '.', ',', '-', '"']))




def      __magic_name__							(					A						:   Union[str, Any],    A						:   str=0,    A						:   str=None  ):






			'''simple docstring'''







			return [" ".join(x.split(" "  )[begin:end]  ) for x in answers if len(x.split(" "  )  ) > 1]




def      __magic_name__							(					A						:   Union[str, Any],    A						:   str=False  ):






			'''simple docstring'''







			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"
						a       =     (
						    given_answers + get_sub_answers(A,    begin=1  ) + get_sub_answers(A,    end=-1  )
						)
			a       =     []
			for answer in given_answers:
						a       =     answer.replace("_",    " "  ).lower()
						a       =     "".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      __magic_name__							(					A						:   Union[str, Any],    A						:   Any,    A						:   Tuple=1,    A						:   List[str]=100  ):






			'''simple docstring'''







			a       ,						a       =     jax.lax.top_k(A,    A  )
			a       ,						a       =     jax.lax.top_k(A,    A  )

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

			return best_start_idx[best_score % top_k], best_end_idx[best_score // top_k]




def      __magic_name__							(					A						:   Optional[int]  ):






			'''simple docstring'''







			a       =     sample["question"]["text"]
			a       =     sample["document"]["tokens"]["token"]
			a       =     sample["document"]["tokens"]["is_html"]
			a       =     sample["annotations"]["long_answer"]
			a       =     sample["annotations"]["short_answers"]

			a       =     " ".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",
									}

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

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

			a       =     "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      __magic_name__							(					):






			'''simple docstring'''







			a       =     load_from_disk("natural-questions-validation"  )
			a       =     dataset.map(A  ).remove_columns(["annotations", "document", "id"]  )
			print(A  )

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

			a       =     "vasudevgupta/flax-bigbird-natural-questions"
			a       =     FlaxBigBirdForNaturalQuestions.from_pretrained(A  )
			a       =     BigBirdTokenizerFast.from_pretrained(A  )

			@jax.jit
			def forward(*A						:   Any,    **A						:   Tuple  ):
						a       ,						a       ,						a       =     model(*A,    **A  )
						return start_logits, end_logits, jnp.argmax(A,    axis=-1  )

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

						a       ,						a       ,						a       =     forward(**A  )

						a       =     CATEGORY_MAPPING[category.item()]

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

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

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

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

						a       =     expand_to_aliases(example["targets"],    make_sub_answers=A  )
						a       =     expand_to_aliases(example["output"]  )

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

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

						return example

			a       =     short_validation_dataset.map(A  )

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


if __name__ == "__main__":
		main()












import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def      __magic_name__							(					A						:   str,    A						:   List[str],    A						:   int  ):






			'''simple docstring'''







			a       ,						a       ,						a       =     pt_weights[F"""{prefix}.in_proj_weight"""].chunk(3,    dim=0  )
			a       ,						a       ,						a       =     pt_weights[F"""{prefix}.in_proj_bias"""].chunk(3,    dim=0  )

			a       =     pt_weights[F"""{prefix}.out_proj.weight"""]
			a       =     pt_weights[F"""{prefix}.out_proj.bias"""]

			a       =     q_proj
			a       =     q_proj_bias

			a       =     k_proj
			a       =     k_proj_bias

			a       =     v_proj
			a       =     v_proj_bias

			a       =     out_proj_weights
			a       =     out_proj_bias




def      __magic_name__							(					A						:   Optional[int],    A						:   str,    A						:   Union[str, Any]  ):






			'''simple docstring'''







			copy_linear(hf_mlp.fca,    A,    F"""{prefix}.c_fc"""  )
			copy_linear(hf_mlp.fca,    A,    F"""{prefix}.c_proj"""  )




def      __magic_name__							(					A						:   int,    A						:   Tuple,    A						:   Optional[int]  ):






			'''simple docstring'''







			a       =     pt_weights[F"""{prefix}.weight"""].data
			a       =     pt_weights[F"""{prefix}.bias"""].data




def      __magic_name__							(					A						:   List[Any],    A						:   List[str],    A						:   Any  ):






			'''simple docstring'''







			copy_linear(hf_layer.layer_norma,    A,    F"""{prefix}.ln_1"""  )
			copy_linear(hf_layer.layer_norma,    A,    F"""{prefix}.ln_2"""  )

			# copy MLP
			copy_mlp(hf_layer.mlp,    A,    F"""{prefix}.mlp"""  )

			# copy attn
			copy_attn_layer(hf_layer.self_attn,    A,    F"""{prefix}.attn"""  )




def      __magic_name__							(					A						:   int,    A						:   List[str],    A						:   Any  ):






			'''simple docstring'''







			for layer_id, hf_layer in enumerate(A  ):
						copy_layer(A,    A,    F"""{prefix}.{layer_id}"""  )




def      __magic_name__							(					A						:   Any,    A						:   Any  ):






			'''simple docstring'''







			a       =     pt_weights["text_projection"].data.T

			# copy text encoder
			for name, param in hf_model.text_model.named_parameters():
						a       =     pt_weights[F"""bert.{name}"""].data




def      __magic_name__							(					A						:   Union[str, Any],    A						:   Optional[Any]  ):






			'''simple docstring'''







			a       =     pt_weights["visual.proj"].data.T

			# copy layer norms
			copy_linear(hf_model.vision_model.pre_layrnorm,    A,    "visual.ln_pre"  )
			copy_linear(hf_model.vision_model.post_layernorm,    A,    "visual.ln_post"  )

			# copy embeddings
			a       =     pt_weights["visual.conv1.weight"].data
			a       =     pt_weights["visual.class_embedding"].data
			a       =     pt_weights["visual.positional_embedding"].data

			# copy encoder
			copy_layers(hf_model.vision_model.encoder.layers,    A,    "visual.transformer.resblocks"  )




@torch.no_grad()
def      __magic_name__							(					A						:   Optional[Any],    A						:   List[str],    A						:   Dict=None  ):






			'''simple docstring'''







			assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
			a       =     ChineseCLIPConfig.from_pretrained(A  )

			a       =     ChineseCLIPModel(A  ).eval()

			a       =     torch.load(A,    map_location="cpu"  )["state_dict"]
			a       =     {(name[7:] if name.startswith("module."  ) else name): value for name, value in pt_weights.items()}

			copy_text_model_and_projection(A,    A  )
			copy_vision_model_and_projection(A,    A  )
			a       =     pt_weights["logit_scale"].data

			hf_model.save_pretrained(A  )


if __name__ == "__main__":
		__lowerCAmelCase						:							Dict			     =						argparse.ArgumentParser()
		parser.add_argument(
		    '--pytorch_dump_folder_path',
		    default=None,
		    type=str,
		    help='Path to the output folder storing converted hf PyTorch model.',
		)
		parser.add_argument(
		    '--checkpoint_path', default=None, type=str, help='Path to original github format ChineseCLIP checkpoint.'
		)
		parser.add_argument(
		    '--config_path', default=None, required=True, type=str, help='Path to hf config.json of model to convert.'
		)
		__lowerCAmelCase						:							Dict			     =						parser.parse_args()

		convert_chinese_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
		print('The conversion is finished!')






