
"""simple docstring"""






import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    BertTokenizer,
    ViltConfig,
    ViltForImageAndTextRetrieval,
    ViltForImagesAndTextClassification,
    ViltForMaskedLM,
    ViltForQuestionAnswering,
    ViltImageProcessor,
    ViltProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
UpperCAmelCase        =				logging.get_logger(__name__)




def __magic_name__	(				_lowerCamelCase:		List[str],					_lowerCamelCase:		Tuple=False,					_lowerCamelCase:		Optional[int]=False,					_lowerCamelCase:		Union[str, Any]=False			)					->						Union[str, Any]:

	'''simple docstring'''




	lowerCAmelCase												=  []
	for i in range(config.num_hidden_layers			):
		# encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
		rename_keys.append((F"""transformer.blocks.{i}.norm1.weight""", F"""vilt.encoder.layer.{i}.layernorm_before.weight""")			)
		rename_keys.append((F"""transformer.blocks.{i}.norm1.bias""", F"""vilt.encoder.layer.{i}.layernorm_before.bias""")			)
		rename_keys.append(
		    (F"""transformer.blocks.{i}.attn.proj.weight""", F"""vilt.encoder.layer.{i}.attention.output.dense.weight""")			)
		rename_keys.append(
		    (F"""transformer.blocks.{i}.attn.proj.bias""", F"""vilt.encoder.layer.{i}.attention.output.dense.bias""")			)
		rename_keys.append((F"""transformer.blocks.{i}.norm2.weight""", F"""vilt.encoder.layer.{i}.layernorm_after.weight""")			)
		rename_keys.append((F"""transformer.blocks.{i}.norm2.bias""", F"""vilt.encoder.layer.{i}.layernorm_after.bias""")			)
		rename_keys.append(
		    (F"""transformer.blocks.{i}.mlp.fc1.weight""", F"""vilt.encoder.layer.{i}.intermediate.dense.weight""")			)
		rename_keys.append((F"""transformer.blocks.{i}.mlp.fc1.bias""", F"""vilt.encoder.layer.{i}.intermediate.dense.bias""")			)
		rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.weight""", F"""vilt.encoder.layer.{i}.output.dense.weight""")			)
		rename_keys.append((F"""transformer.blocks.{i}.mlp.fc2.bias""", F"""vilt.encoder.layer.{i}.output.dense.bias""")			)

	# embeddings
	rename_keys.extend(
	    [
	        # text embeddings
	        ('''text_embeddings.word_embeddings.weight''', '''vilt.embeddings.text_embeddings.word_embeddings.weight'''),
	        (
	            '''text_embeddings.position_embeddings.weight''',
	            '''vilt.embeddings.text_embeddings.position_embeddings.weight''',
	        ),
	        ('''text_embeddings.position_ids''', '''vilt.embeddings.text_embeddings.position_ids'''),
	        (
	            '''text_embeddings.token_type_embeddings.weight''',
	            '''vilt.embeddings.text_embeddings.token_type_embeddings.weight''',
	        ),
	        ('''text_embeddings.LayerNorm.weight''', '''vilt.embeddings.text_embeddings.LayerNorm.weight'''),
	        ('''text_embeddings.LayerNorm.bias''', '''vilt.embeddings.text_embeddings.LayerNorm.bias'''),
	        # patch embeddings
	        ('''transformer.cls_token''', '''vilt.embeddings.cls_token'''),
	        ('''transformer.patch_embed.proj.weight''', '''vilt.embeddings.patch_embeddings.projection.weight'''),
	        ('''transformer.patch_embed.proj.bias''', '''vilt.embeddings.patch_embeddings.projection.bias'''),
	        ('''transformer.pos_embed''', '''vilt.embeddings.position_embeddings'''),
	        # token type embeddings
	        ('''token_type_embeddings.weight''', '''vilt.embeddings.token_type_embeddings.weight'''),
	    ]			)

	# final layernorm + pooler
	rename_keys.extend(
	    [
	        ('''transformer.norm.weight''', '''vilt.layernorm.weight'''),
	        ('''transformer.norm.bias''', '''vilt.layernorm.bias'''),
	        ('''pooler.dense.weight''', '''vilt.pooler.dense.weight'''),
	        ('''pooler.dense.bias''', '''vilt.pooler.dense.bias'''),
	    ]			)

	# classifier head(s)
	if vqa_model:
		# classification head
		rename_keys.extend(
		    [
		        ('''vqa_classifier.0.weight''', '''classifier.0.weight'''),
		        ('''vqa_classifier.0.bias''', '''classifier.0.bias'''),
		        ('''vqa_classifier.1.weight''', '''classifier.1.weight'''),
		        ('''vqa_classifier.1.bias''', '''classifier.1.bias'''),
		        ('''vqa_classifier.3.weight''', '''classifier.3.weight'''),
		        ('''vqa_classifier.3.bias''', '''classifier.3.bias'''),
		    ]			)
	elif nlvr_model:
		# classification head
		rename_keys.extend(
		    [
		        ('''nlvr2_classifier.0.weight''', '''classifier.0.weight'''),
		        ('''nlvr2_classifier.0.bias''', '''classifier.0.bias'''),
		        ('''nlvr2_classifier.1.weight''', '''classifier.1.weight'''),
		        ('''nlvr2_classifier.1.bias''', '''classifier.1.bias'''),
		        ('''nlvr2_classifier.3.weight''', '''classifier.3.weight'''),
		        ('''nlvr2_classifier.3.bias''', '''classifier.3.bias'''),
		    ]			)
	else:
		pass

	return rename_keys




def __magic_name__	(				_lowerCamelCase:		Dict,					_lowerCamelCase:		Any			)					->						int:

	'''simple docstring'''




	for i in range(config.num_hidden_layers			):
		lowerCAmelCase												=  '''vilt.'''
		# read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
		lowerCAmelCase												=  state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.weight"""			)
		lowerCAmelCase												=  state_dict.pop(F"""transformer.blocks.{i}.attn.qkv.bias"""			)
		# next, add query, keys and values (in that order) to the state dict
		lowerCAmelCase												=  in_proj_weight[
		    : config.hidden_size, :
		]
		lowerCAmelCase												=  in_proj_bias[: config.hidden_size]
		lowerCAmelCase												=  in_proj_weight[
		    config.hidden_size : config.hidden_size * 2, :
		]
		lowerCAmelCase												=  in_proj_bias[
		    config.hidden_size : config.hidden_size * 2
		]
		lowerCAmelCase												=  in_proj_weight[
		    -config.hidden_size :, :
		]
		lowerCAmelCase												=  in_proj_bias[-config.hidden_size :]




def __magic_name__	(				_lowerCamelCase:		Optional[Any]			)					->						Dict:

	'''simple docstring'''




	lowerCAmelCase												=  ['''head.weight''', '''head.bias''']
	for k in ignore_keys:
		state_dict.pop(_lowerCamelCase,					_lowerCamelCase			)




def __magic_name__	(				_lowerCamelCase:		Tuple,					_lowerCamelCase:		Dict,					_lowerCamelCase:		Optional[int]			)					->						Tuple:

	'''simple docstring'''




	lowerCAmelCase												=  dct.pop(_lowerCamelCase			)
	lowerCAmelCase												=  val




@torch.no_grad()
def __magic_name__	(				_lowerCamelCase:		Union[str, Any],					_lowerCamelCase:		List[Any]			)					->						Union[str, Any]:

	'''simple docstring'''




	lowerCAmelCase												=  ViltConfig(image_size=384,					patch_size=32,					tie_word_embeddings=_lowerCamelCase			)
	lowerCAmelCase												=  False
	lowerCAmelCase												=  False
	lowerCAmelCase												=  False
	lowerCAmelCase												=  False
	if "vqa" in checkpoint_url:
		lowerCAmelCase												=  True
		lowerCAmelCase												=  3_129
		lowerCAmelCase												=  '''huggingface/label-files'''
		lowerCAmelCase												=  '''vqa2-id2label.json'''
		lowerCAmelCase												=  json.load(open(hf_hub_download(_lowerCamelCase,					_lowerCamelCase,					repo_type='''dataset'''			),					'''r'''			)			)
		lowerCAmelCase												=  {int(_lowerCamelCase			): v for k, v in idalabel.items()}
		lowerCAmelCase												=  idalabel
		lowerCAmelCase												=  {v: k for k, v in idalabel.items()}
		lowerCAmelCase												=  ViltForQuestionAnswering(_lowerCamelCase			)
	elif "nlvr" in checkpoint_url:
		lowerCAmelCase												=  True
		lowerCAmelCase												=  2
		lowerCAmelCase												=  {0: '''False''', 1: '''True'''}
		lowerCAmelCase												=  {v: k for k, v in config.idalabel.items()}
		lowerCAmelCase												=  3
		lowerCAmelCase												=  ViltForImagesAndTextClassification(_lowerCamelCase			)
	elif "irtr" in checkpoint_url:
		lowerCAmelCase												=  True
		lowerCAmelCase												=  ViltForImageAndTextRetrieval(_lowerCamelCase			)
	elif "mlm_itm" in checkpoint_url:
		lowerCAmelCase												=  True
		lowerCAmelCase												=  ViltForMaskedLM(_lowerCamelCase			)
	else:
		raise ValueError('''Unknown model type'''			)

	# load state_dict of original model, remove and rename some keys
	lowerCAmelCase												=  torch.hub.load_state_dict_from_url(_lowerCamelCase,					map_location='''cpu'''			)['''state_dict''']
	lowerCAmelCase												=  create_rename_keys(_lowerCamelCase,					_lowerCamelCase,					_lowerCamelCase,					_lowerCamelCase			)
	for src, dest in rename_keys:
		rename_key(_lowerCamelCase,					_lowerCamelCase,					_lowerCamelCase			)
	read_in_q_k_v(_lowerCamelCase,					_lowerCamelCase			)
	if mlm_model or irtr_model:
		lowerCAmelCase												=  ['''itm_score.fc.weight''', '''itm_score.fc.bias''']
		for k in ignore_keys:
			state_dict.pop(_lowerCamelCase,					_lowerCamelCase			)

    # load state dict into HuggingFace model
	model.eval()
	if mlm_model:
		lowerCAmelCase							,       lowerCAmelCase												=  model.load_state_dict(_lowerCamelCase,					strict=_lowerCamelCase			)
		assert missing_keys == ["mlm_score.decoder.bias"]
	else:
		model.load_state_dict(_lowerCamelCase			)

	# Define processor
	lowerCAmelCase												=  ViltImageProcessor(size=384			)
	lowerCAmelCase												=  BertTokenizer.from_pretrained('''bert-base-uncased'''			)
	lowerCAmelCase												=  ViltProcessor(_lowerCamelCase,					_lowerCamelCase			)

	# Forward pass on example inputs (image + text)
	if nlvr_model:
		lowerCAmelCase												=  Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''',					stream=_lowerCamelCase			).raw			)
		lowerCAmelCase												=  Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg''',					stream=_lowerCamelCase			).raw			)
		lowerCAmelCase												=  (
		    '''The left image contains twice the number of dogs as the right image, and at least two dogs in total are'''
		    ''' standing.'''
		)
		lowerCAmelCase												=  processor(_lowerCamelCase,					_lowerCamelCase,					return_tensors='''pt'''			)
		lowerCAmelCase												=  processor(_lowerCamelCase,					_lowerCamelCase,					return_tensors='''pt'''			)
		lowerCAmelCase												=  model(
		    input_ids=encoding_a.input_ids,					pixel_values=encoding_a.pixel_values,					pixel_values_a=encoding_a.pixel_values,					)
	else:
		lowerCAmelCase												=  Image.open(requests.get('''http://images.cocodataset.org/val2017/000000039769.jpg''',					stream=_lowerCamelCase			).raw			)
		if mlm_model:
			lowerCAmelCase												=  '''a bunch of [MASK] laying on a [MASK].'''
		else:
			lowerCAmelCase												=  '''How many cats are there?'''
		lowerCAmelCase												=  processor(_lowerCamelCase,					_lowerCamelCase,					return_tensors='''pt'''			)
		lowerCAmelCase												=  model(**_lowerCamelCase			)

	# Verify outputs
	if mlm_model:
		lowerCAmelCase												=  torch.Size([1, 11, 30_522]			)
		lowerCAmelCase												=  torch.tensor([-12.5061, -12.5123, -12.5174]			)
		assert outputs.logits.shape == expected_shape
		assert torch.allclose(outputs.logits[0, 0, :3],					_lowerCamelCase,					atol=1E-4			)

		# verify masked token prediction equals "cats"
		lowerCAmelCase												=  outputs.logits[0, 4, :].argmax(-1			).item()
		assert tokenizer.decode([predicted_id]			) == "cats"
	elif vqa_model:
		lowerCAmelCase												=  torch.Size([1, 3_129]			)
		lowerCAmelCase												=  torch.tensor([-15.9495, -18.1472, -10.3041]			)
		assert torch.allclose(outputs.logits[0, :3],					_lowerCamelCase,					atol=1E-4			)
		assert outputs.logits.shape == expected_shape
		assert torch.allclose(outputs.logits[0, 0, :3],					_lowerCamelCase,					atol=1E-4			)

		# verify vqa prediction equals "2"
		lowerCAmelCase												=  outputs.logits.argmax(-1			).item()
		assert model.config.idalabel[predicted_idx] == "2"
	elif nlvr_model:
		lowerCAmelCase												=  torch.Size([1, 2]			)
		lowerCAmelCase												=  torch.tensor([-2.8721, 2.1291]			)
		assert torch.allclose(outputs.logits[0, :3],					_lowerCamelCase,					atol=1E-4			)
		assert outputs.logits.shape == expected_shape

	Path(_lowerCamelCase			).mkdir(exist_ok=_lowerCamelCase			)
	print(F"""Saving model and processor to {pytorch_dump_folder_path}"""			)
	model.save_pretrained(_lowerCamelCase			)
	processor.save_pretrained(_lowerCamelCase			)


if __name__ == "__main__":
					UpperCAmelCase        =				argparse.ArgumentParser()
					# Required parameters
					parser.add_argument(
					    """--checkpoint_url""",
					    default="""https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt""",
					    type=str,
					    help="""URL of the checkpoint you'd like to convert.""",
					)
					parser.add_argument(
					    """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory."""
					)

					UpperCAmelCase        =				parser.parse_args()
					convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)




"""simple docstring"""






import os
import unittest

from huggingface_hub.utils import are_progress_bars_disabled

import transformers.models.bart.tokenization_bart
from transformers import logging
from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context
from transformers.utils.logging import disable_progress_bar, enable_progress_bar



class 			lowercase					( unittest.TestCase		):






			def 					UpperCAmelCase							(self  :	Union[str, Any]			)       ->	Optional[Any]:





				"""simple docstring"""


				lowerCAmelCase												=  logging.get_logger()

				# the current default level is logging.WARNING
				lowerCAmelCase												=  logging.get_verbosity()

				logging.set_verbosity_error()
				self.assertEqual(logger.getEffectiveLevel()      ,logging.get_verbosity()			)

				logging.set_verbosity_warning()
				self.assertEqual(logger.getEffectiveLevel()      ,logging.get_verbosity()			)

				logging.set_verbosity_info()
				self.assertEqual(logger.getEffectiveLevel()      ,logging.get_verbosity()			)

				logging.set_verbosity_debug()
				self.assertEqual(logger.getEffectiveLevel()      ,logging.get_verbosity()			)

				# restore to the original level
				logging.set_verbosity(SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Union[str, Any]			)       ->	Optional[int]:





				"""simple docstring"""


				lowerCAmelCase												=  logging.get_verbosity()

				lowerCAmelCase												=  logging.get_logger('''transformers.models.bart.tokenization_bart'''			)
				lowerCAmelCase												=  '''Testing 1, 2, 3'''

				# should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`)
				if level_origin <= logging.WARNING:
					with CaptureLogger(SCREAMING_SNAKE_CASE_			) as cl:
						logger.warning(SCREAMING_SNAKE_CASE_			)
					self.assertEqual(cl.out      ,msg + '''\n'''			)

				# this is setting the level for all of `transformers.*` loggers
				logging.set_verbosity_error()

				# should not be able to log warnings
				with CaptureLogger(SCREAMING_SNAKE_CASE_			) as cl:
					logger.warning(SCREAMING_SNAKE_CASE_			)
				self.assertEqual(cl.out      ,''''''			)

				# should be able to log warnings again
				logging.set_verbosity_warning()
				with CaptureLogger(SCREAMING_SNAKE_CASE_			) as cl:
					logger.warning(SCREAMING_SNAKE_CASE_			)
				self.assertEqual(cl.out      ,msg + '''\n'''			)

				# restore to the original level
				logging.set_verbosity(SCREAMING_SNAKE_CASE_			)






			@mockenv(TRANSFORMERS_VERBOSITY='''error'''			)
			def 					UpperCAmelCase							(self  :	Any			)       ->	str:





				"""simple docstring"""


				transformers.utils.logging._reset_library_root_logger()
				# this action activates the env var
				lowerCAmelCase												=  logging.get_logger('''transformers.models.bart.tokenization_bart'''			)

				lowerCAmelCase												=  os.getenv('''TRANSFORMERS_VERBOSITY'''      ,SCREAMING_SNAKE_CASE_			)
				lowerCAmelCase												=  logging.log_levels[env_level_str]

				lowerCAmelCase												=  logging.get_verbosity()
				self.assertEqual(
				    SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,F"""TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}"""      ,)

				# restore to the original level
				lowerCAmelCase												=  ''''''
				transformers.utils.logging._reset_library_root_logger()






			@mockenv(TRANSFORMERS_VERBOSITY='''super-error'''			)
			def 					UpperCAmelCase							(self  :	List[Any]			)       ->	Tuple:





				"""simple docstring"""


				transformers.utils.logging._reset_library_root_logger()
				lowerCAmelCase												=  logging.logging.getLogger()
				with CaptureLogger(SCREAMING_SNAKE_CASE_			) as cl:
					# this action activates the env var
					logging.get_logger('''transformers.models.bart.tokenization_bart'''			)
				self.assertIn('''Unknown option TRANSFORMERS_VERBOSITY=super-error'''      ,cl.out			)

				# no need to restore as nothing was changed






			def 					UpperCAmelCase							(self  :	Dict			)       ->	Any:





				"""simple docstring"""


				transformers.utils.logging._reset_library_root_logger()

				lowerCAmelCase												=  logging.get_logger('''transformers.models.bart.tokenization_bart'''			)
				lowerCAmelCase												=  '''Testing 1, 2, 3'''

				with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS='''1'''			):
					# nothing should be logged as env var disables this method
					with CaptureLogger(SCREAMING_SNAKE_CASE_			) as cl:
						logger.warning_advice(SCREAMING_SNAKE_CASE_			)
					self.assertEqual(cl.out      ,''''''			)

				with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=''''''			):
					# should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
					with CaptureLogger(SCREAMING_SNAKE_CASE_			) as cl:
						logger.warning_advice(SCREAMING_SNAKE_CASE_			)
					self.assertEqual(cl.out      ,msg + '''\n'''			)





def __magic_name__	(				)					->						List[Any]:

	'''simple docstring'''




	disable_progress_bar()
	assert are_progress_bars_disabled()

	enable_progress_bar()
	assert not are_progress_bars_disabled()


