




"""simple docstring"""

import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
		from PIL import Image

		from transformers import AlignProcessor, EfficientNetImageProcessor

@require_vision
class        lowercase      (    unittest.TestCase):







				def 					a_  (							self   :					Dict       ):



					"""simple docstring"""

					A_  :				int	   =		tempfile.mkdtemp()

					A_  :				List[Any]	   =		[
					    '''[UNK]''',
					    '''[CLS]''',
					    '''[SEP]''',
					    '''[PAD]''',
					    '''[MASK]''',
					    '''want''',
					    '''##want''',
					    '''##ed''',
					    '''wa''',
					    '''un''',
					    '''runn''',
					    '''##ing''',
					    ''',''',
					    '''low''',
					    '''lowest''',
					]
					A_  :				List[str]	   =		os.path.join(self.tmpdirname      ,     VOCAB_FILES_NAMES['''vocab_file''']       )
					with open(self.vocab_file      ,     '''w'''      ,     encoding='''utf-8'''       ) as vocab_writer:
						vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]       )       )

					A_  :				Dict	   =		{
					    '''do_resize''': True,
					    '''size''': 20,
					    '''do_center_crop''': True,
					    '''crop_size''': 18,
					    '''do_normalize''': True,
					    '''image_mean''': [0.48145466, 0.4578275, 0.40821073],
					    '''image_std''': [0.26862954, 0.26130258, 0.27577711],
					}
					A_  :				Tuple	   =		os.path.join(self.tmpdirname      ,     _lowerCamelCase       )
					with open(self.image_processor_file      ,     '''w'''      ,     encoding='''utf-8'''       ) as fp:
						json.dump(_lowerCamelCase      ,     _lowerCamelCase       )







				def 					a_  (							self   :					List[Any]      ,     **_lowerCamelCase   :					str       ):



					"""simple docstring"""

					return BertTokenizer.from_pretrained(self.tmpdirname      ,     **_lowerCamelCase       )







				def 					a_  (							self   :					Tuple      ,     **_lowerCamelCase   :					Dict       ):



					"""simple docstring"""

					return BertTokenizerFast.from_pretrained(self.tmpdirname      ,     **_lowerCamelCase       )







				def 					a_  (							self   :					Union[str, Any]      ,     **_lowerCamelCase   :					List[Any]       ):



					"""simple docstring"""

					return EfficientNetImageProcessor.from_pretrained(self.tmpdirname      ,     **_lowerCamelCase       )







				def 					a_  (							self   :					str       ):



					"""simple docstring"""

					shutil.rmtree(self.tmpdirname       )







				def 					a_  (							self   :					int       ):



					"""simple docstring"""

					A_  :				str	   =		[np.random.randint(2_55      ,     size=(3, 30, 4_00)      ,     dtype=np.uinta       )]
					A_  :				Optional[int]	   =		[Image.fromarray(np.moveaxis(_lowerCamelCase      ,     0      ,     -1       )       ) for x in image_inputs]
					return image_inputs







				def 					a_  (							self   :					Optional[Any]       ):



					"""simple docstring"""

					A_  :				Any	   =		self.get_tokenizer()
					A_  :				Tuple	   =		self.get_rust_tokenizer()
					A_  :				List[str]	   =		self.get_image_processor()

					A_  :				str	   =		AlignProcessor(tokenizer=_lowerCamelCase      ,     image_processor=_lowerCamelCase       )
					processor_slow.save_pretrained(self.tmpdirname       )
					A_  :				str	   =		AlignProcessor.from_pretrained(self.tmpdirname      ,     use_fast=_lowerCamelCase       )

					A_  :				str	   =		AlignProcessor(tokenizer=_lowerCamelCase      ,     image_processor=_lowerCamelCase       )
					processor_fast.save_pretrained(self.tmpdirname       )
					A_  :				Optional[Any]	   =		AlignProcessor.from_pretrained(self.tmpdirname       )

					self.assertEqual(processor_slow.tokenizer.get_vocab()      ,     tokenizer_slow.get_vocab()       )
					self.assertEqual(processor_fast.tokenizer.get_vocab()      ,     tokenizer_fast.get_vocab()       )
					self.assertEqual(tokenizer_slow.get_vocab()      ,     tokenizer_fast.get_vocab()       )
					self.assertIsInstance(processor_slow.tokenizer      ,     _lowerCamelCase       )
					self.assertIsInstance(processor_fast.tokenizer      ,     _lowerCamelCase       )

					self.assertEqual(processor_slow.image_processor.to_json_string()      ,     image_processor.to_json_string()       )
					self.assertEqual(processor_fast.image_processor.to_json_string()      ,     image_processor.to_json_string()       )
					self.assertIsInstance(processor_slow.image_processor      ,     _lowerCamelCase       )
					self.assertIsInstance(processor_fast.image_processor      ,     _lowerCamelCase       )







				def 					a_  (							self   :					Dict       ):



					"""simple docstring"""

					A_  :				Any	   =		AlignProcessor(tokenizer=self.get_tokenizer()      ,     image_processor=self.get_image_processor()       )
					processor.save_pretrained(self.tmpdirname       )

					A_  :				Dict	   =		self.get_tokenizer(bos_token='''(BOS)'''      ,     eos_token='''(EOS)'''       )
					A_  :				str	   =		self.get_image_processor(do_normalize=_lowerCamelCase      ,     padding_value=1.0       )

					A_  :				Union[str, Any]	   =		AlignProcessor.from_pretrained(
					    self.tmpdirname      ,     bos_token='''(BOS)'''      ,     eos_token='''(EOS)'''      ,     do_normalize=_lowerCamelCase      ,     padding_value=1.0       )

					self.assertEqual(processor.tokenizer.get_vocab()      ,     tokenizer_add_kwargs.get_vocab()       )
					self.assertIsInstance(processor.tokenizer      ,     _lowerCamelCase       )

					self.assertEqual(processor.image_processor.to_json_string()      ,     image_processor_add_kwargs.to_json_string()       )
					self.assertIsInstance(processor.image_processor      ,     _lowerCamelCase       )







				def 					a_  (							self   :					List[Any]       ):



					"""simple docstring"""

					A_  :				Tuple	   =		self.get_image_processor()
					A_  :				str	   =		self.get_tokenizer()

					A_  :				str	   =		AlignProcessor(tokenizer=_lowerCamelCase      ,     image_processor=_lowerCamelCase       )

					A_  :				List[Any]	   =		self.prepare_image_inputs()

					A_  :				List[str]	   =		image_processor(_lowerCamelCase      ,     return_tensors='''np'''       )
					A_  :				Any	   =		processor(images=_lowerCamelCase      ,     return_tensors='''np'''       )

					for key in input_image_proc.keys():
						self.assertAlmostEqual(input_image_proc[key].sum()      ,     input_processor[key].sum()      ,     delta=1E-2       )







				def 					a_  (							self   :					Optional[Any]       ):



					"""simple docstring"""

					A_  :				Union[str, Any]	   =		self.get_image_processor()
					A_  :				List[str]	   =		self.get_tokenizer()

					A_  :				str	   =		AlignProcessor(tokenizer=_lowerCamelCase      ,     image_processor=_lowerCamelCase       )

					A_  :				Optional[int]	   =		'''lower newer'''

					A_  :				List[Any]	   =		processor(text=_lowerCamelCase       )

					A_  :				Union[str, Any]	   =		tokenizer(_lowerCamelCase      ,     padding='''max_length'''      ,     max_length=64       )

					for key in encoded_tok.keys():
						self.assertListEqual(encoded_tok[key]      ,     encoded_processor[key]       )







				def 					a_  (							self   :					Any       ):



					"""simple docstring"""

					A_  :				Tuple	   =		self.get_image_processor()
					A_  :				List[Any]	   =		self.get_tokenizer()

					A_  :				Tuple	   =		AlignProcessor(tokenizer=_lowerCamelCase      ,     image_processor=_lowerCamelCase       )

					A_  :				Dict	   =		'''lower newer'''
					A_  :				int	   =		self.prepare_image_inputs()

					A_  :				Optional[Any]	   =		processor(text=_lowerCamelCase      ,     images=_lowerCamelCase       )

					self.assertListEqual(list(inputs.keys()       )      ,     ['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values''']       )

					# test if it raises when no input is passed
					with pytest.raises(_lowerCamelCase       ):
						processor()







				def 					a_  (							self   :					Optional[int]       ):



					"""simple docstring"""

					A_  :				int	   =		self.get_image_processor()
					A_  :				List[Any]	   =		self.get_tokenizer()

					A_  :				Union[str, Any]	   =		AlignProcessor(tokenizer=_lowerCamelCase      ,     image_processor=_lowerCamelCase       )

					A_  :				str	   =		[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

					A_  :				List[Any]	   =		processor.batch_decode(_lowerCamelCase       )
					A_  :				Tuple	   =		tokenizer.batch_decode(_lowerCamelCase       )

					self.assertListEqual(_lowerCamelCase      ,     _lowerCamelCase       )







				def 					a_  (							self   :					Optional[int]       ):



					"""simple docstring"""

					A_  :				Dict	   =		self.get_image_processor()
					A_  :				Union[str, Any]	   =		self.get_tokenizer()

					A_  :				Optional[int]	   =		AlignProcessor(tokenizer=_lowerCamelCase      ,     image_processor=_lowerCamelCase       )

					A_  :				List[str]	   =		'''lower newer'''
					A_  :				Optional[int]	   =		self.prepare_image_inputs()

					A_  :				Any	   =		processor(text=_lowerCamelCase      ,     images=_lowerCamelCase       )

					self.assertListEqual(list(inputs.keys()       )      ,     processor.model_input_names       )











"""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  :			Optional[Any]        =       logging.get_logger(__name__)

_lowerCamelCase  :			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 		lowercase_      (		_UpperCAmelCase	):




	"""simple docstring"""






	A_  :				Union[str, Any]	   =		torch.load(_UpperCAmelCase  ,      map_location='''cpu'''	)
	return sd

def 		lowercase_      (		_UpperCAmelCase  ,      _UpperCAmelCase  ,      _UpperCAmelCase=rename_keys_prefix	):




	"""simple docstring"""






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

@torch.no_grad()
def 		lowercase_      (		_UpperCAmelCase  ,      _UpperCAmelCase	):




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

	A_  :				Dict	   =		VisualBertConfig(**_UpperCAmelCase	)

	# Load State Dict
	A_  :				List[str]	   =		load_state_dict(_UpperCAmelCase	)

	A_  :				List[str]	   =		get_new_dict(_UpperCAmelCase  ,      _UpperCAmelCase	)

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

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


if __name__ == "__main__":
		_lowerCamelCase  :			Dict        =       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)





