

"""simple docstring"""




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     UpperCAmelCase__   (				A__   )      ->      Optional[Any]:




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



def     UpperCAmelCase__   (				A__	,    A__   )      ->      List[Any]:




   """simple docstring"""
   lowerCamelCase__						=			{}

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

      lowerCamelCase__						=			key.replace("heads.cmd.mim_head.cls.predictions"	,    "mmm_image_head"   )
      lowerCamelCase__						=			key.replace("heads.cmd.mlm_head.cls.predictions"	,    "mmm_text_head"   )
      lowerCamelCase__						=			key.replace("heads.cmd.itm_head.cls"	,    "itm_head"   )
      lowerCamelCase__						=			key.replace("heads.cmd.itm_head.pooler"	,    "itm_head.pooler"   )
      lowerCamelCase__						=			key.replace("heads.cmd.clip_head.logit_scale"	,    "flava.logit_scale"   )
      lowerCamelCase__						=			key.replace("heads.fairseq_mlm.cls.predictions"	,    "mlm_head"   )
      lowerCamelCase__						=			key.replace("heads.imagenet.mim_head.cls.predictions"	,    "mim_head"   )
      lowerCamelCase__						=			key.replace("mm_text_projection"	,    "flava.text_to_mm_projection"   )
      lowerCamelCase__						=			key.replace("mm_image_projection"	,    "flava.image_to_mm_projection"   )
      lowerCamelCase__						=			key.replace("image_encoder.module"	,    "flava.image_model"   )
      lowerCamelCase__						=			key.replace("text_encoder.module"	,    "flava.text_model"   )
      lowerCamelCase__						=			key.replace("mm_encoder.module.encoder.cls_token"	,    "flava.multimodal_model.cls_token"   )
      lowerCamelCase__						=			key.replace("mm_encoder.module"	,    "flava.multimodal_model"   )
      lowerCamelCase__						=			key.replace("text_projection"	,    "flava.text_projection"   )
      lowerCamelCase__						=			key.replace("image_projection"	,    "flava.image_projection"   )

      lowerCamelCase__						=			value.float()

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

   return upgrade



@torch.no_grad()
def     UpperCAmelCase__   (				A__	,    A__	,    A__	,    A__=None   )      ->      List[Any]:




   """simple docstring"""
   if config_path is not None:
      lowerCamelCase__						=			FlavaConfig.from_pretrained(A__   )
   else:
      lowerCamelCase__						=			FlavaConfig()

   lowerCamelCase__						=			FlavaForPreTraining(A__   ).eval()

   lowerCamelCase__						=			convert_dalle_checkpoint(A__	,    A__	,    save_checkpoint=A__   )

   if os.path.exists(A__   ):
      lowerCamelCase__						=			torch.load(A__	,    map_location="cpu"   )
   else:
      lowerCamelCase__						=			torch.hub.load_state_dict_from_url(A__	,    map_location="cpu"   )

   lowerCamelCase__						=			upgrade_state_dict(A__	,    A__   )
   hf_model.load_state_dict(A__   )
   lowerCamelCase__						=			hf_model.state_dict()
   lowerCamelCase__						=			count_parameters(A__   )
   lowerCamelCase__						=			count_parameters(A__   ) + count_parameters(A__   )

   assert torch.allclose(A__	,    A__	,    atol=1E-3   )

   hf_model.save_pretrained(A__   )


if __name__ == "__main__":
     SCREAMING_SNAKE_CASE_     :			str	  =			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''')
     SCREAMING_SNAKE_CASE_     :			Union[str, Any]	  =			parser.parse_args()

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







"""simple docstring"""




import numpy as np



def 	UpperCAmelCase__   (				A__   )      ->      np.array:




   """simple docstring"""
   return 1 / (1 + np.exp(-vector   ))


if __name__ == "__main__":
     import doctest

     doctest.testmod()




