




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

a__		     =     [
    ('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'),
]

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





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





@torch.no_grad()
def 				A__ (snake_case   :		str		,	snake_case   :		Optional[Any]				)     ->						Dict:
					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					:	Optional[Any]             =      "pretraining"
										if "vcr" in checkpoint_path:
															__UpperCamelCase					:	int             =      {"visual_embedding_dim": 5_12}
										elif "vqa_advanced" in checkpoint_path:
															__UpperCamelCase					:	Optional[int]             =      {"visual_embedding_dim": 20_48}
										elif "vqa" in checkpoint_path:
															__UpperCamelCase					:	List[Any]             =      {"visual_embedding_dim": 20_48}
										elif "nlvr" in checkpoint_path:
															__UpperCamelCase					:	Optional[int]             =      {"visual_embedding_dim": 10_24}
										else:
															raise NotImplementedError(F'''No implementation found for `{checkpoint_path}`.'''				)
					else:
										if "vcr" in checkpoint_path:
															__UpperCamelCase					:	Union[str, Any]             =      {"visual_embedding_dim": 5_12}
															__UpperCamelCase					:	str             =      "multichoice"
										elif "vqa_advanced" in checkpoint_path:
															__UpperCamelCase					:	Dict             =      {"visual_embedding_dim": 20_48}
															__UpperCamelCase					:	Optional[Any]             =      "vqa_advanced"
										elif "vqa" in checkpoint_path:
															__UpperCamelCase					:	List[Any]             =      {"visual_embedding_dim": 20_48, "num_labels": 31_29}
															__UpperCamelCase					:	str             =      "vqa"
										elif "nlvr" in checkpoint_path:
															__UpperCamelCase					:	Dict             =      {
															    "visual_embedding_dim": 10_24,
															    "num_labels": 2,
															}
															__UpperCamelCase					:	Dict             =      "nlvr"

					__UpperCamelCase					:	Optional[int]             =      VisualBertConfig(**_lowerCamelCase				)

					# Load State Dict
					__UpperCamelCase					:	List[Any]             =      load_state_dict(_lowerCamelCase				)

					__UpperCamelCase					:	str             =      get_new_dict(_lowerCamelCase		,	_lowerCamelCase				)

					if model_type == "pretraining":
										__UpperCamelCase					:	Any             =      VisualBertForPreTraining(_lowerCamelCase				)
					elif model_type == "vqa":
										__UpperCamelCase					:	List[str]             =      VisualBertForQuestionAnswering(_lowerCamelCase				)
					elif model_type == "nlvr":
										__UpperCamelCase					:	Optional[Any]             =      VisualBertForVisualReasoning(_lowerCamelCase				)
					elif model_type == "multichoice":
										__UpperCamelCase					:	Optional[int]             =      VisualBertForMultipleChoice(_lowerCamelCase				)

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


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








from .testing import (
    are_the_same_tensors,
    execute_subprocess_async,
    require_bnb,
    require_cpu,
    require_cuda,
    require_huggingface_suite,
    require_mps,
    require_multi_gpu,
    require_multi_xpu,
    require_safetensors,
    require_single_gpu,
    require_single_xpu,
    require_torch_min_version,
    require_tpu,
    require_xpu,
    skip,
    slow,
)
from .training import RegressionDataset, RegressionModel, RegressionModelaXPU


from .scripts import test_script, test_sync, test_ops  # isort: skip