"""simple docstring"""






import doctest
import logging
import os
import unittest
from pathlib import Path
from typing import List, Union

import transformers
from transformers.testing_utils import require_tf, require_torch, slow


__lowerCAmelCase					: Any        =logging.getLogger()





@unittest.skip('Temporarily disable the doc tests.'			)
@require_torch
@require_tf
@slow
class 				_A		( unittest.TestCase			):

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = None	, __lowerCAmelCase = None	, __lowerCAmelCase = None	, __lowerCAmelCase = True	, ):







            """simple docstring"""



            lowercase														=			[file for file in os.listdir(__lowerCAmelCase		) if os.path.isfile(os.path.join(__lowerCAmelCase	, __lowerCAmelCase		)		)]

            if identifier is not None:
                  lowercase														=			[file for file in files if identifier in file]

            if n_identifier is not None:
                  if isinstance(__lowerCAmelCase	, __lowerCAmelCase		):
                        for n_ in n_identifier:
                              lowercase														=			[file for file in files if n_ not in file]
                  else:
                        lowercase														=			[file for file in files if n_identifier not in file]

            lowercase														=			ignore_files or []
            ignore_files.append("""__init__.py"""		)
            lowercase														=			[file for file in files if file not in ignore_files]

            for file in files:
                  # Open all files
                  print("""Testing"""	, __lowerCAmelCase		)

                  if only_modules:
                        lowercase														=			file.split("""."""		)[0]
                        try:
                              lowercase														=			getattr(__lowerCAmelCase	, __lowerCAmelCase		)
                              lowercase														=			doctest.DocTestSuite(__lowerCAmelCase		)
                              lowercase														=			unittest.TextTestRunner().run(__lowerCAmelCase		)
                              self.assertIs(len(result.failures		)	, 0		)
                        except AttributeError:
                              logger.info(f'{module_identifier} is not a module.'		)
                  else:
                        lowercase														=			doctest.testfile(str("""..""" / directory / file		)	, optionflags=doctest.ELLIPSIS		)
                        self.assertIs(result.failed	, 0		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			Path("""src/transformers"""		)
            lowercase														=			"""modeling"""
            lowercase														=			[
                """modeling_ctrl.py""",
                """modeling_tf_ctrl.py""",
            ]
            self.analyze_directory(__lowerCAmelCase	, identifier=__lowerCAmelCase	, ignore_files=__lowerCAmelCase		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			Path("""src/transformers"""		)
            lowercase														=			"""tokenization"""
            self.analyze_directory(__lowerCAmelCase	, identifier=__lowerCAmelCase		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			Path("""src/transformers"""		)
            lowercase														=			"""configuration"""
            self.analyze_directory(__lowerCAmelCase	, identifier=__lowerCAmelCase		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			Path("""src/transformers"""		)
            lowercase														=			["""configuration""", """modeling""", """tokenization"""]
            self.analyze_directory(__lowerCAmelCase	, n_identifier=__lowerCAmelCase		)







      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			Path("""docs/source"""		)
            lowercase														=			["""favicon.ico"""]
            self.analyze_directory(__lowerCAmelCase	, ignore_files=__lowerCAmelCase	, only_modules=__lowerCAmelCase		)




"""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__ (       lowerCAmelCase__  :str	)				->					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     UpperCAmelCase__ (       lowerCAmelCase__  :Any      ,	lowerCAmelCase__  :Union[str, Any]	)				->					str:
      '''simple docstring'''


      lowercase														=			{}

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

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

            lowercase														=			value.float()

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

      return upgrade


@torch.no_grad()
def     UpperCAmelCase__ (       lowerCAmelCase__  :Union[str, Any]      ,	lowerCAmelCase__  :Dict      ,	lowerCAmelCase__  :Optional[int]      ,	lowerCAmelCase__  :str=None	)				->					int:
      '''simple docstring'''


      if config_path is not None:
            lowercase														=			FlavaConfig.from_pretrained(lowerCAmelCase__	)
      else:
            lowercase														=			FlavaConfig()

      lowercase														=			FlavaForPreTraining(lowerCAmelCase__	).eval()

      lowercase														=			convert_dalle_checkpoint(lowerCAmelCase__      ,	lowerCAmelCase__      ,	save_checkpoint=lowerCAmelCase__	)

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

      lowercase														=			upgrade_state_dict(lowerCAmelCase__      ,	lowerCAmelCase__	)
      hf_model.load_state_dict(lowerCAmelCase__	)
      lowercase														=			hf_model.state_dict()
      lowercase														=			count_parameters(lowerCAmelCase__	)
      lowercase														=			count_parameters(lowerCAmelCase__	) + count_parameters(lowerCAmelCase__	)

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

      hf_model.save_pretrained(lowerCAmelCase__	)


if __name__ == "__main__":
       __lowerCAmelCase					: Tuple        =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""")
       __lowerCAmelCase					: List[str]        =parser.parse_args()

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



