




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     ):


     """simple docstring"""


     def   __A				(	self			: Optional[int]				,_UpperCamelCase			: bool = False						)		->   List[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=1_0

         if only_cross_attention:
             SCREAMING_SNAKE_CASE__								=1_2
         else:
             # when only cross attention is not set, the cross attention dim must be the same as the query dim
             SCREAMING_SNAKE_CASE__								=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   __A				(	self			: int				,_UpperCamelCase			: List[Any]				,_UpperCamelCase			: Tuple						)		->   List[str]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=2

         SCREAMING_SNAKE_CASE__								=torch.rand(_UpperCamelCase				,_UpperCamelCase				,3				,2						)
         SCREAMING_SNAKE_CASE__								=torch.rand(_UpperCamelCase				,4				,_UpperCamelCase						)
         SCREAMING_SNAKE_CASE__								=None

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




     def   __A				(	self			: int						)		->   Dict:


         '''simple docstring'''





         torch.manual_seed(0						)

         SCREAMING_SNAKE_CASE__								=self.get_constructor_arguments(only_cross_attention=_UpperCamelCase						)
         SCREAMING_SNAKE_CASE__								=Attention(**_UpperCamelCase						)

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

         SCREAMING_SNAKE_CASE__								=self.get_forward_arguments(
             query_dim=constructor_args["""query_dim"""]				,added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]						)

         SCREAMING_SNAKE_CASE__								=attn(**_UpperCamelCase						)

         # only self attention

         torch.manual_seed(0						)

         SCREAMING_SNAKE_CASE__								=self.get_constructor_arguments(only_cross_attention=_UpperCamelCase						)
         SCREAMING_SNAKE_CASE__								=Attention(**_UpperCamelCase						)

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

         SCREAMING_SNAKE_CASE__								=self.get_forward_arguments(
             query_dim=constructor_args["""query_dim"""]				,added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]						)

         SCREAMING_SNAKE_CASE__								=attn(**_UpperCamelCase						)

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






class       __a					( unittest.TestCase     ):


     """simple docstring"""


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


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""				,safety_checker=_UpperCamelCase						)

         SCREAMING_SNAKE_CASE__								=pipe(
             """foo"""				,num_inference_steps=2				,generator=torch.Generator("""cpu"""						).manual_seed(0						)				,output_type="""np"""				,).images

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

         SCREAMING_SNAKE_CASE__								=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(_UpperCamelCase						)

             # can also load the converted weights
             SCREAMING_SNAKE_CASE__								=DiffusionPipeline.from_pretrained(_UpperCamelCase				,device_map="""sequential"""				,safety_checker=_UpperCamelCase						)

         SCREAMING_SNAKE_CASE__								=pipe(
             """foo"""				,num_inference_steps=2				,generator=torch.Generator("""cpu"""						).manual_seed(0						)				,output_type="""np"""				,).images

         self.assertTrue(np.allclose(_UpperCamelCase				,_UpperCamelCase				,atol=1e-5						)						)
         self.assertTrue(np.allclose(_UpperCamelCase				,_UpperCamelCase				,atol=1e-5						)						)












def 	UpperCAmelCase_    (	__UpperCamelCase	):
    if not isinstance(__UpperCamelCase,				__UpperCamelCase	):
        raise ValueError("""double_factorial() only accepts integral values"""	)
    if n < 0:
        raise ValueError("""double_factorial() not defined for negative values"""	)
    return 1 if n <= 1 else n * double_factorial(n - 2	)


if __name__ == "__main__":
    import doctest

    doctest.testmod()






