'''simple docstring'''

import argparse
import re

import requests
import torch

# git clone https://github.com/salesforce/BLIP.git
from models.blip import blip_decoder
from models.blip_itm import blip_itm
from models.blip_vqa import blip_vqa
from PIL import Image
from torchvision import transforms
from torchvision.transforms.functional import InterpolationMode

from transformers import (
    BertTokenizer,
    BlipConfig,
    BlipForConditionalGeneration,
    BlipForImageTextRetrieval,
    BlipForQuestionAnswering,
)







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






 '''simple docstring'''

 __lowerCAmelCase											=						'''https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'''
 __lowerCAmelCase											=						Image.open(requests.get(lowerCAmelCase__						,		stream=lowerCAmelCase__      ).raw      ).convert("""RGB"""      )

 __lowerCAmelCase											=						transforms.Compose(
     [
         transforms.Resize((image_size, image_size)						,		interpolation=InterpolationMode.BICUBIC      ),
         transforms.ToTensor(),
         transforms.Normalize((0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3)						,		(0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1)      ),
     ]      )
 __lowerCAmelCase											=						transform(lowerCAmelCase__      ).unsqueeze(0      ).to(lowerCAmelCase__      )
 return image







def      UpperCamelCase_						(					snake_case_       :       Union[str, Any]      )					->     Optional[int]:






 '''simple docstring'''

 if "visual_encoder" in key:
  __lowerCAmelCase											=						re.sub("""visual_encoder*"""						,		"""vision_model.encoder"""						,		lowerCAmelCase__      )
 if "blocks" in key:
  __lowerCAmelCase											=						re.sub(r"""blocks"""						,		"""layers"""						,		lowerCAmelCase__      )
 if "attn" in key:
  __lowerCAmelCase											=						re.sub(r"""attn"""						,		"""self_attn"""						,		lowerCAmelCase__      )
 if "norm1" in key:
  __lowerCAmelCase											=						re.sub(r"""norm1"""						,		"""layer_norm1"""						,		lowerCAmelCase__      )
 if "norm2" in key:
  __lowerCAmelCase											=						re.sub(r"""norm2"""						,		"""layer_norm2"""						,		lowerCAmelCase__      )
 if "encoder.norm" in key:
  __lowerCAmelCase											=						re.sub(r"""encoder.norm"""						,		"""post_layernorm"""						,		lowerCAmelCase__      )
 if "encoder.patch_embed.proj" in key:
  __lowerCAmelCase											=						re.sub(r"""encoder.patch_embed.proj"""						,		"""embeddings.patch_embedding"""						,		lowerCAmelCase__      )

 if "encoder.pos_embed" in key:
  __lowerCAmelCase											=						re.sub(r"""encoder.pos_embed"""						,		"""embeddings.position_embedding"""						,		lowerCAmelCase__      )
 if "encoder.cls_token" in key:
  __lowerCAmelCase											=						re.sub(r"""encoder.cls_token"""						,		"""embeddings.class_embedding"""						,		lowerCAmelCase__      )

 if "self_attn" in key:
  __lowerCAmelCase											=						re.sub(r"""self_attn.proj"""						,		"""self_attn.projection"""						,		lowerCAmelCase__      )

 return key







@torch.no_grad()
def      UpperCamelCase_						(					snake_case_       :       int						,		snake_case_       :       Any=None      )					->     Optional[int]:






 '''simple docstring'''

 if config_path is not None:
  __lowerCAmelCase											=						BlipConfig.from_pretrained(lowerCAmelCase__      )
 else:
  __lowerCAmelCase											=						BlipConfig(projection_dim=5_12						,		text_config={}						,		vision_config={}      )

 __lowerCAmelCase											=						BlipForConditionalGeneration(lowerCAmelCase__      ).eval()

 __lowerCAmelCase											=						'''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth'''

 __lowerCAmelCase											=						blip_decoder(pretrained=lowerCAmelCase__						,		image_size=3_84						,		vit="""base"""      )
 __lowerCAmelCase											=						pt_model.eval()

 __lowerCAmelCase											=						pt_model.state_dict()
 for key in modified_state_dict.copy():
  __lowerCAmelCase											=						modified_state_dict.pop(lowerCAmelCase__      )
  __lowerCAmelCase											=						rename_key(lowerCAmelCase__      )
  __lowerCAmelCase											=						value

 hf_model.load_state_dict(lowerCAmelCase__      )

 __lowerCAmelCase											=						3_84
 __lowerCAmelCase											=						load_demo_image(image_size=lowerCAmelCase__						,		device="""cpu"""      )
 __lowerCAmelCase											=						BertTokenizer.from_pretrained("""bert-base-uncased"""      )
 __lowerCAmelCase											=						tokenizer(["""a picture of"""]      ).input_ids

 __lowerCAmelCase											=						hf_model.generate(lowerCAmelCase__						,		lowerCAmelCase__      )

 assert out[0].tolist() == [3_05_22, 10_37, 38_61, 19_97, 10_37, 24_50, 35_64, 20_06, 19_96, 35_09, 20_07, 20_14, 38_99, 1_02]

 __lowerCAmelCase											=						hf_model.generate(lowerCAmelCase__      )

 assert out[0].tolist() == [3_05_22, 10_37, 24_50, 35_64, 20_06, 19_96, 35_09, 20_07, 20_14, 38_99, 1_02]

 if pytorch_dump_folder_path is not None:
  hf_model.save_pretrained(lowerCAmelCase__      )

 # model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_vqa.pth'
 __lowerCAmelCase											=						(
     '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth'''
 )

 __lowerCAmelCase											=						blip_vqa(pretrained=lowerCAmelCase__						,		image_size=lowerCAmelCase__						,		vit="""base"""      )
 vqa_model.eval()

 __lowerCAmelCase											=						vqa_model.state_dict()
 for key in modified_state_dict.copy():
  __lowerCAmelCase											=						modified_state_dict.pop(lowerCAmelCase__      )
  __lowerCAmelCase											=						rename_key(lowerCAmelCase__      )
  __lowerCAmelCase											=						value

 __lowerCAmelCase											=						BlipForQuestionAnswering(lowerCAmelCase__      )

 hf_vqa_model.load_state_dict(lowerCAmelCase__      )

 __lowerCAmelCase											=						['''How many dogs are in this image?''']
 __lowerCAmelCase											=						tokenizer(lowerCAmelCase__						,		return_tensors="""pt"""      ).input_ids

 __lowerCAmelCase											=						hf_vqa_model.generate(lowerCAmelCase__						,		lowerCAmelCase__      )
 print(tokenizer.decode(answer[0]      )      )

 assert tokenizer.decode(answer[0]      ) == "[UNK] 1 [SEP]"
 if pytorch_dump_folder_path is not None:
  hf_vqa_model.save_pretrained(pytorch_dump_folder_path + """_vqa"""      )

 __lowerCAmelCase											=						'''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth'''

 __lowerCAmelCase											=						blip_itm(pretrained=lowerCAmelCase__						,		image_size=lowerCAmelCase__						,		vit="""base"""      )
 itm_model.eval()

 __lowerCAmelCase											=						itm_model.state_dict()
 for key in modified_state_dict.copy():
  __lowerCAmelCase											=						modified_state_dict.pop(lowerCAmelCase__      )
  __lowerCAmelCase											=						rename_key(lowerCAmelCase__      )
  __lowerCAmelCase											=						value

 __lowerCAmelCase											=						BlipForImageTextRetrieval(lowerCAmelCase__      )

 __lowerCAmelCase											=						['''A picture of a woman with a dog sitting in a beach''']
 __lowerCAmelCase											=						tokenizer(
     lowerCAmelCase__						,		return_tensors="""pt"""						,		padding="""max_length"""						,		truncation=lowerCAmelCase__						,		max_length=35						,		).input_ids

 hf_itm_model.load_state_dict(lowerCAmelCase__      )
 hf_itm_model.eval()

 __lowerCAmelCase											=						hf_itm_model(lowerCAmelCase__						,		lowerCAmelCase__						,		use_itm_head=lowerCAmelCase__      )
 __lowerCAmelCase											=						hf_itm_model(lowerCAmelCase__						,		lowerCAmelCase__						,		use_itm_head=lowerCAmelCase__      )

 assert out[0].item() == 0.2_1_1_0_6_8_7_4_9_4_2_7_7_9_5_4
 assert torch.nn.functional.softmax(out_itm[0]						,		dim=1      )[:, 1].item() == 0.4_5_6_9_8_8_4_5_3_8_6_5_0_5_1_2_7

 if pytorch_dump_folder_path is not None:
  hf_itm_model.save_pretrained(pytorch_dump_folder_path + """_itm"""      )


if __name__ == "__main__":
 _A			:      Dict					      =				argparse.ArgumentParser()
 parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
 parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''')
 _A			:      Optional[Any]					      =				parser.parse_args()

 convert_blip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)








def    __UpperCamelCase     (				lowerCAmelCase__							:				int = 5_0_0_0_0_0_0_0  ):
 __a :  int						=  set()
 __a :  str						=  int((limit - 2_4) ** (1 / 2)  )

 __a :  int						=  set(range(3  ,   prime_square_limit + 1  ,   2  )  )
 primes.add(2  )
 for p in range(3  ,   prime_square_limit + 1  ,   2  ):
  if p not in primes:
   continue
  primes.difference_update(set(range(p * p  ,   prime_square_limit + 1  ,   lowerCAmelCase__  )  )  )

 for primea in primes:
  __a :  Union[str, Any]						=  primea * primea
  for primea in primes:
   __a :  Union[str, Any]						=  primea * primea * primea
   if square + cube >= limit - 1_6:
    break
   for primea in primes:
    __a :  int						=  primea * primea * primea * primea
    __a :  Union[str, Any]						=  square + cube + tetr
    if total >= limit:
     break
    ret.add(lowerCAmelCase__  )

 return len(lowerCAmelCase__  )


if __name__ == "__main__":
      print(F"""{solution() = }""")
