




"""simple docstring"""




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()
lowerCAmelCase							:    Union[str, Any]  							=							logging.get_logger(__name__)

lowerCAmelCase							:    Optional[Any]  							=							[
    ("""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"""),
]

lowerCAmelCase							:    int  							=							[
    """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 					a__				(       snake_case__       )		->      Union[str, Any]:
			lowerCamelCase       =      torch.load(snake_case__ ,				map_location="""cpu"""       )
			return sd



def 					a__				(       snake_case__ ,				snake_case__ ,				snake_case__=rename_keys_prefix       )		->      str:
			lowerCamelCase       =      OrderedDict()
			lowerCamelCase       =      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       =      key
						for name_pair in rename_keys_prefix:
									lowerCamelCase       =      new_key.replace(name_pair[0] ,				name_pair[1]       )
						lowerCamelCase       =      d[key]
						if key == "bert.cls.predictions.decoder.weight":
									# Old bert code didn't have `decoder.bias`, but was added separately
									lowerCamelCase       =      new_d["""cls.predictions.bias"""]
			return new_d



@torch.no_grad()
def 					a__				(       snake_case__ ,				snake_case__       )		->      Any:
			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       =      """pretraining"""
						if "vcr" in checkpoint_path:
									lowerCamelCase       =      {"""visual_embedding_dim""": 5_12}
						elif "vqa_advanced" in checkpoint_path:
									lowerCamelCase       =      {"""visual_embedding_dim""": 20_48}
						elif "vqa" in checkpoint_path:
									lowerCamelCase       =      {"""visual_embedding_dim""": 20_48}
						elif "nlvr" in checkpoint_path:
									lowerCamelCase       =      {"""visual_embedding_dim""": 10_24}
						else:
									raise NotImplementedError(F'No implementation found for `{checkpoint_path}`.'       )
			else:
						if "vcr" in checkpoint_path:
									lowerCamelCase       =      {"""visual_embedding_dim""": 5_12}
									lowerCamelCase       =      """multichoice"""
						elif "vqa_advanced" in checkpoint_path:
									lowerCamelCase       =      {"""visual_embedding_dim""": 20_48}
									lowerCamelCase       =      """vqa_advanced"""
						elif "vqa" in checkpoint_path:
									lowerCamelCase       =      {"""visual_embedding_dim""": 20_48, """num_labels""": 31_29}
									lowerCamelCase       =      """vqa"""
						elif "nlvr" in checkpoint_path:
									lowerCamelCase       =      {
									    """visual_embedding_dim""": 10_24,
									    """num_labels""": 2,
									}
									lowerCamelCase       =      """nlvr"""

			lowerCamelCase       =      VisualBertConfig(**snake_case__       )

			# Load State Dict
			lowerCamelCase       =      load_state_dict(snake_case__       )

			lowerCamelCase       =      get_new_dict(snake_case__ ,				snake_case__       )

			if model_type == "pretraining":
						lowerCamelCase       =      VisualBertForPreTraining(snake_case__       )
			elif model_type == "vqa":
						lowerCamelCase       =      VisualBertForQuestionAnswering(snake_case__       )
			elif model_type == "nlvr":
						lowerCamelCase       =      VisualBertForVisualReasoning(snake_case__       )
			elif model_type == "multichoice":
						lowerCamelCase       =      VisualBertForMultipleChoice(snake_case__       )

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


if __name__ == "__main__":
					lowerCAmelCase							:    List[Any]  							=							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.""")
					lowerCAmelCase							:    Optional[int]  							=							parser.parse_args()
					convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)









"""simple docstring"""




from functools import lru_cache



def 					a__				(       snake_case__       )		->      set:
			lowerCamelCase       =      2
			lowerCamelCase       =      set()
			while i * i <= n:
						if n % i:
									i += 1
						else:
									n //= i
									factors.add(snake_case__       )
			if n > 1:
						factors.add(snake_case__       )
			return factors



@lru_cache
def 					a__				(       snake_case__       )		->      int:
			return len(unique_prime_factors(snake_case__       )       )



def 					a__				(       snake_case__       )		->      bool:
			return len(set(snake_case__       )       ) in (0, 1)



def 					a__				(       snake_case__       )		->      list:
			lowerCamelCase       =      2

			while True:
						# Increment each value of a generated range
						lowerCamelCase       =      [base + i for i in range(snake_case__       )]

						# Run elements through out unique_prime_factors function
						# Append our target number to the end.
						lowerCamelCase       =      [upf_len(snake_case__       ) for x in group]
						checker.append(snake_case__       )

						# If all numbers in the list are equal, return the group variable.
						if equality(snake_case__       ):
									return group

						# Increment our base variable by 1
						base += 1



def 					a__				(       snake_case__ = 4       )		->      int:
			lowerCamelCase       =      run(snake_case__       )
			return results[0] if len(snake_case__       ) else None


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



