




'''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 	lowerCAmelCase_  (					unittest.TestCase       ):







		'''simple docstring'''

		def 				_snake_case    (   self							:       Union[str, Any]				,     SCREAMING_SNAKE_CASE_							:       Dict = False  ) ->		Any:







							'''simple docstring'''



							A:		Any        =		10

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







							'''simple docstring'''



							A:		Tuple        =		2

							A:		Any        =		torch.rand(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_				,     3				,     2  )
							A:		int        =		torch.rand(SCREAMING_SNAKE_CASE_				,     4				,     SCREAMING_SNAKE_CASE_  )
							A:		str        =		None

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




		def 				_snake_case    (   self							:       Optional[Any]  ) ->		Any:







							'''simple docstring'''



							torch.manual_seed(0  )

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

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

							A:		int        =		self.get_forward_arguments(
							    query_dim=constructor_args['''query_dim''']				,     added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']  )

							A:		str        =		attn(**SCREAMING_SNAKE_CASE_  )

							# only self attention

							torch.manual_seed(0  )

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

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

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

							A:		Any        =		attn(**SCREAMING_SNAKE_CASE_  )

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






class 	lowerCAmelCase_  (					unittest.TestCase       ):







		'''simple docstring'''

		def 				_snake_case    (   self							:       Optional[Any]  ) ->		Union[str, Any]:







							'''simple docstring'''



							A:		str        =		DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe'''				,     safety_checker=SCREAMING_SNAKE_CASE_  )

							A:		List[str]        =		pipe(
							    '''foo'''				,     num_inference_steps=2				,     generator=torch.Generator('''cpu'''  ).manual_seed(0  )				,     output_type='''np'''				,     ).images

							# the initial conversion succeeds
							A:		Optional[Any]        =		DiffusionPipeline.from_pretrained(
							    '''hf-internal-testing/tiny-stable-diffusion-pipe'''				,     device_map='''sequential'''				,     safety_checker=SCREAMING_SNAKE_CASE_  )

							A:		Dict        =		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(SCREAMING_SNAKE_CASE_  )

												# can also load the converted weights
												A:		Tuple        =		DiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_				,     device_map='''sequential'''				,     safety_checker=SCREAMING_SNAKE_CASE_  )

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

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












'''simple docstring'''


def 				SCREAMING_SNAKE_CASE(      __lowercase  ) ->   int:
     A:		str        =		str(__lowercase  ).strip()
     if not bin_string:
          raise ValueError('''Empty string was passed to the function'''  )
     A:		int        =		bin_string[0] == '''-'''
     if is_negative:
          A:		Optional[Any]        =		bin_string[1:]
     if not all(char in '''01''' for char in bin_string  ):
          raise ValueError('''Non-binary value was passed to the function'''  )
     A:		Any        =		0
     for char in bin_string:
          A:		Dict        =		2 * decimal_number + int(__lowercase  )
     return -decimal_number if is_negative else decimal_number


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

     testmod()






