





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:	Optional[int],				lowerCamelCase_:	bool = False ):
			lowercase__				:			Tuple            =		10

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

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

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





	def       snake_case__(	self:	Tuple ):
			# self and cross attention

			torch.manual_seed(0 )

			lowercase__				:			Optional[Any]            =		self.get_constructor_arguments(only_cross_attention=lowerCamelCase_ )
			lowercase__				:			str            =		Attention(**lowerCamelCase_ )

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

			lowercase__				:			Tuple            =		self.get_forward_arguments(
			    query_dim=constructor_args['query_dim'],				added_kv_proj_dim=constructor_args['added_kv_proj_dim'] )

			lowercase__				:			Any            =		attn(**lowerCamelCase_ )

			# only self attention

			torch.manual_seed(0 )

			lowercase__				:			int            =		self.get_constructor_arguments(only_cross_attention=lowerCamelCase_ )
			lowercase__				:			Optional[int]            =		Attention(**lowerCamelCase_ )

			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__				:			int            =		attn(**lowerCamelCase_ )

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


class 			__lowerCAmelCase				(					unittest.TestCase			):


	'''simple docstring'''





	def       snake_case__(	self:	List[str] ):
			lowercase__				:			Tuple            =		DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe',				safety_checker=lowerCamelCase_ )

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

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

			lowercase__				:			int            =		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
					lowercase__				:			Dict            =		DiffusionPipeline.from_pretrained(lowerCamelCase_,				device_map='sequential',				safety_checker=lowerCamelCase_ )

			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(lowerCamelCase_,				lowerCamelCase_,				atol=1E-5 ) )
			self.assertTrue(np.allclose(lowerCamelCase_,				lowerCamelCase_,				atol=1E-5 ) )






def 			SCREAMING_SNAKE_CASE__     (					_lowercase    :   int )      ->       bool:




		'''simple docstring'''
		lowercase__				:			Optional[int]            =		9

		while num > 0:
				if num % 10 != digit:
						return False
				num //= 100
				digit -= 1

		return True


def 			SCREAMING_SNAKE_CASE__     (					)      ->       int:




		'''simple docstring'''
		lowercase__				:			List[str]            =		138_902_663

		while not is_square_form(num * num ):
				if num % 10 == 3:
						num -= 6  # (3 - 6) % 10 = 7
				else:
						num -= 4  # (7 - 4) % 10 = 3

		return num * 10


if __name__ == "__main__":
			print(F'{solution() = }')


