


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__	(    SCREAMING_SNAKE_CASE__		):
 # 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  UpperCamelCase__	(    SCREAMING_SNAKE_CASE__     ,			SCREAMING_SNAKE_CASE__		):
 __lowerCamelCase						:				Tuple						   =		{}

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

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

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

 for key, value in codebook_state_dict.items():
  __lowerCamelCase						:				List[Any]						   =		value

 return upgrade
@torch.no_grad()
def  UpperCamelCase__	(    SCREAMING_SNAKE_CASE__     ,			SCREAMING_SNAKE_CASE__     ,			SCREAMING_SNAKE_CASE__     ,			SCREAMING_SNAKE_CASE__=None		):
 if config_path is not None:
  __lowerCamelCase						:				Any						   =		FlavaConfig.from_pretrained(UpperCamelCase__		)
 else:
  __lowerCamelCase						:				List[str]						   =		FlavaConfig()

 __lowerCamelCase						:				List[str]						   =		FlavaForPreTraining(UpperCamelCase__		).eval()

 __lowerCamelCase						:				List[str]						   =		convert_dalle_checkpoint(UpperCamelCase__     ,			UpperCamelCase__     ,			save_checkpoint=UpperCamelCase__		)

 if os.path.exists(UpperCamelCase__		):
  __lowerCamelCase						:				Optional[Any]						   =		torch.load(UpperCamelCase__     ,			map_location='cpu'		)
 else:
  __lowerCamelCase						:				Optional[int]						   =		torch.hub.load_state_dict_from_url(UpperCamelCase__     ,			map_location='cpu'		)

 __lowerCamelCase						:				List[str]						   =		upgrade_state_dict(UpperCamelCase__     ,			UpperCamelCase__		)
 hf_model.load_state_dict(UpperCamelCase__		)
 __lowerCamelCase						:				int						   =		hf_model.state_dict()
 __lowerCamelCase						:				Any						   =		count_parameters(UpperCamelCase__		)
 __lowerCamelCase						:				Dict						   =		count_parameters(UpperCamelCase__		) + count_parameters(UpperCamelCase__		)

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

 hf_model.save_pretrained(UpperCamelCase__		)


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

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








import math


def       UpperCamelCase__(			UpperCamelCase__     :			int    )->list:
       A__		   =		[True] * n
       A__		   =		False
       A__		   =		False
       A__		   =		True

       for i in range(3					,	int(n**0.5 + 1    )					,	2    ):
              A__		   =		i * 2
              while index < n:
                     A__		   =		False
                     A__		   =		index + i

       A__		   =		[2]

       for i in range(3					,	UpperCamelCase__					,	2    ):
              if is_prime[i]:
                     primes.append(UpperCamelCase__    )

       return primes


def       UpperCamelCase__(			UpperCamelCase__     :			int = 99_99_66_66_33_33    )->int:
       A__		   =		math.floor(math.sqrt(UpperCamelCase__    )    ) + 1_00
       A__		   =		prime_sieve(UpperCamelCase__    )

       A__		   =		0
       A__		   =		0
       A__		   =		primes[prime_index]

       while (last_prime**2) <= limit:
              A__		   =		primes[prime_index + 1]

              A__		   =		last_prime**2
              A__		   =		next_prime**2

              # Get numbers divisible by lps(current)
              A__		   =		lower_bound + last_prime
              while upper_bound > current <= limit:
                     matches_sum += current
                     current += last_prime

              # Reset the upper_bound
              while (upper_bound - next_prime) > limit:
                     upper_bound -= next_prime

              # Add the numbers divisible by ups(current)
              A__		   =		upper_bound - next_prime
              while current > lower_bound:
                     matches_sum += current
                     current -= next_prime

              # Remove the numbers divisible by both ups and lps
              A__		   =		0
              while upper_bound > current <= limit:
                     if current <= lower_bound:
                            # Increment the current number
                            current += last_prime * next_prime
                            continue

                     if current > limit:
                            break

                     # Remove twice since it was added by both ups and lps
                     matches_sum -= current * 2

                     # Increment the current number
                     current += last_prime * next_prime

              # Setup for next pair
              A__		   =		next_prime
              prime_index += 1

       return matches_sum


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





