



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     __lowercase(	UpperCAmelCase__ ):


      """simple docstring"""



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

def     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__ ):


      """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     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__=None ):


      """simple docstring"""



      if config_path is not None:
            lowerCamelCase								=		FlavaConfig.from_pretrained(UpperCAmelCase__ )
      else:
            lowerCamelCase								=		FlavaConfig()

      lowerCamelCase								=		FlavaForPreTraining(UpperCAmelCase__ ).eval()

      lowerCamelCase								=		convert_dalle_checkpoint(UpperCAmelCase__   ,   UpperCAmelCase__   ,   save_checkpoint=UpperCAmelCase__ )

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

      lowerCamelCase								=		upgrade_state_dict(UpperCAmelCase__   ,   UpperCAmelCase__ )
      hf_model.load_state_dict(UpperCAmelCase__ )
      lowerCamelCase								=		hf_model.state_dict()
      lowerCamelCase								=		count_parameters(UpperCAmelCase__ )
      lowerCamelCase								=		count_parameters(UpperCAmelCase__ ) + count_parameters(UpperCAmelCase__ )

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

      hf_model.save_pretrained(UpperCAmelCase__ )


if __name__ == "__main__":
     a_						:     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')
     a_						:     str	     =		parser.parse_args()

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




from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch





@require_torch
@require_tf
class    lowerCamelCase__  (						UpperCAmelCase_):
     """simple docstring"""



     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		SMALL_MODEL_IDENTIFIER
           lowerCamelCase								=		"pt"
           lowerCamelCase								=		"tf"







     def 						_a      (self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		AutoModel.from_pretrained(self.test_model    )
           model_pt.save_pretrained(__a    )







     def 						_a      (self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		TFAutoModel.from_pretrained(self.test_model				,			from_pt=__a    )
           model_tf.save_pretrained(__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"mock_framework"

           # Framework provided - return whatever the user provides
           lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model				,			__a    )
           self.assertEqual(__a				,			__a    )

           # Local checkpoint and framework provided - return provided framework
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                 self._setup_pt_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a				,			__a    )
                 self.assertEqual(__a				,			__a    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                 self._setup_tf_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a				,			__a    )
                 self.assertEqual(__a				,			__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           with TemporaryDirectory() as local_pt_ckpt:
                 self._setup_pt_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a    )
                 self.assertEqual(__a				,			self.framework_pt    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                 self._setup_tf_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a    )
                 self.assertEqual(__a				,			self.framework_tf    )

           # Invalid local checkpoint
           with TemporaryDirectory() as local_invalid_ckpt:
                 with self.assertRaises(__a    ):
                       lowerCamelCase								=		FeaturesManager.determine_framework(__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_tf_available"				,			__a    ):
                 lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )
                 self.assertEqual(__a				,			self.framework_pt    )

           # PyTorch not in environment -> use TensorFlow
           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_torch_available"				,			__a    ):
                 lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )
                 self.assertEqual(__a				,			self.framework_tf    )

           # Both in environment -> use PyTorch
           lowerCamelCase								=		MagicMock(return_value=__a    )
           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_tf_available"				,			__a    ), patch(
               "transformers.onnx.features.is_torch_available"				,			__a    ):
                 lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )
                 self.assertEqual(__a				,			self.framework_pt    )

           # Both not in environment -> raise error
           lowerCamelCase								=		MagicMock(return_value=__a    )
           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_tf_available"				,			__a    ), patch(
               "transformers.onnx.features.is_torch_available"				,			__a    ):
                 with self.assertRaises(__a    ):
                       lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )