



"""simple docstring"""
import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
class    a		(					unittest.TestCase    ):



  def 		lowerCamelCase__    (  self	:   List[Any]    ,						lowerCAmelCase	:   bool = False						)       ->   Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	List[Any]            =10

     if only_cross_attention:
        SCREAMING_SNAKE_CASE_:	Tuple            =12
     else:
        # when only cross attention is not set, the cross attention dim must be the same as the query dim
        SCREAMING_SNAKE_CASE_:	int            =query_dim

     return {
         "query_dim": query_dim,
         "cross_attention_dim": cross_attention_dim,
         "heads": 2,
         "dim_head": 4,
         "added_kv_proj_dim": 6,
         "norm_num_groups": 1,
         "only_cross_attention": only_cross_attention,
         "processor": AttnAddedKVProcessor(),
     }



  def 		lowerCamelCase__    (  self	:   Dict    ,						lowerCAmelCase	:   Optional[int]    ,						lowerCAmelCase	:   Dict						)       ->   List[str]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Tuple            =2

     SCREAMING_SNAKE_CASE_:	Tuple            =torch.rand(lowerCAmelCase    ,						lowerCAmelCase    ,						3    ,						2						)
     SCREAMING_SNAKE_CASE_:	Dict            =torch.rand(lowerCAmelCase    ,						4    ,						lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:	Union[str, Any]            =None

     return {
         "hidden_states": hidden_states,
         "encoder_hidden_states": encoder_hidden_states,
         "attention_mask": attention_mask,
     }





  def 		lowerCamelCase__    (  self	:   str						)       ->   Any:


     '''simple docstring'''




     torch.manual_seed(0						)

     SCREAMING_SNAKE_CASE_:	List[Any]            =self.get_constructor_arguments(only_cross_attention=lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:	Optional[int]            =Attention(**lowerCAmelCase						)

     self.assertTrue(attn.to_k is not None						)
     self.assertTrue(attn.to_v is not None						)

     SCREAMING_SNAKE_CASE_:	Tuple            =self.get_forward_arguments(
         query_dim=constructor_args["""query_dim"""]    ,						added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]						)

     SCREAMING_SNAKE_CASE_:	Optional[Any]            =attn(**lowerCAmelCase						)

     # only self attention

     torch.manual_seed(0						)

     SCREAMING_SNAKE_CASE_:	Tuple            =self.get_constructor_arguments(only_cross_attention=lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:	str            =Attention(**lowerCAmelCase						)

     self.assertTrue(attn.to_k is None						)
     self.assertTrue(attn.to_v is None						)

     SCREAMING_SNAKE_CASE_:	int            =self.get_forward_arguments(
         query_dim=constructor_args["""query_dim"""]    ,						added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]						)

     SCREAMING_SNAKE_CASE_:	Tuple            =attn(**lowerCAmelCase						)

     self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()						)







class    a		(					unittest.TestCase    ):



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	Tuple            =DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""    ,						safety_checker=lowerCAmelCase						)

     SCREAMING_SNAKE_CASE_:	Optional[Any]            =pipe(
         """foo"""    ,						num_inference_steps=2    ,						generator=torch.Generator("""cpu"""						).manual_seed(0						)    ,						output_type="""np"""    ,						).images

     # the initial conversion succeeds
     SCREAMING_SNAKE_CASE_:	str            =DiffusionPipeline.from_pretrained(
         """hf-internal-testing/tiny-stable-diffusion-pipe"""    ,						device_map="""sequential"""    ,						safety_checker=lowerCAmelCase						)

     SCREAMING_SNAKE_CASE_:	Optional[Any]            =pipe(
         """foo"""    ,						num_inference_steps=2    ,						generator=torch.Generator("""cpu"""						).manual_seed(0						)    ,						output_type="""np"""    ,						).images

     with tempfile.TemporaryDirectory() as tmpdir:
        # save the converted model
        pipe.save_pretrained(lowerCAmelCase						)

        # can also load the converted weights
        SCREAMING_SNAKE_CASE_:	Optional[Any]            =DiffusionPipeline.from_pretrained(lowerCAmelCase    ,						device_map="""sequential"""    ,						safety_checker=lowerCAmelCase						)

     SCREAMING_SNAKE_CASE_:	List[Any]            =pipe(
         """foo"""    ,						num_inference_steps=2    ,						generator=torch.Generator("""cpu"""						).manual_seed(0						)    ,						output_type="""np"""    ,						).images

     self.assertTrue(np.allclose(lowerCAmelCase    ,						lowerCAmelCase    ,						atol=1E-5						)						)
     self.assertTrue(np.allclose(lowerCAmelCase    ,						lowerCAmelCase    ,						atol=1E-5						)						)








"""simple docstring"""
import copy

from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging


_UpperCAmelCase       =     logging.get_logger(__name__)
class    a		(					UpperCAmelCase__    ):
  UpperCamelCase		:			int         =   'hybrid-clip'
  UpperCamelCase		:			Optional[int]         =   True



  def __init__(  self	:   List[Any]    ,						lowerCAmelCase	:   Any=512    ,						**lowerCAmelCase	:   Optional[Any]						)       ->   List[str]:


     '''simple docstring'''




     super().__init__(**lowerCAmelCase						)

     if "text_config" not in kwargs:
        raise ValueError("""`text_config` can not be `None`."""						)

     if "vision_config" not in kwargs:
        raise ValueError("""`vision_config` can not be `None`."""						)

     SCREAMING_SNAKE_CASE_:	Optional[int]            =kwargs.pop("""text_config"""						)
     SCREAMING_SNAKE_CASE_:	Optional[Any]            =kwargs.pop("""vision_config"""						)

     SCREAMING_SNAKE_CASE_:	int            =text_config.pop("""model_type"""						)
     SCREAMING_SNAKE_CASE_:	str            =vision_config.pop("""model_type"""						)

     from transformers import AutoConfig

     SCREAMING_SNAKE_CASE_:	Optional[int]            =AutoConfig.for_model(lowerCAmelCase    ,						**lowerCAmelCase						)

     if vision_model_type == "clip":
        SCREAMING_SNAKE_CASE_:	Tuple            =AutoConfig.for_model(lowerCAmelCase    ,						**lowerCAmelCase						).vision_config
     elif vision_model_type == "clip_vision_model":
        from transformers import CLIPVisionConfig

        SCREAMING_SNAKE_CASE_:	List[str]            =CLIPVisionConfig(**lowerCAmelCase						)
     else:
        SCREAMING_SNAKE_CASE_:	Optional[Any]            =AutoConfig.for_model(lowerCAmelCase    ,						**lowerCAmelCase						)

     SCREAMING_SNAKE_CASE_:	Any            =projection_dim
     SCREAMING_SNAKE_CASE_:	int            =1.0



  @classmethod
  def 		lowerCamelCase__    (  cls	:   List[Any]    ,						lowerCAmelCase	:   PretrainedConfig    ,						lowerCAmelCase	:   PretrainedConfig    ,						**lowerCAmelCase	:   str						)       ->   Optional[Any]:


     '''simple docstring'''




     return cls(text_config=text_config.to_dict()    ,						vision_config=vision_config.to_dict()    ,						**lowerCAmelCase						)





  def 		lowerCamelCase__    (  self	:   List[str]						)       ->   Tuple:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:	int            =copy.deepcopy(self.__dict__						)
     SCREAMING_SNAKE_CASE_:	List[str]            =self.text_config.to_dict()
     SCREAMING_SNAKE_CASE_:	Optional[int]            =self.vision_config.to_dict()
     SCREAMING_SNAKE_CASE_:	Union[str, Any]            =self.__class__.model_type
     return output



