'''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 	lowerCAmelCase_				(					snake_case_       : str			)    ->      str:


     '''simple docstring'''




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





def 	lowerCAmelCase_				(					snake_case_       : str					,    snake_case_       : List[str]			)    ->      str:


     '''simple docstring'''




     UpperCAmelCase_           =   {}

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

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

          UpperCAmelCase_           =   value.float()

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

     return upgrade





@torch.no_grad()
def 	lowerCAmelCase_				(					snake_case_       : Union[str, Any]					,    snake_case_       : List[str]					,    snake_case_       : str					,    snake_case_       : str=None			)    ->      Tuple:


     '''simple docstring'''




     if config_path is not None:
          UpperCAmelCase_           =   FlavaConfig.from_pretrained(snake_case_			)
     else:
          UpperCAmelCase_           =   FlavaConfig()

     UpperCAmelCase_           =   FlavaForPreTraining(snake_case_			).eval()

     UpperCAmelCase_           =   convert_dalle_checkpoint(snake_case_					,    snake_case_					,    save_checkpoint=snake_case_			)

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

     UpperCAmelCase_           =   upgrade_state_dict(snake_case_					,    snake_case_			)
     hf_model.load_state_dict(snake_case_			)
     UpperCAmelCase_           =   hf_model.state_dict()
     UpperCAmelCase_           =   count_parameters(snake_case_			)
     UpperCAmelCase_           =   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__":
    SCREAMING_SNAKE_CASE_:     List[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 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         =    logging.getLogger()







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





  """simple docstring"""

  def   __lowercase		(		self      :     str							,_a      :     Path							,_a      :     Union[str, None] = None							,_a      :     Union[List[str], None] = None							,_a      :     Union[str, List[str], None] = None							,_a      :     bool = True							,):


     '''simple docstring'''






     _a				:	Optional[int]			=   [file for file in os.listdir(_a       ) if os.path.isfile(os.path.join(_a							,_a       )       )]

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

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

     _a				:	List[str]			=   ignore_files or []
     ignore_files.append('__init__.py'       )
     _a				:	Tuple			=   [file for file in files if file not in ignore_files]

     for file in files:
        # Open all files
        print('Testing'							,_a       )

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


  def   __lowercase		(		self      :     Any       ):


     '''simple docstring'''






     _a				:	int			=   Path('src/transformers'       )
     _a				:	List[Any]			=   'modeling'
     _a				:	Optional[Any]			=   [
         'modeling_ctrl.py',
         'modeling_tf_ctrl.py',
     ]
     self.analyze_directory(_a							,identifier=_a							,ignore_files=_a       )


  def   __lowercase		(		self      :     Optional[int]       ):


     '''simple docstring'''






     _a				:	Optional[Any]			=   Path('src/transformers'       )
     _a				:	Optional[Any]			=   'tokenization'
     self.analyze_directory(_a							,identifier=_a       )


  def   __lowercase		(		self      :     List[str]       ):


     '''simple docstring'''






     _a				:	Dict			=   Path('src/transformers'       )
     _a				:	str			=   'configuration'
     self.analyze_directory(_a							,identifier=_a       )


  def   __lowercase		(		self      :     Any       ):


     '''simple docstring'''






     _a				:	Tuple			=   Path('src/transformers'       )
     _a				:	List[Any]			=   ['configuration', 'modeling', 'tokenization']
     self.analyze_directory(_a							,n_identifier=_a       )


  def   __lowercase		(		self      :     Tuple       ):


     '''simple docstring'''






     _a				:	List[Any]			=   Path('docs/source'       )
     _a				:	List[str]			=   ['favicon.ico']
     self.analyze_directory(_a							,ignore_files=_a							,only_modules=_a       )



