



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



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_=False						,      lowerCAmelCase_=False						,      lowerCAmelCase_=False  )      ->				Optional[int]:
		_a					:				Dict				=							[]
		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 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_  )      ->				Tuple:
		for i in range(config.num_hidden_layers  ):
				_a					:				List[str]				=							'vilt.'
				# read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
				_a					:				List[Any]				=							state_dict.pop(f"""transformer.blocks.{i}.attn.qkv.weight"""  )
				_a					:				List[str]				=							state_dict.pop(f"""transformer.blocks.{i}.attn.qkv.bias"""  )
				# next, add query, keys and values (in that order) to the state dict
				_a					:				Tuple				=							in_proj_weight[
				    : config.hidden_size, :
				]
				_a					:				Tuple				=							in_proj_bias[: config.hidden_size]
				_a					:				List[Any]				=							in_proj_weight[
				    config.hidden_size : config.hidden_size * 2, :
				]
				_a					:				str				=							in_proj_bias[
				    config.hidden_size : config.hidden_size * 2
				]
				_a					:				Optional[int]				=							in_proj_weight[
				    -config.hidden_size :, :
				]
				_a					:				List[Any]				=							in_proj_bias[-config.hidden_size :]



def 					__lowerCamelCase					(				lowerCAmelCase_  )      ->				List[str]:
		_a					:				int				=							['head.weight', 'head.bias']
		for k in ignore_keys:
				state_dict.pop(lowerCAmelCase_						,      lowerCAmelCase_  )



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_						,      lowerCAmelCase_  )      ->				Optional[int]:
		_a					:				Tuple				=							dct.pop(lowerCAmelCase_  )
		_a					:				int				=							val



@torch.no_grad()
def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_  )      ->				Tuple:
		_a					:				int				=							ViltConfig(image_size=384						,      patch_size=32						,      tie_word_embeddings=lowerCAmelCase_  )
		_a					:				Tuple				=							False
		_a					:				Any				=							False
		_a					:				Optional[Any]				=							False
		_a					:				int				=							False
		if "vqa" in checkpoint_url:
				_a					:				List[Any]				=							True
				_a					:				Any				=							3129
				_a					:				List[str]				=							'huggingface/label-files'
				_a					:				str				=							'vqa2-id2label.json'
				_a					:				int				=							json.load(open(hf_hub_download(lowerCAmelCase_						,      lowerCAmelCase_						,      repo_type='dataset'  )						,      'r'  )  )
				_a					:				Dict				=							{int(lowerCAmelCase_  ): v for k, v in idalabel.items()}
				_a					:				Tuple				=							idalabel
				_a					:				Tuple				=							{v: k for k, v in idalabel.items()}
				_a					:				Tuple				=							ViltForQuestionAnswering(lowerCAmelCase_  )
		elif "nlvr" in checkpoint_url:
				_a					:				Any				=							True
				_a					:				int				=							2
				_a					:				Optional[Any]				=							{0: 'False', 1: 'True'}
				_a					:				Dict				=							{v: k for k, v in config.idalabel.items()}
				_a					:				Union[str, Any]				=							3
				_a					:				List[Any]				=							ViltForImagesAndTextClassification(lowerCAmelCase_  )
		elif "irtr" in checkpoint_url:
				_a					:				Tuple				=							True
				_a					:				Any				=							ViltForImageAndTextRetrieval(lowerCAmelCase_  )
		elif "mlm_itm" in checkpoint_url:
				_a					:				Optional[int]				=							True
				_a					:				Tuple				=							ViltForMaskedLM(lowerCAmelCase_  )
		else:
				raise ValueError('Unknown model type'  )

		# load state_dict of original model, remove and rename some keys
		_a					:				Dict				=							torch.hub.load_state_dict_from_url(lowerCAmelCase_						,      map_location='cpu'  )['state_dict']
		_a					:				List[Any]				=							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:
				_a					:				List[Any]				=							['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:
				_a	, _a					:				Tuple				=							model.load_state_dict(lowerCAmelCase_						,      strict=lowerCAmelCase_  )
				assert missing_keys == ["mlm_score.decoder.bias"]
		else:
				model.load_state_dict(lowerCAmelCase_  )

		# Define processor
		_a					:				Union[str, Any]				=							ViltImageProcessor(size=384  )
		_a					:				Optional[int]				=							BertTokenizer.from_pretrained('bert-base-uncased'  )
		_a					:				Optional[Any]				=							ViltProcessor(lowerCAmelCase_						,      lowerCAmelCase_  )

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

		# Verify outputs
		if mlm_model:
				_a					:				str				=							torch.Size([1, 11, 30522]  )
				_a					:				Optional[Any]				=							torch.tensor([-12.5_061, -12.5_123, -12.5_174]  )
				assert outputs.logits.shape == expected_shape
				assert torch.allclose(outputs.logits[0, 0, :3]						,      lowerCAmelCase_						,      atol=1E-4  )

				# verify masked token prediction equals "cats"
				_a					:				Any				=							outputs.logits[0, 4, :].argmax(-1  ).item()
				assert tokenizer.decode([predicted_id]  ) == "cats"
		elif vqa_model:
				_a					:				Optional[Any]				=							torch.Size([1, 3129]  )
				_a					:				Dict				=							torch.tensor([-15.9_495, -18.1_472, -10.3_041]  )
				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"
				_a					:				str				=							outputs.logits.argmax(-1  ).item()
				assert model.config.idalabel[predicted_idx] == "2"
		elif nlvr_model:
				_a					:				Union[str, Any]				=							torch.Size([1, 2]  )
				_a					:				List[Any]				=							torch.tensor([-2.8_721, 2.1_291]  )
				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__":
							__lowerCAmelCase						      =       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.'''
							)

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









'''simple docstring'''

from __future__ import annotations

import random

# Maximum size of the population.  Bigger could be faster but is more memory expensive.
__lowerCAmelCase						      =       200
# Number of elements selected in every generation of evolution. The selection takes
# place from best to worst of that generation and must be smaller than N_POPULATION.
__lowerCAmelCase						      =       50
# Probability that an element of a generation can mutate, changing one of its genes.
# This will guarantee that all genes will be used during evolution.
__lowerCAmelCase						      =       0.4
# Just a seed to improve randomness required by the algorithm.
random.seed(random.randint(0, 1_000))



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_  )      ->				tuple[str, float]:
		_a					:				List[Any]				=							len([g for position, g in enumerate(lowerCAmelCase_  ) if g == main_target[position]]  )
		return (item, float(lowerCAmelCase_  ))



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_  )      ->				tuple[str, str]:
		_a					:				Dict				=							random.randint(0						,      len(lowerCAmelCase_  ) - 1  )
		_a					:				Optional[int]				=							parent_a[:random_slice] + parent_a[random_slice:]
		_a					:				Optional[int]				=							parent_a[:random_slice] + parent_a[random_slice:]
		return (child_a, child_a)



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_  )      ->				str:
		_a					:				Optional[Any]				=							list(lowerCAmelCase_  )
		if random.uniform(0						,      1  ) < MUTATION_PROBABILITY:
				_a					:				Optional[int]				=							random.choice(lowerCAmelCase_  )
		return "".join(lowerCAmelCase_  )



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_						,      lowerCAmelCase_						,      )      ->				list[str]:
		_a					:				List[str]				=							[]
		# Generate more children proportionally to the fitness score.
		_a					:				Tuple				=							int(parent_a[1] * 100  ) + 1
		_a					:				Tuple				=							10 if child_n >= 10 else child_n
		for _ in range(lowerCAmelCase_  ):
				_a					:				Any				=							population_score[random.randint(0						,      lowerCAmelCase_  )][0]

				_a	, _a					:				Tuple				=							crossover(parent_a[0]						,      lowerCAmelCase_  )
				# Append new string to the population list.
				pop.append(mutate(lowerCAmelCase_						,      lowerCAmelCase_  )  )
				pop.append(mutate(lowerCAmelCase_						,      lowerCAmelCase_  )  )
		return pop



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_						,      lowerCAmelCase_ = True  )      ->				tuple[int, int, str]:

		# Verify if N_POPULATION is bigger than N_SELECTED
		if N_POPULATION < N_SELECTED:
				_a					:				Dict				=							f"""{N_POPULATION} must be bigger than {N_SELECTED}"""
				raise ValueError(lowerCAmelCase_  )
		# Verify that the target contains no genes besides the ones inside genes variable.
		_a					:				Optional[int]				=							sorted({c for c in target if c not in genes}  )
		if not_in_genes_list:
				_a					:				List[Any]				=							f"""{not_in_genes_list} is not in genes list, evolution cannot converge"""
				raise ValueError(lowerCAmelCase_  )

		# Generate random starting population.
		_a					:				Union[str, Any]				=							[]
		for _ in range(lowerCAmelCase_  ):
				population.append(''.join([random.choice(lowerCAmelCase_  ) for i in range(len(lowerCAmelCase_  )  )]  )  )

		# Just some logs to know what the algorithms is doing.
		_a	, _a					:				Union[str, Any]				=							0, 0

		# This loop will end when we find a perfect match for our target.
		while True:
				generation += 1
				total_population += len(lowerCAmelCase_  )

				# Random population created. Now it's time to evaluate.

				# Adding a bit of concurrency can make everything faster,
				#
				# import concurrent.futures
				# population_score: list[tuple[str, float]] = []
				# with concurrent.futures.ThreadPoolExecutor(
				#                                   max_workers=NUM_WORKERS) as executor:
				#     futures = {executor.submit(evaluate, item) for item in population}
				#     concurrent.futures.wait(futures)
				#     population_score = [item.result() for item in futures]
				#
				# but with a simple algorithm like this, it will probably be slower.
				# We just need to call evaluate for every item inside the population.
				_a					:				Optional[Any]				=							[evaluate(lowerCAmelCase_						,      lowerCAmelCase_  ) for item in population]

				# Check if there is a matching evolution.
				_a					:				Tuple				=							sorted(lowerCAmelCase_						,      key=lambda lowerCAmelCase_  : x[1]						,      reverse=lowerCAmelCase_  )
				if population_score[0][0] == target:
						return (generation, total_population, population_score[0][0])

				# Print the best result every 10 generation.
				# Just to know that the algorithm is working.
				if debug and generation % 10 == 0:
						print(
						    f"""\nGeneration: {generation}"""
						    f"""\nTotal Population:{total_population}"""
						    f"""\nBest score: {population_score[0][1]}"""
						    f"""\nBest string: {population_score[0][0]}"""  )

				# Flush the old population, keeping some of the best evolutions.
				# Keeping this avoid regression of evolution.
				_a					:				Dict				=							population[: int(N_POPULATION / 3  )]
				population.clear()
				population.extend(lowerCAmelCase_  )
				# Normalize population score to be between 0 and 1.
				_a					:				Tuple				=							[
				    (item, score / len(lowerCAmelCase_  )) for item, score in population_score
				]

				# This is selection
				for i in range(lowerCAmelCase_  ):
						population.extend(select(population_score[int(lowerCAmelCase_  )]						,      lowerCAmelCase_						,      lowerCAmelCase_  )  )
						# Check if the population has already reached the maximum value and if so,
						# break the cycle.  If this check is disabled, the algorithm will take
						# forever to compute large strings, but will also calculate small strings in
						# a far fewer generations.
						if len(lowerCAmelCase_  ) > N_POPULATION:
								break


if __name__ == "__main__":
							__lowerCAmelCase						      =       (
							    '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!'''
							)
							__lowerCAmelCase						      =       list(
							    ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm'''
							    '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\'''
							)
							__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase						      =       basic(target_str, genes_list)
							print(
							    f"""\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}"""
							)




