

import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel







def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)       ->      Any:

     """simple docstring"""







     _lowerCAmelCase   :					List[str]        =    ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value")

     _lowerCAmelCase   :					Optional[int]        =    (
         ("layer.", "layer_"),
         ("word_embeddings.weight", "word_embeddings"),
         ("position_embeddings.weight", "position_embeddings"),
         ("token_type_embeddings.weight", "token_type_embeddings"),
         (".", "/"),
         ("LayerNorm/weight", "LayerNorm/gamma"),
         ("LayerNorm/bias", "LayerNorm/beta"),
         ("weight", "kernel"),
     )

     if not os.path.isdir(SCREAMING_SNAKE_CASE				):
          os.makedirs(SCREAMING_SNAKE_CASE				)

     _lowerCAmelCase   :					Any        =    model.state_dict()

     def to_tf_var_name(SCREAMING_SNAKE_CASE				):
          for patt, repl in iter(SCREAMING_SNAKE_CASE				):
               _lowerCAmelCase   :					Optional[int]        =    name.replace(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)
          return f'''bert/{name}'''

     def create_tf_var(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				):
          _lowerCAmelCase   :					int        =    tf.dtypes.as_dtype(tensor.dtype				)
          _lowerCAmelCase   :					List[str]        =    tf.get_variable(dtype=SCREAMING_SNAKE_CASE		,							shape=tensor.shape		,							name=SCREAMING_SNAKE_CASE		,							initializer=tf.zeros_initializer()				)
          session.run(tf.variables_initializer([tf_var]				)				)
          session.run(SCREAMING_SNAKE_CASE				)
          return tf_var

     tf.reset_default_graph()
     with tf.Session() as session:
          for var_name in state_dict:
               _lowerCAmelCase   :					Any        =    to_tf_var_name(SCREAMING_SNAKE_CASE				)
               _lowerCAmelCase   :					str        =    state_dict[var_name].numpy()
               if any(x in var_name for x in tensors_to_transpose				):
                    _lowerCAmelCase   :					Any        =    torch_tensor.T
               _lowerCAmelCase   :					Optional[int]        =    create_tf_var(tensor=SCREAMING_SNAKE_CASE		,							name=SCREAMING_SNAKE_CASE		,							session=SCREAMING_SNAKE_CASE				)
               tf.keras.backend.set_value(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)
               _lowerCAmelCase   :					Tuple        =    session.run(SCREAMING_SNAKE_CASE				)
               print(f'''Successfully created {tf_name}: {np.allclose(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)}'''				)

          _lowerCAmelCase   :					List[str]        =    tf.train.Saver(tf.trainable_variables()				)
          saver.save(SCREAMING_SNAKE_CASE		,							os.path.join(SCREAMING_SNAKE_CASE		,							model_name.replace("-"		,							"_"				) + ".ckpt"				)				)







def 	_snake_case		(    SCREAMING_SNAKE_CASE=None				)       ->      Optional[Any]:

     """simple docstring"""







     _lowerCAmelCase   :					List[str]        =    argparse.ArgumentParser()
     parser.add_argument("--model_name"		,							type=SCREAMING_SNAKE_CASE		,							required=SCREAMING_SNAKE_CASE		,							help="model name e.g. bert-base-uncased"				)
     parser.add_argument(
         "--cache_dir"		,							type=SCREAMING_SNAKE_CASE		,							default=SCREAMING_SNAKE_CASE		,							required=SCREAMING_SNAKE_CASE		,							help="Directory containing pytorch model"				)
     parser.add_argument("--pytorch_model_path"		,							type=SCREAMING_SNAKE_CASE		,							required=SCREAMING_SNAKE_CASE		,							help="/path/to/<pytorch-model-name>.bin"				)
     parser.add_argument("--tf_cache_dir"		,							type=SCREAMING_SNAKE_CASE		,							required=SCREAMING_SNAKE_CASE		,							help="Directory in which to save tensorflow model"				)
     _lowerCAmelCase   :					Tuple        =    parser.parse_args(SCREAMING_SNAKE_CASE				)

     _lowerCAmelCase   :					List[Any]        =    BertModel.from_pretrained(
         pretrained_model_name_or_path=args.model_name		,							state_dict=torch.load(args.pytorch_model_path				)		,							cache_dir=args.cache_dir		,							)

     convert_pytorch_checkpoint_to_tf(model=SCREAMING_SNAKE_CASE		,							ckpt_dir=args.tf_cache_dir		,							model_name=args.model_name				)


if __name__ == "__main__":
  main()









import argparse
import os

import torch

from transformers import FlavaConfig, FlavaForPreTraining
from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint







def 	_snake_case		(    SCREAMING_SNAKE_CASE				)       ->      Optional[int]:

     """simple docstring"""







     return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items()				)







def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)       ->      Tuple:

     """simple docstring"""







     _lowerCAmelCase   :					str        =    {}

     for key, value in state_dict.items():
          if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key:
               continue

          _lowerCAmelCase   :					Union[str, Any]        =    key.replace("heads.cmd.mim_head.cls.predictions"		,							"mmm_image_head"				)
          _lowerCAmelCase   :					int        =    key.replace("heads.cmd.mlm_head.cls.predictions"		,							"mmm_text_head"				)
          _lowerCAmelCase   :					Any        =    key.replace("heads.cmd.itm_head.cls"		,							"itm_head"				)
          _lowerCAmelCase   :					Optional[Any]        =    key.replace("heads.cmd.itm_head.pooler"		,							"itm_head.pooler"				)
          _lowerCAmelCase   :					Any        =    key.replace("heads.cmd.clip_head.logit_scale"		,							"flava.logit_scale"				)
          _lowerCAmelCase   :					List[Any]        =    key.replace("heads.fairseq_mlm.cls.predictions"		,							"mlm_head"				)
          _lowerCAmelCase   :					Union[str, Any]        =    key.replace("heads.imagenet.mim_head.cls.predictions"		,							"mim_head"				)
          _lowerCAmelCase   :					Optional[int]        =    key.replace("mm_text_projection"		,							"flava.text_to_mm_projection"				)
          _lowerCAmelCase   :					int        =    key.replace("mm_image_projection"		,							"flava.image_to_mm_projection"				)
          _lowerCAmelCase   :					List[str]        =    key.replace("image_encoder.module"		,							"flava.image_model"				)
          _lowerCAmelCase   :					Optional[int]        =    key.replace("text_encoder.module"		,							"flava.text_model"				)
          _lowerCAmelCase   :					List[str]        =    key.replace("mm_encoder.module.encoder.cls_token"		,							"flava.multimodal_model.cls_token"				)
          _lowerCAmelCase   :					Dict        =    key.replace("mm_encoder.module"		,							"flava.multimodal_model"				)
          _lowerCAmelCase   :					Dict        =    key.replace("text_projection"		,							"flava.text_projection"				)
          _lowerCAmelCase   :					Optional[Any]        =    key.replace("image_projection"		,							"flava.image_projection"				)

          _lowerCAmelCase   :					List[Any]        =    value.float()

     for key, value in codebook_state_dict.items():
          _lowerCAmelCase   :					int        =    value

     return upgrade







@torch.no_grad()
def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE=None				)       ->      Tuple:

     """simple docstring"""







     if config_path is not None:
          _lowerCAmelCase   :					Dict        =    FlavaConfig.from_pretrained(SCREAMING_SNAKE_CASE				)
     else:
          _lowerCAmelCase   :					Dict        =    FlavaConfig()

     _lowerCAmelCase   :					List[Any]        =    FlavaForPreTraining(SCREAMING_SNAKE_CASE				).eval()

     _lowerCAmelCase   :					Optional[int]        =    convert_dalle_checkpoint(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							save_checkpoint=SCREAMING_SNAKE_CASE				)

     if os.path.exists(SCREAMING_SNAKE_CASE				):
          _lowerCAmelCase   :					Dict        =    torch.load(SCREAMING_SNAKE_CASE		,							map_location="cpu"				)
     else:
          _lowerCAmelCase   :					Tuple        =    torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE		,							map_location="cpu"				)

     _lowerCAmelCase   :					Optional[int]        =    upgrade_state_dict(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)
     hf_model.load_state_dict(SCREAMING_SNAKE_CASE				)
     _lowerCAmelCase   :					Optional[Any]        =    hf_model.state_dict()
     _lowerCAmelCase   :					Optional[Any]        =    count_parameters(SCREAMING_SNAKE_CASE				)
     _lowerCAmelCase   :					Optional[Any]        =    count_parameters(SCREAMING_SNAKE_CASE				) + count_parameters(SCREAMING_SNAKE_CASE				)

     assert torch.allclose(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							atol=1e-3				)

     hf_model.save_pretrained(SCREAMING_SNAKE_CASE				)


if __name__ == "__main__":
  __UpperCAmelCase    				=     argparse.ArgumentParser()
  parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
  parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to flava checkpoint')
  parser.add_argument('--codebook_path', default=None, type=str, help='Path to flava codebook checkpoint')
  parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert')
  __UpperCAmelCase    				=     parser.parse_args()

  convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)






