

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()
UpperCAmelCase__     = logging.get_logger(__name__)

UpperCAmelCase__     = [
    ("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"),
]

UpperCAmelCase__     = [
    "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			(  _UpperCAmelCase       :     List[Any]					)       ->				Union[str, Any]:







			'''simple docstring'''
			_UpperCAmelCase	 =     torch.load(_UpperCAmelCase							,     map_location='cpu'					)
			return sd





def 							A			(  _UpperCAmelCase       :     Any							,     _UpperCAmelCase       :     str							,     _UpperCAmelCase       :     List[str]=rename_keys_prefix					)       ->				str:







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





@torch.no_grad()
def 							A			(  _UpperCAmelCase       :     List[Any]							,     _UpperCAmelCase       :     Dict					)       ->				List[str]:







			'''simple docstring'''
			assert (
			    checkpoint_path.split('/'					)[-1] in ACCEPTABLE_CHECKPOINTS
			), F"The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}."

			# Get Config
			if "pre" in checkpoint_path:
						_UpperCAmelCase	 =     'pretraining'
						if "vcr" in checkpoint_path:
									_UpperCAmelCase	 =     {'visual_embedding_dim': 512}
						elif "vqa_advanced" in checkpoint_path:
									_UpperCAmelCase	 =     {'visual_embedding_dim': 2_048}
						elif "vqa" in checkpoint_path:
									_UpperCAmelCase	 =     {'visual_embedding_dim': 2_048}
						elif "nlvr" in checkpoint_path:
									_UpperCAmelCase	 =     {'visual_embedding_dim': 1_024}
						else:
									raise NotImplementedError(F"No implementation found for `{checkpoint_path}`."					)
			else:
						if "vcr" in checkpoint_path:
									_UpperCAmelCase	 =     {'visual_embedding_dim': 512}
									_UpperCAmelCase	 =     'multichoice'
						elif "vqa_advanced" in checkpoint_path:
									_UpperCAmelCase	 =     {'visual_embedding_dim': 2_048}
									_UpperCAmelCase	 =     'vqa_advanced'
						elif "vqa" in checkpoint_path:
									_UpperCAmelCase	 =     {'visual_embedding_dim': 2_048, 'num_labels': 3_129}
									_UpperCAmelCase	 =     'vqa'
						elif "nlvr" in checkpoint_path:
									_UpperCAmelCase	 =     {
									    'visual_embedding_dim': 1_024,
									    'num_labels': 2,
									}
									_UpperCAmelCase	 =     'nlvr'

			_UpperCAmelCase	 =     VisualBertConfig(**_UpperCAmelCase					)

			# Load State Dict
			_UpperCAmelCase	 =     load_state_dict(_UpperCAmelCase					)

			_UpperCAmelCase	 =     get_new_dict(_UpperCAmelCase							,     _UpperCAmelCase					)

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

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


if __name__ == "__main__":
							UpperCAmelCase__     = 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.")
							UpperCAmelCase__     = parser.parse_args()
							convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)





import os

# Precomputes a list of the 100 first triangular numbers
UpperCAmelCase__     = [int(0.5 * n * (n + 1)) for n in range(1, 101)]





def 							A			(  )       ->				List[str]:







			'''simple docstring'''
			_UpperCAmelCase	 =     os.path.dirname(os.path.realpath(_UpperCAmelCase					)					)
			_UpperCAmelCase	 =     os.path.join(_UpperCAmelCase							,     'words.txt'					)

			_UpperCAmelCase	 =     ''
			with open(_UpperCAmelCase					) as f:
						_UpperCAmelCase	 =     f.readline()

			_UpperCAmelCase	 =     [word.strip('"'					) for word in words.strip('\r\n'					).split(','					)]
			_UpperCAmelCase	 =     [
			    word
			    for word in [sum(ord(_UpperCAmelCase					) - 64 for x in word					) for word in words]
			    if word in TRIANGULAR_NUMBERS
			]
			return len(_UpperCAmelCase					)


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