
"""simple docstring"""


import gc
import random
import unittest

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

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class  __lowerCamelCase  ( lowerCAmelCase						,	lowerCAmelCase						,	unittest.TestCase			):
				a__:      List[Any]          =    StableDiffusionXLImgaImgPipeline
				a__:      Any          =    TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width'}
				a__:      Dict          =    PipelineTesterMixin.required_optional_params - {'latents'}
				a__:      int          =    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
				a__:      Optional[int]          =    IMAGE_TO_IMAGE_IMAGE_PARAMS
				a__:      str          =    IMAGE_TO_IMAGE_IMAGE_PARAMS




				def        UpperCAmelCase__						(				self   ):
							torch.manual_seed(0   )
							lowerCamelCase_					=  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''')							,						attention_head_dim=(2, 4)							,						use_linear_projection=UpperCAmelCase							,						addition_embed_type='''text_time'''							,						addition_time_embed_dim=8							,						transformer_layers_per_block=(1, 2)							,						projection_class_embeddings_input_dim=80							,						cross_attention_dim=64							,						)
							lowerCamelCase_					=  EulerDiscreteScheduler(
							    beta_start=0.0_0_0_8_5							,						beta_end=0.0_1_2							,						steps_offset=1							,						beta_schedule='''scaled_linear'''							,						timestep_spacing='''leading'''							,						)
							torch.manual_seed(0   )
							lowerCamelCase_					=  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							,						sample_size=128							,						)
							torch.manual_seed(0   )
							lowerCamelCase_					=  CLIPTextConfig(
							    bos_token_id=0							,						eos_token_id=2							,						hidden_size=32							,						intermediate_size=37							,						layer_norm_eps=1e-0_5							,						num_attention_heads=4							,						num_hidden_layers=5							,						pad_token_id=1							,						vocab_size=1000							,						hidden_act='''gelu'''							,						projection_dim=32							,						)
							lowerCamelCase_					=  CLIPTextModel(UpperCAmelCase   )
							lowerCamelCase_					=  CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''							,						local_files_only=UpperCAmelCase   )

							lowerCamelCase_					=  CLIPTextModelWithProjection(UpperCAmelCase   )
							lowerCamelCase_					=  CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''							,						local_files_only=UpperCAmelCase   )

							lowerCamelCase_					=  {
							    '''unet''': unet,
							    '''scheduler''': scheduler,
							    '''vae''': vae,
							    '''text_encoder''': text_encoder,
							    '''tokenizer''': tokenizer,
							    '''text_encoder_2''': text_encoder_a,
							    '''tokenizer_2''': tokenizer_a,
							    # "safety_checker": None,
							    # "feature_extractor": None,
							}
							return components




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase=0   ):
							lowerCamelCase_					=  floats_tensor((1, 3, 32, 32)							,						rng=random.Random(UpperCAmelCase   )   ).to(UpperCAmelCase   )
							lowerCamelCase_					=  image / 2 + 0.5
							if str(UpperCAmelCase   ).startswith('''mps'''   ):
										lowerCamelCase_					=  torch.manual_seed(UpperCAmelCase   )
							else:
										lowerCamelCase_					=  torch.Generator(device=UpperCAmelCase   ).manual_seed(UpperCAmelCase   )
							lowerCamelCase_					=  {
							    '''prompt''': '''A painting of a squirrel eating a burger''',
							    '''image''': image,
							    '''generator''': generator,
							    '''num_inference_steps''': 2,
							    '''guidance_scale''': 5.0,
							    '''output_type''': '''numpy''',
							    '''strength''': 0.7_5,
							}
							return inputs




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  '''cpu'''  # ensure determinism for the device-dependent torch.Generator
							lowerCamelCase_					=  self.get_dummy_components()
							lowerCamelCase_					=  StableDiffusionXLImgaImgPipeline(**UpperCAmelCase   )
							lowerCamelCase_					=  sd_pipe.to(UpperCAmelCase   )
							sd_pipe.set_progress_bar_config(disable=UpperCAmelCase   )

							lowerCamelCase_					=  self.get_dummy_inputs(UpperCAmelCase   )
							lowerCamelCase_					=  sd_pipe(**UpperCAmelCase   ).images
							lowerCamelCase_					=  image[0, -3:, -3:, -1]

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

							lowerCamelCase_					=  np.array([0.4_6_5_6, 0.4_8_4_0, 0.4_4_3_9, 0.6_6_9_8, 0.5_5_7_4, 0.4_5_2_4, 0.5_7_9_9, 0.5_9_4_3, 0.5_1_6_5]   )

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




				def        UpperCAmelCase__						(				self   ):
							super().test_attention_slicing_forward_pass(expected_max_diff=3e-3   )




				def        UpperCAmelCase__						(				self   ):
							super().test_inference_batch_single_identical(expected_max_diff=3e-3   )




				def        UpperCAmelCase__						(				self   ):
							pass





				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  self.get_dummy_components()
							lowerCamelCase_					=  StableDiffusionXLImgaImgPipeline(**UpperCAmelCase   )
							lowerCamelCase_					=  sd_pipe.to(UpperCAmelCase   )
							lowerCamelCase_					=  sd_pipe.to(UpperCAmelCase   )
							sd_pipe.set_progress_bar_config(disable=UpperCAmelCase   )

							# forward without prompt embeds
							lowerCamelCase_					=  self.get_dummy_inputs(UpperCAmelCase   )
							lowerCamelCase_					=  3 * ['''this is a negative prompt''']
							lowerCamelCase_					=  negative_prompt
							lowerCamelCase_					=  3 * [inputs['''prompt''']]

							lowerCamelCase_					=  sd_pipe(**UpperCAmelCase   )
							lowerCamelCase_					=  output.images[0, -3:, -3:, -1]

							# forward with prompt embeds
							lowerCamelCase_					=  self.get_dummy_inputs(UpperCAmelCase   )
							lowerCamelCase_					=  3 * ['''this is a negative prompt''']
							lowerCamelCase_					=  3 * [inputs.pop('''prompt'''   )]

							(
							    (
							    lowerCamelCase_
							)		, (
							    lowerCamelCase_
							)		, (
							    lowerCamelCase_
							)		, (
							    lowerCamelCase_
							)		, 
							)					=  sd_pipe.encode_prompt(UpperCAmelCase							,						negative_prompt=UpperCAmelCase   )

							lowerCamelCase_					=  sd_pipe(
							    **UpperCAmelCase							,						prompt_embeds=UpperCAmelCase							,						negative_prompt_embeds=UpperCAmelCase							,						pooled_prompt_embeds=UpperCAmelCase							,						negative_pooled_prompt_embeds=UpperCAmelCase							,						)
							lowerCamelCase_					=  output.images[0, -3:, -3:, -1]

							# make sure that it's equal
							assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()   ).max() < 1e-4




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




				def        UpperCAmelCase__						(				self   ):
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase="cpu"							,						UpperCAmelCase=torch.floataa							,						UpperCAmelCase=0   ):
							lowerCamelCase_					=  torch.Generator(device=UpperCAmelCase   ).manual_seed(UpperCAmelCase   )
							lowerCamelCase_					=  np.random.RandomState(UpperCAmelCase   ).standard_normal((1, 4, 64, 64)   )
							lowerCamelCase_					=  torch.from_numpy(UpperCAmelCase   ).to(device=UpperCAmelCase							,						dtype=UpperCAmelCase   )
							lowerCamelCase_					=  {
							    '''prompt''': '''a photograph of an astronaut riding a horse''',
							    '''latents''': latents,
							    '''generator''': generator,
							    '''num_inference_steps''': 3,
							    '''guidance_scale''': 7.5,
							    '''output_type''': '''numpy''',
							}
							return inputs





				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  DiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-base'''   )
							pipe.to(UpperCAmelCase   )
							pipe.set_progress_bar_config(disable=UpperCAmelCase   )

							lowerCamelCase_					=  self.get_inputs(UpperCAmelCase   )
							lowerCamelCase_					=  pipe(**UpperCAmelCase   ).images
							lowerCamelCase_					=  image[0, -3:, -3:, -1].flatten()

							assert image.shape == (1, 512, 512, 3)
							lowerCamelCase_					=  np.array([0.4_9_4_9_3, 0.4_7_8_9_6, 0.4_0_7_9_8, 0.5_4_2_1_4, 0.5_3_2_1_2, 0.4_8_2_0_2, 0.4_7_6_5_6, 0.4_6_3_2_9, 0.4_8_5_0_6]   )
							assert np.abs(image_slice - expected_slice   ).max() < 7e-3





"""simple docstring"""


import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
from torchvision import transforms

from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling






def  lowercase						(	lowerCAmelCase__						):
			lowerCamelCase_					=  [2, 2, 6, 2] if '''tiny''' in model_name else [2, 2, 18, 2]
			lowerCamelCase_					=  True if '''large''' in model_name or '''huge''' in model_name else False
			lowerCamelCase_					=  True if '''large''' in model_name or '''huge''' in model_name else False
			lowerCamelCase_					=  True if '''large''' in model_name or '''huge''' in model_name else False

			if "large" in model_name or "xlarge" in model_name or "huge" in model_name:
						if "fl3" in model_name:
									lowerCamelCase_					=  [3, 3, 3, 3]
									lowerCamelCase_					=  [5, 5, 5, 5]
						elif "fl4" in model_name:
									lowerCamelCase_					=  [4, 4, 4, 4]
									lowerCamelCase_					=  [3, 3, 3, 3]

			if "tiny" in model_name or "small" in model_name or "base" in model_name:
						lowerCamelCase_					=  [3, 3, 3, 3]
						if "lrf" in model_name:
									lowerCamelCase_					=  [3, 3, 3, 3]
						else:
									lowerCamelCase_					=  [2, 2, 2, 2]

			if "tiny" in model_name:
						lowerCamelCase_					=  96
			elif "small" in model_name:
						lowerCamelCase_					=  96
			elif "base" in model_name:
						lowerCamelCase_					=  128
			elif "large" in model_name:
						lowerCamelCase_					=  192
			elif "xlarge" in model_name:
						lowerCamelCase_					=  256
			elif "huge" in model_name:
						lowerCamelCase_					=  352

			# set label information
			lowerCamelCase_					=  '''huggingface/label-files'''
			if "large" in model_name or "huge" in model_name:
						lowerCamelCase_					=  '''imagenet-22k-id2label.json'''
			else:
						lowerCamelCase_					=  '''imagenet-1k-id2label.json'''

			lowerCamelCase_					=  json.load(open(hf_hub_download(lowerCAmelCase__	,lowerCAmelCase__	,repo_type='''dataset'''						)	,'''r'''						)						)
			lowerCamelCase_					=  {int(lowerCAmelCase__						): v for k, v in idalabel.items()}
			lowerCamelCase_					=  {v: k for k, v in idalabel.items()}

			lowerCamelCase_					=  FocalNetConfig(
			    embed_dim=lowerCAmelCase__	,depths=lowerCAmelCase__	,focal_levels=lowerCAmelCase__	,focal_windows=lowerCAmelCase__	,use_conv_embed=lowerCAmelCase__	,idalabel=lowerCAmelCase__	,labelaid=lowerCAmelCase__	,use_post_layernorm=lowerCAmelCase__	,use_layerscale=lowerCAmelCase__	,)

			return config






def  lowercase						(	lowerCAmelCase__						):
			if "patch_embed.proj" in name:
						lowerCamelCase_					=  name.replace('''patch_embed.proj'''	,'''embeddings.patch_embeddings.projection'''						)
			if "patch_embed.norm" in name:
						lowerCamelCase_					=  name.replace('''patch_embed.norm'''	,'''embeddings.norm'''						)
			if "layers" in name:
						lowerCamelCase_					=  '''encoder.''' + name
			if "encoder.layers" in name:
						lowerCamelCase_					=  name.replace('''encoder.layers'''	,'''encoder.stages'''						)
			if "downsample.proj" in name:
						lowerCamelCase_					=  name.replace('''downsample.proj'''	,'''downsample.projection'''						)
			if "blocks" in name:
						lowerCamelCase_					=  name.replace('''blocks'''	,'''layers'''						)
			if "modulation.f.weight" in name or "modulation.f.bias" in name:
						lowerCamelCase_					=  name.replace('''modulation.f'''	,'''modulation.projection_in'''						)
			if "modulation.h.weight" in name or "modulation.h.bias" in name:
						lowerCamelCase_					=  name.replace('''modulation.h'''	,'''modulation.projection_context'''						)
			if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
						lowerCamelCase_					=  name.replace('''modulation.proj'''	,'''modulation.projection_out'''						)

			if name == "norm.weight":
						lowerCamelCase_					=  '''layernorm.weight'''
			if name == "norm.bias":
						lowerCamelCase_					=  '''layernorm.bias'''

			if "head" in name:
						lowerCamelCase_					=  name.replace('''head'''	,'''classifier'''						)
			else:
						lowerCamelCase_					=  '''focalnet.''' + name

			return name






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__	,lowerCAmelCase__=False						):
			# fmt: off
			lowerCamelCase_					=  {
			    '''focalnet-tiny''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth''',
			    '''focalnet-tiny-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth''',
			    '''focalnet-small''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth''',
			    '''focalnet-small-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth''',
			    '''focalnet-base''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth''',
			    '''focalnet-base-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth''',
			    '''focalnet-large-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth''',
			    '''focalnet-large-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth''',
			    '''focalnet-xlarge-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth''',
			    '''focalnet-xlarge-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth''',
			}
			# fmt: on

			lowerCamelCase_					=  model_name_to_url[model_name]
			print('''Checkpoint URL: '''	,lowerCAmelCase__						)
			lowerCamelCase_					=  torch.hub.load_state_dict_from_url(lowerCAmelCase__	,map_location='''cpu'''						)['''model''']

			# rename keys
			for key in state_dict.copy().keys():
						lowerCamelCase_					=  state_dict.pop(lowerCAmelCase__						)
						lowerCamelCase_					=  val

			lowerCamelCase_					=  get_focalnet_config(lowerCAmelCase__						)
			lowerCamelCase_					=  FocalNetForImageClassification(lowerCAmelCase__						)
			model.eval()

			# load state dict
			model.load_state_dict(lowerCAmelCase__						)

			# verify conversion
			lowerCamelCase_					=  '''http://images.cocodataset.org/val2017/000000039769.jpg'''

			lowerCamelCase_					=  BitImageProcessor(
			    do_resize=lowerCAmelCase__	,size={'''shortest_edge''': 256}	,resample=PILImageResampling.BILINEAR	,do_center_crop=lowerCAmelCase__	,crop_size=224	,do_normalize=lowerCAmelCase__	,image_mean=lowerCAmelCase__	,image_std=lowerCAmelCase__	,)
			lowerCamelCase_					=  Image.open(requests.get(lowerCAmelCase__	,stream=lowerCAmelCase__						).raw						)
			lowerCamelCase_					=  processor(images=lowerCAmelCase__	,return_tensors='''pt'''						)

			lowerCamelCase_					=  transforms.Compose(
			    [
			        transforms.Resize(256						),
			        transforms.CenterCrop(224						),
			        transforms.ToTensor(),
			        transforms.Normalize(mean=[0.485, 0.456, 0.406]	,std=[0.229, 0.224, 0.225]						),
			    ]						)

			lowerCamelCase_					=  image_transforms(lowerCAmelCase__						).unsqueeze(0						)

			# verify pixel_values
			assert torch.allclose(inputs.pixel_values	,lowerCAmelCase__	,atol=1E-4						)

			lowerCamelCase_					=  model(**lowerCAmelCase__						)

			lowerCamelCase_					=  outputs.logits.argmax(-1						).item()
			print('''Predicted class:'''	,model.config.idalabel[predicted_class_idx]						)

			print('''First values of logits:'''	,outputs.logits[0, :3]						)

			if model_name == "focalnet-tiny":
						lowerCamelCase_					=  torch.tensor([0.2_166, -0.4_368, 0.2_191]						)
			elif model_name == "focalnet-tiny-lrf":
						lowerCamelCase_					=  torch.tensor([1.1_669, 0.0_125, -0.1_695]						)
			elif model_name == "focalnet-small":
						lowerCamelCase_					=  torch.tensor([0.4_917, -0.0_430, 0.1_341]						)
			elif model_name == "focalnet-small-lrf":
						lowerCamelCase_					=  torch.tensor([-0.2_588, -0.5_342, -0.2_331]						)
			elif model_name == "focalnet-base":
						lowerCamelCase_					=  torch.tensor([-0.1_655, -0.4_090, -0.1_730]						)
			elif model_name == "focalnet-base-lrf":
						lowerCamelCase_					=  torch.tensor([0.5_306, -0.0_483, -0.3_928]						)
			assert torch.allclose(outputs.logits[0, :3]	,lowerCAmelCase__	,atol=1E-4						)
			print('''Looks ok!'''						)

			if pytorch_dump_folder_path is not None:
						print(f"Saving model and processor of {model_name} to {pytorch_dump_folder_path}"						)
						model.save_pretrained(lowerCAmelCase__						)
						processor.save_pretrained(lowerCAmelCase__						)

			if push_to_hub:
						print(f"Pushing model and processor of {model_name} to the hub..."						)
						model.push_to_hub(f"{model_name}"						)
						processor.push_to_hub(f"{model_name}"						)


if __name__ == "__main__":
						A_    							=					argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    """--model_name""",
						    default="""focalnet-tiny""",
						    type=str,
						    help="""Name of the FocalNet 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 push the model and processor to the hub.""",
						)

						A_    							=					parser.parse_args()
						convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)



