






'''simple docstring'''
import argparse

import requests
import torch
from PIL import Image

from transformers import CLIPProcessor, GroupViTConfig, GroupViTModel




def 		__SCREAMING_SNAKE_CASE					(    _UpperCamelCase							):


   """simple docstring"""




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

   return name




def 		__SCREAMING_SNAKE_CASE					(    _UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   for key in orig_state_dict.copy().keys():
      lowercase_	:    List[Any]           =     orig_state_dict.pop(_UpperCamelCase							)

      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
         lowercase_	:    Optional[int]           =     key.split("."							)
         lowercase_	:    int           =     int(key_split[2]							), int(key_split[4]							)
         lowercase_	:    Tuple           =     config.vision_config.hidden_size
         if "weight" in key:
            lowercase_	:    Union[str, Any]           =     val[:dim, :]
            lowercase_	:    Union[str, Any]           =     val[dim : dim * 2, :]
            lowercase_	:    Tuple           =     val[-dim:, :]
         else:
            lowercase_	:    int           =     val[:dim]
            lowercase_	:    Optional[int]           =     val[dim : dim * 2]
            lowercase_	:    List[Any]           =     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
         lowercase_	:    List[str]           =     key.split("."							)
         lowercase_	:    str           =     int(key_split[3]							)
         lowercase_	:    Union[str, Any]           =     config.text_config.hidden_size
         if "weight" in key:
            lowercase_	:    List[str]           =     val[:dim, :]
            lowercase_	:    Any           =     val[
                dim : dim * 2, :
            ]
            lowercase_	:    Dict           =     val[-dim:, :]
         else:
            lowercase_	:    Union[str, Any]           =     val[:dim]
            lowercase_	:    Dict           =     val[dim : dim * 2]
            lowercase_	:    int           =     val[-dim:]
      else:
         lowercase_	:    Optional[Any]           =     rename_key(_UpperCamelCase							)
         # 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
         ):
            lowercase_	:    int           =     val.squeeze_()
         else:
            lowercase_	:    Dict           =     val

   return orig_state_dict




def 		__SCREAMING_SNAKE_CASE					(    ):


   """simple docstring"""




   lowercase_	:    List[str]           =     "http://images.cocodataset.org/val2017/000000039769.jpg"
   lowercase_	:    int           =     Image.open(requests.get(_UpperCamelCase						,		stream=_UpperCamelCase							).raw							)
   return im




@torch.no_grad()
def 		__SCREAMING_SNAKE_CASE					(    _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase="groupvit-gcc-yfcc"						,		_UpperCamelCase=False							):


   """simple docstring"""




   lowercase_	:    Union[str, Any]           =     GroupViTConfig()
   lowercase_	:    Tuple           =     GroupViTModel(_UpperCamelCase							).eval()

   lowercase_	:    int           =     torch.load(_UpperCamelCase						,		map_location="cpu"							)["model"]
   lowercase_	:    Tuple           =     convert_state_dict(_UpperCamelCase						,		_UpperCamelCase							)
   lowercase_	:    Tuple           =     model.load_state_dict(_UpperCamelCase						,		strict=_UpperCamelCase							)
   assert missing_keys == ["text_model.embeddings.position_ids"]
   assert (unexpected_keys == ["multi_label_logit_scale"]) or (len(_UpperCamelCase							) == 0)

   # verify result
   lowercase_	:    Tuple           =     CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32"							)
   lowercase_	:    List[Any]           =     prepare_img()
   lowercase_	:    List[str]           =     processor(text=["a photo of a cat", "a photo of a dog"]						,		images=_UpperCamelCase						,		padding=_UpperCamelCase						,		return_tensors="pt"							)

   with torch.no_grad():
      lowercase_	:    int           =     model(**_UpperCamelCase							)

   if model_name == "groupvit-gcc-yfcc":
      lowercase_	:    List[Any]           =     torch.tensor([[13.3523, 6.3629]]							)
   elif model_name == "groupvit-gcc-redcaps":
      lowercase_	:    Optional[Any]           =     torch.tensor([[16.1873, 8.6230]]							)
   else:
      raise ValueError(F"""Model name {model_name} not supported."""							)
   assert torch.allclose(outputs.logits_per_image						,		_UpperCamelCase						,		atol=1e-3							)

   processor.save_pretrained(_UpperCamelCase							)
   model.save_pretrained(_UpperCamelCase							)
   print("Successfully saved processor and model to"						,		_UpperCamelCase							)

   if push_to_hub:
      print("Pushing to the hub..."							)
      processor.push_to_hub(_UpperCamelCase						,		organization="nielsr"							)
      model.push_to_hub(_UpperCamelCase						,		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 unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class        _UpperCAmelCase						(		snake_case				,					unittest.TestCase			):
    __lowerCamelCase:   Dict		       =    KandinskyVaaPriorPipeline
    __lowerCamelCase:   Optional[int]		       =    ['prompt']
    __lowerCamelCase:   Any		       =    ['prompt', 'negative_prompt']
    __lowerCamelCase:   List[Any]		       =    [
        'num_images_per_prompt',
        'generator',
        'num_inference_steps',
        'latents',
        'negative_prompt',
        'guidance_scale',
        'output_type',
        'return_dict',
    ]
    __lowerCamelCase:   List[Any]		       =    False




    @property
    def 				lowerCAmelCase__				(			self  :       Optional[Any]			):






       '''simple docstring'''






       return 3_2




    @property
    def 				lowerCAmelCase__				(			self  :       Any			):






       '''simple docstring'''






       return 3_2




    @property
    def 				lowerCAmelCase__				(			self  :       Any			):






       '''simple docstring'''






       return self.time_input_dim




    @property
    def 				lowerCAmelCase__				(			self  :       str			):






       '''simple docstring'''






       return self.time_input_dim * 4




    @property
    def 				lowerCAmelCase__				(			self  :       Union[str, Any]			):






       '''simple docstring'''






       return 1_0_0




    @property
    def 				lowerCAmelCase__				(			self  :       List[Any]			):






       '''simple docstring'''






       lowercase_	:    Any           =     CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"			)
       return tokenizer




    @property
    def 				lowerCAmelCase__				(			self  :       Optional[Any]			):






       '''simple docstring'''






       torch.manual_seed(0			)
       lowercase_	:    Tuple           =     CLIPTextConfig(
           bos_token_id=0  ,     eos_token_id=2  ,     hidden_size=self.text_embedder_hidden_size  ,     projection_dim=self.text_embedder_hidden_size  ,     intermediate_size=3_7  ,     layer_norm_eps=1e-05  ,     num_attention_heads=4  ,     num_hidden_layers=5  ,     pad_token_id=1  ,     vocab_size=1_0_0_0  ,     )
       return CLIPTextModelWithProjection(a			)




    @property
    def 				lowerCAmelCase__				(			self  :       Tuple			):






       '''simple docstring'''






       torch.manual_seed(0			)

       lowercase_	:    List[str]           =     {
           "num_attention_heads": 2,
           "attention_head_dim": 1_2,
           "embedding_dim": self.text_embedder_hidden_size,
           "num_layers": 1,
       }

       lowercase_	:    Union[str, Any]           =     PriorTransformer(**a			)
       # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
       lowercase_	:    List[Any]           =     nn.Parameter(torch.ones(model.clip_std.shape			)			)
       return model




    @property
    def 				lowerCAmelCase__				(			self  :       Tuple			):






       '''simple docstring'''






       torch.manual_seed(0			)
       lowercase_	:    Dict           =     CLIPVisionConfig(
           hidden_size=self.text_embedder_hidden_size  ,     image_size=2_2_4  ,     projection_dim=self.text_embedder_hidden_size  ,     intermediate_size=3_7  ,     num_attention_heads=4  ,     num_channels=3  ,     num_hidden_layers=5  ,     patch_size=1_4  ,     )

       lowercase_	:    Optional[Any]           =     CLIPVisionModelWithProjection(a			)
       return model




    @property
    def 				lowerCAmelCase__				(			self  :       List[str]			):






       '''simple docstring'''






       lowercase_	:    List[str]           =     CLIPImageProcessor(
           crop_size=2_2_4  ,     do_center_crop=a  ,     do_normalize=a  ,     do_resize=a  ,     image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073]  ,     image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711]  ,     resample=3  ,     size=2_2_4  ,     )

       return image_processor




    def 				lowerCAmelCase__				(			self  :       List[str]			):






       '''simple docstring'''






       lowercase_	:    Any           =     self.dummy_prior
       lowercase_	:    Optional[Any]           =     self.dummy_image_encoder
       lowercase_	:    List[Any]           =     self.dummy_text_encoder
       lowercase_	:    Any           =     self.dummy_tokenizer
       lowercase_	:    Optional[Any]           =     self.dummy_image_processor

       lowercase_	:    List[str]           =     UnCLIPScheduler(
           variance_type="fixed_small_log"  ,     prediction_type="sample"  ,     num_train_timesteps=1_0_0_0  ,     clip_sample=a  ,     clip_sample_range=10.0  ,     )

       lowercase_	:    List[Any]           =     {
           "prior": prior,
           "image_encoder": image_encoder,
           "text_encoder": text_encoder,
           "tokenizer": tokenizer,
           "scheduler": scheduler,
           "image_processor": image_processor,
       }

       return components




    def 				lowerCAmelCase__				(			self  :       Any  ,     a  :       Dict  ,     a  :       Dict=0			):






       '''simple docstring'''






       if str(a			).startswith("mps"			):
          lowercase_	:    int           =     torch.manual_seed(a			)
       else:
          lowercase_	:    Optional[Any]           =     torch.Generator(device=a			).manual_seed(a			)
       lowercase_	:    Any           =     {
           "prompt": "horse",
           "generator": generator,
           "guidance_scale": 4.0,
           "num_inference_steps": 2,
           "output_type": "np",
       }
       return inputs




    def 				lowerCAmelCase__				(			self  :       Union[str, Any]			):






       '''simple docstring'''






       lowercase_	:    str           =     "cpu"

       lowercase_	:    Any           =     self.get_dummy_components()

       lowercase_	:    int           =     self.pipeline_class(**a			)
       lowercase_	:    Any           =     pipe.to(a			)

       pipe.set_progress_bar_config(disable=a			)

       lowercase_	:    Any           =     pipe(**self.get_dummy_inputs(a			)			)
       lowercase_	:    List[Any]           =     output.image_embeds

       lowercase_	:    str           =     pipe(
           **self.get_dummy_inputs(a			)  ,     return_dict=a  ,     )[0]

       lowercase_	:    Any           =     image[0, -1_0:]
       lowercase_	:    Dict           =     image_from_tuple[0, -1_0:]

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

       lowercase_	:    int           =     np.array(
           [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156]			)

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




    @skip_mps
    def 				lowerCAmelCase__				(			self  :       Optional[Any]			):






       '''simple docstring'''






       lowercase_	:    int           =     torch_device == "cpu"
       lowercase_	:    Tuple           =     True
       lowercase_	:    str           =     False

       self._test_inference_batch_single_identical(
           test_max_difference=a  ,     relax_max_difference=a  ,     test_mean_pixel_difference=a  ,     )





    @skip_mps
    def 				lowerCAmelCase__				(			self  :       Union[str, Any]			):






       '''simple docstring'''






       lowercase_	:    Any           =     torch_device == "cpu"
       lowercase_	:    int           =     False

       self._test_attention_slicing_forward_pass(
           test_max_difference=a  ,     test_mean_pixel_difference=a  ,     )




