






import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image

from diffusers import (
    DDIMScheduler,
    KandinskyVaaInpaintPipeline,
    KandinskyVaaPriorPipeline,
    UNetaDConditionModel,
    VQModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()





class __lowerCamelCase (      snake_case__     ,       unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								=      KandinskyVaaInpaintPipeline
	UpperCamelCase__								=      ["image_embeds", "negative_image_embeds", "image", "mask_image"]
	UpperCamelCase__								=      [
	    "image_embeds",
	    "negative_image_embeds",
	    "image",
	    "mask_image",
	]
	UpperCamelCase__								=      [
	    "generator",
	    "height",
	    "width",
	    "latents",
	    "guidance_scale",
	    "num_inference_steps",
	    "return_dict",
	    "guidance_scale",
	    "num_images_per_prompt",
	    "output_type",
	    "return_dict",
	]
	UpperCamelCase__								=      False

	@property
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							return 32

	@property
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							return 32

	@property
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							return self.time_input_dim

	@property
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							return self.time_input_dim * 4

	@property
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							return 100

	@property
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							torch.manual_seed(0    )

							_UpperCAmelCase									=			{
							    'in_channels': 9,
							    # Out channels is double in channels because predicts mean and variance
							    'out_channels': 8,
							    'addition_embed_type': 'image',
							    'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'),
							    'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'),
							    'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn',
							    'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2),
							    'layers_per_block': 1,
							    'encoder_hid_dim': self.text_embedder_hidden_size,
							    'encoder_hid_dim_type': 'image_proj',
							    'cross_attention_dim': self.cross_attention_dim,
							    'attention_head_dim': 4,
							    'resnet_time_scale_shift': 'scale_shift',
							    'class_embed_type': None,
							}

							_UpperCAmelCase									=			UNetaDConditionModel(**UpperCAmelCase    )
							return model

	@property
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							return {
							    "block_out_channels": [32, 64],
							    "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
							    "in_channels": 3,
							    "latent_channels": 4,
							    "layers_per_block": 1,
							    "norm_num_groups": 8,
							    "norm_type": "spatial",
							    "num_vq_embeddings": 12,
							    "out_channels": 3,
							    "up_block_types": [
							        "AttnUpDecoderBlock2D",
							        "UpDecoderBlock2D",
							    ],
							    "vq_embed_dim": 4,
							}

	@property
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							torch.manual_seed(0    )
							_UpperCAmelCase									=			VQModel(**self.dummy_movq_kwargs    )
							return model

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.dummy_unet
							_UpperCAmelCase									=			self.dummy_movq

							_UpperCAmelCase									=			DDIMScheduler(
							    num_train_timesteps=1000       ,    beta_schedule='linear'       ,    beta_start=0.0_00_85       ,    beta_end=0.0_12       ,    clip_sample=UpperCAmelCase       ,    set_alpha_to_one=UpperCAmelCase       ,    steps_offset=1       ,    prediction_type='epsilon'       ,    thresholding=UpperCAmelCase       ,    )

							_UpperCAmelCase									=			{
							    'unet': unet,
							    'scheduler': scheduler,
							    'movq': movq,
							}

							return components

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase=0    ):
							"""simple docstring"""







							_UpperCAmelCase									=			floats_tensor((1, self.text_embedder_hidden_size)       ,    rng=random.Random(UpperCAmelCase    )    ).to(UpperCAmelCase    )
							_UpperCAmelCase									=			floats_tensor((1, self.text_embedder_hidden_size)       ,    rng=random.Random(seed + 1    )    ).to(
							    UpperCAmelCase    )
							# create init_image
							_UpperCAmelCase									=			floats_tensor((1, 3, 64, 64)       ,    rng=random.Random(UpperCAmelCase    )    ).to(UpperCAmelCase    )
							_UpperCAmelCase									=			image.cpu().permute(0       ,    2       ,    3       ,    1    )[0]
							_UpperCAmelCase									=			Image.fromarray(np.uinta(UpperCAmelCase    )    ).convert('RGB'    ).resize((256, 256)    )
							# create mask
							_UpperCAmelCase									=			np.ones((64, 64)       ,    dtype=np.floataa    )
							_UpperCAmelCase									=			0

							if str(UpperCAmelCase    ).startswith('mps'    ):
													_UpperCAmelCase									=			torch.manual_seed(UpperCAmelCase    )
							else:
													_UpperCAmelCase									=			torch.Generator(device=UpperCAmelCase    ).manual_seed(UpperCAmelCase    )
							_UpperCAmelCase									=			{
							    'image': init_image,
							    'mask_image': mask,
							    'image_embeds': image_embeds,
							    'negative_image_embeds': negative_image_embeds,
							    'generator': generator,
							    'height': 64,
							    'width': 64,
							    'num_inference_steps': 2,
							    'guidance_scale': 4.0,
							    'output_type': 'np',
							}
							return inputs

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			'cpu'

							_UpperCAmelCase									=			self.get_dummy_components()

							_UpperCAmelCase									=			self.pipeline_class(**UpperCAmelCase    )
							_UpperCAmelCase									=			pipe.to(UpperCAmelCase    )

							pipe.set_progress_bar_config(disable=UpperCAmelCase    )

							_UpperCAmelCase									=			pipe(**self.get_dummy_inputs(UpperCAmelCase    )    )
							_UpperCAmelCase									=			output.images

							_UpperCAmelCase									=			pipe(
							    **self.get_dummy_inputs(UpperCAmelCase    )       ,    return_dict=UpperCAmelCase       ,    )[0]

							_UpperCAmelCase									=			image[0, -3:, -3:, -1]
							_UpperCAmelCase									=			image_from_tuple[0, -3:, -3:, -1]

							print(F"""image.shape {image.shape}"""    )

							assert image.shape == (1, 64, 64, 3)

							_UpperCAmelCase									=			np.array(
							    [0.50_77_59_03, 0.49_52_71_95, 0.48_82_45_43, 0.50_19_22_37, 0.48_64_49_06, 0.49_37_38_14, 0.4_78_05_98, 0.47_23_48_27, 0.48_32_78_48]    )

							assert (
							    np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2
							), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}"""
							assert (
							    np.abs(image_from_tuple_slice.flatten() - expected_slice    ).max() < 1e-2
							), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"""

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							super().test_inference_batch_single_identical(expected_max_diff=3e-3    )





@slow
@require_torch_gpu
class __lowerCamelCase (      unittest.TestCase):





	"""simple docstring"""






	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			load_numpy(
							    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
							    '/kandinskyv22/kandinskyv22_inpaint_cat_with_hat_fp16.npy'    )

							_UpperCAmelCase									=			load_image(
							    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png'    )
							_UpperCAmelCase									=			np.ones((768, 768)       ,    dtype=np.floataa    )
							_UpperCAmelCase									=			0

							_UpperCAmelCase									=			'a hat'

							_UpperCAmelCase									=			KandinskyVaaPriorPipeline.from_pretrained(
							    'kandinsky-community/kandinsky-2-2-prior'       ,    torch_dtype=torch.floataa    )
							pipe_prior.to(UpperCAmelCase    )

							_UpperCAmelCase									=			KandinskyVaaInpaintPipeline.from_pretrained(
							    'kandinsky-community/kandinsky-2-2-decoder-inpaint'       ,    torch_dtype=torch.floataa    )
							_UpperCAmelCase									=			pipeline.to(UpperCAmelCase    )
							pipeline.set_progress_bar_config(disable=UpperCAmelCase    )

							_UpperCAmelCase									=			torch.Generator(device='cpu'    ).manual_seed(0    )
							_UpperCAmelCase					, _UpperCAmelCase									=			pipe_prior(
							    UpperCAmelCase       ,    generator=UpperCAmelCase       ,    num_inference_steps=5       ,    negative_prompt=''       ,    ).to_tuple()

							_UpperCAmelCase									=			pipeline(
							    image=UpperCAmelCase       ,    mask_image=UpperCAmelCase       ,    image_embeds=UpperCAmelCase       ,    negative_image_embeds=UpperCAmelCase       ,    generator=UpperCAmelCase       ,    num_inference_steps=100       ,    height=768       ,    width=768       ,    output_type='np'       ,    )

							_UpperCAmelCase									=			output.images[0]

							assert image.shape == (768, 768, 3)

							assert_mean_pixel_difference(UpperCAmelCase       ,    UpperCAmelCase    )








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 ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel
from transformers.utils import logging


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


def __A    (      __lowerCAmelCase ,		__lowerCAmelCase=False	)->  Union[str, Any]:





						"""simple docstring"""



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

						# projection layer + position embeddings
						rename_keys.extend(
						    [
						        ('cls_token', 'vit.embeddings.cls_token'),
						        ('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight'),
						        ('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias'),
						        ('pos_embed', 'vit.embeddings.position_embeddings'),
						    ]	)

						if base_model:
												# layernorm + pooler
												rename_keys.extend(
												    [
												        ('norm.weight', 'layernorm.weight'),
												        ('norm.bias', 'layernorm.bias'),
												    ]	)

												# if just the base model, we should remove "vit" from all keys that start with "vit"
												_UpperCAmelCase									=			[(pair[0], pair[1][4:]) if pair[1].startswith('vit'	) else pair for pair in rename_keys]
						else:
												# layernorm + classification head
												rename_keys.extend(
												    [
												        ('norm.weight', 'vit.layernorm.weight'),
												        ('norm.bias', 'vit.layernorm.bias'),
												        ('head.weight', 'classifier.weight'),
												        ('head.bias', 'classifier.bias'),
												    ]	)

						return rename_keys


def __A    (      __lowerCAmelCase ,		__lowerCAmelCase ,		__lowerCAmelCase=False	)->  List[str]:





						"""simple docstring"""



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


def __A    (      __lowerCAmelCase	)->  Optional[Any]:





						"""simple docstring"""



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


def __A    (      __lowerCAmelCase ,		__lowerCAmelCase ,		__lowerCAmelCase	)->  int:





						"""simple docstring"""



						_UpperCAmelCase									=			dct.pop(__lowerCAmelCase	)
						_UpperCAmelCase									=			val


def __A    (      )->  str:





						"""simple docstring"""



						_UpperCAmelCase									=			'http://images.cocodataset.org/val2017/000000039769.jpg'
						_UpperCAmelCase									=			Image.open(requests.get(__lowerCAmelCase ,		stream=__lowerCAmelCase	).raw	)
						return im


@torch.no_grad()
def __A    (      __lowerCAmelCase ,		__lowerCAmelCase ,		__lowerCAmelCase=True	)->  List[str]:





						"""simple docstring"""



						_UpperCAmelCase									=			ViTConfig()
						# patch_size
						if model_name[-1] == "8":
												_UpperCAmelCase									=			8
						# set labels if required
						if not base_model:
												_UpperCAmelCase									=			1_000
												_UpperCAmelCase									=			'huggingface/label-files'
												_UpperCAmelCase									=			'imagenet-1k-id2label.json'
												_UpperCAmelCase									=			json.load(open(hf_hub_download(__lowerCAmelCase ,		__lowerCAmelCase ,		repo_type='dataset'	) ,		'r'	)	)
												_UpperCAmelCase									=			{int(__lowerCAmelCase	): v for k, v in idalabel.items()}
												_UpperCAmelCase									=			idalabel
												_UpperCAmelCase									=			{v: k for k, v in idalabel.items()}
						# size of the architecture
						if model_name in ["dino_vits8", "dino_vits16"]:
												_UpperCAmelCase									=			384
												_UpperCAmelCase									=			1_536
												_UpperCAmelCase									=			12
												_UpperCAmelCase									=			6

						# load original model from torch hub
						_UpperCAmelCase									=			torch.hub.load('facebookresearch/dino:main' ,		__lowerCAmelCase	)
						original_model.eval()

						# load state_dict of original model, remove and rename some keys
						_UpperCAmelCase									=			original_model.state_dict()
						if base_model:
												remove_classification_head_(__lowerCAmelCase	)
						_UpperCAmelCase									=			create_rename_keys(__lowerCAmelCase ,		base_model=__lowerCAmelCase	)
						for src, dest in rename_keys:
												rename_key(__lowerCAmelCase ,		__lowerCAmelCase ,		__lowerCAmelCase	)
						read_in_q_k_v(__lowerCAmelCase ,		__lowerCAmelCase ,		__lowerCAmelCase	)

						# load HuggingFace model
						if base_model:
												_UpperCAmelCase									=			ViTModel(__lowerCAmelCase ,		add_pooling_layer=__lowerCAmelCase	).eval()
						else:
												_UpperCAmelCase									=			ViTForImageClassification(__lowerCAmelCase	).eval()
						model.load_state_dict(__lowerCAmelCase	)

						# Check outputs on an image, prepared by ViTImageProcessor
						_UpperCAmelCase									=			ViTImageProcessor()
						_UpperCAmelCase									=			image_processor(images=prepare_img() ,		return_tensors='pt'	)
						_UpperCAmelCase									=			encoding['pixel_values']
						_UpperCAmelCase									=			model(__lowerCAmelCase	)

						if base_model:
												_UpperCAmelCase									=			original_model(__lowerCAmelCase	)
												assert torch.allclose(__lowerCAmelCase ,		outputs.last_hidden_state[:, 0, :] ,		atol=1E-1	)
						else:
												_UpperCAmelCase									=			original_model(__lowerCAmelCase	)
												assert logits.shape == outputs.logits.shape
												assert torch.allclose(__lowerCAmelCase ,		outputs.logits ,		atol=1E-3	)

						Path(__lowerCAmelCase	).mkdir(exist_ok=__lowerCAmelCase	)
						print(F"""Saving model {model_name} 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__":
		_a      	=  argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '''--model_name''',
		    default='''dino_vitb16''',
		    type=str,
		    help='''Name of the model trained with DINO you\'d like to convert.''',
		)
		parser.add_argument(
		    '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.'''
		)
		parser.add_argument(
		    '''--base_model''',
		    action='''store_true''',
		    help='''Whether to only convert the base model (no projection head weights).''',
		)

		parser.set_defaults(base_model=True)
		_a      	=  parser.parse_args()
		convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
