

from ..utils import DummyObject, requires_backends





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Optional[int]      =						['''sentencepiece''']

 def __init__(		self :	Dict     ,						*A_ :	Tuple     ,						**A_ :	Tuple       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					List[Any]      =						['''sentencepiece''']

 def __init__(		self :	str     ,						*A_ :	List[Any]     ,						**A_ :	Optional[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					int      =						['''sentencepiece''']

 def __init__(		self :	List[str]     ,						*A_ :	Any     ,						**A_ :	int       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Tuple      =						['''sentencepiece''']

 def __init__(		self :	List[str]     ,						*A_ :	Optional[int]     ,						**A_ :	Any       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Union[str, Any]      =						['''sentencepiece''']

 def __init__(		self :	int     ,						*A_ :	str     ,						**A_ :	Optional[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Optional[int]      =						['''sentencepiece''']

 def __init__(		self :	Dict     ,						*A_ :	Tuple     ,						**A_ :	str       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Any      =						['''sentencepiece''']

 def __init__(		self :	Optional[int]     ,						*A_ :	Any     ,						**A_ :	str       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Dict      =						['''sentencepiece''']

 def __init__(		self :	Tuple     ,						*A_ :	Optional[Any]     ,						**A_ :	Optional[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					List[Any]      =						['''sentencepiece''']

 def __init__(		self :	List[Any]     ,						*A_ :	Union[str, Any]     ,						**A_ :	Union[str, Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Union[str, Any]      =						['''sentencepiece''']

 def __init__(		self :	int     ,						*A_ :	str     ,						**A_ :	str       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Tuple      =						['''sentencepiece''']

 def __init__(		self :	List[str]     ,						*A_ :	Any     ,						**A_ :	List[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					List[str]      =						['''sentencepiece''']

 def __init__(		self :	List[str]     ,						*A_ :	Union[str, Any]     ,						**A_ :	Optional[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Optional[Any]      =						['''sentencepiece''']

 def __init__(		self :	List[Any]     ,						*A_ :	Tuple     ,						**A_ :	Optional[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Dict      =						['''sentencepiece''']

 def __init__(		self :	Dict     ,						*A_ :	str     ,						**A_ :	Dict       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					str      =						['''sentencepiece''']

 def __init__(		self :	List[Any]     ,						*A_ :	List[str]     ,						**A_ :	Dict       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Any      =						['''sentencepiece''']

 def __init__(		self :	Optional[Any]     ,						*A_ :	int     ,						**A_ :	Dict       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					str      =						['''sentencepiece''']

 def __init__(		self :	Any     ,						*A_ :	Dict     ,						**A_ :	Any       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Union[str, Any]      =						['''sentencepiece''']

 def __init__(		self :	Tuple     ,						*A_ :	List[Any]     ,						**A_ :	str       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Union[str, Any]      =						['''sentencepiece''']

 def __init__(		self :	Union[str, Any]     ,						*A_ :	str     ,						**A_ :	Optional[int]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					int      =						['''sentencepiece''']

 def __init__(		self :	List[Any]     ,						*A_ :	Optional[Any]     ,						**A_ :	Union[str, Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Optional[Any]      =						['''sentencepiece''']

 def __init__(		self :	int     ,						*A_ :	Optional[Any]     ,						**A_ :	Dict       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Union[str, Any]      =						['''sentencepiece''']

 def __init__(		self :	Optional[Any]     ,						*A_ :	List[str]     ,						**A_ :	Optional[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Dict      =						['''sentencepiece''']

 def __init__(		self :	Union[str, Any]     ,						*A_ :	Union[str, Any]     ,						**A_ :	Tuple       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					List[str]      =						['''sentencepiece''']

 def __init__(		self :	List[str]     ,						*A_ :	Tuple     ,						**A_ :	Union[str, Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Optional[Any]      =						['''sentencepiece''']

 def __init__(		self :	Optional[Any]     ,						*A_ :	Dict     ,						**A_ :	Optional[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Tuple      =						['''sentencepiece''']

 def __init__(		self :	Any     ,						*A_ :	Tuple     ,						**A_ :	int       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Optional[int]      =						['''sentencepiece''']

 def __init__(		self :	Any     ,						*A_ :	Dict     ,						**A_ :	str       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					List[Any]      =						['''sentencepiece''']

 def __init__(		self :	Any     ,						*A_ :	str     ,						**A_ :	int       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Union[str, Any]      =						['''sentencepiece''']

 def __init__(		self :	Dict     ,						*A_ :	Optional[int]     ,						**A_ :	int       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					Union[str, Any]      =						['''sentencepiece''']

 def __init__(		self :	List[str]     ,						*A_ :	Any     ,						**A_ :	List[Any]       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )





class A__    ( metaclass=A ):



 """simple docstring"""







 _lowercase     :					List[str]      =						['''sentencepiece''']

 def __init__(		self :	int     ,						*A_ :	Optional[int]     ,						**A_ :	int       ):






      '''simple docstring'''




      requires_backends(self     ,						["sentencepiece"]       )









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 	_snake_case		(    SCREAMING_SNAKE_CASE				)       ->      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 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)       ->      Tuple:

     """simple docstring"""







     _lowerCAmelCase   :					str        =    {}

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

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

          _lowerCAmelCase   :					List[Any]        =    value.float()

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

     return upgrade







@torch.no_grad()
def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE=None				)       ->      Tuple:

     """simple docstring"""







     if config_path is not None:
          _lowerCAmelCase   :					Dict        =    FlavaConfig.from_pretrained(SCREAMING_SNAKE_CASE				)
     else:
          _lowerCAmelCase   :					Dict        =    FlavaConfig()

     _lowerCAmelCase   :					List[Any]        =    FlavaForPreTraining(SCREAMING_SNAKE_CASE				).eval()

     _lowerCAmelCase   :					Optional[int]        =    convert_dalle_checkpoint(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE		,							save_checkpoint=SCREAMING_SNAKE_CASE				)

     if os.path.exists(SCREAMING_SNAKE_CASE				):
          _lowerCAmelCase   :					Dict        =    torch.load(SCREAMING_SNAKE_CASE		,							map_location="cpu"				)
     else:
          _lowerCAmelCase   :					Tuple        =    torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE		,							map_location="cpu"				)

     _lowerCAmelCase   :					Optional[int]        =    upgrade_state_dict(SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)
     hf_model.load_state_dict(SCREAMING_SNAKE_CASE				)
     _lowerCAmelCase   :					Optional[Any]        =    hf_model.state_dict()
     _lowerCAmelCase   :					Optional[Any]        =    count_parameters(SCREAMING_SNAKE_CASE				)
     _lowerCAmelCase   :					Optional[Any]        =    count_parameters(SCREAMING_SNAKE_CASE				) + count_parameters(SCREAMING_SNAKE_CASE				)

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

     hf_model.save_pretrained(SCREAMING_SNAKE_CASE				)


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

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






