




'''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,
)


snake_case__          =						{
    """vit_b32""": {
        """embed_dim""": 5_12,
        """image_resolution""": 7_68,
        """context_length""": 16,
        """vocab_size""": 4_94_08,
        """vision_layers""": 12,
        """vision_width""": 7_68,
        """vision_patch_size""": 32,
        """transformer_width""": 5_12,
        """transformer_heads""": 8,
        """transformer_layers""": 12,
    },
    """vit_b16""": {
        """embed_dim""": 5_12,
        """image_resolution""": 7_68,
        """context_length""": 16,
        """vocab_size""": 4_94_08,
        """vision_layers""": 12,
        """vision_width""": 7_68,
        """vision_patch_size""": 16,
        """transformer_width""": 5_12,
        """transformer_heads""": 8,
        """transformer_layers""": 12,
    },
    """vit_l14""": {
        """embed_dim""": 7_68,
        """image_resolution""": 8_40,
        """context_length""": 16,
        """vocab_size""": 4_94_08,
        """vision_layers""": 24,
        """vision_width""": 10_24,
        """vision_patch_size""": 14,
        """transformer_width""": 7_68,
        """transformer_heads""": 12,
        """transformer_layers""": 12,
    },
}




def 						snake_case__    ( lowerCamelCase__				:   Any			,		lowerCamelCase__				:   Any=""			,		lowerCamelCase__				:   Optional[int]="/"			)   ->   Optional[Any]:
					A_ :     List[str] =							[]

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

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




def 						snake_case__    ( lowerCamelCase__				:   Optional[int]			)   ->   Optional[Any]:
					return jax.tree_util.tree_map(lambda lowerCamelCase__			: x.astype(jnp.floataa			) if x.dtype == jnp.bfloataa else x			,		lowerCamelCase__			)




def 						snake_case__    ( lowerCamelCase__				:   int			,		lowerCamelCase__				:   int			)   ->   Tuple:
					A_ ,A_ ,A_ :     Optional[Any] =							pt_attn_layer.in_proj_weight.chunk(3			,		dim=0			)
					A_ ,A_ ,A_ :     Any =							pt_attn_layer.in_proj_bias.chunk(3			,		dim=0			)

					A_ :     Union[str, Any] =							pt_attn_layer.out_proj.weight
					A_ :     Tuple =							pt_attn_layer.out_proj.bias

					A_ :     List[str] =							q_proj
					A_ :     Tuple =							q_proj_bias

					A_ :     Dict =							k_proj
					A_ :     int =							k_proj_bias

					A_ :     Dict =							v_proj
					A_ :     Union[str, Any] =							v_proj_bias

					A_ :     Tuple =							out_proj_weights
					A_ :     Optional[Any] =							out_proj_bias




def 						snake_case__    ( lowerCamelCase__				:   Optional[int]			,		lowerCamelCase__				:   List[Any]			)   ->   str:
					copy_linear(hf_mlp.fca			,		pt_mlp.c_fc			)
					copy_linear(hf_mlp.fca			,		pt_mlp.c_proj			)




def 						snake_case__    ( lowerCamelCase__				:   List[str]			,		lowerCamelCase__				:   str			)   ->   Optional[Any]:
					A_ :     List[Any] =							pt_linear.weight
					A_ :     Union[str, Any] =							pt_linear.bias




def 						snake_case__    ( lowerCamelCase__				:   Dict			,		lowerCamelCase__				:   Optional[int]			)   ->   int:
					# copy layer norms
					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 						snake_case__    ( lowerCamelCase__				:   Optional[Any]			,		lowerCamelCase__				:   Tuple			)   ->   int:
					for hf_layer, pt_layer in zip(lowerCamelCase__			,		lowerCamelCase__			):
										copy_layer(lowerCamelCase__			,		lowerCamelCase__			)




def 						snake_case__    ( lowerCamelCase__				:   List[str]			,		lowerCamelCase__				:   Optional[Any]			)   ->   Optional[int]:
					# copy  embeds
					A_ :     Tuple =							pt_model.token_embedding.weight
					A_ :     Optional[Any] =							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 						snake_case__    ( lowerCamelCase__				:   Union[str, Any]			,		lowerCamelCase__				:   List[Any]			)   ->   Dict:
					# copy projection
					A_ :     Optional[int] =							pt_model.text_projection.data.T

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




def 						snake_case__    ( lowerCamelCase__				:   Optional[Any]			,		lowerCamelCase__				:   List[str]			)   ->   Dict:
					# copy projection
					A_ :     Optional[int] =							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
					A_ :     Any =							pt_model.visual.conva.weight.data
					A_ :     Tuple =							pt_model.visual.class_embedding
					A_ :     int =							pt_model.visual.positional_embedding.data

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




def 						snake_case__    ( lowerCamelCase__				:   int			,		lowerCamelCase__				:   Any			)   ->   Union[str, Any]:
					A_ :     int =							flatten_nested_dict(flax_params['''backbone''']['''merged_class_token''']			)

					A_ :     List[str] =							torch.from_numpy(flax_class_token_params['''scale''']			)
					A_ :     List[Any] =							torch.from_numpy(flax_class_token_params['''bias''']			)
					A_ :     str =							nn.Parameter(lowerCamelCase__			)
					A_ :     Union[str, Any] =							nn.Parameter(lowerCamelCase__			)




def 						snake_case__    ( lowerCamelCase__				:   List[Any]			,		lowerCamelCase__				:   List[Any]			)   ->   List[Any]:
					A_ :     Optional[int] =							hf_model.state_dict()
					A_ :     Union[str, Any] =							{}

					# Rename class prediction head flax params to pytorch HF
					A_ :     str =							flatten_nested_dict(flax_params['''class_head''']			)

					for flax_key, v in flax_class_params.items():
										A_ :     Tuple =							flax_key.replace('''/'''			,		'''.'''			)
										A_ :     Union[str, Any] =							torch_key.replace('''.kernel'''			,		'''.weight'''			)
										A_ :     Optional[Any] =							torch_key.replace('''Dense_0'''			,		'''dense0'''			)
										A_ :     Any =							'''class_head.''' + torch_key

										if "weight" in torch_key and v.ndim == 2:
															A_ :     Optional[Any] =							v.T

										A_ :     Union[str, Any] =							nn.Parameter(torch.from_numpy(lowerCamelCase__			)			)

					# Rename box prediction box flax params to pytorch HF
					A_ :     Tuple =							flatten_nested_dict(flax_params['''obj_box_head''']			)

					for flax_key, v in flax_box_params.items():
										A_ :     Any =							flax_key.replace('''/'''			,		'''.'''			)
										A_ :     Dict =							torch_key.replace('''.kernel'''			,		'''.weight'''			)
										A_ :     List[str] =							torch_key.replace('''_'''			,		''''''			).lower()
										A_ :     Union[str, Any] =							'''box_head.''' + torch_key

										if "weight" in torch_key and v.ndim == 2:
															A_ :     Optional[int] =							v.T

										A_ :     List[str] =							nn.Parameter(torch.from_numpy(lowerCamelCase__			)			)

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




def 						snake_case__    ( lowerCamelCase__				:   Union[str, Any]			,		lowerCamelCase__				:   int			)   ->   Optional[int]:
					for k, v in flax_attn_params.items():
										if k.startswith('''transformer'''			):
															A_ :     Optional[Any] =							k.replace('''transformer.resblocks'''			,		'''text_model.encoder.layers'''			)
										else:
															A_ :     Optional[Any] =							k.replace('''visual.transformer.resblocks'''			,		'''vision_model.encoder.layers'''			)

										A_ :     Any =							torch_key.replace('''attn'''			,		'''self_attn'''			)
										A_ :     int =							torch_key.replace('''key'''			,		'''k_proj'''			)
										A_ :     Any =							torch_key.replace('''value'''			,		'''v_proj'''			)
										A_ :     List[str] =							torch_key.replace('''query'''			,		'''q_proj'''			)
										A_ :     int =							torch_key.replace('''out'''			,		'''out_proj'''			)

										if "bias" in torch_key and v.ndim == 2:
															A_ :     List[Any] =							v.shape[0] * v.shape[1]
															A_ :     Optional[Any] =							v.reshape(lowerCamelCase__			)

										if "weight" in torch_key and "out" in torch_key:
															A_ :     Optional[Any] =							(v.shape[0] * v.shape[1], v.shape[2])
															A_ :     Any =							v.reshape(lowerCamelCase__			).T

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

										# Copy flax CLIP attn params to HF PyTorch params
										A_ :     List[Any] =							torch.from_numpy(lowerCamelCase__			)
										hf_backbone.state_dict()[torch_key].copy_(lowerCamelCase__			)




def 						snake_case__    ( lowerCamelCase__				:   Any			)   ->   Union[str, Any]:
					A_ :     List[str] =							{}
					A_ :     Tuple =							[]

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

															processed_attn_layers.append(lowerCamelCase__			)
															A_ :     Union[str, Any] =							params[base + '''out.weight'''].shape[-1]
															A_ :     Tuple =							params[base + '''out.weight'''].reshape(lowerCamelCase__			,		lowerCamelCase__			).T
															A_ :     int =							params[base + '''out.bias''']
										else:
															A_ :     str =							v
					return new_params




def 						snake_case__    ( lowerCamelCase__				:   Union[str, Any]			,		lowerCamelCase__				:   str			)   ->   Any:
					A_ :     Union[str, Any] =							CLIP(**lowerCamelCase__			)
					torch_model.eval()
					A_ :     Union[str, Any] =							torch_model.state_dict()

					A_ :     Optional[Any] =							flatten_nested_dict(flax_params['''backbone''']['''clip''']			)
					A_ :     Dict =							{}

					for flax_key, v in flax_clip_params.items():
										A_ :     Union[str, Any] =							flax_key.replace('''/'''			,		'''.'''			)
										A_ :     int =							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'''			)
										):
															A_ :     List[str] =							torch_key[5:]

										A_ :     Any =							torch_key.replace('''text_projection.kernel'''			,		'''text_projection'''			)
										A_ :     str =							torch_key.replace('''visual.proj.kernel'''			,		'''visual.proj'''			)
										A_ :     Optional[int] =							torch_key.replace('''.scale'''			,		'''.weight'''			)
										A_ :     Optional[Any] =							torch_key.replace('''.kernel'''			,		'''.weight'''			)

										if "conv" in torch_key or "downsample.0.weight" in torch_key:
															A_ :     List[Any] =							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
															A_ :     int =							v.T

										A_ :     int =							v

					A_ :     Any =							_convert_attn_layers(lowerCamelCase__			)
					new_torch_params.update(lowerCamelCase__			)
					A_ :     Any =							{}

					# Copy flax CLIP backbone params to PyTorch params
					for name, param in new_torch_params.items():
										if name in torch_clip_params.keys():
															A_ :     List[Any] =							torch.from_numpy(new_torch_params[name]			)
															torch_clip_params[name].copy_(lowerCamelCase__			)
										else:
															A_ :     Union[str, Any] =							param

					return torch_clip_params, torch_model, attn_params




@torch.no_grad()
def 						snake_case__    ( lowerCamelCase__				:   int			,		lowerCamelCase__				:   str			,		lowerCamelCase__				:   List[Any]			,		lowerCamelCase__				:   Tuple			,		lowerCamelCase__				:   Any=None			)   ->   List[Any]:
					A_ :     List[Any] =							Repository(lowerCamelCase__			,		clone_from=f'google/{pytorch_dump_folder_path}'			)
					repo.git_pull()

					if config_path is not None:
										A_ :     Optional[int] =							OwlViTConfig.from_pretrained(lowerCamelCase__			)
					else:
										A_ :     List[str] =							OwlViTConfig()

					A_ :     Optional[Any] =							OwlViTModel(lowerCamelCase__			).eval()
					A_ :     Optional[Any] =							OwlViTForObjectDetection(lowerCamelCase__			).eval()

					copy_text_model_and_projection(lowerCamelCase__			,		lowerCamelCase__			)
					copy_vision_model_and_projection(lowerCamelCase__			,		lowerCamelCase__			)
					A_ :     Dict =							pt_backbone.logit_scale
					copy_flax_attn_params(lowerCamelCase__			,		lowerCamelCase__			)

					A_ :     Tuple =							hf_backbone
					copy_class_merge_token(lowerCamelCase__			,		lowerCamelCase__			)
					copy_class_box_heads(lowerCamelCase__			,		lowerCamelCase__			)

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

					# Initialize image processor
					A_ :     Optional[int] =							OwlViTImageProcessor(
					    size=config.vision_config.image_size			,		crop_size=config.vision_config.image_size			)
					# Initialize tokenizer
					A_ :     Tuple =							CLIPTokenizer.from_pretrained('''openai/clip-vit-base-patch32'''			,		pad_token='''!'''			,		model_max_length=1_6			)

					# Initialize processor
					A_ :     Optional[int] =							OwlViTProcessor(image_processor=lowerCamelCase__			,		tokenizer=lowerCamelCase__			)
					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__":
							snake_case__          =						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."""
							)
							snake_case__          =						parser.parse_args()

							# Initialize PyToch clip model
							snake_case__          =						args.owlvit_version
							if model_name == "clip_b16":
														snake_case__          =						CONFIGS["""vit_b16"""]
							elif model_name == "clip_b32":
														snake_case__          =						CONFIGS["""vit_b32"""]
							elif model_name == "clip_l14":
														snake_case__          =						CONFIGS["""vit_l14"""]

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

							# Convert CLIP backbone
							snake_case__       ,						snake_case__       ,						snake_case__          =						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 argparse
import logging
import sys
import time

import tensorflow as tf
from datasets import load_dataset

from transformers import AutoTokenizer, TFAutoModelForSequenceClassification


if __name__ == "__main__":
							snake_case__          =						argparse.ArgumentParser()

							# Hyperparameters sent by the client are passed as command-line arguments to the script.
							parser.add_argument("""--epochs""", type=int, default=1)
							parser.add_argument("""--per_device_train_batch_size""", type=int, default=16)
							parser.add_argument("""--per_device_eval_batch_size""", type=int, default=8)
							parser.add_argument("""--model_name_or_path""", type=str)
							parser.add_argument("""--learning_rate""", type=str, default=5e-5)
							parser.add_argument("""--do_train""", type=bool, default=True)
							parser.add_argument("""--do_eval""", type=bool, default=True)
							parser.add_argument("""--output_dir""", type=str)

							snake_case__       ,						snake_case__          =						parser.parse_known_args()

							# overwrite batch size until we have tf_glue.py
							snake_case__          =						16
							snake_case__          =						16

							# Set up logging
							snake_case__          =						logging.getLogger(__name__)

							logging.basicConfig(
							    level=logging.getLevelName("""INFO"""),
							    handlers=[logging.StreamHandler(sys.stdout)],
							    format="""%(asctime)s - %(name)s - %(levelname)s - %(message)s""",
							)

							# Load model and tokenizer
							snake_case__          =						TFAutoModelForSequenceClassification.from_pretrained(args.model_name_or_path)
							snake_case__          =						AutoTokenizer.from_pretrained(args.model_name_or_path)

							# Load dataset
							snake_case__       ,						snake_case__          =						load_dataset("""imdb""", split=["""train""", """test"""])
							snake_case__          =						train_dataset.shuffle().select(range(50_00))  # smaller the size for train dataset to 5k
							snake_case__          =						test_dataset.shuffle().select(range(5_00))  # smaller the size for test dataset to 500

							# Preprocess train dataset
							snake_case__          =						train_dataset.map(
							    lambda e: tokenizer(e["""text"""], truncation=True, padding="""max_length"""), batched=True
							)
							train_dataset.set_format(type="""tensorflow""", columns=["""input_ids""", """attention_mask""", """label"""])

							snake_case__          =						{
							    x: train_dataset[x].to_tensor(default_value=0, shape=[None, tokenizer.model_max_length])
							    for x in ["""input_ids""", """attention_mask"""]
							}
							snake_case__          =						tf.data.Dataset.from_tensor_slices((train_features, train_dataset["""label"""])).batch(
							    args.per_device_train_batch_size
							)

							# Preprocess test dataset
							snake_case__          =						test_dataset.map(
							    lambda e: tokenizer(e["""text"""], truncation=True, padding="""max_length"""), batched=True
							)
							test_dataset.set_format(type="""tensorflow""", columns=["""input_ids""", """attention_mask""", """label"""])

							snake_case__          =						{
							    x: test_dataset[x].to_tensor(default_value=0, shape=[None, tokenizer.model_max_length])
							    for x in ["""input_ids""", """attention_mask"""]
							}
							snake_case__          =						tf.data.Dataset.from_tensor_slices((test_features, test_dataset["""label"""])).batch(
							    args.per_device_eval_batch_size
							)

							# fine optimizer and loss
							snake_case__          =						tf.keras.optimizers.Adam(learning_rate=args.learning_rate)
							snake_case__          =						tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
							snake_case__          =						[tf.keras.metrics.SparseCategoricalAccuracy()]
							model.compile(optimizer=optimizer, loss=loss, metrics=metrics)

							snake_case__          =						time.time()
							snake_case__          =						model.fit(tf_train_dataset, epochs=args.epochs, batch_size=args.per_device_train_batch_size)
							snake_case__          =						time.time() - start_train_time

							logger.info("""*** Train ***""")
							logger.info(F'train_runtime = {end_train_time}')
							for key, value in train_results.history.items():
														logger.info(F'  {key} = {value}')



