

'''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  _A		(     snake_case					)      ->     Tuple:
   # encoder.embeddings are double copied in original FLAVA
   return sum(param.float().sum() if "encoder.embeddings" not in key else 0 for key, param in state_dict.items()					)







def  _A		(     snake_case		,							snake_case					)      ->     Union[str, Any]:
   _lowercase      :				List[str]      					=       {}

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

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

      _lowercase      :				Tuple      					=       value.float()

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

   return upgrade







@torch.no_grad()
def  _A		(     snake_case		,							snake_case		,							snake_case		,							snake_case=None					)      ->     int:
   if config_path is not None:
      _lowercase      :				Any      					=       FlavaConfig.from_pretrained(snake_case					)
   else:
      _lowercase      :				Tuple      					=       FlavaConfig()

   _lowercase      :				Any      					=       FlavaForPreTraining(snake_case					).eval()

   _lowercase      :				Union[str, Any]      					=       convert_dalle_checkpoint(snake_case		,							snake_case		,							save_checkpoint=snake_case					)

   if os.path.exists(snake_case					):
      _lowercase      :				Tuple      					=       torch.load(snake_case		,							map_location="cpu"					)
   else:
      _lowercase      :				List[str]      					=       torch.hub.load_state_dict_from_url(snake_case		,							map_location="cpu"					)

   _lowercase      :				Tuple      					=       upgrade_state_dict(snake_case		,							snake_case					)
   hf_model.load_state_dict(snake_case					)
   _lowercase      :				int      					=       hf_model.state_dict()
   _lowercase      :				int      					=       count_parameters(snake_case					)
   _lowercase      :				Optional[int]      					=       count_parameters(snake_case					) + count_parameters(snake_case					)

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

   hf_model.save_pretrained(snake_case					)


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

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









'''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


_snake_case         =  logging.getLogger()


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


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase = None      ,    _UpperCamelCase = None      ,    _UpperCamelCase = None      ,    _UpperCamelCase = True      ,    ):





      """simple docstring"""





      _lowercase      :				str      					=       [file for file in os.listdir(_UpperCamelCase  ) if os.path.isfile(os.path.join(_UpperCamelCase      ,    _UpperCamelCase  )  )]

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

      if n_identifier is not None:
         if isinstance(_UpperCamelCase      ,    _UpperCamelCase  ):
            for n_ in n_identifier:
               _lowercase      :				Dict      					=       [file for file in files if n_ not in file]
         else:
            _lowercase      :				Optional[Any]      					=       [file for file in files if n_identifier not in file]

      _lowercase      :				Dict      					=       ignore_files or []
      ignore_files.append("__init__.py"  )
      _lowercase      :				List[str]      					=       [file for file in files if file not in ignore_files]

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

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       Path("src/transformers"  )
      _lowercase      :				str      					=       "modeling"
      _lowercase      :				Tuple      					=       [
          "modeling_ctrl.py",
          "modeling_tf_ctrl.py",
      ]
      self.analyze_directory(_UpperCamelCase      ,    identifier=_UpperCamelCase      ,    ignore_files=_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       Path("src/transformers"  )
      _lowercase      :				Any      					=       "tokenization"
      self.analyze_directory(_UpperCamelCase      ,    identifier=_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       Path("src/transformers"  )
      _lowercase      :				Optional[Any]      					=       "configuration"
      self.analyze_directory(_UpperCamelCase      ,    identifier=_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Union[str, Any]      					=       Path("src/transformers"  )
      _lowercase      :				List[Any]      					=       ["configuration", "modeling", "tokenization"]
      self.analyze_directory(_UpperCamelCase      ,    n_identifier=_UpperCamelCase  )






   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Dict      					=       Path("docs/source"  )
      _lowercase      :				int      					=       ["favicon.ico"]
      self.analyze_directory(_UpperCamelCase      ,    ignore_files=_UpperCamelCase      ,    only_modules=_UpperCamelCase  )






