






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





   '''simple docstring'''






   def __lowercase    (   self			,				UpperCAmelCase_ = False)	-> int:




          '''simple docstring'''




          lowercase__:    Optional[int]        =      10

          if only_cross_attention:
                 lowercase__:    List[Any]        =      12
          else:
                 # when only cross attention is not set, the cross attention dim must be the same as the query dim
                 lowercase__:    str        =      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 __lowercase    (   self			,				UpperCAmelCase_			,				UpperCAmelCase_)	-> int:




          '''simple docstring'''




          lowercase__:    Optional[Any]        =      2

          lowercase__:    Tuple        =      torch.rand(UpperCAmelCase_			,				UpperCAmelCase_			,				3			,				2)
          lowercase__:    Any        =      torch.rand(UpperCAmelCase_			,				4			,				UpperCAmelCase_)
          lowercase__:    List[str]        =      None

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





   def __lowercase    (   self)	-> Any:




          '''simple docstring'''




          torch.manual_seed(0)

          lowercase__:    Tuple        =      self.get_constructor_arguments(only_cross_attention=UpperCAmelCase_)
          lowercase__:    Any        =      Attention(**UpperCAmelCase_)

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

          lowercase__:    Union[str, Any]        =      self.get_forward_arguments(
              query_dim=constructor_args["query_dim"]			,				added_kv_proj_dim=constructor_args["added_kv_proj_dim"])

          lowercase__:    Tuple        =      attn(**UpperCAmelCase_)

          # only self attention

          torch.manual_seed(0)

          lowercase__:    Tuple        =      self.get_constructor_arguments(only_cross_attention=UpperCAmelCase_)
          lowercase__:    str        =      Attention(**UpperCAmelCase_)

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

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

          lowercase__:    Optional[Any]        =      attn(**UpperCAmelCase_)

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







class 		_a       (						unittest.TestCase   ):





   '''simple docstring'''






   def __lowercase    (   self)	-> str:




          '''simple docstring'''




          lowercase__:    Union[str, Any]        =      DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-pipe"			,				safety_checker=UpperCAmelCase_)

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

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

          lowercase__:    Union[str, 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(UpperCAmelCase_)

                 # can also load the converted weights
                 lowercase__:    Any        =      DiffusionPipeline.from_pretrained(UpperCAmelCase_			,				device_map="sequential"			,				safety_checker=UpperCAmelCase_)

          lowercase__:    Union[str, Any]        =      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))














"""simple docstring"""





def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       assert isinstance(snake_case_						,       snake_case_		), F"""The input value of [set_a={set_a}] is not a set"""
       assert isinstance(snake_case_						,       snake_case_		), F"""The input value of [set_b={set_b}] is not a set"""

       return {a + b for a in set_a for b in set_b}


if __name__ == "__main__":
  from doctest import testmod

  testmod()






