


"""simple docstring"""
import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
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


enable_full_determinism()





class        __A       (unittest.TestCase):





							'''simple docstring'''

							def 						lowerCAmelCase    (			self							: List[Any]    )   ->Optional[int]:



														"""simple docstring"""



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



							@property
							def 						lowerCAmelCase    (			self							: List[Any]    )   ->Optional[int]:



														"""simple docstring"""



														snake_case_          =						1
														snake_case_          =						3
														snake_case_          =						(32, 32)

														snake_case_          =						floats_tensor((batch_size, num_channels) + sizes						,    rng=random.Random(0    )    ).to(UpperCAmelCase_    )
														return image



							@property
							def 						lowerCAmelCase    (			self							: List[str]    )   ->Dict:



														"""simple docstring"""



														torch.manual_seed(0    )
														snake_case_          =						UNetaDConditionModel(
														    block_out_channels=(32, 32, 64)						,    layers_per_block=2						,    sample_size=32						,    in_channels=7						,    out_channels=4						,    down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""", """CrossAttnDownBlock2D""")						,    up_block_types=("""CrossAttnUpBlock2D""", """CrossAttnUpBlock2D""", """UpBlock2D""")						,    cross_attention_dim=32						,    attention_head_dim=8						,    use_linear_projection=UpperCAmelCase_						,    only_cross_attention=(True, True, False)						,    num_class_embeds=100						,    )
														return model



							@property
							def 						lowerCAmelCase    (			self							: List[Any]    )   ->Optional[int]:



														"""simple docstring"""



														torch.manual_seed(0    )
														snake_case_          =						AutoencoderKL(
														    block_out_channels=[32, 32, 64]						,    in_channels=3						,    out_channels=3						,    down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D""", """DownEncoderBlock2D"""]						,    up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D"""]						,    latent_channels=4						,    )
														return model



							@property
							def 						lowerCAmelCase    (			self							: Dict    )   ->Union[str, Any]:



														"""simple docstring"""



														torch.manual_seed(0    )
														snake_case_          =						CLIPTextConfig(
														    bos_token_id=0						,    eos_token_id=2						,    hidden_size=32						,    intermediate_size=37						,    layer_norm_eps=1E-05						,    num_attention_heads=4						,    num_hidden_layers=5						,    pad_token_id=1						,    vocab_size=1_000						,    hidden_act="""gelu"""						,    projection_dim=512						,    )
														return CLIPTextModel(UpperCAmelCase_    )



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



														"""simple docstring"""



														snake_case_          =						"""cpu"""  # ensure determinism for the device-dependent torch.Generator
														snake_case_          =						self.dummy_cond_unet_upscale
														snake_case_          =						DDPMScheduler()
														snake_case_          =						DDIMScheduler(prediction_type="""v_prediction"""    )
														snake_case_          =						self.dummy_vae
														snake_case_          =						self.dummy_text_encoder
														snake_case_          =						CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

														snake_case_          =						self.dummy_image.cpu().permute(0						,    2						,    3						,    1    )[0]
														snake_case_          =						Image.fromarray(np.uinta(UpperCAmelCase_    )    ).convert("""RGB"""    ).resize((64, 64)    )

														# make sure here that pndm scheduler skips prk
														snake_case_          =						StableDiffusionUpscalePipeline(
														    unet=UpperCAmelCase_						,    low_res_scheduler=UpperCAmelCase_						,    scheduler=UpperCAmelCase_						,    vae=UpperCAmelCase_						,    text_encoder=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_						,    max_noise_level=350						,    )
														snake_case_          =						sd_pipe.to(UpperCAmelCase_    )
														sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_    )

														snake_case_          =						"""A painting of a squirrel eating a burger"""
														snake_case_          =						torch.Generator(device=UpperCAmelCase_    ).manual_seed(0    )
														snake_case_          =						sd_pipe(
														    [prompt]						,    image=UpperCAmelCase_						,    generator=UpperCAmelCase_						,    guidance_scale=6.0						,    noise_level=20						,    num_inference_steps=2						,    output_type="""np"""						,    )

														snake_case_          =						output.images

														snake_case_          =						torch.Generator(device=UpperCAmelCase_    ).manual_seed(0    )
														snake_case_          =						sd_pipe(
														    [prompt]						,    image=UpperCAmelCase_						,    generator=UpperCAmelCase_						,    guidance_scale=6.0						,    noise_level=20						,    num_inference_steps=2						,    output_type="""np"""						,    return_dict=UpperCAmelCase_						,    )[0]

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

														snake_case_          =						low_res_image.size[0] * 4
														assert image.shape == (1, expected_height_width, expected_height_width, 3)
														snake_case_          =						np.array([0.3_113, 0.3_910, 0.4_272, 0.4_859, 0.5_061, 0.4_652, 0.5_362, 0.5_715, 0.5_661]    )

														assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-2
														assert np.abs(image_from_tuple_slice.flatten() - expected_slice    ).max() < 1E-2



							def 						lowerCAmelCase    (			self							: Dict    )   ->Tuple:



														"""simple docstring"""



														snake_case_          =						"""cpu"""  # ensure determinism for the device-dependent torch.Generator
														snake_case_          =						self.dummy_cond_unet_upscale
														snake_case_          =						DDPMScheduler()
														snake_case_          =						DDIMScheduler(prediction_type="""v_prediction"""    )
														snake_case_          =						self.dummy_vae
														snake_case_          =						self.dummy_text_encoder
														snake_case_          =						CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

														snake_case_          =						self.dummy_image.cpu().permute(0						,    2						,    3						,    1    )[0]
														snake_case_          =						Image.fromarray(np.uinta(UpperCAmelCase_    )    ).convert("""RGB"""    ).resize((64, 64)    )

														# make sure here that pndm scheduler skips prk
														snake_case_          =						StableDiffusionUpscalePipeline(
														    unet=UpperCAmelCase_						,    low_res_scheduler=UpperCAmelCase_						,    scheduler=UpperCAmelCase_						,    vae=UpperCAmelCase_						,    text_encoder=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_						,    max_noise_level=350						,    )
														snake_case_          =						sd_pipe.to(UpperCAmelCase_    )
														sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_    )

														snake_case_          =						"""A painting of a squirrel eating a burger"""
														snake_case_          =						sd_pipe(
														    2 * [prompt]						,    image=2 * [low_res_image]						,    guidance_scale=6.0						,    noise_level=20						,    num_inference_steps=2						,    output_type="""np"""						,    )
														snake_case_          =						output.images
														assert image.shape[0] == 2

														snake_case_          =						torch.Generator(device=UpperCAmelCase_    ).manual_seed(0    )
														snake_case_          =						sd_pipe(
														    [prompt]						,    image=UpperCAmelCase_						,    generator=UpperCAmelCase_						,    num_images_per_prompt=2						,    guidance_scale=6.0						,    noise_level=20						,    num_inference_steps=2						,    output_type="""np"""						,    )
														snake_case_          =						output.images
														assert image.shape[0] == 2



							@unittest.skipIf(torch_device != """cuda"""						,    """This test requires a GPU"""    )
							def 						lowerCAmelCase    (			self							: Union[str, Any]    )   ->Tuple:



														"""simple docstring"""



														snake_case_          =						self.dummy_cond_unet_upscale
														snake_case_          =						DDPMScheduler()
														snake_case_          =						DDIMScheduler(prediction_type="""v_prediction"""    )
														snake_case_          =						self.dummy_vae
														snake_case_          =						self.dummy_text_encoder
														snake_case_          =						CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

														snake_case_          =						self.dummy_image.cpu().permute(0						,    2						,    3						,    1    )[0]
														snake_case_          =						Image.fromarray(np.uinta(UpperCAmelCase_    )    ).convert("""RGB"""    ).resize((64, 64)    )

														# put models in fp16, except vae as it overflows in fp16
														snake_case_          =						unet.half()
														snake_case_          =						text_encoder.half()

														# make sure here that pndm scheduler skips prk
														snake_case_          =						StableDiffusionUpscalePipeline(
														    unet=UpperCAmelCase_						,    low_res_scheduler=UpperCAmelCase_						,    scheduler=UpperCAmelCase_						,    vae=UpperCAmelCase_						,    text_encoder=UpperCAmelCase_						,    tokenizer=UpperCAmelCase_						,    max_noise_level=350						,    )
														snake_case_          =						sd_pipe.to(UpperCAmelCase_    )
														sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_    )

														snake_case_          =						"""A painting of a squirrel eating a burger"""
														snake_case_          =						torch.manual_seed(0    )
														snake_case_          =						sd_pipe(
														    [prompt]						,    image=UpperCAmelCase_						,    generator=UpperCAmelCase_						,    num_inference_steps=2						,    output_type="""np"""						,    ).images

														snake_case_          =						low_res_image.size[0] * 4
														assert image.shape == (1, expected_height_width, expected_height_width, 3)





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





							'''simple docstring'''

							def 						lowerCAmelCase    (			self							: List[str]    )   ->List[str]:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: List[str]    )   ->Any:



														"""simple docstring"""



														snake_case_          =						load_image(
														    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
														    """/sd2-upscale/low_res_cat.png"""    )
														snake_case_          =						load_numpy(
														    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"""
														    """/upsampled_cat.npy"""    )

														snake_case_          =						"""stabilityai/stable-diffusion-x4-upscaler"""
														snake_case_          =						StableDiffusionUpscalePipeline.from_pretrained(UpperCAmelCase_    )
														pipe.to(UpperCAmelCase_    )
														pipe.set_progress_bar_config(disable=UpperCAmelCase_    )
														pipe.enable_attention_slicing()

														snake_case_          =						"""a cat sitting on a park bench"""

														snake_case_          =						torch.manual_seed(0    )
														snake_case_          =						pipe(
														    prompt=UpperCAmelCase_						,    image=UpperCAmelCase_						,    generator=UpperCAmelCase_						,    output_type="""np"""						,    )
														snake_case_          =						output.images[0]

														assert image.shape == (512, 512, 3)
														assert np.abs(expected_image - image    ).max() < 1E-3



							def 						lowerCAmelCase    (			self							: int    )   ->Optional[Any]:



														"""simple docstring"""



														snake_case_          =						load_image(
														    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
														    """/sd2-upscale/low_res_cat.png"""    )
														snake_case_          =						load_numpy(
														    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"""
														    """/upsampled_cat_fp16.npy"""    )

														snake_case_          =						"""stabilityai/stable-diffusion-x4-upscaler"""
														snake_case_          =						StableDiffusionUpscalePipeline.from_pretrained(
														    UpperCAmelCase_						,    torch_dtype=torch.floataa						,    )
														pipe.to(UpperCAmelCase_    )
														pipe.set_progress_bar_config(disable=UpperCAmelCase_    )
														pipe.enable_attention_slicing()

														snake_case_          =						"""a cat sitting on a park bench"""

														snake_case_          =						torch.manual_seed(0    )
														snake_case_          =						pipe(
														    prompt=UpperCAmelCase_						,    image=UpperCAmelCase_						,    generator=UpperCAmelCase_						,    output_type="""np"""						,    )
														snake_case_          =						output.images[0]

														assert image.shape == (512, 512, 3)
														assert np.abs(expected_image - image    ).max() < 5E-1



							def 						lowerCAmelCase    (			self							: Tuple    )   ->int:



														"""simple docstring"""



														torch.cuda.empty_cache()
														torch.cuda.reset_max_memory_allocated()
														torch.cuda.reset_peak_memory_stats()

														snake_case_          =						load_image(
														    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
														    """/sd2-upscale/low_res_cat.png"""    )

														snake_case_          =						"""stabilityai/stable-diffusion-x4-upscaler"""
														snake_case_          =						StableDiffusionUpscalePipeline.from_pretrained(
														    UpperCAmelCase_						,    torch_dtype=torch.floataa						,    )
														pipe.to(UpperCAmelCase_    )
														pipe.set_progress_bar_config(disable=UpperCAmelCase_    )
														pipe.enable_attention_slicing(1    )
														pipe.enable_sequential_cpu_offload()

														snake_case_          =						"""a cat sitting on a park bench"""

														snake_case_          =						torch.manual_seed(0    )
														snake_case_          =						pipe(
														    prompt=UpperCAmelCase_						,    image=UpperCAmelCase_						,    generator=UpperCAmelCase_						,    num_inference_steps=5						,    output_type="""np"""						,    )

														snake_case_          =						torch.cuda.max_memory_allocated()
														# make sure that less than 2.9 GB is allocated
														assert mem_bytes < 2.9 * 10**9







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


logging.set_verbosity_info()
__SCREAMING_SNAKE_CASE							:      List[str]						=      logging.get_logger(__name__)







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







def   _a			( _SCREAMING_SNAKE_CASE			)      ->			Union[str, Any]:
							snake_case_          =						["""head.weight""", """head.bias"""]
							for k in ignore_keys:
														state_dict.pop(_SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE			)







def   _a			( _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE			)      ->			int:
							snake_case_          =						dct.pop(_SCREAMING_SNAKE_CASE			)
							snake_case_          =						val







def   _a			( )      ->			Dict:
							snake_case_          =						"""http://images.cocodataset.org/val2017/000000039769.jpg"""
							snake_case_          =						Image.open(requests.get(_SCREAMING_SNAKE_CASE			,	stream=_SCREAMING_SNAKE_CASE			).raw			)
							return im







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

							# load original model from torch hub
							snake_case_          =						torch.hub.load("""facebookresearch/dino:main"""			,	_SCREAMING_SNAKE_CASE			)
							original_model.eval()

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

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

							# Check outputs on an image, prepared by ViTImageProcessor
							snake_case_          =						ViTImageProcessor()
							snake_case_          =						image_processor(images=prepare_img()			,	return_tensors="""pt"""			)
							snake_case_          =						encoding["""pixel_values"""]
							snake_case_          =						model(_SCREAMING_SNAKE_CASE			)

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

							Path(_SCREAMING_SNAKE_CASE			).mkdir(exist_ok=_SCREAMING_SNAKE_CASE			)
							print(f"""Saving model {model_name} to {pytorch_dump_folder_path}"""			)
							model.save_pretrained(_SCREAMING_SNAKE_CASE			)
							print(f"""Saving image processor to {pytorch_dump_folder_path}"""			)
							image_processor.save_pretrained(_SCREAMING_SNAKE_CASE			)


if __name__ == "__main__":
				__SCREAMING_SNAKE_CASE							:      Optional[int]						=      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)
				__SCREAMING_SNAKE_CASE							:      int						=      parser.parse_args()
				convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)



