


'''simple docstring'''



import argparse

import gdown
import numpy as np
import torch
from huggingface_hub import hf_hub_download

from transformers import (
    CLIPTokenizer,
    CLIPTokenizerFast,
    VideoMAEImageProcessor,
    XCLIPConfig,
    XCLIPModel,
    XCLIPProcessor,
    XCLIPTextConfig,
    XCLIPVisionConfig,
)




def      _a     (						_lowercase :     int      , _lowercase :     str  ):





     '''simple docstring'''






     __UpperCAmelCase     :			Optional[Any]  							=      XCLIPTextConfig()

     # derive patch size from model name
     __UpperCAmelCase     :			str  							=      model_name.find('''patch'''  )
     __UpperCAmelCase     :			Dict  							=      int(model_name[start_idx + len('''patch'''  ) : start_idx + len('''patch'''  ) + 2]  )
     __UpperCAmelCase     :			Optional[int]  							=      XCLIPVisionConfig(patch_size=_lowercase      , num_frames=_lowercase  )

     if "large" in model_name:
          __UpperCAmelCase     :			Union[str, Any]  							=      768
          __UpperCAmelCase     :			Optional[Any]  							=      3072
          __UpperCAmelCase     :			Any  							=      12

          __UpperCAmelCase     :			List[Any]  							=      1024
          __UpperCAmelCase     :			int  							=      4096
          __UpperCAmelCase     :			str  							=      16
          __UpperCAmelCase     :			List[str]  							=      24
          __UpperCAmelCase     :			int  							=      768
          __UpperCAmelCase     :			List[Any]  							=      3072

     if model_name == "xclip-large-patch14-16-frames":
          __UpperCAmelCase     :			Optional[Any]  							=      336

     __UpperCAmelCase     :			Any  							=      XCLIPConfig.from_text_vision_configs(_lowercase      , _lowercase  )

     if "large" in model_name:
          __UpperCAmelCase     :			List[Any]  							=      768

     return config




def      _a     (						_lowercase :     Optional[Any]  ):





     '''simple docstring'''






     if name == "token_embedding.weight":
          __UpperCAmelCase     :			Any  							=      name.replace('''token_embedding.weight'''      , '''text_model.embeddings.token_embedding.weight'''  )
     if name == "positional_embedding":
          __UpperCAmelCase     :			Optional[int]  							=      name.replace('''positional_embedding'''      , '''text_model.embeddings.position_embedding.weight'''  )
     if "ln_1" in name:
          __UpperCAmelCase     :			List[str]  							=      name.replace('''ln_1'''      , '''layer_norm1'''  )
     if "ln_2" in name:
          __UpperCAmelCase     :			str  							=      name.replace('''ln_2'''      , '''layer_norm2'''  )
     if "c_fc" in name:
          __UpperCAmelCase     :			Optional[int]  							=      name.replace('''c_fc'''      , '''fc1'''  )
     if "c_proj" in name:
          __UpperCAmelCase     :			Any  							=      name.replace('''c_proj'''      , '''fc2'''  )
     if name.startswith('''transformer.resblocks'''  ):
          __UpperCAmelCase     :			int  							=      name.replace('''transformer.resblocks'''      , '''text_model.encoder.layers'''  )
     if "attn.out_proj" in name and "message" not in name:
          __UpperCAmelCase     :			Optional[Any]  							=      name.replace('''attn.out_proj'''      , '''self_attn.out_proj'''  )
     if "ln_final" in name:
          __UpperCAmelCase     :			Dict  							=      name.replace('''ln_final'''      , '''text_model.final_layer_norm'''  )
     # visual encoder
     if name == "visual.class_embedding":
          __UpperCAmelCase     :			int  							=      name.replace('''visual.class_embedding'''      , '''vision_model.embeddings.class_embedding'''  )
     if name == "visual.positional_embedding":
          __UpperCAmelCase     :			Optional[Any]  							=      name.replace('''visual.positional_embedding'''      , '''vision_model.embeddings.position_embedding.weight'''  )
     if name.startswith('''visual.transformer.resblocks'''  ):
          __UpperCAmelCase     :			str  							=      name.replace('''visual.transformer.resblocks'''      , '''vision_model.encoder.layers'''  )
     if "visual.conv1" in name:
          __UpperCAmelCase     :			Optional[int]  							=      name.replace('''visual.conv1'''      , '''vision_model.embeddings.patch_embedding'''  )
     if "visual.ln_pre" in name:
          __UpperCAmelCase     :			Union[str, Any]  							=      name.replace('''visual.ln_pre'''      , '''vision_model.pre_layernorm'''  )
     if "visual.ln_post" in name:
          __UpperCAmelCase     :			Optional[Any]  							=      name.replace('''visual.ln_post'''      , '''vision_model.post_layernorm'''  )
     if "visual.proj" in name:
          __UpperCAmelCase     :			Optional[Any]  							=      name.replace('''visual.proj'''      , '''visual_projection.weight'''  )
     if "text_projection" in name:
          __UpperCAmelCase     :			Union[str, Any]  							=      name.replace('''text_projection'''      , '''text_projection.weight'''  )
     # things on top
     if "prompts_visual_proj" in name:
          __UpperCAmelCase     :			Dict  							=      name.replace('''prompts_visual_proj'''      , '''prompts_visual_projection'''  )
     if "prompts_visual_ln" in name:
          __UpperCAmelCase     :			str  							=      name.replace('''prompts_visual_ln'''      , '''prompts_visual_layernorm'''  )
     # mit
     if name == "mit.positional_embedding":
          __UpperCAmelCase     :			str  							=      name.replace('''positional'''      , '''position'''  )
     if name.startswith('''mit.resblocks'''  ):
          __UpperCAmelCase     :			str  							=      name.replace('''mit.resblocks'''      , '''mit.encoder.layers'''  )
     # prompts generator
     if name.startswith('''prompts_generator.norm'''  ):
          __UpperCAmelCase     :			str  							=      name.replace('''prompts_generator.norm'''      , '''prompts_generator.layernorm'''  )

     return name




def      _a     (						_lowercase :     Optional[Any]      , _lowercase :     str  ):





     '''simple docstring'''






     for key in orig_state_dict.copy().keys():
          __UpperCAmelCase     :			int  							=      orig_state_dict.pop(_lowercase  )

          if "attn.in_proj" in key:
               __UpperCAmelCase     :			Dict  							=      key.split('''.'''  )
               if key.startswith('''visual'''  ):
                    __UpperCAmelCase     :			Optional[Any]  							=      key_split[3]
                    __UpperCAmelCase     :			List[Any]  							=      config.vision_config.hidden_size
                    if "message_attn" in key:
                         if "weight" in key:
                              __UpperCAmelCase     :			Optional[int]  							=      val[
                                  :dim, :
                              ]
                              __UpperCAmelCase     :			Optional[int]  							=      val[
                                  dim : dim * 2, :
                              ]
                              __UpperCAmelCase     :			int  							=      val[
                                  -dim:, :
                              ]
                         else:
                              __UpperCAmelCase     :			Any  							=      val[
                                  :dim
                              ]
                              __UpperCAmelCase     :			Optional[Any]  							=      val[
                                  dim : dim * 2
                              ]
                              __UpperCAmelCase     :			Tuple  							=      val[
                                  -dim:
                              ]
                    else:
                         if "weight" in key:
                              __UpperCAmelCase     :			Optional[int]  							=      val[
                                  :dim, :
                              ]
                              __UpperCAmelCase     :			Optional[int]  							=      val[
                                  dim : dim * 2, :
                              ]
                              __UpperCAmelCase     :			List[Any]  							=      val[
                                  -dim:, :
                              ]
                         else:
                              __UpperCAmelCase     :			str  							=      val[:dim]
                              __UpperCAmelCase     :			str  							=      val[
                                  dim : dim * 2
                              ]
                              __UpperCAmelCase     :			Optional[int]  							=      val[-dim:]
               elif key.startswith('''mit'''  ):
                    __UpperCAmelCase     :			Union[str, Any]  							=      key_split[2]
                    __UpperCAmelCase     :			Optional[Any]  							=      config.vision_config.mit_hidden_size
                    if "weight" in key:
                         __UpperCAmelCase     :			Any  							=      val[:dim, :]
                         __UpperCAmelCase     :			Union[str, Any]  							=      val[dim : dim * 2, :]
                         __UpperCAmelCase     :			Dict  							=      val[-dim:, :]
                    else:
                         __UpperCAmelCase     :			Tuple  							=      val[:dim]
                         __UpperCAmelCase     :			Optional[int]  							=      val[dim : dim * 2]
                         __UpperCAmelCase     :			Optional[int]  							=      val[-dim:]
               else:
                    __UpperCAmelCase     :			Any  							=      key_split[2]
                    __UpperCAmelCase     :			List[str]  							=      config.text_config.hidden_size
                    if "weight" in key:
                         __UpperCAmelCase     :			List[str]  							=      val[:dim, :]
                         __UpperCAmelCase     :			List[str]  							=      val[
                             dim : dim * 2, :
                         ]
                         __UpperCAmelCase     :			str  							=      val[-dim:, :]
                    else:
                         __UpperCAmelCase     :			Tuple  							=      val[:dim]
                         __UpperCAmelCase     :			Any  							=      val[
                             dim : dim * 2
                         ]
                         __UpperCAmelCase     :			List[str]  							=      val[-dim:]
          else:
               __UpperCAmelCase     :			List[Any]  							=      rename_key(_lowercase  )
               if new_key_name in ["visual_projection.weight", "text_projection.weight"]:
                    __UpperCAmelCase     :			Optional[int]  							=      val.T
               __UpperCAmelCase     :			Tuple  							=      val

     return orig_state_dict




def      _a     (						_lowercase :     Optional[int]  ):





     '''simple docstring'''






     if num_frames == 8:
          __UpperCAmelCase     :			Dict  							=      '''eating_spaghetti_8_frames.npy'''
     elif num_frames == 16:
          __UpperCAmelCase     :			Optional[int]  							=      '''eating_spaghetti.npy'''
     elif num_frames == 32:
          __UpperCAmelCase     :			Tuple  							=      '''eating_spaghetti_32_frames.npy'''
     __UpperCAmelCase     :			List[str]  							=      hf_hub_download(
         repo_id='''hf-internal-testing/spaghetti-video'''      , filename=_lowercase      , repo_type='''dataset'''      , )
     __UpperCAmelCase     :			List[str]  							=      np.load(_lowercase  )
     return list(_lowercase  )




def      _a     (						_lowercase :     str      , _lowercase :     int=None      , _lowercase :     Optional[int]=False  ):





     '''simple docstring'''






     __UpperCAmelCase     :			Optional[int]  							=      {
         # fully supervised kinetics-400 checkpoints
         '''xclip-base-patch32''': '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth''',
         '''xclip-base-patch32-16-frames''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth'''
         ),
         '''xclip-base-patch16''': '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth''',
         '''xclip-base-patch16-16-frames''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth'''
         ),
         '''xclip-large-patch14''': '''https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb''',
         '''xclip-large-patch14-16-frames''': '''https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f''',
         # fully supervised kinetics-600 checkpoints
         '''xclip-base-patch16-kinetics-600''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth'''
         ),
         '''xclip-base-patch16-kinetics-600-16-frames''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth'''
         ),
         '''xclip-large-patch14-kinetics-600''': '''https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be''',
         # few shot
         '''xclip-base-patch16-hmdb-2-shot''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth'''
         ),
         '''xclip-base-patch16-hmdb-4-shot''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth'''
         ),
         '''xclip-base-patch16-hmdb-8-shot''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth'''
         ),
         '''xclip-base-patch16-hmdb-16-shot''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth'''
         ),
         '''xclip-base-patch16-ucf-2-shot''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth'''
         ),
         '''xclip-base-patch16-ucf-4-shot''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth'''
         ),
         '''xclip-base-patch16-ucf-8-shot''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth'''
         ),
         '''xclip-base-patch16-ucf-16-shot''': (
             '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth'''
         ),
         # zero shot
         '''xclip-base-patch16-zero-shot''': '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth''',
     }

     __UpperCAmelCase     :			str  							=      model_to_url[model_name]
     __UpperCAmelCase     :			Dict  							=      8
     if "16-frames" in model_name:
          __UpperCAmelCase     :			int  							=      16
     elif "shot" in model_name:
          __UpperCAmelCase     :			int  							=      32

     __UpperCAmelCase     :			int  							=      get_xclip_config(_lowercase      , _lowercase  )
     __UpperCAmelCase     :			Optional[int]  							=      XCLIPModel(_lowercase  )
     model.eval()

     if "drive" in checkpoint_url:
          __UpperCAmelCase     :			List[Any]  							=      '''pytorch_model.bin'''
          gdown.cached_download(_lowercase      , _lowercase      , quiet=_lowercase  )
          __UpperCAmelCase     :			int  							=      torch.load(_lowercase      , map_location='''cpu'''  )['''model''']
     else:
          __UpperCAmelCase     :			Any  							=      torch.hub.load_state_dict_from_url(_lowercase  )['''model''']

     __UpperCAmelCase     :			Any  							=      convert_state_dict(_lowercase      , _lowercase  )

     __UpperCAmelCase     :			Union[str, Any]  							=      XCLIPModel(_lowercase  )
     __UpperCAmelCase							,       __UpperCAmelCase     :			Optional[Any]  							=      model.load_state_dict(_lowercase      , strict=_lowercase  )
     assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"]
     model.eval()

     __UpperCAmelCase     :			List[str]  							=      336 if model_name == '''xclip-large-patch14-16-frames''' else 224
     __UpperCAmelCase     :			int  							=      VideoMAEImageProcessor(size=_lowercase  )
     __UpperCAmelCase     :			List[str]  							=      CLIPTokenizer.from_pretrained('''openai/clip-vit-base-patch32'''  )
     __UpperCAmelCase     :			Dict  							=      CLIPTokenizerFast.from_pretrained('''openai/clip-vit-base-patch32'''  )
     __UpperCAmelCase     :			int  							=      XCLIPProcessor(image_processor=_lowercase      , tokenizer=_lowercase  )

     __UpperCAmelCase     :			Union[str, Any]  							=      prepare_video(_lowercase  )
     __UpperCAmelCase     :			Optional[int]  							=      processor(
         text=['''playing sports''', '''eating spaghetti''', '''go shopping''']      , videos=_lowercase      , return_tensors='''pt'''      , padding=_lowercase  )

     print('''Shape of pixel values:'''      , inputs.pixel_values.shape  )

     with torch.no_grad():
          __UpperCAmelCase     :			str  							=      model(**_lowercase  )

     # Verify outputs
     __UpperCAmelCase     :			Union[str, Any]  							=      outputs.logits_per_video
     __UpperCAmelCase     :			List[str]  							=      logits_per_video.softmax(dim=1  )
     print('''Probs:'''      , _lowercase  )
     # kinetics-400
     if model_name == "xclip-base-patch32":
          __UpperCAmelCase     :			Tuple  							=      torch.tensor([[0.0019, 0.9951, 0.0030]]  )
     elif model_name == "xclip-base-patch32-16-frames":
          __UpperCAmelCase     :			List[Any]  							=      torch.tensor([[7.09_99E-04, 9.98_83E-01, 4.55_80E-04]]  )
     elif model_name == "xclip-base-patch16":
          __UpperCAmelCase     :			Any  							=      torch.tensor([[0.0083, 0.9681, 0.0236]]  )
     elif model_name == "xclip-base-patch16-16-frames":
          __UpperCAmelCase     :			List[str]  							=      torch.tensor([[7.69_37E-04, 9.97_28E-01, 1.94_73E-03]]  )
     elif model_name == "xclip-large-patch14":
          __UpperCAmelCase     :			Optional[Any]  							=      torch.tensor([[0.0062, 0.9864, 0.0075]]  )
     elif model_name == "xclip-large-patch14-16-frames":
          __UpperCAmelCase     :			Tuple  							=      torch.tensor([[3.38_77E-04, 9.99_37E-01, 2.88_88E-04]]  )
     # kinetics-600
     elif model_name == "xclip-base-patch16-kinetics-600":
          __UpperCAmelCase     :			List[Any]  							=      torch.tensor([[0.0555, 0.8914, 0.0531]]  )
     elif model_name == "xclip-base-patch16-kinetics-600-16-frames":
          __UpperCAmelCase     :			List[str]  							=      torch.tensor([[3.85_54E-04, 9.99_29E-01, 3.27_54E-04]]  )
     elif model_name == "xclip-large-patch14-kinetics-600":
          __UpperCAmelCase     :			Tuple  							=      torch.tensor([[0.0036, 0.9920, 0.0045]]  )
     # few shot
     elif model_name == "xclip-base-patch16-hmdb-2-shot":
          __UpperCAmelCase     :			int  							=      torch.tensor([[7.18_90E-06, 9.99_94E-01, 5.65_59E-05]]  )
     elif model_name == "xclip-base-patch16-hmdb-4-shot":
          __UpperCAmelCase     :			Optional[Any]  							=      torch.tensor([[1.03_20E-05, 9.99_93E-01, 6.24_35E-05]]  )
     elif model_name == "xclip-base-patch16-hmdb-8-shot":
          __UpperCAmelCase     :			Dict  							=      torch.tensor([[4.13_77E-06, 9.99_90E-01, 9.83_86E-05]]  )
     elif model_name == "xclip-base-patch16-hmdb-16-shot":
          __UpperCAmelCase     :			List[str]  							=      torch.tensor([[4.13_47E-05, 9.99_62E-01, 3.34_11E-04]]  )
     elif model_name == "xclip-base-patch16-ucf-2-shot":
          __UpperCAmelCase     :			str  							=      torch.tensor([[8.58_57E-05, 9.99_28E-01, 6.32_91E-04]]  )
     elif model_name == "xclip-base-patch16-ucf-4-shot":
          __UpperCAmelCase     :			Dict  							=      torch.tensor([[8.58_57E-05, 9.99_28E-01, 6.32_91E-04]]  )
     elif model_name == "xclip-base-patch16-ucf-8-shot":
          __UpperCAmelCase     :			str  							=      torch.tensor([[0.0027, 0.9904, 0.0070]]  )
     elif model_name == "xclip-base-patch16-ucf-16-shot":
          __UpperCAmelCase     :			Optional[Any]  							=      torch.tensor([[9.82_19E-04, 9.95_93E-01, 3.08_63E-03]]  )
     # zero shot
     elif model_name == "xclip-base-patch16-zero-shot":
          __UpperCAmelCase     :			List[str]  							=      torch.tensor([[3.50_82E-04, 9.97_85E-01, 1.79_66E-03]]  )
     else:
          raise ValueError(F'Model name {model_name} not supported'  )
     assert torch.allclose(_lowercase      , _lowercase      , atol=1E-3  )
     print('''Looks ok!'''  )

     if pytorch_dump_folder_path is not None:
          print(F'Saving model {model_name} to {pytorch_dump_folder_path}'  )
          model.save_pretrained(_lowercase  )

     if push_to_hub:
          print('''Pushing model, processor and slow tokenizer files to the hub...'''  )
          model.push_to_hub(_lowercase      , organization='''nielsr'''  )
          processor.push_to_hub(_lowercase      , organization='''nielsr'''  )
          slow_tokenizer.push_to_hub(_lowercase      , organization='''nielsr'''  )


if __name__ == "__main__":
      __UpperCAmelCase      :List[str]			    =       argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          "--model_name",
          default="xclip-base-patch32",
          type=str,
          help="Name of the model.",
      )
      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 or not to push the converted model to the 🤗 hub."
      )

      __UpperCAmelCase      :str			    =       parser.parse_args()
      convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)



'''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      a    (		_a     ,  _a     ,  unittest.TestCase	):


     """simple docstring"""






     SCREAMING_SNAKE_CASE	:			Dict							       =       StableDiffusionXLImgaImgPipeline
     SCREAMING_SNAKE_CASE	:			Tuple							       =       TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"height", "width"}
     SCREAMING_SNAKE_CASE	:			Optional[Any]							       =       PipelineTesterMixin.required_optional_params - {"latents"}
     SCREAMING_SNAKE_CASE	:			int							       =       TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     SCREAMING_SNAKE_CASE	:			Union[str, Any]							       =       IMAGE_TO_IMAGE_IMAGE_PARAMS
     SCREAMING_SNAKE_CASE	:			Any							       =       IMAGE_TO_IMAGE_IMAGE_PARAMS

     def 			lowerCamelCase__			(    self					:		Any      )						->							Any:
          torch.manual_seed(0      )
          __UpperCAmelCase     :			Optional[Any]  							=      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=snake_case							,			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							,			)
          __UpperCAmelCase     :			int  							=      EulerDiscreteScheduler(
              beta_start=0.00_085							,			beta_end=0.012							,			steps_offset=1							,			beta_schedule='''scaled_linear'''							,			timestep_spacing='''leading'''							,			)
          torch.manual_seed(0      )
          __UpperCAmelCase     :			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							,			sample_size=128							,			)
          torch.manual_seed(0      )
          __UpperCAmelCase     :			int  							=      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=1000							,			hidden_act='''gelu'''							,			projection_dim=32							,			)
          __UpperCAmelCase     :			Tuple  							=      CLIPTextModel(snake_case      )
          __UpperCAmelCase     :			Optional[int]  							=      CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''							,			local_files_only=snake_case      )

          __UpperCAmelCase     :			Optional[Any]  							=      CLIPTextModelWithProjection(snake_case      )
          __UpperCAmelCase     :			Union[str, Any]  							=      CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''							,			local_files_only=snake_case      )

          __UpperCAmelCase     :			Tuple  							=      {
              '''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 			lowerCamelCase__			(    self					:		Dict							,			snake_case					:		Optional[int]							,			snake_case					:		List[str]=0      )						->							List[str]:
          __UpperCAmelCase     :			List[str]  							=      floats_tensor((1, 3, 32, 32)							,			rng=random.Random(snake_case      )      ).to(snake_case      )
          __UpperCAmelCase     :			Optional[Any]  							=      image / 2 + 0.5
          if str(snake_case      ).startswith('''mps'''      ):
               __UpperCAmelCase     :			List[str]  							=      torch.manual_seed(snake_case      )
          else:
               __UpperCAmelCase     :			Any  							=      torch.Generator(device=snake_case      ).manual_seed(snake_case      )
          __UpperCAmelCase     :			Optional[int]  							=      {
              '''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.75,
          }
          return inputs

     def 			lowerCamelCase__			(    self					:		Tuple      )						->							Optional[int]:
          __UpperCAmelCase     :			Any  							=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
          __UpperCAmelCase     :			Optional[Any]  							=      self.get_dummy_components()
          __UpperCAmelCase     :			Any  							=      StableDiffusionXLImgaImgPipeline(**snake_case      )
          __UpperCAmelCase     :			Union[str, Any]  							=      sd_pipe.to(snake_case      )
          sd_pipe.set_progress_bar_config(disable=snake_case      )

          __UpperCAmelCase     :			Optional[int]  							=      self.get_dummy_inputs(snake_case      )
          __UpperCAmelCase     :			int  							=      sd_pipe(**snake_case      ).images
          __UpperCAmelCase     :			List[str]  							=      image[0, -3:, -3:, -1]

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

          __UpperCAmelCase     :			Any  							=      np.array([0.4_656, 0.4_840, 0.4_439, 0.6_698, 0.5_574, 0.4_524, 0.5_799, 0.5_943, 0.5_165]      )

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

     def 			lowerCamelCase__			(    self					:		Any      )						->							int:
          super().test_attention_slicing_forward_pass(expected_max_diff=3E-3      )

     def 			lowerCamelCase__			(    self					:		Optional[Any]      )						->							Optional[int]:
          super().test_inference_batch_single_identical(expected_max_diff=3E-3      )

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






     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							int:
          __UpperCAmelCase     :			Union[str, Any]  							=      self.get_dummy_components()
          __UpperCAmelCase     :			List[Any]  							=      StableDiffusionXLImgaImgPipeline(**snake_case      )
          __UpperCAmelCase     :			Tuple  							=      sd_pipe.to(snake_case      )
          __UpperCAmelCase     :			Tuple  							=      sd_pipe.to(snake_case      )
          sd_pipe.set_progress_bar_config(disable=snake_case      )

          # forward without prompt embeds
          __UpperCAmelCase     :			Tuple  							=      self.get_dummy_inputs(snake_case      )
          __UpperCAmelCase     :			Optional[Any]  							=      3 * ['''this is a negative prompt''']
          __UpperCAmelCase     :			Optional[int]  							=      negative_prompt
          __UpperCAmelCase     :			Tuple  							=      3 * [inputs['''prompt''']]

          __UpperCAmelCase     :			Optional[Any]  							=      sd_pipe(**snake_case      )
          __UpperCAmelCase     :			int  							=      output.images[0, -3:, -3:, -1]

          # forward with prompt embeds
          __UpperCAmelCase     :			Union[str, Any]  							=      self.get_dummy_inputs(snake_case      )
          __UpperCAmelCase     :			Tuple  							=      3 * ['''this is a negative prompt''']
          __UpperCAmelCase     :			str  							=      3 * [inputs.pop('''prompt'''      )]

          (
              (
              __UpperCAmelCase
          )							,       (
              __UpperCAmelCase
          )							,       (
              __UpperCAmelCase
          )							,       (
              __UpperCAmelCase
          )							,       
          )     :			Any  							=      sd_pipe.encode_prompt(snake_case							,			negative_prompt=snake_case      )

          __UpperCAmelCase     :			Dict  							=      sd_pipe(
              **snake_case							,			prompt_embeds=snake_case							,			negative_prompt_embeds=snake_case							,			pooled_prompt_embeds=snake_case							,			negative_pooled_prompt_embeds=snake_case							,			)
          __UpperCAmelCase     :			List[str]  							=      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      a    (		unittest.TestCase	):


     """simple docstring"""






     def 			lowerCamelCase__			(    self					:		Union[str, Any]      )						->							Optional[Any]:
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()

     def 			lowerCamelCase__			(    self					:		int							,			snake_case					:		Any							,			snake_case					:		str="cpu"							,			snake_case					:		Tuple=torch.floataa							,			snake_case					:		List[str]=0      )						->							Tuple:
          __UpperCAmelCase     :			Optional[Any]  							=      torch.Generator(device=snake_case      ).manual_seed(snake_case      )
          __UpperCAmelCase     :			int  							=      np.random.RandomState(snake_case      ).standard_normal((1, 4, 64, 64)      )
          __UpperCAmelCase     :			str  							=      torch.from_numpy(snake_case      ).to(device=snake_case							,			dtype=snake_case      )
          __UpperCAmelCase     :			str  							=      {
              '''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 			lowerCamelCase__			(    self					:		Union[str, Any]      )						->							List[Any]:
          __UpperCAmelCase     :			str  							=      DiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-base'''      )
          pipe.to(snake_case      )
          pipe.set_progress_bar_config(disable=snake_case      )

          __UpperCAmelCase     :			str  							=      self.get_inputs(snake_case      )
          __UpperCAmelCase     :			Dict  							=      pipe(**snake_case      ).images
          __UpperCAmelCase     :			List[str]  							=      image[0, -3:, -3:, -1].flatten()

          assert image.shape == (1, 512, 512, 3)
          __UpperCAmelCase     :			Tuple  							=      np.array([0.49_493, 0.47_896, 0.40_798, 0.54_214, 0.53_212, 0.48_202, 0.47_656, 0.46_329, 0.48_506]      )
          assert np.abs(image_slice - expected_slice      ).max() < 7E-3