






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


UpperCamelCase			=      {
    """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     A(  snake_case_						,       snake_case_=""						,       snake_case_="/"		):
       """simple docstring"""





       lowercase__:    str        =      []

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

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






def     A(  snake_case_		):
       """simple docstring"""





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






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    str        =      pt_attn_layer.in_proj_weight.chunk(3						,       dim=0		)
       lowercase__:    str        =      pt_attn_layer.in_proj_bias.chunk(3						,       dim=0		)

       lowercase__:    List[str]        =      pt_attn_layer.out_proj.weight
       lowercase__:    str        =      pt_attn_layer.out_proj.bias

       lowercase__:    List[str]        =      q_proj
       lowercase__:    List[str]        =      q_proj_bias

       lowercase__:    List[Any]        =      k_proj
       lowercase__:    Any        =      k_proj_bias

       lowercase__:    Optional[int]        =      v_proj
       lowercase__:    Optional[int]        =      v_proj_bias

       lowercase__:    List[Any]        =      out_proj_weights
       lowercase__:    int        =      out_proj_bias






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





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






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    Dict        =      pt_linear.weight
       lowercase__:    List[str]        =      pt_linear.bias






def     A(  snake_case_						,       snake_case_		):
       """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     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





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






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    List[str]        =      pt_model.token_embedding.weight
       lowercase__:    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     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    Dict        =      pt_model.text_projection.data.T

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






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    Dict        =      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
       lowercase__:    Any        =      pt_model.visual.conva.weight.data
       lowercase__:    Union[str, Any]        =      pt_model.visual.class_embedding
       lowercase__:    int        =      pt_model.visual.positional_embedding.data

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






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    Dict        =      flatten_nested_dict(flax_params["backbone"]["merged_class_token"]		)

       lowercase__:    str        =      torch.from_numpy(flax_class_token_params["scale"]		)
       lowercase__:    Tuple        =      torch.from_numpy(flax_class_token_params["bias"]		)
       lowercase__:    List[Any]        =      nn.Parameter(snake_case_		)
       lowercase__:    Dict        =      nn.Parameter(snake_case_		)






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    Union[str, Any]        =      hf_model.state_dict()
       lowercase__:    int        =      {}

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

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

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

              lowercase__:    Any        =      nn.Parameter(torch.from_numpy(snake_case_		)		)

       # Rename box prediction box flax params to pytorch HF
       lowercase__:    Optional[Any]        =      flatten_nested_dict(flax_params["obj_box_head"]		)

       for flax_key, v in flax_box_params.items():
              lowercase__:    int        =      flax_key.replace("/"						,       "."		)
              lowercase__:    Dict        =      torch_key.replace(".kernel"						,       ".weight"		)
              lowercase__:    Union[str, Any]        =      torch_key.replace("_"						,       ""		).lower()
              lowercase__:    Optional[int]        =      "box_head." + torch_key

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

              lowercase__:    Tuple        =      nn.Parameter(torch.from_numpy(snake_case_		)		)

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






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       for k, v in flax_attn_params.items():
              if k.startswith("transformer"		):
                     lowercase__:    int        =      k.replace("transformer.resblocks"						,       "text_model.encoder.layers"		)
              else:
                     lowercase__:    List[str]        =      k.replace("visual.transformer.resblocks"						,       "vision_model.encoder.layers"		)

              lowercase__:    Union[str, Any]        =      torch_key.replace("attn"						,       "self_attn"		)
              lowercase__:    Union[str, Any]        =      torch_key.replace("key"						,       "k_proj"		)
              lowercase__:    Any        =      torch_key.replace("value"						,       "v_proj"		)
              lowercase__:    Any        =      torch_key.replace("query"						,       "q_proj"		)
              lowercase__:    List[str]        =      torch_key.replace("out"						,       "out_proj"		)

              if "bias" in torch_key and v.ndim == 2:
                     lowercase__:    Optional[Any]        =      v.shape[0] * v.shape[1]
                     lowercase__:    Union[str, Any]        =      v.reshape(snake_case_		)

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

              if "weight" in torch_key and "out" not in torch_key:
                     lowercase__:    List[str]        =      (v.shape[0], v.shape[1] * v.shape[2])
                     lowercase__:    Tuple        =      v.reshape(snake_case_		).T

              # Copy flax CLIP attn params to HF PyTorch params
              lowercase__:    int        =      torch.from_numpy(snake_case_		)
              hf_backbone.state_dict()[torch_key].copy_(snake_case_		)






def     A(  snake_case_		):
       """simple docstring"""





       lowercase__:    Union[str, Any]        =      {}
       lowercase__:    List[str]        =      []

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

                     processed_attn_layers.append(snake_case_		)
                     lowercase__:    Dict        =      params[base + "out.weight"].shape[-1]
                     lowercase__:    Tuple        =      params[base + "out.weight"].reshape(snake_case_						,       snake_case_		).T
                     lowercase__:    Dict        =      params[base + "out.bias"]
              else:
                     lowercase__:    Optional[int]        =      v
       return new_params






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    Optional[int]        =      CLIP(**snake_case_		)
       torch_model.eval()
       lowercase__:    Tuple        =      torch_model.state_dict()

       lowercase__:    List[str]        =      flatten_nested_dict(flax_params["backbone"]["clip"]		)
       lowercase__:    int        =      {}

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

              lowercase__:    Union[str, Any]        =      torch_key.replace("text_projection.kernel"						,       "text_projection"		)
              lowercase__:    Any        =      torch_key.replace("visual.proj.kernel"						,       "visual.proj"		)
              lowercase__:    Dict        =      torch_key.replace(".scale"						,       ".weight"		)
              lowercase__:    int        =      torch_key.replace(".kernel"						,       ".weight"		)

              if "conv" in torch_key or "downsample.0.weight" in torch_key:
                     lowercase__:    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
                     lowercase__:    Tuple        =      v.T

              lowercase__:    str        =      v

       lowercase__:    str        =      _convert_attn_layers(snake_case_		)
       new_torch_params.update(snake_case_		)
       lowercase__:    Union[str, Any]        =      {}

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

       return torch_clip_params, torch_model, attn_params






@torch.no_grad()
def     A(  snake_case_						,       snake_case_						,       snake_case_						,       snake_case_						,       snake_case_=None		):
       """simple docstring"""





       lowercase__:    Optional[int]        =      Repository(snake_case_						,       clone_from=F"""google/{pytorch_dump_folder_path}"""		)
       repo.git_pull()

       if config_path is not None:
              lowercase__:    Dict        =      OwlViTConfig.from_pretrained(snake_case_		)
       else:
              lowercase__:    Any        =      OwlViTConfig()

       lowercase__:    Dict        =      OwlViTModel(snake_case_		).eval()
       lowercase__:    Tuple        =      OwlViTForObjectDetection(snake_case_		).eval()

       copy_text_model_and_projection(snake_case_						,       snake_case_		)
       copy_vision_model_and_projection(snake_case_						,       snake_case_		)
       lowercase__:    Optional[int]        =      pt_backbone.logit_scale
       copy_flax_attn_params(snake_case_						,       snake_case_		)

       lowercase__:    Dict        =      hf_backbone
       copy_class_merge_token(snake_case_						,       snake_case_		)
       copy_class_box_heads(snake_case_						,       snake_case_		)

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

       # Initialize image processor
       lowercase__:    List[str]        =      OwlViTImageProcessor(
           size=config.vision_config.image_size						,       crop_size=config.vision_config.image_size		)
       # Initialize tokenizer
       lowercase__:    Tuple        =      CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32"						,       pad_token="!"						,       model_max_length=16		)

       # Initialize processor
       lowercase__:    Any        =      OwlViTProcessor(image_processor=snake_case_						,       tokenizer=snake_case_		)
       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__":
  UpperCamelCase			=      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."""
  )
  UpperCamelCase			=      parser.parse_args()

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

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

  # Convert CLIP backbone
  UpperCamelCase,      UpperCamelCase,      UpperCamelCase			=      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 warnings
from collections.abc import Callable
from typing import Any

import numpy
from matplotlib import pyplot

UpperCamelCase			=      0.25 + 0.0j
UpperCamelCase			=      -0.4 + 0.6j
UpperCamelCase			=      -0.1 + 0.651j
UpperCamelCase			=      -2.0
UpperCamelCase			=      56
UpperCamelCase			=      2.0
UpperCamelCase			=      6_66






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       return numpy.exp(snake_case_		) + c_parameter






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       return z_values * z_values + c_parameter






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    str        =      numpy.linspace(-window_size						,       snake_case_						,       snake_case_		)
       lowercase__:    Union[str, Any]        =      x.reshape((nb_pixels, 1)		)
       lowercase__:    Any        =      numpy.linspace(-window_size						,       snake_case_						,       snake_case_		)
       lowercase__:    List[Any]        =      y.reshape((1, nb_pixels)		)
       return x + 1.0J * y






def     A(  snake_case_						,       snake_case_						,       snake_case_						,       snake_case_						,       snake_case_ = None						,       ):
       """simple docstring"""





       lowercase__:    List[Any]        =      z_a.astype("complex64"		)
       for _ in range(snake_case_		):
              lowercase__:    Any        =      eval_function(snake_case_						,       snake_case_		)
              if infinity is not None:
                     numpy.nan_to_num(snake_case_						,       copy=snake_case_						,       nan=snake_case_		)
                     lowercase__:    Union[str, Any]        =      infinity
       return z_n






def     A(  snake_case_						,       snake_case_						,       snake_case_						,       snake_case_						,       ):
       """simple docstring"""





       lowercase__:    Union[str, Any]        =      (abs(snake_case_		)).transpose()
       lowercase__:    List[Any]        =      abs_z_final[::-1, :]
       pyplot.matshow(abs_z_final < escape_radius		)
       pyplot.title(F"""Julia set of ${function_label}$, $c={function_params}$"""		)
       pyplot.show()






def     A(  ):
       """simple docstring"""





       warnings.filterwarnings(
           "ignore"						,       category=snake_case_						,       message="overflow encountered in multiply"		)
       warnings.filterwarnings(
           "ignore"						,       category=snake_case_						,       message="invalid value encountered in multiply"						,       )
       warnings.filterwarnings(
           "ignore"						,       category=snake_case_						,       message="overflow encountered in absolute"		)
       warnings.filterwarnings(
           "ignore"						,       category=snake_case_						,       message="overflow encountered in exp"		)


if __name__ == "__main__":
  UpperCamelCase			=      prepare_grid(window_size, nb_pixels)

  ignore_overflow_warnings()  # See file header for explanations

  UpperCamelCase			=      24
  UpperCamelCase			=      2 * abs(c_cauliflower) + 1
  UpperCamelCase			=      iterate_function(
      eval_quadratic_polynomial,
      c_cauliflower,
      nb_iterations,
      z_a,
      infinity=1.1 * escape_radius,
  )
  show_results("""z^2+c""", c_cauliflower, escape_radius, z_final)

  UpperCamelCase			=      64
  UpperCamelCase			=      2 * abs(c_polynomial_a) + 1
  UpperCamelCase			=      iterate_function(
      eval_quadratic_polynomial,
      c_polynomial_a,
      nb_iterations,
      z_a,
      infinity=1.1 * escape_radius,
  )
  show_results("""z^2+c""", c_polynomial_a, escape_radius, z_final)

  UpperCamelCase			=      1_61
  UpperCamelCase			=      2 * abs(c_polynomial_a) + 1
  UpperCamelCase			=      iterate_function(
      eval_quadratic_polynomial,
      c_polynomial_a,
      nb_iterations,
      z_a,
      infinity=1.1 * escape_radius,
  )
  show_results("""z^2+c""", c_polynomial_a, escape_radius, z_final)

  UpperCamelCase			=      12
  UpperCamelCase			=      10_000.0
  UpperCamelCase			=      iterate_function(
      eval_exponential,
      c_exponential,
      nb_iterations,
      z_a + 2,
      infinity=1.0e10,
  )
  show_results("""e^z+c""", c_exponential, escape_radius, z_final)






