



'''simple docstring'''




import argparse

import requests
import torch
from PIL import Image

from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor



def 	a			( lowerCamelCase__    ):
			'''simple docstring'''





			if "cls_token" in name:
						A_	:						Union[str, Any]	=     name.replace("""cls_token"""						,      """vit.embeddings.cls_token"""    )
			if "mask_token" in name:
						A_	:						Dict	=     name.replace("""mask_token"""						,      """decoder.mask_token"""    )
			if "decoder_pos_embed" in name:
						A_	:						int	=     name.replace("""decoder_pos_embed"""						,      """decoder.decoder_pos_embed"""    )
			if "pos_embed" in name and "decoder" not in name:
						A_	:						List[Any]	=     name.replace("""pos_embed"""						,      """vit.embeddings.position_embeddings"""    )
			if "patch_embed.proj" in name:
						A_	:						Dict	=     name.replace("""patch_embed.proj"""						,      """vit.embeddings.patch_embeddings.projection"""    )
			if "patch_embed.norm" in name:
						A_	:						Union[str, Any]	=     name.replace("""patch_embed.norm"""						,      """vit.embeddings.norm"""    )
			if "decoder_blocks" in name:
						A_	:						Union[str, Any]	=     name.replace("""decoder_blocks"""						,      """decoder.decoder_layers"""    )
			if "blocks" in name:
						A_	:						Optional[Any]	=     name.replace("""blocks"""						,      """vit.encoder.layer"""    )
			if "attn.proj" in name:
						A_	:						Optional[int]	=     name.replace("""attn.proj"""						,      """attention.output.dense"""    )
			if "attn" in name:
						A_	:						int	=     name.replace("""attn"""						,      """attention.self"""    )
			if "norm1" in name:
						A_	:						Any	=     name.replace("""norm1"""						,      """layernorm_before"""    )
			if "norm2" in name:
						A_	:						List[Any]	=     name.replace("""norm2"""						,      """layernorm_after"""    )
			if "mlp.fc1" in name:
						A_	:						str	=     name.replace("""mlp.fc1"""						,      """intermediate.dense"""    )
			if "mlp.fc2" in name:
						A_	:						Optional[int]	=     name.replace("""mlp.fc2"""						,      """output.dense"""    )
			if "decoder_embed" in name:
						A_	:						Tuple	=     name.replace("""decoder_embed"""						,      """decoder.decoder_embed"""    )
			if "decoder_norm" in name:
						A_	:						Optional[Any]	=     name.replace("""decoder_norm"""						,      """decoder.decoder_norm"""    )
			if "decoder_pred" in name:
						A_	:						Tuple	=     name.replace("""decoder_pred"""						,      """decoder.decoder_pred"""    )
			if "norm.weight" in name and "decoder" not in name:
						A_	:						Dict	=     name.replace("""norm.weight"""						,      """vit.layernorm.weight"""    )
			if "norm.bias" in name and "decoder" not in name:
						A_	:						List[str]	=     name.replace("""norm.bias"""						,      """vit.layernorm.bias"""    )

			return name



def 	a			( lowerCamelCase__						,      lowerCamelCase__    ):
			'''simple docstring'''





			for key in orig_state_dict.copy().keys():
						A_	:						Any	=     orig_state_dict.pop(lowerCamelCase__    )

						if "qkv" in key:
									A_	:						List[Any]	=     key.split("""."""    )
									A_	:						str	=     int(key_split[1]    )
									if "decoder_blocks" in key:
												A_	:						Any	=     config.decoder_hidden_size
												A_	:						str	=     """decoder.decoder_layers."""
												if "weight" in key:
															A_	:						Any	=     val[:dim, :]
															A_	:						Optional[int]	=     val[dim : dim * 2, :]
															A_	:						List[str]	=     val[-dim:, :]
												elif "bias" in key:
															A_	:						int	=     val[:dim]
															A_	:						Union[str, Any]	=     val[dim : dim * 2]
															A_	:						str	=     val[-dim:]
									else:
												A_	:						Dict	=     config.hidden_size
												A_	:						Optional[int]	=     """vit.encoder.layer."""
												if "weight" in key:
															A_	:						Optional[int]	=     val[:dim, :]
															A_	:						List[str]	=     val[dim : dim * 2, :]
															A_	:						str	=     val[-dim:, :]
												elif "bias" in key:
															A_	:						List[Any]	=     val[:dim]
															A_	:						List[str]	=     val[dim : dim * 2]
															A_	:						List[str]	=     val[-dim:]

						else:
									A_	:						Tuple	=     val

			return orig_state_dict



def 	a			( lowerCamelCase__						,      lowerCamelCase__    ):
			'''simple docstring'''





			A_	:						int	=     ViTMAEConfig()
			if "large" in checkpoint_url:
						A_	:						Any	=     10_24
						A_	:						Tuple	=     40_96
						A_	:						List[str]	=     24
						A_	:						Optional[Any]	=     16
			elif "huge" in checkpoint_url:
						A_	:						str	=     14
						A_	:						List[Any]	=     12_80
						A_	:						List[str]	=     51_20
						A_	:						Any	=     32
						A_	:						Tuple	=     16

			A_	:						Union[str, Any]	=     ViTMAEForPreTraining(lowerCamelCase__    )

			A_	:						Union[str, Any]	=     torch.hub.load_state_dict_from_url(lowerCamelCase__						,      map_location="""cpu"""    )["""model"""]

			A_	:						str	=     ViTMAEImageProcessor(size=config.image_size    )

			A_	:						Dict	=     convert_state_dict(lowerCamelCase__						,      lowerCamelCase__    )

			model.load_state_dict(lowerCamelCase__    )
			model.eval()

			A_	:						str	=     """https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg"""

			A_	:						Any	=     Image.open(requests.get(lowerCamelCase__						,      stream=lowerCamelCase__    ).raw    )
			A_	:						Any	=     ViTMAEImageProcessor(size=config.image_size    )
			A_	:						Any	=     image_processor(images=lowerCamelCase__						,      return_tensors="""pt"""    )

			# forward pass
			torch.manual_seed(2    )
			A_	:						Dict	=     model(**lowerCamelCase__    )
			A_	:						Optional[Any]	=     outputs.logits

			if "large" in checkpoint_url:
						A_	:						int	=     torch.tensor(
						    [[-0.7_309, -0.7_128, -1.0_169], [-1.0_161, -0.9_058, -1.1_878], [-1.0_478, -0.9_411, -1.1_911]]    )
			elif "huge" in checkpoint_url:
						A_	:						Optional[int]	=     torch.tensor(
						    [[-1.1_599, -0.9_199, -1.2_221], [-1.1_952, -0.9_269, -1.2_307], [-1.2_143, -0.9_337, -1.2_262]]    )
			else:
						A_	:						List[str]	=     torch.tensor(
						    [[-0.9_192, -0.8_481, -1.1_259], [-1.1_349, -1.0_034, -1.2_599], [-1.1_757, -1.0_429, -1.2_726]]    )

			# verify logits
			assert torch.allclose(logits[0, :3, :3]						,      lowerCamelCase__						,      atol=1E-4    )

			print(f'Saving model to {pytorch_dump_folder_path}'    )
			model.save_pretrained(lowerCamelCase__    )

			print(f'Saving image processor to {pytorch_dump_folder_path}'    )
			image_processor.save_pretrained(lowerCamelCase__    )


if __name__ == "__main__":
						lowerCamelCase							:List[str]				    =					argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    '''--checkpoint_url''',
						    default='''https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth''',
						    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							:Union[str, Any]				    =					parser.parse_args()
						convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)




'''simple docstring'''




from collections import deque
from math import floor
from random import random
from time import time







class 	_lowerCAmelCase							:


	def __init__(self							):
				A_	:						int	=     {}


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase=1							):
				if self.graph.get(lowercase							):
							if self.graph[u].count([w, v]							) == 0:
										self.graph[u].append([w, v]							)
				else:
							A_	:						Tuple	=     [[w, v]]
				if not self.graph.get(lowercase							):
							A_	:						Union[str, Any]	=     []


	def     _a	(self							):
				return list(self.graph							)


	def     _a	(self       ,      lowercase       ,      lowercase							):
				if self.graph.get(lowercase							):
							for _ in self.graph[u]:
										if _[1] == v:
													self.graph[u].remove(lowercase							)


	def     _a	(self       ,      lowercase=-2       ,      lowercase=-1							):
				if s == d:
							return []
				A_	:						Union[str, Any]	=     []
				A_	:						Dict	=     []
				if s == -2:
							A_	:						List[Any]	=     list(self.graph							)[0]
				stack.append(lowercase							)
				visited.append(lowercase							)
				A_	:						Any	=     s

				while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]							) != 0:
										A_	:						Tuple	=     s
										for node in self.graph[s]:
													if visited.count(node[1]							) < 1:
																if node[1] == d:
																			visited.append(lowercase							)
																			return visited
																else:
																			stack.append(node[1]							)
																			visited.append(node[1]							)
																			A_	:						List[Any]	=     node[1]
																			break

            # check if all the children are visited
							if s == ss:
										stack.pop()
										if len(lowercase							) != 0:
													A_	:						List[str]	=     stack[len(lowercase							) - 1]
							else:
										A_	:						str	=     ss

							# check if se have reached the starting point
							if len(lowercase							) == 0:
										return visited


	def     _a	(self       ,      lowercase=-1							):
				if c == -1:
							A_	:						Tuple	=     floor(random() * 10000							) + 10
				for i in range(lowercase							):
							# every vertex has max 100 edges
							for _ in range(floor(random() * 102							) + 1							):
										A_	:						List[str]	=     floor(random() * c							) + 1
										if n != i:
													self.add_pair(lowercase       ,      lowercase       ,      1							)


	def     _a	(self       ,      lowercase=-2							):
				A_	:						Union[str, Any]	=     deque()
				A_	:						Tuple	=     []
				if s == -2:
							A_	:						int	=     list(self.graph							)[0]
				d.append(lowercase							)
				visited.append(lowercase							)
				while d:
							A_	:						Any	=     d.popleft()
							if len(self.graph[s]							) != 0:
										for node in self.graph[s]:
													if visited.count(node[1]							) < 1:
																d.append(node[1]							)
																visited.append(node[1]							)
				return visited


	def     _a	(self       ,      lowercase							):
				A_	:						Dict	=     0
				for x in self.graph:
							for y in self.graph[x]:
										if y[1] == u:
													count += 1
				return count


	def     _a	(self       ,      lowercase							):
				return len(self.graph[u]							)


	def     _a	(self       ,      lowercase=-2							):
				A_	:						Dict	=     []
				A_	:						Optional[Any]	=     []
				if s == -2:
							A_	:						int	=     list(self.graph							)[0]
				stack.append(lowercase							)
				visited.append(lowercase							)
				A_	:						Optional[Any]	=     s
				A_	:						int	=     []

				while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]							) != 0:
										A_	:						Optional[int]	=     s
										for node in self.graph[s]:
													if visited.count(node[1]							) < 1:
																stack.append(node[1]							)
																visited.append(node[1]							)
																A_	:						Optional[Any]	=     node[1]
																break

            # check if all the children are visited
							if s == ss:
										sorted_nodes.append(stack.pop()							)
										if len(lowercase							) != 0:
													A_	:						int	=     stack[len(lowercase							) - 1]
							else:
										A_	:						Optional[Any]	=     ss

							# check if se have reached the starting point
							if len(lowercase							) == 0:
										return sorted_nodes


	def     _a	(self							):
				A_	:						Dict	=     []
				A_	:						Tuple	=     []
				A_	:						Any	=     list(self.graph							)[0]
				stack.append(lowercase							)
				visited.append(lowercase							)
				A_	:						Optional[int]	=     -2
				A_	:						List[Any]	=     []
				A_	:						List[str]	=     s
				A_	:						Optional[int]	=     False
				A_	:						Union[str, Any]	=     set()

				while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]							) != 0:
										A_	:						List[Any]	=     s
										for node in self.graph[s]:
													if (
													    visited.count(node[1]							) > 0
													    and node[1] != parent
													    and indirect_parents.count(node[1]							) > 0
													    and not on_the_way_back
													):
																A_	:						Dict	=     len(lowercase							) - 1
																while len_stack >= 0:
																			if stack[len_stack] == node[1]:
																						anticipating_nodes.add(node[1]							)
																						break
																			else:
																						anticipating_nodes.add(stack[len_stack]							)
																						len_stack -= 1
													if visited.count(node[1]							) < 1:
																stack.append(node[1]							)
																visited.append(node[1]							)
																A_	:						List[str]	=     node[1]
																break

            # check if all the children are visited
							if s == ss:
										stack.pop()
										A_	:						str	=     True
										if len(lowercase							) != 0:
													A_	:						Union[str, Any]	=     stack[len(lowercase							) - 1]
							else:
										A_	:						Tuple	=     False
										indirect_parents.append(lowercase							)
										A_	:						Tuple	=     s
										A_	:						Tuple	=     ss

							# check if se have reached the starting point
							if len(lowercase							) == 0:
										return list(lowercase							)


	def     _a	(self							):
				A_	:						Union[str, Any]	=     []
				A_	:						str	=     []
				A_	:						List[Any]	=     list(self.graph							)[0]
				stack.append(lowercase							)
				visited.append(lowercase							)
				A_	:						List[Any]	=     -2
				A_	:						Tuple	=     []
				A_	:						Optional[Any]	=     s
				A_	:						Union[str, Any]	=     False
				A_	:						Optional[Any]	=     set()

				while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]							) != 0:
										A_	:						int	=     s
										for node in self.graph[s]:
													if (
													    visited.count(node[1]							) > 0
													    and node[1] != parent
													    and indirect_parents.count(node[1]							) > 0
													    and not on_the_way_back
													):
																A_	:						Dict	=     len(lowercase							) - 1
																while len_stack_minus_one >= 0:
																			if stack[len_stack_minus_one] == node[1]:
																						anticipating_nodes.add(node[1]							)
																						break
																			else:
																						return True
													if visited.count(node[1]							) < 1:
																stack.append(node[1]							)
																visited.append(node[1]							)
																A_	:						Optional[Any]	=     node[1]
																break

            # check if all the children are visited
							if s == ss:
										stack.pop()
										A_	:						List[str]	=     True
										if len(lowercase							) != 0:
													A_	:						Dict	=     stack[len(lowercase							) - 1]
							else:
										A_	:						int	=     False
										indirect_parents.append(lowercase							)
										A_	:						List[Any]	=     s
										A_	:						int	=     ss

							# check if se have reached the starting point
							if len(lowercase							) == 0:
										return False


	def     _a	(self       ,      lowercase=-2       ,      lowercase=-1							):
				A_	:						str	=     time()
				self.dfs(lowercase       ,      lowercase							)
				A_	:						Any	=     time()
				return end - begin


	def     _a	(self       ,      lowercase=-2							):
				A_	:						Union[str, Any]	=     time()
				self.bfs(lowercase							)
				A_	:						Optional[Any]	=     time()
				return end - begin







class 	_lowerCAmelCase							:


	def __init__(self							):
				A_	:						List[str]	=     {}


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase=1							):
				# check if the u exists
				if self.graph.get(lowercase							):
							# if there already is a edge
							if self.graph[u].count([w, v]							) == 0:
										self.graph[u].append([w, v]							)
				else:
							# if u does not exist
							A_	:						int	=     [[w, v]]
				# add the other way
				if self.graph.get(lowercase							):
							# if there already is a edge
							if self.graph[v].count([w, u]							) == 0:
										self.graph[v].append([w, u]							)
				else:
							# if u does not exist
							A_	:						Any	=     [[w, u]]


	def     _a	(self       ,      lowercase       ,      lowercase							):
				if self.graph.get(lowercase							):
							for _ in self.graph[u]:
										if _[1] == v:
													self.graph[u].remove(lowercase							)
        # the other way round
				if self.graph.get(lowercase							):
							for _ in self.graph[v]:
										if _[1] == u:
													self.graph[v].remove(lowercase							)


	def     _a	(self       ,      lowercase=-2       ,      lowercase=-1							):
				if s == d:
							return []
				A_	:						Dict	=     []
				A_	:						List[Any]	=     []
				if s == -2:
							A_	:						str	=     list(self.graph							)[0]
				stack.append(lowercase							)
				visited.append(lowercase							)
				A_	:						List[str]	=     s

				while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]							) != 0:
										A_	:						Union[str, Any]	=     s
										for node in self.graph[s]:
													if visited.count(node[1]							) < 1:
																if node[1] == d:
																			visited.append(lowercase							)
																			return visited
																else:
																			stack.append(node[1]							)
																			visited.append(node[1]							)
																			A_	:						Optional[Any]	=     node[1]
																			break

            # check if all the children are visited
							if s == ss:
										stack.pop()
										if len(lowercase							) != 0:
													A_	:						Dict	=     stack[len(lowercase							) - 1]
							else:
										A_	:						List[str]	=     ss

							# check if se have reached the starting point
							if len(lowercase							) == 0:
										return visited


	def     _a	(self       ,      lowercase=-1							):
				if c == -1:
							A_	:						Union[str, Any]	=     floor(random() * 10000							) + 10
				for i in range(lowercase							):
							# every vertex has max 100 edges
							for _ in range(floor(random() * 102							) + 1							):
										A_	:						Union[str, Any]	=     floor(random() * c							) + 1
										if n != i:
													self.add_pair(lowercase       ,      lowercase       ,      1							)


	def     _a	(self       ,      lowercase=-2							):
				A_	:						int	=     deque()
				A_	:						Optional[Any]	=     []
				if s == -2:
							A_	:						Optional[int]	=     list(self.graph							)[0]
				d.append(lowercase							)
				visited.append(lowercase							)
				while d:
							A_	:						Union[str, Any]	=     d.popleft()
							if len(self.graph[s]							) != 0:
										for node in self.graph[s]:
													if visited.count(node[1]							) < 1:
																d.append(node[1]							)
																visited.append(node[1]							)
				return visited


	def     _a	(self       ,      lowercase							):
				return len(self.graph[u]							)


	def     _a	(self							):
				A_	:						Optional[int]	=     []
				A_	:						Dict	=     []
				A_	:						Union[str, Any]	=     list(self.graph							)[0]
				stack.append(lowercase							)
				visited.append(lowercase							)
				A_	:						List[Any]	=     -2
				A_	:						List[str]	=     []
				A_	:						int	=     s
				A_	:						Optional[int]	=     False
				A_	:						Any	=     set()

				while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]							) != 0:
										A_	:						Tuple	=     s
										for node in self.graph[s]:
													if (
													    visited.count(node[1]							) > 0
													    and node[1] != parent
													    and indirect_parents.count(node[1]							) > 0
													    and not on_the_way_back
													):
																A_	:						int	=     len(lowercase							) - 1
																while len_stack >= 0:
																			if stack[len_stack] == node[1]:
																						anticipating_nodes.add(node[1]							)
																						break
																			else:
																						anticipating_nodes.add(stack[len_stack]							)
																						len_stack -= 1
													if visited.count(node[1]							) < 1:
																stack.append(node[1]							)
																visited.append(node[1]							)
																A_	:						Tuple	=     node[1]
																break

            # check if all the children are visited
							if s == ss:
										stack.pop()
										A_	:						str	=     True
										if len(lowercase							) != 0:
													A_	:						Any	=     stack[len(lowercase							) - 1]
							else:
										A_	:						str	=     False
										indirect_parents.append(lowercase							)
										A_	:						Dict	=     s
										A_	:						List[Any]	=     ss

							# check if se have reached the starting point
							if len(lowercase							) == 0:
										return list(lowercase							)


	def     _a	(self							):
				A_	:						Optional[int]	=     []
				A_	:						Optional[int]	=     []
				A_	:						List[str]	=     list(self.graph							)[0]
				stack.append(lowercase							)
				visited.append(lowercase							)
				A_	:						Any	=     -2
				A_	:						Any	=     []
				A_	:						Tuple	=     s
				A_	:						str	=     False
				A_	:						str	=     set()

				while True:
							# check if there is any non isolated nodes
							if len(self.graph[s]							) != 0:
										A_	:						Dict	=     s
										for node in self.graph[s]:
													if (
													    visited.count(node[1]							) > 0
													    and node[1] != parent
													    and indirect_parents.count(node[1]							) > 0
													    and not on_the_way_back
													):
																A_	:						Any	=     len(lowercase							) - 1
																while len_stack_minus_one >= 0:
																			if stack[len_stack_minus_one] == node[1]:
																						anticipating_nodes.add(node[1]							)
																						break
																			else:
																						return True
													if visited.count(node[1]							) < 1:
																stack.append(node[1]							)
																visited.append(node[1]							)
																A_	:						Optional[Any]	=     node[1]
																break

            # check if all the children are visited
							if s == ss:
										stack.pop()
										A_	:						Union[str, Any]	=     True
										if len(lowercase							) != 0:
													A_	:						List[Any]	=     stack[len(lowercase							) - 1]
							else:
										A_	:						int	=     False
										indirect_parents.append(lowercase							)
										A_	:						int	=     s
										A_	:						int	=     ss

							# check if se have reached the starting point
							if len(lowercase							) == 0:
										return False


	def     _a	(self							):
				return list(self.graph							)


	def     _a	(self       ,      lowercase=-2       ,      lowercase=-1							):
				A_	:						Any	=     time()
				self.dfs(lowercase       ,      lowercase							)
				A_	:						Optional[Any]	=     time()
				return end - begin


	def     _a	(self       ,      lowercase=-2							):
				A_	:						List[Any]	=     time()
				self.bfs(lowercase							)
				A_	:						List[Any]	=     time()
				return end - begin