




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     _SCREAMING_SNAKE_CASE    (							lowercase			: Tuple  ):


      '''simple docstring'''



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

def     _SCREAMING_SNAKE_CASE    (							lowercase			: Optional[int]			,							lowercase			: Any  ):


      '''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     _SCREAMING_SNAKE_CASE    (							lowercase			: Dict			,							lowercase			: Optional[int]			,							lowercase			: Optional[Any]			,							lowercase			: Optional[int]=None  ):


      '''simple docstring'''



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

      lowerCamelCase_	  =					FlavaForPreTraining(lowercase  ).eval()

      lowerCamelCase_	  =					convert_dalle_checkpoint(lowercase			,							lowercase			,							save_checkpoint=lowercase  )

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

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

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

      hf_model.save_pretrained(lowercase  )


if __name__ == "__main__":
 lowerCamelCase       :	Union[str, Any]									=     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       :	str									=     parser.parse_args()

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









from __future__ import annotations

from fractions import Fraction

def     _SCREAMING_SNAKE_CASE    (							lowercase			: int			,							lowercase			: int  ):


      '''simple docstring'''



      return (
          num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den
      )

def     _SCREAMING_SNAKE_CASE    (							lowercase			: int  ):


      '''simple docstring'''



      lowerCamelCase_	  =					[]
      lowerCamelCase_	  =					11
      lowerCamelCase_	  =					int('1' + '0' * digit_len  )
      for num in range(lowercase			,							lowercase  ):
            while den <= 99:
                  if (num != den) and (num % 10 == den // 10) and (den % 10 != 0):
                        if is_digit_cancelling(lowercase			,							lowercase  ):
                              solutions.append(f"""{num}/{den}"""  )
                  den += 1
            num += 1
            lowerCamelCase_	  =					10
      return solutions

def     _SCREAMING_SNAKE_CASE    (							lowercase			: int = 2  ):


      '''simple docstring'''



      lowerCamelCase_	  =					1.0
      for fraction in fraction_list(lowercase  ):
            lowerCamelCase_	  =					Fraction(lowercase  )
            result *= frac.denominator / frac.numerator
      return int(lowercase  )


if __name__ == "__main__":
 print(solution())



