


"""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 			SCREAMING_SNAKE_CASE_  (   unittest.TestCase):




							'''simple docstring'''





							def        UpperCAmelCase (						self	,					lowerCamelCase__ = False)   ->	Dict:







								'''simple docstring'''




								snake_case__  :						str		=      10

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







								'''simple docstring'''




								snake_case__  :						Optional[int]		=      2

								snake_case__  :						Optional[Any]		=      torch.rand(lowerCamelCase__	,					lowerCamelCase__	,					3	,					2)
								snake_case__  :						List[Any]		=      torch.rand(lowerCamelCase__	,					4	,					lowerCamelCase__)
								snake_case__  :						Tuple		=      None

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







							def        UpperCAmelCase (						self)   ->	Optional[int]:







								'''simple docstring'''




								torch.manual_seed(0)

								snake_case__  :						int		=      self.get_constructor_arguments(only_cross_attention=lowerCamelCase__)
								snake_case__  :						List[Any]		=      Attention(**lowerCamelCase__)

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

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

								snake_case__  :						List[Any]		=      attn(**lowerCamelCase__)

								# only self attention

								torch.manual_seed(0)

								snake_case__  :						Any		=      self.get_constructor_arguments(only_cross_attention=lowerCamelCase__)
								snake_case__  :						List[Any]		=      Attention(**lowerCamelCase__)

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

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

								snake_case__  :						Optional[Any]		=      attn(**lowerCamelCase__)

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


class 			SCREAMING_SNAKE_CASE_  (   unittest.TestCase):




							'''simple docstring'''





							def        UpperCAmelCase (						self)   ->	List[Any]:







								'''simple docstring'''




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

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

								# the initial conversion succeeds
								snake_case__  :						List[str]		=      DiffusionPipeline.from_pretrained(
								    "hf-internal-testing/tiny-stable-diffusion-pipe"	,					device_map="sequential"	,					safety_checker=lowerCamelCase__)

								snake_case__  :						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
									snake_case__  :						Tuple		=      DiffusionPipeline.from_pretrained(lowerCamelCase__	,					device_map="sequential"	,					safety_checker=lowerCamelCase__)

								snake_case__  :						Tuple		=      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 __future__ import annotations

from enum import Enum, unique
from typing import TypeVar

# Create a generic variable that can be 'Enum', or any subclass.
lowercase						:   Any      						=							TypeVar('''T''', bound='''Enum''')

@unique
class 	SCREAMING_SNAKE_CASE_		(							lowerCamelCase_					):
 __snake_case						:  Union[str, Any]       =   8_0
 __snake_case						:  Optional[Any]       =   7_0
 __snake_case						:  List[str]       =   6_0
 __snake_case						:  str       =   5_0
 __snake_case						:  Tuple       =   4_0
 __snake_case						:  Union[str, Any]       =   3_0
 __snake_case						:  Dict       =   2_0
 __snake_case						:  str       =   1_0






@unique
class 	SCREAMING_SNAKE_CASE_		(							lowerCamelCase_					):
 __snake_case						:  Optional[Any]       =   2_4
 __snake_case						:  List[str]       =   2_1
 __snake_case						:  Any       =   1_8
 __snake_case						:  Union[str, Any]       =   1_5
 __snake_case						:  Dict       =   1_2
 __snake_case						:  Union[str, Any]       =   9
 __snake_case						:  List[str]       =   6
 __snake_case						:  Optional[int]       =   3
 __snake_case						:  Tuple       =   2
 __snake_case						:  Any       =   1
 __snake_case						:  Optional[int]       =   -1
 __snake_case						:  Optional[int]       =   -2
 __snake_case						:  str       =   -3
 __snake_case						:  Optional[Any]       =   -6
 __snake_case						:  Optional[int]       =   -9
 __snake_case						:  List[Any]       =   -1_2
 __snake_case						:  str       =   -1_5
 __snake_case						:  str       =   -1_8
 __snake_case						:  Dict       =   -2_1
 __snake_case						:  int       =   -2_4



 @classmethod
 def  SCREAMING_SNAKE_CASE(	cls			):






        '''simple docstring'''




        return {unit.name: unit.value for unit in cls if unit.value > 0}





 @classmethod
 def  SCREAMING_SNAKE_CASE(	cls			):






        '''simple docstring'''




        return {unit.name: unit.value for unit in cls if unit.value < 0}






def 		a_(			lowerCamelCase   )					->str:






       '''simple docstring'''




       lowercase	:		Tuple    =   SIUnit.get_positive() if value > 0 else SIUnit.get_negative()
       for name_prefix, value_prefix in prefixes.items():
              lowercase	:		Dict    =   value / (10**value_prefix)
              if numerical_part > 1:
                     return f"""{numerical_part!s} {name_prefix}"""
       return str(lowerCamelCase   )




def 		a_(			lowerCamelCase   )					->str:






       '''simple docstring'''




       for prefix in BinaryUnit:
              lowercase	:		Dict    =   value / (2**prefix.value)
              if numerical_part > 1:
                     return f"""{numerical_part!s} {prefix.name}"""
       return str(lowerCamelCase   )


if __name__ == "__main__":
      import doctest

      doctest.testmod()




