




'''simple docstring'''







import argparse
import collections

import jax
import jax.numpy as jnp
import torch
import torch.nn as nn
from clip.model import CLIP
from flax.training import checkpoints
from huggingface_hub import Repository

from transformers import (
    CLIPTokenizer,
    OwlViTConfig,
    OwlViTForObjectDetection,
    OwlViTImageProcessor,
    OwlViTModel,
    OwlViTProcessor,
)


_lowerCamelCase				:		List[str]				      =    {
    'vit_b32': {
        'embed_dim': 512,
        'image_resolution': 768,
        'context_length': 16,
        'vocab_size': 49_408,
        'vision_layers': 12,
        'vision_width': 768,
        'vision_patch_size': 32,
        'transformer_width': 512,
        'transformer_heads': 8,
        'transformer_layers': 12,
    },
    'vit_b16': {
        'embed_dim': 512,
        'image_resolution': 768,
        'context_length': 16,
        'vocab_size': 49_408,
        'vision_layers': 12,
        'vision_width': 768,
        'vision_patch_size': 16,
        'transformer_width': 512,
        'transformer_heads': 8,
        'transformer_layers': 12,
    },
    'vit_l14': {
        'embed_dim': 768,
        'image_resolution': 840,
        'context_length': 16,
        'vocab_size': 49_408,
        'vision_layers': 24,
        'vision_width': 1_024,
        'vision_patch_size': 14,
        'transformer_width': 768,
        'transformer_heads': 12,
        'transformer_layers': 12,
    },
}






def 						_lowercase						(      lowercase_		,				lowercase_=""		,				lowercase_="/"    ):



		'''simple docstring'''







		_snake_case      			=			[]

		for k, v in params.items():
				_snake_case      			=			parent_key + sep + k if parent_key else k

				if isinstance(lowercase_		,				collections.MutableMapping    ):
						items.extend(flatten_nested_dict(lowercase_		,				lowercase_		,				sep=lowercase_    ).items()    )
				else:
						items.append((new_key, v)    )
		return dict(lowercase_    )






def 						_lowercase						(      lowercase_    ):



		'''simple docstring'''







		return jax.tree_util.tree_map(lambda lowercase_    : x.astype(jnp.floataa    ) if x.dtype == jnp.bfloataa else x		,				lowercase_    )






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case			,       _snake_case			,       _snake_case      			=			pt_attn_layer.in_proj_weight.chunk(3		,				dim=0    )
		_snake_case			,       _snake_case			,       _snake_case      			=			pt_attn_layer.in_proj_bias.chunk(3		,				dim=0    )

		_snake_case      			=			pt_attn_layer.out_proj.weight
		_snake_case      			=			pt_attn_layer.out_proj.bias

		_snake_case      			=			q_proj
		_snake_case      			=			q_proj_bias

		_snake_case      			=			k_proj
		_snake_case      			=			k_proj_bias

		_snake_case      			=			v_proj
		_snake_case      			=			v_proj_bias

		_snake_case      			=			out_proj_weights
		_snake_case      			=			out_proj_bias






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		copy_linear(hf_mlp.fca		,				pt_mlp.c_fc    )
		copy_linear(hf_mlp.fca		,				pt_mlp.c_proj    )






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			pt_linear.weight
		_snake_case      			=			pt_linear.bias






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		copy_linear(hf_layer.layer_norma		,				pt_layer.ln_a    )
		copy_linear(hf_layer.layer_norma		,				pt_layer.ln_a    )

		# copy MLP
		copy_mlp(hf_layer.mlp		,				pt_layer.mlp    )

		# copy attn
		copy_attn_layer(hf_layer.self_attn		,				pt_layer.attn    )






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		for hf_layer, pt_layer in zip(lowercase_		,				lowercase_    ):
				copy_layer(lowercase_		,				lowercase_    )






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			pt_model.token_embedding.weight
		_snake_case      			=			pt_model.positional_embedding

		# copy layer norm
		copy_linear(hf_encoder.final_layer_norm		,				pt_model.ln_final    )

		# copy hidden layers
		copy_layers(hf_encoder.encoder.layers		,				pt_model.transformer.resblocks    )






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			pt_model.text_projection.data.T

		# copy text encoder
		copy_encoder(hf_model.text_model		,				lowercase_    )






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			pt_model.visual.proj.data.T

		# copy layer norms
		copy_linear(hf_model.vision_model.pre_layernorm		,				pt_model.visual.ln_pre    )
		copy_linear(hf_model.vision_model.post_layernorm		,				pt_model.visual.ln_post    )

		# copy embeds
		_snake_case      			=			pt_model.visual.conva.weight.data
		_snake_case      			=			pt_model.visual.class_embedding
		_snake_case      			=			pt_model.visual.positional_embedding.data

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






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			flatten_nested_dict(flax_params["backbone"]["merged_class_token"]    )

		_snake_case      			=			torch.from_numpy(flax_class_token_params["scale"]    )
		_snake_case      			=			torch.from_numpy(flax_class_token_params["bias"]    )
		_snake_case      			=			nn.Parameter(lowercase_    )
		_snake_case      			=			nn.Parameter(lowercase_    )






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			hf_model.state_dict()
		_snake_case      			=			{}

		# Rename class prediction head flax params to pytorch HF
		_snake_case      			=			flatten_nested_dict(flax_params["class_head"]    )

		for flax_key, v in flax_class_params.items():
				_snake_case      			=			flax_key.replace("/"		,				"."    )
				_snake_case      			=			torch_key.replace(".kernel"		,				".weight"    )
				_snake_case      			=			torch_key.replace("Dense_0"		,				"dense0"    )
				_snake_case      			=			"class_head." + torch_key

				if "weight" in torch_key and v.ndim == 2:
						_snake_case      			=			v.T

				_snake_case      			=			nn.Parameter(torch.from_numpy(lowercase_    )    )

		# Rename box prediction box flax params to pytorch HF
		_snake_case      			=			flatten_nested_dict(flax_params["obj_box_head"]    )

		for flax_key, v in flax_box_params.items():
				_snake_case      			=			flax_key.replace("/"		,				"."    )
				_snake_case      			=			torch_key.replace(".kernel"		,				".weight"    )
				_snake_case      			=			torch_key.replace("_"		,				""    ).lower()
				_snake_case      			=			"box_head." + torch_key

				if "weight" in torch_key and v.ndim == 2:
						_snake_case      			=			v.T

				_snake_case      			=			nn.Parameter(torch.from_numpy(lowercase_    )    )

		# Copy flax params to PyTorch params
		for name, param in new_params.items():
				if name in pt_params.keys():
						pt_params[name].copy_(lowercase_    )






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		for k, v in flax_attn_params.items():
				if k.startswith("transformer"    ):
						_snake_case      			=			k.replace("transformer.resblocks"		,				"text_model.encoder.layers"    )
				else:
						_snake_case      			=			k.replace("visual.transformer.resblocks"		,				"vision_model.encoder.layers"    )

				_snake_case      			=			torch_key.replace("attn"		,				"self_attn"    )
				_snake_case      			=			torch_key.replace("key"		,				"k_proj"    )
				_snake_case      			=			torch_key.replace("value"		,				"v_proj"    )
				_snake_case      			=			torch_key.replace("query"		,				"q_proj"    )
				_snake_case      			=			torch_key.replace("out"		,				"out_proj"    )

				if "bias" in torch_key and v.ndim == 2:
						_snake_case      			=			v.shape[0] * v.shape[1]
						_snake_case      			=			v.reshape(lowercase_    )

				if "weight" in torch_key and "out" in torch_key:
						_snake_case      			=			(v.shape[0] * v.shape[1], v.shape[2])
						_snake_case      			=			v.reshape(lowercase_    ).T

				if "weight" in torch_key and "out" not in torch_key:
						_snake_case      			=			(v.shape[0], v.shape[1] * v.shape[2])
						_snake_case      			=			v.reshape(lowercase_    ).T

				# Copy flax CLIP attn params to HF PyTorch params
				_snake_case      			=			torch.from_numpy(lowercase_    )
				hf_backbone.state_dict()[torch_key].copy_(lowercase_    )






def 						_lowercase						(      lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			{}
		_snake_case      			=			[]

		for k, v in params.items():
				if "attn." in k:
						_snake_case      			=			k[: k.rindex("attn."    ) + 5]
						if base in processed_attn_layers:
								continue

						processed_attn_layers.append(lowercase_    )
						_snake_case      			=			params[base + "out.weight"].shape[-1]
						_snake_case      			=			params[base + "out.weight"].reshape(lowercase_		,				lowercase_    ).T
						_snake_case      			=			params[base + "out.bias"]
				else:
						_snake_case      			=			v
		return new_params






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			CLIP(**lowercase_    )
		torch_model.eval()
		_snake_case      			=			torch_model.state_dict()

		_snake_case      			=			flatten_nested_dict(flax_params["backbone"]["clip"]    )
		_snake_case      			=			{}

		for flax_key, v in flax_clip_params.items():
				_snake_case      			=			flax_key.replace("/"		,				"."    )
				_snake_case      			=			torch_key.replace("text.token_embedding.embedding"		,				"token_embedding.kernel"    )

				if (
				    torch_key.startswith("text.transformer"    )
				    or torch_key.startswith("text.text_projection"    )
				    or torch_key.startswith("text.ln_final"    )
				    or torch_key.startswith("text.positional_embedding"    )
				):
						_snake_case      			=			torch_key[5:]

				_snake_case      			=			torch_key.replace("text_projection.kernel"		,				"text_projection"    )
				_snake_case      			=			torch_key.replace("visual.proj.kernel"		,				"visual.proj"    )
				_snake_case      			=			torch_key.replace(".scale"		,				".weight"    )
				_snake_case      			=			torch_key.replace(".kernel"		,				".weight"    )

				if "conv" in torch_key or "downsample.0.weight" in torch_key:
						_snake_case      			=			v.transpose(3		,				2		,				0		,				1    )

				elif "weight" in torch_key and v.ndim == 2 and "embedding" not in torch_key:
						# Fully connected layers are transposed, embeddings are not
						_snake_case      			=			v.T

				_snake_case      			=			v

		_snake_case      			=			_convert_attn_layers(lowercase_    )
		new_torch_params.update(lowercase_    )
		_snake_case      			=			{}

		# Copy flax CLIP backbone params to PyTorch params
		for name, param in new_torch_params.items():
				if name in torch_clip_params.keys():
						_snake_case      			=			torch.from_numpy(new_torch_params[name]    )
						torch_clip_params[name].copy_(lowercase_    )
				else:
						_snake_case      			=			param

		return torch_clip_params, torch_model, attn_params






@torch.no_grad()
def 						_lowercase						(      lowercase_		,				lowercase_		,				lowercase_		,				lowercase_		,				lowercase_=None    ):



		'''simple docstring'''







		_snake_case      			=			Repository(lowercase_		,				clone_from=f'''google/{pytorch_dump_folder_path}'''    )
		repo.git_pull()

		if config_path is not None:
				_snake_case      			=			OwlViTConfig.from_pretrained(lowercase_    )
		else:
				_snake_case      			=			OwlViTConfig()

		_snake_case      			=			OwlViTModel(lowercase_    ).eval()
		_snake_case      			=			OwlViTForObjectDetection(lowercase_    ).eval()

		copy_text_model_and_projection(lowercase_		,				lowercase_    )
		copy_vision_model_and_projection(lowercase_		,				lowercase_    )
		_snake_case      			=			pt_backbone.logit_scale
		copy_flax_attn_params(lowercase_		,				lowercase_    )

		_snake_case      			=			hf_backbone
		copy_class_merge_token(lowercase_		,				lowercase_    )
		copy_class_box_heads(lowercase_		,				lowercase_    )

		# Save HF model
		hf_model.save_pretrained(repo.local_dir    )

		# Initialize image processor
		_snake_case      			=			OwlViTImageProcessor(
		    size=config.vision_config.image_size		,				crop_size=config.vision_config.image_size    )
		# Initialize tokenizer
		_snake_case      			=			CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32"		,				pad_token="!"		,				model_max_length=16    )

		# Initialize processor
		_snake_case      			=			OwlViTProcessor(image_processor=lowercase_		,				tokenizer=lowercase_    )
		image_processor.save_pretrained(repo.local_dir    )
		processor.save_pretrained(repo.local_dir    )

		repo.git_add()
		repo.git_commit("Upload model and processor"    )
		repo.git_push()


if __name__ == "__main__":
						_lowerCamelCase				:		Tuple				      =    argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    '--owlvit_version',
						    default=None,
						    type=str,
						    required=True,
						    help='OWL-ViT model name [clip_b16, clip_b32, clip_l14].',
						)
						parser.add_argument(
						    '--owlvit_checkpoint', default=None, type=str, required=True, help='Path to flax model checkpoint.'
						)
						parser.add_argument('--hf_config', default=None, type=str, required=True, help='Path to HF model config.')
						parser.add_argument(
						    '--pytorch_dump_folder_path', default='hf_model', type=str, help='Path to the output PyTorch model.'
						)
						_lowerCamelCase				:		List[str]				      =    parser.parse_args()

						# Initialize PyToch clip model
						_lowerCamelCase				:		Optional[Any]				      =    args.owlvit_version
						if model_name == "clip_b16":
												_lowerCamelCase				:		List[Any]				      =    CONFIGS['vit_b16']
						elif model_name == "clip_b32":
												_lowerCamelCase				:		Optional[Any]				      =    CONFIGS['vit_b32']
						elif model_name == "clip_l14":
												_lowerCamelCase				:		List[str]				      =    CONFIGS['vit_l14']

						# Load from checkpoint and convert params to float-32
						_lowerCamelCase				:		Any				      =    checkpoints.restore_checkpoint(args.owlvit_checkpoint, target=None)['optimizer']['target']
						_lowerCamelCase				:		Dict				      =    jax.tree_util.tree_map(lambda x: x.astype(jnp.floataa) if x.dtype == jnp.bfloataa else x, variables)
						del variables

						# Convert CLIP backbone
						_lowerCamelCase      ,_lowerCamelCase      ,_lowerCamelCase				:		Optional[int]				      =    convert_clip_backbone(flax_params, torch_config)

						convert_owlvit_checkpoint(clip_pt, flax_params, attn_params, args.pytorch_dump_folder_path, args.hf_config)











'''simple docstring'''







import datasets

from .evaluate import (
    apply_no_ans_threshold,
    find_all_best_thresh,
    get_raw_scores,
    make_eval_dict,
    make_qid_to_has_ans,
    merge_eval,
)


_lowerCamelCase				:		str				      =    '\\n@inproceedings{Rajpurkar2016SQuAD10,\n  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n  booktitle={EMNLP},\n  year={2016}\n}\n'

_lowerCamelCase				:		Union[str, Any]				      =    '\nThis metric wrap the official scoring script for version 2 of the Stanford Question\nAnswering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n\nSQuAD2.0 combines the 100,000 questions in SQuAD1.1 with over 50,000 unanswerable questions\nwritten adversarially by crowdworkers to look similar to answerable ones.\nTo do well on SQuAD2.0, systems must not only answer questions when possible, but also\ndetermine when no answer is supported by the paragraph and abstain from answering.\n'

_lowerCamelCase				:		List[Any]				      =    '\nComputes SQuAD v2 scores (F1 and EM).\nArgs:\n    predictions: List of triple for question-answers to score with the following elements:\n        - the question-answer \'id\' field as given in the references (see below)\n        - the text of the answer\n        - the probability that the question has no answer\n    references: List of question-answers dictionaries with the following key-values:\n            - \'id\': id of the question-answer pair (see above),\n            - \'answers\': a list of Dict {\'text\': text of the answer as a string}\n    no_answer_threshold: float\n        Probability threshold to decide that a question has no answer.\nReturns:\n    \'exact\': Exact match (the normalized answer exactly match the gold answer)\n    \'f1\': The F-score of predicted tokens versus the gold answer\n    \'total\': Number of score considered\n    \'HasAns_exact\': Exact match (the normalized answer exactly match the gold answer)\n    \'HasAns_f1\': The F-score of predicted tokens versus the gold answer\n    \'HasAns_total\': Number of score considered\n    \'NoAns_exact\': Exact match (the normalized answer exactly match the gold answer)\n    \'NoAns_f1\': The F-score of predicted tokens versus the gold answer\n    \'NoAns_total\': Number of score considered\n    \'best_exact\': Best exact match (with varying threshold)\n    \'best_exact_thresh\': No-answer probability threshold associated to the best exact match\n    \'best_f1\': Best F1 (with varying threshold)\n    \'best_f1_thresh\': No-answer probability threshold associated to the best F1\nExamples:\n\n    >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\', \'no_answer_probability\': 0.}]\n    >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}]\n    >>> squad_v2_metric = datasets.load_metric("squad_v2")\n    >>> results = squad_v2_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'exact\': 100.0, \'f1\': 100.0, \'total\': 1, \'HasAns_exact\': 100.0, \'HasAns_f1\': 100.0, \'HasAns_total\': 1, \'best_exact\': 100.0, \'best_exact_thresh\': 0.0, \'best_f1\': 100.0, \'best_f1_thresh\': 0.0}\n'





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION,			_KWARGS_DESCRIPTION				)
class 	lowerCamelCase_				(			datasets.Metric				):




					'''simple docstring'''






					def 							__SCREAMING_SNAKE_CASE      (       self					):
							return datasets.MetricInfo(
							    description=_DESCRIPTION,    citation=_CITATION,    inputs_description=_KWARGS_DESCRIPTION,    features=datasets.Features(
							        {
							            "predictions": {
							                "id": datasets.Value("string"					),
							                "prediction_text": datasets.Value("string"					),
							                "no_answer_probability": datasets.Value("float32"					),
							            },
							            "references": {
							                "id": datasets.Value("string"					),
							                "answers": datasets.features.Sequence(
							                    {"text": datasets.Value("string"					), "answer_start": datasets.Value("int32"					)}					),
							            },
							        }					),    codebase_urls=["https://rajpurkar.github.io/SQuAD-explorer/"],    reference_urls=["https://rajpurkar.github.io/SQuAD-explorer/"],    )






					def 							__SCREAMING_SNAKE_CASE      (       self,    __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase=1.0					):
							_snake_case      			=			{p["id"]: p["no_answer_probability"] for p in predictions}
							_snake_case      			=			[{"paragraphs": [{"qas": references}]}]
							_snake_case      			=			{p["id"]: p["prediction_text"] for p in predictions}

							_snake_case      			=			make_qid_to_has_ans(__UpperCamelCase					)  # maps qid to True/False
							_snake_case      			=			[k for k, v in qid_to_has_ans.items() if v]
							_snake_case      			=			[k for k, v in qid_to_has_ans.items() if not v]

							_snake_case			,       _snake_case      			=			get_raw_scores(__UpperCamelCase,    __UpperCamelCase					)
							_snake_case      			=			apply_no_ans_threshold(__UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase					)
							_snake_case      			=			apply_no_ans_threshold(__UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase					)
							_snake_case      			=			make_eval_dict(__UpperCamelCase,    __UpperCamelCase					)

							if has_ans_qids:
									_snake_case      			=			make_eval_dict(__UpperCamelCase,    __UpperCamelCase,    qid_list=__UpperCamelCase					)
									merge_eval(__UpperCamelCase,    __UpperCamelCase,    "HasAns"					)
							if no_ans_qids:
									_snake_case      			=			make_eval_dict(__UpperCamelCase,    __UpperCamelCase,    qid_list=__UpperCamelCase					)
									merge_eval(__UpperCamelCase,    __UpperCamelCase,    "NoAns"					)
							find_all_best_thresh(__UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase					)
							return dict(__UpperCamelCase					)





