


import argparse
from collections import OrderedDict
from pathlib import Path

import torch

from transformers import (
    VisualBertConfig,
    VisualBertForMultipleChoice,
    VisualBertForPreTraining,
    VisualBertForQuestionAnswering,
    VisualBertForVisualReasoning,
)
from transformers.utils import logging


logging.set_verbosity_info()
lowercase_			       =      logging.get_logger(__name__)

lowercase_			       =      [
    ('bert.bert', 'visual_bert'),
    ('bert.cls', 'cls'),
    ('bert.classifier', 'cls'),
    ('token_type_embeddings_visual', 'visual_token_type_embeddings'),
    ('position_embeddings_visual', 'visual_position_embeddings'),
    ('projection', 'visual_projection'),
]

lowercase_			       =      [
    'nlvr2_coco_pre_trained.th',
    'nlvr2_fine_tuned.th',
    'nlvr2_pre_trained.th',
    'vcr_coco_pre_train.th',
    'vcr_fine_tune.th',
    'vcr_pre_train.th',
    'vqa_coco_pre_trained.th',
    'vqa_fine_tuned.th',
    'vqa_pre_trained.th',
]
def  UpperCamelCase__	(    SCREAMING_SNAKE_CASE__		):
 __lowerCamelCase						:				List[Any]						   =		torch.load(UpperCamelCase__     ,			map_location='cpu'		)
 return sd
def  UpperCamelCase__	(    SCREAMING_SNAKE_CASE__     ,			SCREAMING_SNAKE_CASE__     ,			SCREAMING_SNAKE_CASE__=rename_keys_prefix		):
 __lowerCamelCase						:				List[str]						   =		OrderedDict()
 __lowerCamelCase						:				Optional[Any]						   =		torch.arange(config.max_position_embeddings		).expand((1, -1)		)
 # detector_d = OrderedDict()
 for key in d:
  if "detector" in key:
   # detector_d[key.replace('detector.','')] = d[key]
   continue
  __lowerCamelCase						:				Union[str, Any]						   =		key
  for name_pair in rename_keys_prefix:
   __lowerCamelCase						:				Optional[int]						   =		new_key.replace(name_pair[0]     ,			name_pair[1]		)
  __lowerCamelCase						:				Optional[Any]						   =		d[key]
  if key == "bert.cls.predictions.decoder.weight":
   # Old bert code didn't have `decoder.bias`, but was added separately
   __lowerCamelCase						:				int						   =		new_d['cls.predictions.bias']
 return new_d
@torch.no_grad()
def  UpperCamelCase__	(    SCREAMING_SNAKE_CASE__     ,			SCREAMING_SNAKE_CASE__		):
 assert (
     checkpoint_path.split('/'		)[-1] in ACCEPTABLE_CHECKPOINTS
 ), f'The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.'

 # Get Config
 if "pre" in checkpoint_path:
  __lowerCamelCase						:				Any						   =		'pretraining'
  if "vcr" in checkpoint_path:
   __lowerCamelCase						:				str						   =		{'visual_embedding_dim': 512}
  elif "vqa_advanced" in checkpoint_path:
   __lowerCamelCase						:				Union[str, Any]						   =		{'visual_embedding_dim': 2_048}
  elif "vqa" in checkpoint_path:
   __lowerCamelCase						:				Any						   =		{'visual_embedding_dim': 2_048}
  elif "nlvr" in checkpoint_path:
   __lowerCamelCase						:				List[str]						   =		{'visual_embedding_dim': 1_024}
  else:
   raise NotImplementedError(f'No implementation found for `{checkpoint_path}`.'		)
 else:
  if "vcr" in checkpoint_path:
   __lowerCamelCase						:				List[str]						   =		{'visual_embedding_dim': 512}
   __lowerCamelCase						:				Optional[Any]						   =		'multichoice'
  elif "vqa_advanced" in checkpoint_path:
   __lowerCamelCase						:				Optional[Any]						   =		{'visual_embedding_dim': 2_048}
   __lowerCamelCase						:				Dict						   =		'vqa_advanced'
  elif "vqa" in checkpoint_path:
   __lowerCamelCase						:				Union[str, Any]						   =		{'visual_embedding_dim': 2_048, 'num_labels': 3_129}
   __lowerCamelCase						:				Union[str, Any]						   =		'vqa'
  elif "nlvr" in checkpoint_path:
   __lowerCamelCase						:				Union[str, Any]						   =		{
       'visual_embedding_dim': 1_024,
       'num_labels': 2,
   }
   __lowerCamelCase						:				List[Any]						   =		'nlvr'

 __lowerCamelCase						:				str						   =		VisualBertConfig(**UpperCamelCase__		)

 # Load State Dict
 __lowerCamelCase						:				List[Any]						   =		load_state_dict(UpperCamelCase__		)

 __lowerCamelCase						:				str						   =		get_new_dict(UpperCamelCase__     ,			UpperCamelCase__		)

 if model_type == "pretraining":
  __lowerCamelCase						:				Dict						   =		VisualBertForPreTraining(UpperCamelCase__		)
 elif model_type == "vqa":
  __lowerCamelCase						:				str						   =		VisualBertForQuestionAnswering(UpperCamelCase__		)
 elif model_type == "nlvr":
  __lowerCamelCase						:				Union[str, Any]						   =		VisualBertForVisualReasoning(UpperCamelCase__		)
 elif model_type == "multichoice":
  __lowerCamelCase						:				Union[str, Any]						   =		VisualBertForMultipleChoice(UpperCamelCase__		)

 model.load_state_dict(UpperCamelCase__		)
 # Save Checkpoints
 Path(UpperCamelCase__		).mkdir(exist_ok=UpperCamelCase__		)
 model.save_pretrained(UpperCamelCase__		)


if __name__ == "__main__":
       lowercase_			       =      argparse.ArgumentParser()
       # Required parameters
       parser.add_argument('orig_checkpoint_path', type=str, help='A path to .th on local filesystem.')
       parser.add_argument('pytorch_dump_folder_path', type=str, help='Path to the output PyTorch model.')
       lowercase_			       =      parser.parse_args()
       convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_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())





