





"""simple docstring"""






import argparse
import json
from pathlib import Path

import requests
import timm
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform

from transformers import (
    BitConfig,
    ViTHybridConfig,
    ViTHybridForImageClassification,
    ViTHybridImageProcessor,
    ViTHybridModel,
)
from transformers.image_utils import PILImageResampling
from transformers.utils import logging


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

def      __A   (      a_    :List[str] ,							a_    :Tuple=False)		->		Optional[int]:
				__a			:    Tuple      							=	[]

				# fmt: off
				# stem:
				rename_keys.append(('''cls_token''', '''vit.embeddings.cls_token'''))
				rename_keys.append(('''pos_embed''', '''vit.embeddings.position_embeddings'''))

				rename_keys.append(('''patch_embed.proj.weight''', '''vit.embeddings.patch_embeddings.projection.weight'''))
				rename_keys.append(('''patch_embed.proj.bias''', '''vit.embeddings.patch_embeddings.projection.bias'''))

				# backbone
				rename_keys.append(('''patch_embed.backbone.stem.conv.weight''', '''vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight'''))
				rename_keys.append(('''patch_embed.backbone.stem.norm.weight''', '''vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight'''))
				rename_keys.append(('''patch_embed.backbone.stem.norm.bias''', '''vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias'''))

				for stage_idx in range(len(config.backbone_config.depths)):
								for layer_idx in range(config.backbone_config.depths[stage_idx]):
												rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight"""))
												rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight"""))
												rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias"""))
												rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight"""))
												rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight"""))
												rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias"""))
												rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight"""))
												rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight"""))
												rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias"""))

								rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight"""))
								rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight"""))
								rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias"""))

				# transformer encoder
				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"""))

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

								# if just the base model, we should remove "vit" from all keys that start with "vit"
								__a			:    Optional[int]      							=	[(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'''),
								    ])
				# fmt: on

				return rename_keys

def      __A   (      a_    :Optional[int] ,							a_    :Any ,							a_    :Any=False)		->		List[str]:



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

def      __A   (      a_    :Tuple)		->		Tuple:
				__a			:    Any      							=	["""head.weight""", """head.bias"""]
				for k in ignore_keys:
								state_dict.pop(a__ ,							a__)

def      __A   (      a_    :int ,							a_    :Dict ,							a_    :str)		->		Any:
				__a			:    Any      							=	dct.pop(a__)
				__a			:    int      							=	val

def      __A   (      )		->		Tuple:
				__a			:    Any      							=	"""http://images.cocodataset.org/val2017/000000039769.jpg"""
				__a			:    Optional[Any]      							=	Image.open(requests.get(a__ ,							stream=a__).raw)
				return im

@torch.no_grad()
def      __A   (      a_    :Any ,							a_    :List[str] ,							a_    :str=False)		->		Tuple:
				__a			:    List[str]      							=	BitConfig(
				    global_padding='''same''' ,							layer_type='''bottleneck''' ,							depths=(3, 4, 9) ,							out_features=['''stage3'''] ,							embedding_dynamic_padding=a__ ,							)
				__a			:    Any      							=	ViTHybridConfig(backbone_config=a__ ,							image_size=3_84 ,							num_labels=10_00)
				__a			:    Union[str, Any]      							=	False

				# load original model from timm
				__a			:    Dict      							=	timm.create_model(a__ ,							pretrained=a__)
				timm_model.eval()

				# load state_dict of original model, remove and rename some keys
				__a			:    Dict      							=	timm_model.state_dict()
				if base_model:
								remove_classification_head_(a__)
				__a			:    List[str]      							=	create_rename_keys(a__ ,							a__)
				for src, dest in rename_keys:
								rename_key(a__ ,							a__ ,							a__)
				read_in_q_k_v(a__ ,							a__ ,							a__)

				__a			:    str      							=	"""huggingface/label-files"""
				__a			:    Any      							=	"""imagenet-1k-id2label.json"""
				__a			:    Optional[Any]      							=	json.load(open(hf_hub_download(a__ ,							a__ ,							repo_type='''dataset''') ,							'''r'''))
				__a			:    Optional[int]      							=	{int(a__): v for k, v in idalabel.items()}
				__a			:    Optional[Any]      							=	idalabel
				__a			:    str      							=	{v: k for k, v in idalabel.items()}

				# load HuggingFace model
				if vit_name[-5:] == "in21k":
								__a			:    List[str]      							=	ViTHybridModel(a__).eval()
				else:
								__a			:    Optional[Any]      							=	ViTHybridForImageClassification(a__).eval()
				model.load_state_dict(a__)

				# create image processor
				__a			:    Optional[Any]      							=	create_transform(**resolve_data_config({} ,							model=a__))
				__a			:    int      							=	transform.transforms

				__a			:    str      							=	{
				    """bilinear""": PILImageResampling.BILINEAR,
				    """bicubic""": PILImageResampling.BICUBIC,
				    """nearest""": PILImageResampling.NEAREST,
				}

				__a			:    Dict      							=	ViTHybridImageProcessor(
				    do_resize=a__ ,							size={'''shortest_edge''': timm_transforms[0].size} ,							resample=pillow_resamplings[timm_transforms[0].interpolation.value] ,							do_center_crop=a__ ,							crop_size={'''height''': timm_transforms[1].size[0], '''width''': timm_transforms[1].size[1]} ,							do_normalize=a__ ,							image_mean=timm_transforms[-1].mean.tolist() ,							image_std=timm_transforms[-1].std.tolist() ,							)

				__a			:    Tuple      							=	prepare_img()
				__a			:    Any      							=	transform(a__).unsqueeze(0)
				__a			:    Any      							=	processor(a__ ,							return_tensors='''pt''').pixel_values

				# verify pixel values
				assert torch.allclose(a__ ,							a__)

				# verify logits
				with torch.no_grad():
								__a			:    Tuple      							=	model(a__)
								__a			:    str      							=	outputs.logits

				print('''Predicted class:''' ,							logits.argmax(-1).item())
				if base_model:
								__a			:    List[Any]      							=	timm_model.forward_features(a__)
								assert timm_pooled_output.shape == outputs.pooler_output.shape
								assert torch.allclose(a__ ,							outputs.pooler_output ,							atol=1e-3)
				else:
								__a			:    Any      							=	timm_model(a__)
								assert timm_logits.shape == outputs.logits.shape
								assert torch.allclose(a__ ,							outputs.logits ,							atol=1e-3)
				print('''Looks ok!''')

				if pytorch_dump_folder_path is not None:
								Path(a__).mkdir(exist_ok=a__)
								print(F"""Saving model {vit_name} to {pytorch_dump_folder_path}""")
								model.save_pretrained(a__)
								print(F"""Saving processor to {pytorch_dump_folder_path}""")
								processor.save_pretrained(a__)

				if push_to_hub:
								print(F"""Pushing model and processor to the hub {vit_name}""")
								model.push_to_hub(F"""ybelkada/{vit_name}""")
								processor.push_to_hub(F"""ybelkada/{vit_name}""")


if __name__ == "__main__":
		A			      =	argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '''--vit_name''',
		    default='''vit_base_r50_s16_384''',
		    type=str,
		    help='''Name of the hybrid ViT timm model 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(
		    '''--push_to_hub''', action='''store_true''', help='''Whether to upload the model to the HuggingFace hub.'''
		)

		A			      =	parser.parse_args()
		convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)


import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()



class        A__(a_,  a_,  unittest.TestCase							):
						"""simple docstring"""






						_A    :				Optional[Any]       					=			StableDiffusionSAGPipeline
						_A    :				Any       					=			TEXT_TO_IMAGE_PARAMS
						_A    :				Dict       					=			TEXT_TO_IMAGE_BATCH_PARAMS
						_A    :				Dict       					=			TEXT_TO_IMAGE_IMAGE_PARAMS
						_A    :				int       					=			TEXT_TO_IMAGE_IMAGE_PARAMS
						_A    :				Optional[int]       					=			False





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									torch.manual_seed(0    )
									a_						:						Tuple 							= UNetaDConditionModel(
									    block_out_channels=(32, 64)  ,      layers_per_block=2  ,      sample_size=32  ,      in_channels=4  ,      out_channels=4  ,      down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")  ,      up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")  ,      cross_attention_dim=32  ,      )
									a_						:						int 							= DDIMScheduler(
									    beta_start=0.0_0_0_8_5  ,      beta_end=0.0_1_2  ,      beta_schedule="""scaled_linear"""  ,      clip_sample=_lowercase  ,      set_alpha_to_one=_lowercase  ,      )
									torch.manual_seed(0    )
									a_						:						Tuple 							= AutoencoderKL(
									    block_out_channels=[32, 64]  ,      in_channels=3  ,      out_channels=3  ,      down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]  ,      up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]  ,      latent_channels=4  ,      )
									torch.manual_seed(0    )
									a_						:						str 							= 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  ,      )
									a_						:						Union[str, Any] 							= CLIPTextModel(_lowercase    )
									a_						:						int 							= CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

									a_						:						Union[str, Any] 							= {
									    """unet""": unet,
									    """scheduler""": scheduler,
									    """vae""": vae,
									    """text_encoder""": text_encoder,
									    """tokenizer""": tokenizer,
									    """safety_checker""": None,
									    """feature_extractor""": None,
									}
									return components





						def 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase=0    )     -> Dict:
									if str(_lowercase    ).startswith("""mps"""    ):
												a_						:						Optional[int] 							= torch.manual_seed(_lowercase    )
									else:
												a_						:						str 							= torch.Generator(device=_lowercase    ).manual_seed(_lowercase    )
									a_						:						Tuple 							= {
									    """prompt""": """.""",
									    """generator""": generator,
									    """num_inference_steps""": 2,
									    """guidance_scale""": 1.0,
									    """sag_scale""": 1.0,
									    """output_type""": """numpy""",
									}
									return inputs





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									super().test_inference_batch_single_identical(expected_max_diff=3e-3    )







@slow
@require_torch_gpu
class        A__(unittest.TestCase							):
						"""simple docstring"""






						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						Tuple 							= StableDiffusionSAGPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""    )
									a_						:						List[str] 							= sag_pipe.to(_lowercase    )
									sag_pipe.set_progress_bar_config(disable=_lowercase    )

									a_						:						Optional[int] 							= """."""
									a_						:						int 							= torch.manual_seed(0    )
									a_						:						Any 							= sag_pipe(
									    [prompt]  ,      generator=_lowercase  ,      guidance_scale=7.5  ,      sag_scale=1.0  ,      num_inference_steps=20  ,      output_type="""np"""    )

									a_						:						Any 							= output.images

									a_						:						Optional[int] 							= image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									a_						:						str 							= np.array([0.1_5_6_8, 0.1_7_3_8, 0.1_6_9_5, 0.1_6_9_3, 0.1_5_0_7, 0.1_7_0_5, 0.1_5_4_7, 0.1_7_5_1, 0.1_9_4_9]    )

									assert np.abs(image_slice.flatten() - expected_slice    ).max() < 5e-2





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									a_						:						Optional[int] 							= StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""    )
									a_						:						List[str] 							= sag_pipe.to(_lowercase    )
									sag_pipe.set_progress_bar_config(disable=_lowercase    )

									a_						:						int 							= """."""
									a_						:						Dict 							= torch.manual_seed(0    )
									a_						:						Union[str, Any] 							= sag_pipe(
									    [prompt]  ,      generator=_lowercase  ,      guidance_scale=7.5  ,      sag_scale=1.0  ,      num_inference_steps=20  ,      output_type="""np"""    )

									a_						:						Optional[Any] 							= output.images

									a_						:						int 							= image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									a_						:						Dict 							= np.array([0.3_4_5_9, 0.2_8_7_6, 0.2_5_3_7, 0.3_0_0_2, 0.2_6_7_1, 0.2_1_6_0, 0.3_0_2_6, 0.2_2_6_2, 0.2_3_7_1]    )

									assert np.abs(image_slice.flatten() - expected_slice    ).max() < 5e-2





						def 			UpperCamelCase__					(    self    )     -> Any:
									a_						:						int 							= StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""    )
									a_						:						Optional[Any] 							= sag_pipe.to(_lowercase    )
									sag_pipe.set_progress_bar_config(disable=_lowercase    )

									a_						:						List[Any] 							= """."""
									a_						:						str 							= torch.manual_seed(0    )
									a_						:						int 							= sag_pipe(
									    [prompt]  ,      width=768  ,      height=512  ,      generator=_lowercase  ,      guidance_scale=7.5  ,      sag_scale=1.0  ,      num_inference_steps=20  ,      output_type="""np"""  ,      )

									a_						:						Any 							= output.images

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


