





'''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 : str											=			logging.get_logger(__name__)

__lowerCamelCase : Optional[int]											=			[
    ("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 : str											=			[
    "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_    (	lowerCAmelCase_							):

				"""simple docstring"""






				lowercase         = torch.load(lowerCAmelCase_		,							map_location="cpu"							)
				return sd







def 							UpperCAmelCase_    (	lowerCAmelCase_		,							lowerCAmelCase_		,							lowerCAmelCase_=rename_keys_prefix							):

				"""simple docstring"""






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







@torch.no_grad()
def 							UpperCAmelCase_    (	lowerCAmelCase_		,							lowerCAmelCase_							):

				"""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:
								lowercase         = "pretraining"
								if "vcr" in checkpoint_path:
												lowercase         = {"visual_embedding_dim": 512}
								elif "vqa_advanced" in checkpoint_path:
												lowercase         = {"visual_embedding_dim": 2048}
								elif "vqa" in checkpoint_path:
												lowercase         = {"visual_embedding_dim": 2048}
								elif "nlvr" in checkpoint_path:
												lowercase         = {"visual_embedding_dim": 1024}
								else:
												raise NotImplementedError(f'No implementation found for `{checkpoint_path}`.'							)
				else:
								if "vcr" in checkpoint_path:
												lowercase         = {"visual_embedding_dim": 512}
												lowercase         = "multichoice"
								elif "vqa_advanced" in checkpoint_path:
												lowercase         = {"visual_embedding_dim": 2048}
												lowercase         = "vqa_advanced"
								elif "vqa" in checkpoint_path:
												lowercase         = {"visual_embedding_dim": 2048, "num_labels": 3129}
												lowercase         = "vqa"
								elif "nlvr" in checkpoint_path:
												lowercase         = {
												    "visual_embedding_dim": 1024,
												    "num_labels": 2,
												}
												lowercase         = "nlvr"

				lowercase         = VisualBertConfig(**lowerCAmelCase_							)

				# Load State Dict
				lowercase         = load_state_dict(lowerCAmelCase_							)

				lowercase         = get_new_dict(lowerCAmelCase_		,							lowerCAmelCase_							)

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

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


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











'''simple docstring'''

import inspect
import unittest

from transformers import RegNetConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch
			from torch import nn

			from transformers import RegNetForImageClassification, RegNetModel
			from transformers.models.regnet.modeling_regnet import REGNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor

class       UpperCAmelCase :




		def __init__(self      :	Dict							,			A__      :	str							,			A__      :	int=3							,			A__      :	Dict=3_2							,			A__      :	str=3							,			A__      :	str=1_0							,			A__      :	Optional[int]=[1_0, 2_0, 3_0, 4_0]							,			A__      :	Tuple=[1, 1, 2, 1]							,			A__      :	int=True							,			A__      :	List[Any]=True							,			A__      :	List[Any]="relu"							,			A__      :	Any=3							,			A__      :	Any=None							,			)					-> Tuple:
						lowercase         = parent
						lowercase         = batch_size
						lowercase         = image_size
						lowercase         = num_channels
						lowercase         = embeddings_size
						lowercase         = hidden_sizes
						lowercase         = depths
						lowercase         = is_training
						lowercase         = use_labels
						lowercase         = hidden_act
						lowercase         = num_labels
						lowercase         = scope
						lowercase         = len(A__				)




		def 				UpperCAmelCase__   (self      :	str				)					-> List[str]:
						lowercase         = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

						lowercase         = None
						if self.use_labels:
										lowercase         = ids_tensor([self.batch_size]							,			self.num_labels				)

						lowercase         = self.get_config()

						return config, pixel_values, labels




		def 				UpperCAmelCase__   (self      :	Optional[Any]				)					-> List[str]:
						return RegNetConfig(
						    num_channels=self.num_channels							,			embeddings_size=self.embeddings_size							,			hidden_sizes=self.hidden_sizes							,			depths=self.depths							,			hidden_act=self.hidden_act							,			num_labels=self.num_labels							,			)




		def 				UpperCAmelCase__   (self      :	Dict							,			A__      :	Union[str, Any]							,			A__      :	Optional[int]							,			A__      :	Dict				)					-> str:
						lowercase         = RegNetModel(config=A__				)
						model.to(A__				)
						model.eval()
						lowercase         = model(A__				)
						# expected last hidden states: B, C, H // 32, W // 32
						self.parent.assertEqual(
						    result.last_hidden_state.shape							,			(self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)							,			)




		def 				UpperCAmelCase__   (self      :	List[str]							,			A__      :	List[str]							,			A__      :	Union[str, Any]							,			A__      :	str				)					-> Dict:
						lowercase         = self.num_labels
						lowercase         = RegNetForImageClassification(A__				)
						model.to(A__				)
						model.eval()
						lowercase         = model(A__							,			labels=A__				)
						self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_labels)				)





		def 				UpperCAmelCase__   (self      :	Any				)					-> Union[str, Any]:
						lowercase         = self.prepare_config_and_inputs()
						lowercase   ,							lowercase   ,							lowercase         = config_and_inputs
						lowercase         = {"pixel_values": pixel_values}
						return config, inputs_dict




@require_torch
class       UpperCAmelCase (						_lowercase			,       _lowercase			,       unittest.TestCase  ):
		UpperCAmelCase					:	str 			=		(RegNetModel, RegNetForImageClassification) if is_torch_available() else ()
		UpperCAmelCase					:	Dict 			=		(
		    {'''feature-extraction''': RegNetModel, '''image-classification''': RegNetForImageClassification}
		    if is_torch_available()
		    else {}
		)

		UpperCAmelCase					:	Dict 			=		False
		UpperCAmelCase					:	int 			=		False
		UpperCAmelCase					:	Tuple 			=		False
		UpperCAmelCase					:	Tuple 			=		False




		def 				UpperCAmelCase__   (self      :	Optional[int]				)					-> Tuple:
						lowercase         = RegNetModelTester(self				)
						lowercase         = ConfigTester(self							,			config_class=A__							,			has_text_modality=A__				)




		def 				UpperCAmelCase__   (self      :	List[Any]				)					-> str:
						self.create_and_test_config_common_properties()
						self.config_tester.create_and_test_config_to_json_string()
						self.config_tester.create_and_test_config_to_json_file()
						self.config_tester.create_and_test_config_from_and_save_pretrained()
						self.config_tester.create_and_test_config_with_num_labels()
						self.config_tester.check_config_can_be_init_without_params()
						self.config_tester.check_config_arguments_init()




		def 				UpperCAmelCase__   (self      :	Optional[Any]				)					-> int:
						return




		@unittest.skip(reason="RegNet does not use inputs_embeds"				)
		def 				UpperCAmelCase__   (self      :	int				)					-> Optional[int]:
						pass




		@unittest.skip(reason="RegNet does not support input and output embeddings"				)
		def 				UpperCAmelCase__   (self      :	List[Any]				)					-> Optional[Any]:
						pass




		def 				UpperCAmelCase__   (self      :	Any				)					-> str:
						lowercase   ,							lowercase         = self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										lowercase         = model_class(A__				)
										lowercase         = inspect.signature(model.forward				)
										# signature.parameters is an OrderedDict => so arg_names order is deterministic
										lowercase         = [*signature.parameters.keys()]

										lowercase         = ["pixel_values"]
										self.assertListEqual(arg_names[:1]							,			A__				)




		def 				UpperCAmelCase__   (self      :	List[Any]				)					-> Union[str, Any]:
						lowercase         = self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A__				)




		def 				UpperCAmelCase__   (self      :	List[str]				)					-> Tuple:
						lowercase   ,							lowercase         = self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										lowercase         = model_class(config=A__				)
										for name, module in model.named_modules():
														if isinstance(A__							,			(nn.BatchNormad, nn.GroupNorm)				):
																		self.assertTrue(
																		    torch.all(module.weight == 1				)							,			msg=f'Parameter {name} of model {model_class} seems not properly initialized'							,			)
																		self.assertTrue(
																		    torch.all(module.bias == 0				)							,			msg=f'Parameter {name} of model {model_class} seems not properly initialized'							,			)




		def 				UpperCAmelCase__   (self      :	Optional[Any]				)					-> List[str]:
						def check_hidden_states_output(A__      :	Optional[Any]							,			A__      :	List[str]							,			A__      :	Tuple				):
										lowercase         = model_class(A__				)
										model.to(A__				)
										model.eval()

										with torch.no_grad():
														lowercase         = model(**self._prepare_for_class(A__							,			A__				)				)

										lowercase         = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

										lowercase         = self.model_tester.num_stages
										self.assertEqual(len(A__				)							,			expected_num_stages + 1				)

										# RegNet's feature maps are of shape (batch_size, num_channels, height, width)
										self.assertListEqual(
										    list(hidden_states[0].shape[-2:]				)							,			[self.model_tester.image_size // 2, self.model_tester.image_size // 2]							,			)

						lowercase   ,							lowercase         = self.model_tester.prepare_config_and_inputs_for_common()
						lowercase         = ["basic", "bottleneck"]
						for model_class in self.all_model_classes:
										for layer_type in layers_type:
														lowercase         = layer_type
														lowercase         = True
														check_hidden_states_output(A__							,			A__							,			A__				)

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														lowercase         = True

														check_hidden_states_output(A__							,			A__							,			A__				)




		def 				UpperCAmelCase__   (self      :	List[Any]				)					-> Tuple:
						lowercase         = self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*A__				)





		@slow
		def 				UpperCAmelCase__   (self      :	Tuple				)					-> Any:
						for model_name in REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowercase         = RegNetModel.from_pretrained(A__				)
										self.assertIsNotNone(A__				)







def 							UpperCAmelCase_    (	):

				"""simple docstring"""






				lowercase         = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
				return image


@require_torch
@require_vision
class       UpperCAmelCase (						unittest.TestCase  ):




		@cached_property
		def 				UpperCAmelCase__   (self      :	Optional[int]				)					-> Union[str, Any]:
						return (
						    AutoImageProcessor.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]				)
						    if is_vision_available()
						    else None
						)





		@slow
		def 				UpperCAmelCase__   (self      :	List[str]				)					-> int:
						lowercase         = RegNetForImageClassification.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]				).to(A__				)

						lowercase         = self.default_image_processor
						lowercase         = prepare_img()
						lowercase         = image_processor(images=A__							,			return_tensors="pt"				).to(A__				)

						# forward pass
						with torch.no_grad():
										lowercase         = model(**A__				)

						# verify the logits
						lowercase         = torch.Size((1, 1_0_0_0)				)
						self.assertEqual(outputs.logits.shape							,			A__				)

						lowercase         = torch.tensor([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6]				).to(A__				)

						self.assertTrue(torch.allclose(outputs.logits[0, :3]							,			A__							,			atol=1e-4				)				)




