


"""simple docstring"""





import argparse

import requests
import torch
from PIL import Image

from transformers import CLIPProcessor, GroupViTConfig, GroupViTModel



def 				lowerCAmelCase_			(      SCREAMING_SNAKE_CASE       :							int   ):


							'''simple docstring'''







							if "img_encoder.pos_embed" in name:
														__lowerCamelCase      :      List[Any]         =name.replace('''img_encoder.pos_embed'''    ,						'''vision_model.embeddings.position_embeddings'''   )
							if "img_encoder.patch_embed.proj" in name:
														__lowerCamelCase      :      List[str]         =name.replace('''img_encoder.patch_embed.proj'''    ,						'''vision_model.embeddings.patch_embeddings.projection'''   )
							if "img_encoder.patch_embed.norm" in name:
														__lowerCamelCase      :      str         =name.replace('''img_encoder.patch_embed.norm'''    ,						'''vision_model.embeddings.layernorm'''   )
							if "img_encoder.layers" in name:
														__lowerCamelCase      :      Dict         =name.replace('''img_encoder.layers'''    ,						'''vision_model.encoder.stages'''   )
							if "blocks" in name and "res" not in name:
														__lowerCamelCase      :      Union[str, Any]         =name.replace('''blocks'''    ,						'''layers'''   )
							if "attn" in name and "pre_assign" not in name:
														__lowerCamelCase      :      Tuple         =name.replace('''attn'''    ,						'''self_attn'''   )
							if "proj" in name and "self_attn" in name and "text" not in name:
														__lowerCamelCase      :      Optional[Any]         =name.replace('''proj'''    ,						'''out_proj'''   )
							if "pre_assign_attn.attn.proj" in name:
														__lowerCamelCase      :      str         =name.replace('''pre_assign_attn.attn.proj'''    ,						'''pre_assign_attn.attn.out_proj'''   )
							if "norm1" in name:
														__lowerCamelCase      :      str         =name.replace('''norm1'''    ,						'''layer_norm1'''   )
							if "norm2" in name and "pre_assign" not in name:
														__lowerCamelCase      :      Dict         =name.replace('''norm2'''    ,						'''layer_norm2'''   )
							if "img_encoder.norm" in name:
														__lowerCamelCase      :      Optional[Any]         =name.replace('''img_encoder.norm'''    ,						'''vision_model.layernorm'''   )
							# text encoder
							if "text_encoder.token_embedding" in name:
														__lowerCamelCase      :      Union[str, Any]         =name.replace('''text_encoder.token_embedding'''    ,						'''text_model.embeddings.token_embedding'''   )
							if "text_encoder.positional_embedding" in name:
														__lowerCamelCase      :      int         =name.replace('''text_encoder.positional_embedding'''    ,						'''text_model.embeddings.position_embedding.weight'''   )
							if "text_encoder.transformer.resblocks." in name:
														__lowerCamelCase      :      Optional[Any]         =name.replace('''text_encoder.transformer.resblocks.'''    ,						'''text_model.encoder.layers.'''   )
							if "ln_1" in name:
														__lowerCamelCase      :      int         =name.replace('''ln_1'''    ,						'''layer_norm1'''   )
							if "ln_2" in name:
														__lowerCamelCase      :      List[Any]         =name.replace('''ln_2'''    ,						'''layer_norm2'''   )
							if "c_fc" in name:
														__lowerCamelCase      :      Dict         =name.replace('''c_fc'''    ,						'''fc1'''   )
							if "c_proj" in name:
														__lowerCamelCase      :      List[Any]         =name.replace('''c_proj'''    ,						'''fc2'''   )
							if "text_encoder" in name:
														__lowerCamelCase      :      Union[str, Any]         =name.replace('''text_encoder'''    ,						'''text_model'''   )
							if "ln_final" in name:
														__lowerCamelCase      :      Union[str, Any]         =name.replace('''ln_final'''    ,						'''final_layer_norm'''   )
							# projection layers
							if "img_projector.linear_hidden." in name:
														__lowerCamelCase      :      int         =name.replace('''img_projector.linear_hidden.'''    ,						'''visual_projection.'''   )
							if "img_projector.linear_out." in name:
														__lowerCamelCase      :      List[str]         =name.replace('''img_projector.linear_out.'''    ,						'''visual_projection.3.'''   )
							if "text_projector.linear_hidden" in name:
														__lowerCamelCase      :      Optional[Any]         =name.replace('''text_projector.linear_hidden'''    ,						'''text_projection'''   )
							if "text_projector.linear_out" in name:
														__lowerCamelCase      :      Optional[Any]         =name.replace('''text_projector.linear_out'''    ,						'''text_projection.3'''   )

							return name



def 				lowerCAmelCase_			(      SCREAMING_SNAKE_CASE       :							Tuple    ,						SCREAMING_SNAKE_CASE       :							List[Any]   ):


							'''simple docstring'''







							for key in orig_state_dict.copy().keys():
														__lowerCamelCase      :      List[str]         =orig_state_dict.pop(SCREAMING_SNAKE_CASE   )

														if "qkv" in key:
																					# weights and biases of the key, value and query projections of vision encoder's attention layers require special treatment:
																					# we need to split them up into separate matrices/vectors
																					__lowerCamelCase      :      Union[str, Any]         =key.split('''.'''   )
																					__lowerCamelCase      :      Any         =int(key_split[2]   ), int(key_split[4]   )
																					__lowerCamelCase      :      Any         =config.vision_config.hidden_size
																					if "weight" in key:
																												__lowerCamelCase      :      Dict         =val[:dim, :]
																												__lowerCamelCase      :      List[str]         =val[dim : dim * 2, :]
																												__lowerCamelCase      :      Optional[int]         =val[-dim:, :]
																					else:
																												__lowerCamelCase      :      int         =val[:dim]
																												__lowerCamelCase      :      Tuple         =val[dim : dim * 2]
																												__lowerCamelCase      :      Dict         =val[-dim:]
														elif "in_proj" in key:
																					# weights and biases of the key, value and query projections of text encoder's attention layers require special treatment:
																					# we need to split them up into separate matrices/vectors
																					__lowerCamelCase      :      Optional[Any]         =key.split('''.'''   )
																					__lowerCamelCase      :      Optional[int]         =int(key_split[3]   )
																					__lowerCamelCase      :      Any         =config.text_config.hidden_size
																					if "weight" in key:
																												__lowerCamelCase      :      int         =val[:dim, :]
																												__lowerCamelCase      :      Union[str, Any]         =val[
																												    dim : dim * 2, :
																												]
																												__lowerCamelCase      :      List[str]         =val[-dim:, :]
																					else:
																												__lowerCamelCase      :      Union[str, Any]         =val[:dim]
																												__lowerCamelCase      :      str         =val[dim : dim * 2]
																												__lowerCamelCase      :      int         =val[-dim:]
														else:
																					__lowerCamelCase      :      Union[str, Any]         =rename_key(SCREAMING_SNAKE_CASE   )
																					# squeeze if necessary
																					if (
																					    "text_projection.0" in new_name
																					    or "text_projection.3" in new_name
																					    or "visual_projection.0" in new_name
																					    or "visual_projection.3" in new_name
																					):
																												__lowerCamelCase      :      List[Any]         =val.squeeze_()
																					else:
																												__lowerCamelCase      :      str         =val

							return orig_state_dict



def 				lowerCAmelCase_			(      ):


							'''simple docstring'''







							__lowerCamelCase      :      Optional[Any]         ='''http://images.cocodataset.org/val2017/000000039769.jpg'''
							__lowerCamelCase      :      Dict         =Image.open(requests.get(SCREAMING_SNAKE_CASE    ,						stream=SCREAMING_SNAKE_CASE   ).raw   )
							return im



@torch.no_grad()
def 				lowerCAmelCase_			(      SCREAMING_SNAKE_CASE       :							str    ,						SCREAMING_SNAKE_CASE       :							Union[str, Any]    ,						SCREAMING_SNAKE_CASE       :							Optional[Any]="groupvit-gcc-yfcc"    ,						SCREAMING_SNAKE_CASE       :							Any=False   ):


							'''simple docstring'''







							__lowerCamelCase      :      Optional[int]         =GroupViTConfig()
							__lowerCamelCase      :      List[str]         =GroupViTModel(SCREAMING_SNAKE_CASE   ).eval()

							__lowerCamelCase      :      Any         =torch.load(SCREAMING_SNAKE_CASE    ,						map_location='''cpu'''   )['''model''']
							__lowerCamelCase      :      Optional[int]         =convert_state_dict(SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE   )
							__lowerCamelCase      :      Any         =model.load_state_dict(SCREAMING_SNAKE_CASE    ,						strict=SCREAMING_SNAKE_CASE   )
							assert missing_keys == ["text_model.embeddings.position_ids"]
							assert (unexpected_keys == ["multi_label_logit_scale"]) or (len(SCREAMING_SNAKE_CASE   ) == 0)

							# verify result
							__lowerCamelCase      :      List[str]         =CLIPProcessor.from_pretrained('''openai/clip-vit-base-patch32'''   )
							__lowerCamelCase      :      Optional[int]         =prepare_img()
							__lowerCamelCase      :      Any         =processor(text=['''a photo of a cat''', '''a photo of a dog''']    ,						images=SCREAMING_SNAKE_CASE    ,						padding=SCREAMING_SNAKE_CASE    ,						return_tensors='''pt'''   )

							with torch.no_grad():
														__lowerCamelCase      :      List[Any]         =model(**SCREAMING_SNAKE_CASE   )

							if model_name == "groupvit-gcc-yfcc":
														__lowerCamelCase      :      str         =torch.tensor([[13.3_523, 6.3_629]]   )
							elif model_name == "groupvit-gcc-redcaps":
														__lowerCamelCase      :      str         =torch.tensor([[16.1_873, 8.6_230]]   )
							else:
														raise ValueError(F'Model name {model_name} not supported.'   )
							assert torch.allclose(outputs.logits_per_image    ,						SCREAMING_SNAKE_CASE    ,						atol=1E-3   )

							processor.save_pretrained(SCREAMING_SNAKE_CASE   )
							model.save_pretrained(SCREAMING_SNAKE_CASE   )
							print('''Successfully saved processor and model to'''    ,						SCREAMING_SNAKE_CASE   )

							if push_to_hub:
														print('''Pushing to the hub...'''   )
														processor.push_to_hub(SCREAMING_SNAKE_CASE    ,						organization='''nielsr'''   )
														model.push_to_hub(SCREAMING_SNAKE_CASE    ,						organization='''nielsr'''   )


if __name__ == "__main__":
							_UpperCamelCase							=      argparse.ArgumentParser()
							parser.add_argument(
							    '--pytorch_dump_folder_path', default=None, type=str, help='Path to dump the processor and PyTorch model.'
							)
							parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to GroupViT checkpoint')
							parser.add_argument(
							    '--model_name',
							    default='groupvit-gccy-fcc',
							    type=str,
							    help='Name of the model. Expecting either \'groupvit-gcc-yfcc\' or \'groupvit-gcc-redcaps\'',
							)
							parser.add_argument(
							    '--push_to_hub',
							    action='store_true',
							    help='Whether or not to push the converted model and processor to the 🤗 hub using the provided `model_name`.',
							)
							_UpperCamelCase							=      parser.parse_args()

							convert_groupvit_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)











"""simple docstring"""





import gc
import unittest

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

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

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


enable_full_determinism()
class 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  unittest.TestCase   ):
				"""simple docstring"""
				__snake_case   :       Union[str, Any]	 =		LDMTextToImagePipeline
				__snake_case   :       Optional[Any]	 =		TEXT_TO_IMAGE_PARAMS - {
				    """negative_prompt""",
				    """negative_prompt_embeds""",
				    """cross_attention_kwargs""",
				    """prompt_embeds""",
				}
				__snake_case   :       str	 =		PipelineTesterMixin.required_optional_params - {
				    """num_images_per_prompt""",
				    """callback""",
				    """callback_steps""",
				}
				__snake_case   :       Union[str, Any]	 =		TEXT_TO_IMAGE_BATCH_PARAMS
				__snake_case   :       Optional[Any]	 =		False




				def 					__lowercase  (     self  :List[str]			):
											torch.manual_seed(0			)
											__lowerCamelCase      :      str         =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						,    )
											__lowerCamelCase      :      str         =DDIMScheduler(
											    beta_start=0.00085						,    beta_end=0.012						,    beta_schedule='''scaled_linear'''						,    clip_sample=__lowercase						,    set_alpha_to_one=__lowercase						,    )
											torch.manual_seed(0			)
											__lowerCamelCase      :      Optional[int]         =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			)
											__lowerCamelCase      :      Any         =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						,    )
											__lowerCamelCase      :      Optional[int]         =CLIPTextModel(__lowercase			)
											__lowerCamelCase      :      Dict         =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''			)

											__lowerCamelCase      :      Optional[int]         ={
											    '''unet''': unet,
											    '''scheduler''': scheduler,
											    '''vqvae''': vae,
											    '''bert''': text_encoder,
											    '''tokenizer''': tokenizer,
											}
											return components




				def 					__lowercase  (     self  :int						,    __lowercase  :Optional[int]						,    __lowercase  :Optional[Any]=0			):
											if str(__lowercase			).startswith('''mps'''			):
																		__lowerCamelCase      :      Any         =torch.manual_seed(__lowercase			)
											else:
																		__lowerCamelCase      :      str         =torch.Generator(device=__lowercase			).manual_seed(__lowercase			)
											__lowerCamelCase      :      Any         ={
											    '''prompt''': '''A painting of a squirrel eating a burger''',
											    '''generator''': generator,
											    '''num_inference_steps''': 2,
											    '''guidance_scale''': 6.0,
											    '''output_type''': '''numpy''',
											}
											return inputs




				def 					__lowercase  (     self  :List[str]			):
											__lowerCamelCase      :      List[str]         ='''cpu'''  # ensure determinism for the device-dependent torch.Generator

											__lowerCamelCase      :      str         =self.get_dummy_components()
											__lowerCamelCase      :      Optional[int]         =LDMTextToImagePipeline(**__lowercase			)
											pipe.to(__lowercase			)
											pipe.set_progress_bar_config(disable=__lowercase			)

											__lowerCamelCase      :      str         =self.get_dummy_inputs(__lowercase			)
											__lowerCamelCase      :      List[Any]         =pipe(**__lowercase			).images
											__lowerCamelCase      :      Optional[int]         =image[0, -3:, -3:, -1]

											assert image.shape == (1, 16, 16, 3)
											__lowerCamelCase      :      Optional[Any]         =np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014]			)

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

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




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




				def 					__lowercase  (     self  :int						,    __lowercase  :Any						,    __lowercase  :Optional[int]=torch.floataa						,    __lowercase  :Dict=0			):
											__lowerCamelCase      :      List[str]         =torch.manual_seed(__lowercase			)
											__lowerCamelCase      :      List[str]         =np.random.RandomState(__lowercase			).standard_normal((1, 4, 32, 32)			)
											__lowerCamelCase      :      List[str]         =torch.from_numpy(__lowercase			).to(device=__lowercase						,    dtype=__lowercase			)
											__lowerCamelCase      :      Any         ={
											    '''prompt''': '''A painting of a squirrel eating a burger''',
											    '''latents''': latents,
											    '''generator''': generator,
											    '''num_inference_steps''': 3,
											    '''guidance_scale''': 6.0,
											    '''output_type''': '''numpy''',
											}
											return inputs




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      int         =LDMTextToImagePipeline.from_pretrained('''CompVis/ldm-text2im-large-256'''			).to(__lowercase			)
											pipe.set_progress_bar_config(disable=__lowercase			)

											__lowerCamelCase      :      Tuple         =self.get_inputs(__lowercase			)
											__lowerCamelCase      :      Optional[Any]         =pipe(**__lowercase			).images
											__lowerCamelCase      :      Union[str, Any]         =image[0, -3:, -3:, -1].flatten()

											assert image.shape == (1, 256, 256, 3)
											__lowerCamelCase      :      Union[str, Any]         =np.array([0.51825, 0.52850, 0.52543, 0.54258, 0.52304, 0.52569, 0.54363, 0.55276, 0.56878]			)
											__lowerCamelCase      :      Dict         =np.abs(expected_slice - image_slice			).max()
											assert max_diff < 1e-3

@nightly
@require_torch_gpu
class 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




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




				def 					__lowercase  (     self  :Dict						,    __lowercase  :Optional[Any]						,    __lowercase  :int=torch.floataa						,    __lowercase  :Dict=0			):
											__lowerCamelCase      :      Any         =torch.manual_seed(__lowercase			)
											__lowerCamelCase      :      Dict         =np.random.RandomState(__lowercase			).standard_normal((1, 4, 32, 32)			)
											__lowerCamelCase      :      str         =torch.from_numpy(__lowercase			).to(device=__lowercase						,    dtype=__lowercase			)
											__lowerCamelCase      :      Dict         ={
											    '''prompt''': '''A painting of a squirrel eating a burger''',
											    '''latents''': latents,
											    '''generator''': generator,
											    '''num_inference_steps''': 50,
											    '''guidance_scale''': 6.0,
											    '''output_type''': '''numpy''',
											}
											return inputs




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      Optional[int]         =LDMTextToImagePipeline.from_pretrained('''CompVis/ldm-text2im-large-256'''			).to(__lowercase			)
											pipe.set_progress_bar_config(disable=__lowercase			)

											__lowerCamelCase      :      List[Any]         =self.get_inputs(__lowercase			)
											__lowerCamelCase      :      Optional[int]         =pipe(**__lowercase			).images[0]

											__lowerCamelCase      :      Optional[int]         =load_numpy(
											    '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy'''			)
											__lowerCamelCase      :      Dict         =np.abs(expected_image - image			).max()
											assert max_diff < 1e-3




