

'''simple docstring'''





import copy
import os
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union


if TYPE_CHECKING:
							from ...processing_utils import ProcessorMixin
							from ...utils import TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCamelCase_							=					logging.get_logger(__name__)

UpperCamelCase_							=					{
    """google/owlvit-base-patch32""": """https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json""",
    """google/owlvit-base-patch16""": """https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json""",
    """google/owlvit-large-patch14""": """https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json""",
}





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):
				lowerCamelCase_							=						'owlvit_text_model'







				def __init__(					self    :		Optional[Any]		,				UpperCAmelCase__    :		Optional[int]=49408		,				UpperCAmelCase__    :		int=512		,				UpperCAmelCase__    :		Dict=2048		,				UpperCAmelCase__    :		int=12		,				UpperCAmelCase__    :		Dict=8		,				UpperCAmelCase__    :		Any=16		,				UpperCAmelCase__    :		List[Any]="quick_gelu"		,				UpperCAmelCase__    :		Union[str, Any]=1E-5		,				UpperCAmelCase__    :		List[Any]=0.0		,				UpperCAmelCase__    :		Optional[Any]=0.02		,				UpperCAmelCase__    :		Union[str, Any]=1.0		,				UpperCAmelCase__    :		Any=0		,				UpperCAmelCase__    :		Dict=49406		,				UpperCAmelCase__    :		str=49407		,				**UpperCAmelCase__    :		Tuple		,				):
										'''simple docstring'''
										super().__init__(pad_token_id=UpperCAmelCase__		,				bos_token_id=UpperCAmelCase__		,				eos_token_id=UpperCAmelCase__		,				**UpperCAmelCase__      )

										lowercase   :       Tuple								=vocab_size
										lowercase   :       Optional[Any]								=hidden_size
										lowercase   :       Dict								=intermediate_size
										lowercase   :       Optional[Any]								=num_hidden_layers
										lowercase   :       Union[str, Any]								=num_attention_heads
										lowercase   :       Tuple								=max_position_embeddings
										lowercase   :       Optional[int]								=hidden_act
										lowercase   :       Tuple								=layer_norm_eps
										lowercase   :       List[Any]								=attention_dropout
										lowercase   :       Dict								=initializer_range
										lowercase   :       Tuple								=initializer_factor







				@classmethod
				def    lowerCamelCase_  (					cls    :		Optional[int]		,				UpperCAmelCase__    :		Union[str, os.PathLike]		,				**UpperCAmelCase__    :		List[str]      ):
										'''simple docstring'''
										cls._set_token_in_kwargs(UpperCAmelCase__      )

										lowercase		,  lowercase   :       List[Any]								=cls.get_config_dict(UpperCAmelCase__		,				**UpperCAmelCase__      )

										# get the text config dict if we are loading from OwlViTConfig
										if config_dict.get('''model_type'''      ) == "owlvit":
																lowercase   :       Optional[Any]								=config_dict['''text_config''']

										if "model_type" in config_dict and hasattr(cls		,				'''model_type'''      ) and config_dict["model_type"] != cls.model_type:
																logger.warning(
																    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
																    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''      )

										return cls.from_dict(UpperCAmelCase__		,				**UpperCAmelCase__      )





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):
				lowerCamelCase_							=						'owlvit_vision_model'







				def __init__(					self    :		Tuple		,				UpperCAmelCase__    :		int=768		,				UpperCAmelCase__    :		Tuple=3072		,				UpperCAmelCase__    :		Dict=12		,				UpperCAmelCase__    :		Optional[Any]=12		,				UpperCAmelCase__    :		Any=3		,				UpperCAmelCase__    :		Union[str, Any]=768		,				UpperCAmelCase__    :		int=32		,				UpperCAmelCase__    :		int="quick_gelu"		,				UpperCAmelCase__    :		str=1E-5		,				UpperCAmelCase__    :		Optional[Any]=0.0		,				UpperCAmelCase__    :		Union[str, Any]=0.02		,				UpperCAmelCase__    :		Dict=1.0		,				**UpperCAmelCase__    :		Union[str, Any]		,				):
										'''simple docstring'''
										super().__init__(**UpperCAmelCase__      )

										lowercase   :       Dict								=hidden_size
										lowercase   :       List[Any]								=intermediate_size
										lowercase   :       Optional[int]								=num_hidden_layers
										lowercase   :       Tuple								=num_attention_heads
										lowercase   :       str								=num_channels
										lowercase   :       Tuple								=image_size
										lowercase   :       Optional[Any]								=patch_size
										lowercase   :       List[str]								=hidden_act
										lowercase   :       Any								=layer_norm_eps
										lowercase   :       List[str]								=attention_dropout
										lowercase   :       Union[str, Any]								=initializer_range
										lowercase   :       List[str]								=initializer_factor







				@classmethod
				def    lowerCamelCase_  (					cls    :		Tuple		,				UpperCAmelCase__    :		Union[str, os.PathLike]		,				**UpperCAmelCase__    :		Union[str, Any]      ):
										'''simple docstring'''
										cls._set_token_in_kwargs(UpperCAmelCase__      )

										lowercase		,  lowercase   :       str								=cls.get_config_dict(UpperCAmelCase__		,				**UpperCAmelCase__      )

										# get the vision config dict if we are loading from OwlViTConfig
										if config_dict.get('''model_type'''      ) == "owlvit":
																lowercase   :       Optional[Any]								=config_dict['''vision_config''']

										if "model_type" in config_dict and hasattr(cls		,				'''model_type'''      ) and config_dict["model_type"] != cls.model_type:
																logger.warning(
																    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
																    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''      )

										return cls.from_dict(UpperCAmelCase__		,				**UpperCAmelCase__      )





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):
				lowerCamelCase_							=						'owlvit'
				lowerCamelCase_							=						True







				def __init__(					self    :		str		,				UpperCAmelCase__    :		str=None		,				UpperCAmelCase__    :		Any=None		,				UpperCAmelCase__    :		Union[str, Any]=512		,				UpperCAmelCase__    :		List[Any]=2.65_92		,				UpperCAmelCase__    :		Tuple=True		,				**UpperCAmelCase__    :		Optional[int]		,				):
										'''simple docstring'''
										super().__init__(**UpperCAmelCase__      )

										if text_config is None:
																lowercase   :       Any								={}
																logger.info('''text_config is None. Initializing the OwlViTTextConfig with default values.'''      )

										if vision_config is None:
																lowercase   :       Dict								={}
																logger.info('''vision_config is None. initializing the OwlViTVisionConfig with default values.'''      )

										lowercase   :       Tuple								=OwlViTTextConfig(**UpperCAmelCase__      )
										lowercase   :       Any								=OwlViTVisionConfig(**UpperCAmelCase__      )

										lowercase   :       int								=projection_dim
										lowercase   :       List[str]								=logit_scale_init_value
										lowercase   :       Union[str, Any]								=return_dict
										lowercase   :       int								=1.0







				@classmethod
				def    lowerCamelCase_  (					cls    :		Tuple		,				UpperCAmelCase__    :		Union[str, os.PathLike]		,				**UpperCAmelCase__    :		Tuple      ):
										'''simple docstring'''
										cls._set_token_in_kwargs(UpperCAmelCase__      )

										lowercase		,  lowercase   :       int								=cls.get_config_dict(UpperCAmelCase__		,				**UpperCAmelCase__      )

										if "model_type" in config_dict and hasattr(cls		,				'''model_type'''      ) and config_dict["model_type"] != cls.model_type:
																logger.warning(
																    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
																    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''      )

										return cls.from_dict(UpperCAmelCase__		,				**UpperCAmelCase__      )







				@classmethod
				def    lowerCamelCase_  (					cls    :		str		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		Dict		,				**UpperCAmelCase__    :		Tuple      ):
										'''simple docstring'''
										lowercase   :       Dict								={}
										lowercase   :       Tuple								=text_config
										lowercase   :       Optional[Any]								=vision_config

										return cls.from_dict(UpperCAmelCase__		,				**UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		int      ):
										'''simple docstring'''
										lowercase   :       Optional[int]								=copy.deepcopy(self.__dict__      )
										lowercase   :       List[str]								=self.text_config.to_dict()
										lowercase   :       Optional[Any]								=self.vision_config.to_dict()
										lowercase   :       Tuple								=self.__class__.model_type
										return output





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):







				@property
				def    lowerCamelCase_  (					self    :		Optional[Any]      ):
										'''simple docstring'''
										return OrderedDict(
										    [
										        ('''input_ids''', {0: '''batch''', 1: '''sequence'''}),
										        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
										        ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}),
										    ]      )







				@property
				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										return OrderedDict(
										    [
										        ('''logits_per_image''', {0: '''batch'''}),
										        ('''logits_per_text''', {0: '''batch'''}),
										        ('''text_embeds''', {0: '''batch'''}),
										        ('''image_embeds''', {0: '''batch'''}),
										    ]      )







				@property
				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										return 1E-4







				def    lowerCamelCase_  (					self    :		str		,				UpperCAmelCase__    :		"ProcessorMixin"		,				UpperCAmelCase__    :		int = -1		,				UpperCAmelCase__    :		int = -1		,				UpperCAmelCase__    :		Optional["TensorType"] = None		,				):
										'''simple docstring'''
										lowercase   :       Dict								=super().generate_dummy_inputs(
										    processor.tokenizer		,				batch_size=UpperCAmelCase__		,				seq_length=UpperCAmelCase__		,				framework=UpperCAmelCase__      )
										lowercase   :       List[str]								=super().generate_dummy_inputs(
										    processor.image_processor		,				batch_size=UpperCAmelCase__		,				framework=UpperCAmelCase__      )
										return {**text_input_dict, **image_input_dict}







				@property
				def    lowerCamelCase_  (					self    :		Optional[int]      ):
										'''simple docstring'''
										return 14








'''simple docstring'''





import argparse
import json
import os
from collections import OrderedDict

import numpy as np
import tensorflow as tf
import torch






def 							_lowerCAmelCase (      __magic_name__	:    str	)							->     Union[str, Any]:
						lowercase   :       Union[str, Any]								=os.path.join(args.tf_model_dir      ,   '''parameters.json'''	)
						lowercase   :       List[str]								=json.loads(open(__magic_name__	).read()	)
						if not params:
												raise ValueError(
												    f'''It seems that the json file at {parameter_file} is empty. Make sure you have a correct json file.'''	)
						if not args.output.endswith('''.pt'''	):
												lowercase   :       Tuple								=args.output + '''.pt'''
						lowercase   :       int								=OrderedDict()
						with tf.device('''/CPU:0'''	):
												lowercase   :       List[Any]								=tf.train.load_checkpoint(args.tf_model_dir	)
												lowercase   :       int								=reader.get_variable_to_shape_map()
												for key_name in shapes.keys():
																		lowercase   :       Any								=reader.get_tensor(__magic_name__	).astype(np.floataa	)
																		if key_name.endswith('''/adam_m'''	) or key_name.endswith('''/adam_v'''	):
																								continue
																		if key_name.startswith('''pasts/'''	):
																								if key_name.startswith('''pasts/mlp'''	):
																														lowercase   :       int								=int(key_name[9]	)
																								elif key_name.startswith('''pasts/out'''	):
																														lowercase   :       Union[str, Any]								=8
																								lowercase   :       Any								='''model.sqout.%d.weight''' % (player * 2)  # enter to nn.Sequencial with Tanh, so 2 at a time
																								lowercase   :       Dict								=vnp.transpose([1, 0]	).copy()  # Mesh-Tensorflow is a diagonal matrix
																								lowercase   :       List[str]								=torch.tensor(__magic_name__	)
																		elif key_name.startswith('''model/moe'''	):
																								lowercase   :       Union[str, Any]								=int(key_name[9:].split('''/'''	)[0]	)
																								if key_name.endswith('''/switch_gating/kernel'''	):
																														lowercase   :       Dict								='''model.blocks.%d.feed_forward.mlp.router.classifier.weight''' % player
																														lowercase   :       Any								=vnp.transpose([1, 0]	).copy()  # Mesh-Tensorflow is a diagonal matrix
																														lowercase   :       List[Any]								=torch.tensor(__magic_name__	)
																								elif key_name.endswith('''/softmlp/kernel'''	):
																														lowercase   :       Optional[int]								='''model.blocks.%d.feed_forward.soft_bypass_mlp.weight''' % player
																														lowercase   :       int								=vnp.transpose([1, 0]	).copy()  # Mesh-Tensorflow is a diagonal matrix
																														lowercase   :       Optional[Any]								=torch.tensor(__magic_name__	)
																								elif key_name.endswith('''/wo/kernel'''	) or key_name.endswith('''/wi/kernel'''	):
																														lowercase   :       Union[str, Any]								=key_name[-9:-7]
																														for i in range(16	):
																																				lowercase   :       Dict								='''model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight''' % (player, i, nlayer)
																																				lowercase   :       Any								=(
																																				    vnp[i].transpose([1, 0]	).copy()
																																				)  # In Mesh-Tensorflow, it is one array, so it is divided
																																				lowercase   :       List[str]								=torch.tensor(__magic_name__	)
																		elif key_name.startswith('''model/mlp'''	):
																								lowercase   :       Dict								=int(key_name[9:].split('''/'''	)[0]	)
																								if key_name.endswith('''/p1/kernel'''	):
																														lowercase   :       Any								='''model.blocks.%d.feed_forward.mlp.wi.weight''' % player
																														lowercase   :       str								=vnp.transpose([1, 0]	).copy()  # Mesh-Tensorflow is a diagonal matrix
																														lowercase   :       Any								=torch.tensor(__magic_name__	)
																								elif key_name.endswith('''/p1/bias'''	):
																														lowercase   :       List[Any]								='''model.blocks.%d.feed_forward.mlp.wi.bias''' % player
																														lowercase   :       Union[str, Any]								=vnp.copy()  # same because it is one dimensional
																														lowercase   :       Union[str, Any]								=torch.tensor(__magic_name__	)
																								elif key_name.endswith('''/p2/kernel'''	):
																														lowercase   :       int								='''model.blocks.%d.feed_forward.mlp.wo.weight''' % player
																														lowercase   :       Tuple								=vnp.transpose([1, 0]	).copy()  # Mesh-Tensorflow is a diagonal matrix
																														lowercase   :       int								=torch.tensor(__magic_name__	)
																								elif key_name.endswith('''/p2/bias'''	):
																														lowercase   :       str								='''model.blocks.%d.feed_forward.mlp.wo.bias''' % player
																														lowercase   :       Optional[int]								=vnp.copy()  # same because it is one dimensional
																														lowercase   :       List[Any]								=torch.tensor(__magic_name__	)
																		elif key_name.startswith('''model/ln'''	):
																								lowercase   :       int								=int(key_name[8:].split('''/'''	)[0]	)
																								if key_name.endswith('''/b'''	):
																														lowercase   :       Any								='''model.blocks.%d.feed_forward.norm.bias''' % player
																														lowercase   :       Optional[int]								=vnp.copy()  # same because it is one dimensional
																														lowercase   :       Union[str, Any]								=torch.tensor(__magic_name__	)
																								elif key_name.endswith('''/g'''	):
																														lowercase   :       Optional[Any]								='''model.blocks.%d.feed_forward.norm.weight''' % player
																														lowercase   :       Any								=vnp.copy()  # same because it is one dimensional
																														lowercase   :       List[Any]								=torch.tensor(__magic_name__	)
																		elif key_name.startswith('''model/att'''	):
																								lowercase   :       int								=int(key_name[9:].split('''/'''	)[0]	)
																								if key_name.endswith('''/qkv/kernel'''	):
																														lowercase   :       Optional[int]								=vnp.copy()  # Compute same dimension as Mesh-tensorflow using einsum
																														lowercase   :       Dict								=state[:, 0, :, :]
																														lowercase   :       Tuple								=state[:, 1, :, :]
																														lowercase   :       List[Any]								=state[:, 2, :, :]
																														lowercase   :       Optional[int]								=(
																														    state_q.reshape([state_q.shape[0], state_q.shape[1] * state_q.shape[2]]	)
																														    .transpose([1, 0]	)
																														    .copy()
																														)  # Mesh-Tensorflow is a diagonal matrix
																														lowercase   :       Optional[Any]								=(
																														    state_k.reshape([state_k.shape[0], state_k.shape[1] * state_k.shape[2]]	)
																														    .transpose([1, 0]	)
																														    .copy()
																														)  # Mesh-Tensorflow is a diagonal matrix
																														lowercase   :       Optional[int]								=(
																														    state_v.reshape([state_v.shape[0], state_v.shape[1] * state_v.shape[2]]	)
																														    .transpose([1, 0]	)
																														    .copy()
																														)  # Mesh-Tensorflow is a diagonal matrix
																														lowercase   :       Dict								='''model.blocks.%d.self_attn.self_attn.q_proj.weight''' % player
																														lowercase   :       Dict								=torch.tensor(__magic_name__	)
																														lowercase   :       List[Any]								='''model.blocks.%d.self_attn.self_attn.k_proj.weight''' % player
																														lowercase   :       Optional[Any]								=torch.tensor(__magic_name__	)
																														lowercase   :       Optional[Any]								='''model.blocks.%d.self_attn.self_attn.v_proj.weight''' % player
																														lowercase   :       Tuple								=torch.tensor(__magic_name__	)
																								elif key_name.endswith('''/o/kernel'''	):
																														lowercase   :       Dict								='''model.blocks.%d.self_attn.self_attn.out_proj.weight''' % player
																														lowercase   :       List[Any]								=(
																														    vnp.reshape([vnp.shape[0] * vnp.shape[1], vnp.shape[2]]	).transpose([1, 0]	).copy()
																														)  # Mesh-Tensorflow is a diagonal matrix
																														lowercase   :       str								=torch.tensor(__magic_name__	)
																		elif key_name.startswith('''model/an'''	):
																								lowercase   :       Optional[Any]								=int(key_name[8:].split('''/'''	)[0]	)
																								if key_name.endswith('''/b'''	):
																														lowercase   :       List[str]								='''model.blocks.%d.self_attn.norm.bias''' % player
																														lowercase   :       Union[str, Any]								=vnp.copy()  # same because it is one dimensional
																														lowercase   :       List[str]								=torch.tensor(__magic_name__	)
																								elif key_name.endswith('''/g'''	):
																														lowercase   :       Any								='''model.blocks.%d.self_attn.norm.weight''' % player
																														lowercase   :       Any								=vnp.copy()  # same because it is one dimensional
																														lowercase   :       Optional[Any]								=torch.tensor(__magic_name__	)
																		elif (
																		    key_name.startswith('''model/wte'''	)
																		    or key_name.startswith('''model/wpe'''	)
																		    or key_name.startswith('''model/ete'''	)
																		):
																								lowercase   :       Any								={'''wte''': '''embed_tokens''', '''wpe''': '''position_embeddings''', '''ete''': '''extra_position_embeddings'''}[
																								    key_name[-3:]
																								]
																								lowercase   :       Optional[Any]								='''model.%s.weight''' % nlayer
																								lowercase   :       Optional[int]								=vnp.copy()  # same in embedded
																								lowercase   :       List[Any]								=torch.tensor(__magic_name__	)
																								if key_name.startswith('''model/wte'''	):
																														lowercase   :       Tuple								='''lm_head.weight'''
																														lowercase   :       str								=vnp.copy()  # same in embedded
																														lowercase   :       Union[str, Any]								=torch.tensor(__magic_name__	)
																		elif key_name.startswith('''model/wob'''	):
																								lowercase   :       List[str]								='''final_logits_bias'''
																								lowercase   :       Dict								=vnp.copy()  # same in embedded
																								lowercase   :       Tuple								=state.reshape((1, -1)	)
																								lowercase   :       Dict								=torch.tensor(__magic_name__	)
																		elif key_name == "model/dense/kernel":
																								lowercase   :       Dict								='''model.last_project.weight'''
																								lowercase   :       int								=vnp.transpose([1, 0]	).copy()  # Mesh-Tensorflow is a diagonal matrix
																								lowercase   :       Optional[Any]								=torch.tensor(__magic_name__	)
																		elif key_name == "model/dense_1/bias":
																								lowercase   :       List[Any]								='''model.last_project.bias'''
																								lowercase   :       str								=vnp.copy()  # same because it is one dimensional
																								lowercase   :       Optional[Any]								=torch.tensor(__magic_name__	)
						torch.save(__magic_name__      ,   args.output	)


if __name__ == "__main__":
							UpperCamelCase_							=					argparse.ArgumentParser(
							    description="""model converter.""", formatter_class=argparse.ArgumentDefaultsHelpFormatter
							)
							parser.add_argument("""--tf_model_dir""", metavar="""PATH""", type=str, required=True, help="""import model""")
							parser.add_argument("""--output""", metavar="""PATH""", type=str, required=True, help="""output model""")
							UpperCamelCase_							=					parser.parse_args()
							convert_tf_gptsan_to_pt(args)





