


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     UpperCAmelCase( self   :  List[str]   ,				lowerCamelCase_   :  bool = False     ):
            a_     :  List[Any]				      =     1_0

            if only_cross_attention:
                 a_     :  Any				      =     1_2
            else:
                 # when only cross attention is not set, the cross attention dim must be the same as the query dim
                 a_     :  Union[str, Any]				      =     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     UpperCAmelCase( self   :  Union[str, Any]   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  str     ):
            a_     :  str				      =     2

            a_     :  int				      =     torch.rand(lowerCamelCase_   ,				lowerCamelCase_   ,				3   ,				2     )
            a_     :  List[str]				      =     torch.rand(lowerCamelCase_   ,				4   ,				lowerCamelCase_     )
            a_     :  Optional[int]				      =     None

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

       def     UpperCAmelCase( self   :  List[Any]     ):
            # self and cross attention

            torch.manual_seed(0     )

            a_     :  List[str]				      =     self.get_constructor_arguments(only_cross_attention=lowerCamelCase_     )
            a_     :  List[Any]				      =     Attention(**lowerCamelCase_     )

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

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

            a_     :  Dict				      =     attn(**lowerCamelCase_     )

            # only self attention

            torch.manual_seed(0     )

            a_     :  Tuple				      =     self.get_constructor_arguments(only_cross_attention=lowerCamelCase_     )
            a_     :  int				      =     Attention(**lowerCamelCase_     )

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

            a_     :  List[str]				      =     self.get_forward_arguments(
                query_dim=constructor_args["""query_dim"""]   ,				added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]     )

            a_     :  Union[str, Any]				      =     attn(**lowerCamelCase_     )

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


class 	a__						(    unittest.TestCase     ):

       def     UpperCAmelCase( self   :  str     ):
            a_     :  List[str]				      =     DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""   ,				safety_checker=lowerCamelCase_     )

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

            # the initial conversion succeeds
            a_     :  Optional[int]				      =     DiffusionPipeline.from_pretrained(
                """hf-internal-testing/tiny-stable-diffusion-pipe"""   ,				device_map="""sequential"""   ,				safety_checker=lowerCamelCase_     )

            a_     :  List[str]				      =     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
                 a_     :  List[str]				      =     DiffusionPipeline.from_pretrained(lowerCamelCase_   ,				device_map="""sequential"""   ,				safety_checker=lowerCamelCase_     )

            a_     :  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'''







from ..utils import DummyObject, requires_backends


class      _a    (      metaclass=__a					):
       __a							:	str  							=   ["""torch""", """librosa"""]
       def __init__(      self		:      int  ,							*lowercase		:      Any  ,							**lowercase		:      Optional[int]							):



           '''simple docstring'''

           requires_backends(self  ,							['''torch''', '''librosa''']							)
       @classmethod
       def        A (      cls		:      Optional[int]  ,							*lowercase		:      List[str]  ,							**lowercase		:      str							):



           '''simple docstring'''

           requires_backends(cls  ,							['''torch''', '''librosa''']							)







       @classmethod
       def        A (      cls		:      Dict  ,							*lowercase		:      Optional[int]  ,							**lowercase		:      List[Any]							):



           '''simple docstring'''

           requires_backends(cls  ,							['''torch''', '''librosa''']							)






class      _a    (      metaclass=__a					):
       __a							:	List[Any]  							=   ["""torch""", """librosa"""]
       def __init__(      self		:      int  ,							*lowercase		:      Optional[int]  ,							**lowercase		:      Optional[int]							):



           '''simple docstring'''

           requires_backends(self  ,							['''torch''', '''librosa''']							)
       @classmethod
       def        A (      cls		:      Dict  ,							*lowercase		:      Tuple  ,							**lowercase		:      List[str]							):



           '''simple docstring'''

           requires_backends(cls  ,							['''torch''', '''librosa''']							)







       @classmethod
       def        A (      cls		:      Any  ,							*lowercase		:      Any  ,							**lowercase		:      Tuple							):



           '''simple docstring'''

           requires_backends(cls  ,							['''torch''', '''librosa''']							)




