



'''simple docstring'''




import unittest

from diffusers.models.unet_ad_blocks import *  # noqa F403
from diffusers.utils import torch_device

from .test_unet_blocks_common import UNetBlockTesterMixin

class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				DownBlockaD  # noqa F405
	lowercase    =				'down'





	def 				_lowercase(						self					)    ->    int:
								UpperCAmelCase  :	Union[str, Any]							  =	[-0.0_2_3_2, -0.9_8_6_9, 0.8_0_5_4, -0.0_6_3_7, -0.1_6_8_8, -1.4_2_6_4, 0.4_4_7_0, -1.3_3_9_4, 0.0_9_0_4]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				ResnetDownsampleBlockaD  # noqa F405
	lowercase    =				'down'





	def 				_lowercase(						self					)    ->    Union[str, Any]:
								UpperCAmelCase  :	Optional[Any]							  =	[0.0_7_1_0, 0.2_4_1_0, -0.7_3_2_0, -1.0_7_5_7, -1.1_3_4_3, 0.3_5_4_0, -0.0_1_3_3, -0.2_5_7_6, 0.0_9_4_8]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				AttnDownBlockaD  # noqa F405
	lowercase    =				'down'





	def 				_lowercase(						self					)    ->    Tuple:
								UpperCAmelCase  :	Optional[int]							  =	[0.0_6_3_6, 0.8_9_6_4, -0.6_2_3_4, -1.0_1_3_1, 0.0_8_4_4, 0.4_9_3_5, 0.3_4_3_7, 0.0_9_1_1, -0.2_9_5_7]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				CrossAttnDownBlockaD  # noqa F405
	lowercase    =				'down'





	def 				_lowercase(						self					)    ->    List[Any]:
								UpperCAmelCase  :	Dict							  =	super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase  :	Optional[int]							  =	32
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    Optional[Any]:
								UpperCAmelCase  :	Any							  =	[0.2_2_3_8, -0.7_3_9_6, -0.2_2_5_5, -0.3_8_2_9, 0.1_9_2_5, 1.1_6_6_5, 0.0_6_0_3, -0.7_2_9_5, 0.1_9_8_3]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				SimpleCrossAttnDownBlockaD  # noqa F405
	lowercase    =				'down'





	@property
	def 				_lowercase(						self					)    ->    List[Any]:
								return super().get_dummy_input(include_encoder_hidden_states=A					)





	def 				_lowercase(						self					)    ->    Tuple:
								UpperCAmelCase  :	Dict							  =	super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase  :	int							  =	32
								return init_dict, inputs_dict






	@unittest.skipIf(torch_device == """mps"""	,	"""MPS result is not consistent"""					)
	def 				_lowercase(						self					)    ->    str:
								UpperCAmelCase  :	str							  =	[0.7_9_2_1, -0.0_9_9_2, -0.1_9_6_2, -0.7_6_9_5, -0.4_2_4_2, 0.7_8_0_4, 0.4_7_3_7, 0.2_7_6_5, 0.3_3_3_8]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				SkipDownBlockaD  # noqa F405
	lowercase    =				'down'





	@property
	def 				_lowercase(						self					)    ->    int:
								return super().get_dummy_input(include_skip_sample=A					)






	def 				_lowercase(						self					)    ->    int:
								UpperCAmelCase  :	List[str]							  =	[-0.0_8_4_5, -0.2_0_8_7, -0.2_4_6_5, 0.0_9_7_1, 0.1_9_0_0, -0.0_4_8_4, 0.2_6_6_4, 0.4_1_7_9, 0.5_0_6_9]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				AttnSkipDownBlockaD  # noqa F405
	lowercase    =				'down'





	@property
	def 				_lowercase(						self					)    ->    Any:
								return super().get_dummy_input(include_skip_sample=A					)






	def 				_lowercase(						self					)    ->    Union[str, Any]:
								UpperCAmelCase  :	int							  =	[0.5_5_3_9, 0.1_6_0_9, 0.4_9_2_4, 0.0_5_3_7, -0.1_9_9_5, 0.4_0_5_0, 0.0_9_7_9, -0.2_7_2_1, -0.0_6_4_2]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				DownEncoderBlockaD  # noqa F405
	lowercase    =				'down'





	@property
	def 				_lowercase(						self					)    ->    Union[str, Any]:
								return super().get_dummy_input(include_temb=A					)





	def 				_lowercase(						self					)    ->    Any:
								UpperCAmelCase  :	Tuple							  =	{
								    """in_channels""": 32,
								    """out_channels""": 32,
								}
								UpperCAmelCase  :	Tuple							  =	self.dummy_input
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    Dict:
								UpperCAmelCase  :	List[Any]							  =	[1.1_1_0_2, 0.5_3_0_2, 0.4_8_7_2, -0.0_0_2_3, -0.8_0_4_2, 0.0_4_8_3, -0.3_4_8_9, -0.5_6_3_2, 0.7_6_2_6]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				AttnDownEncoderBlockaD  # noqa F405
	lowercase    =				'down'





	@property
	def 				_lowercase(						self					)    ->    Optional[Any]:
								return super().get_dummy_input(include_temb=A					)





	def 				_lowercase(						self					)    ->    Tuple:
								UpperCAmelCase  :	Any							  =	{
								    """in_channels""": 32,
								    """out_channels""": 32,
								}
								UpperCAmelCase  :	Any							  =	self.dummy_input
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    List[str]:
								UpperCAmelCase  :	List[str]							  =	[0.8_9_6_6, -0.1_4_8_6, 0.8_5_6_8, 0.8_1_4_1, -0.9_0_4_6, -0.1_3_4_2, -0.0_9_7_2, -0.7_4_1_7, 0.1_5_3_8]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				UNetMidBlockaD  # noqa F405
	lowercase    =				'mid'





	def 				_lowercase(						self					)    ->    Optional[int]:
								UpperCAmelCase  :	Dict							  =	{
								    """in_channels""": 32,
								    """temb_channels""": 128,
								}
								UpperCAmelCase  :	Dict							  =	self.dummy_input
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    Any:
								UpperCAmelCase  :	Tuple							  =	[-0.1_0_6_2, 1.7_2_4_8, 0.3_4_9_4, 1.4_5_6_9, -0.0_9_1_0, -1.2_4_2_1, -0.9_9_8_4, 0.6_7_3_6, 1.0_0_2_8]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				UNetMidBlockaDCrossAttn  # noqa F405
	lowercase    =				'mid'





	def 				_lowercase(						self					)    ->    List[str]:
								UpperCAmelCase  :	List[str]							  =	super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase  :	Any							  =	32
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    str:
								UpperCAmelCase  :	int							  =	[0.0_1_8_7, 2.4_2_2_0, 0.4_4_8_4, 1.1_2_0_3, -0.6_1_2_1, -1.5_1_2_2, -0.8_2_7_0, 0.7_8_5_1, 1.8_3_3_5]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				UNetMidBlockaDSimpleCrossAttn  # noqa F405
	lowercase    =				'mid'





	@property
	def 				_lowercase(						self					)    ->    Any:
								return super().get_dummy_input(include_encoder_hidden_states=A					)





	def 				_lowercase(						self					)    ->    List[Any]:
								UpperCAmelCase  :	Optional[Any]							  =	super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase  :	Optional[int]							  =	32
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    Dict:
								UpperCAmelCase  :	List[str]							  =	[0.7_1_4_3, 1.9_9_7_4, 0.5_4_4_8, 1.3_9_7_7, 0.1_2_8_2, -1.1_2_3_7, -1.4_2_3_8, 0.5_5_3_0, 0.8_8_8_0]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				UpBlockaD  # noqa F405
	lowercase    =				'up'





	@property
	def 				_lowercase(						self					)    ->    str:
								return super().get_dummy_input(include_res_hidden_states_tuple=A					)






	def 				_lowercase(						self					)    ->    Union[str, Any]:
								UpperCAmelCase  :	Optional[int]							  =	[-0.2_0_4_1, -0.4_1_6_5, -0.3_0_2_2, 0.0_0_4_1, -0.6_6_2_8, -0.7_0_5_3, 0.1_9_2_8, -0.0_3_2_5, 0.0_5_2_3]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				ResnetUpsampleBlockaD  # noqa F405
	lowercase    =				'up'





	@property
	def 				_lowercase(						self					)    ->    Optional[int]:
								return super().get_dummy_input(include_res_hidden_states_tuple=A					)






	def 				_lowercase(						self					)    ->    Dict:
								UpperCAmelCase  :	Dict							  =	[0.2_2_8_7, 0.3_5_4_9, -0.1_3_4_6, 0.4_7_9_7, -0.1_7_1_5, -0.9_6_4_9, 0.7_3_0_5, -0.5_8_6_4, -0.6_2_4_4]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				CrossAttnUpBlockaD  # noqa F405
	lowercase    =				'up'





	@property
	def 				_lowercase(						self					)    ->    List[str]:
								return super().get_dummy_input(include_res_hidden_states_tuple=A					)





	def 				_lowercase(						self					)    ->    Tuple:
								UpperCAmelCase  :	Any							  =	super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase  :	str							  =	32
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    Optional[int]:
								UpperCAmelCase  :	Dict							  =	[-0.1_4_0_3, -0.3_5_1_5, -0.0_4_2_0, -0.1_4_2_5, 0.3_1_6_7, 0.5_0_9_4, -0.2_1_8_1, 0.5_9_3_1, 0.5_5_8_2]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				SimpleCrossAttnUpBlockaD  # noqa F405
	lowercase    =				'up'





	@property
	def 				_lowercase(						self					)    ->    List[str]:
								return super().get_dummy_input(include_res_hidden_states_tuple=A	,	include_encoder_hidden_states=A					)





	def 				_lowercase(						self					)    ->    Tuple:
								UpperCAmelCase  :	Optional[int]							  =	super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase  :	Tuple							  =	32
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    Optional[Any]:
								UpperCAmelCase  :	Tuple							  =	[0.2_6_4_5, 0.1_4_8_0, 0.0_9_0_9, 0.8_0_4_4, -0.9_7_5_8, -0.9_0_8_3, 0.0_9_9_4, -1.1_4_5_3, -0.7_4_0_2]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				AttnUpBlockaD  # noqa F405
	lowercase    =				'up'





	@property
	def 				_lowercase(						self					)    ->    List[str]:
								return super().get_dummy_input(include_res_hidden_states_tuple=A					)






	@unittest.skipIf(torch_device == """mps"""	,	"""MPS result is not consistent"""					)
	def 				_lowercase(						self					)    ->    Any:
								UpperCAmelCase  :	Union[str, Any]							  =	[0.0_9_7_9, 0.1_3_2_6, 0.0_0_2_1, 0.0_6_5_9, 0.2_2_4_9, 0.0_0_5_9, 0.1_1_3_2, 0.5_9_5_2, 0.1_0_3_3]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				SkipUpBlockaD  # noqa F405
	lowercase    =				'up'





	@property
	def 				_lowercase(						self					)    ->    int:
								return super().get_dummy_input(include_res_hidden_states_tuple=A					)






	def 				_lowercase(						self					)    ->    Optional[int]:
								UpperCAmelCase  :	int							  =	[-0.0_8_9_3, -0.1_2_3_4, -0.1_5_0_6, -0.0_3_3_2, 0.0_1_2_3, -0.0_2_1_1, 0.0_5_6_6, 0.0_1_4_3, 0.0_3_6_2]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				AttnSkipUpBlockaD  # noqa F405
	lowercase    =				'up'





	@property
	def 				_lowercase(						self					)    ->    Dict:
								return super().get_dummy_input(include_res_hidden_states_tuple=A					)






	def 				_lowercase(						self					)    ->    List[Any]:
								UpperCAmelCase  :	List[Any]							  =	[0.0_3_6_1, 0.0_6_1_7, 0.2_7_8_7, -0.0_3_5_0, 0.0_3_4_2, 0.3_4_2_1, -0.0_8_4_3, 0.0_9_1_3, 0.3_0_1_5]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				UpDecoderBlockaD  # noqa F405
	lowercase    =				'up'





	@property
	def 				_lowercase(						self					)    ->    Union[str, Any]:
								return super().get_dummy_input(include_temb=A					)





	def 				_lowercase(						self					)    ->    List[Any]:
								UpperCAmelCase  :	Union[str, Any]							  =	{"""in_channels""": 32, """out_channels""": 32}

								UpperCAmelCase  :	Tuple							  =	self.dummy_input
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    Tuple:
								UpperCAmelCase  :	Union[str, Any]							  =	[0.4_4_0_4, 0.1_9_9_8, -0.9_8_8_6, -0.3_3_2_0, -0.3_1_2_8, -0.7_0_3_4, -0.6_9_5_5, -0.2_3_3_8, -0.3_1_3_7]
								super().test_output(A					)




class     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	lowercase    =				AttnUpDecoderBlockaD  # noqa F405
	lowercase    =				'up'





	@property
	def 				_lowercase(						self					)    ->    Dict:
								return super().get_dummy_input(include_temb=A					)





	def 				_lowercase(						self					)    ->    str:
								UpperCAmelCase  :	str							  =	{"""in_channels""": 32, """out_channels""": 32}

								UpperCAmelCase  :	Union[str, Any]							  =	self.dummy_input
								return init_dict, inputs_dict






	def 				_lowercase(						self					)    ->    int:
								UpperCAmelCase  :	str							  =	[0.6_7_3_8, 0.4_4_9_1, 0.1_0_5_5, 1.0_7_1_0, 0.7_3_1_6, 0.3_3_3_9, 0.3_3_5_2, 0.1_0_2_3, 0.3_5_6_8]
								super().test_output(A					)






'''simple docstring'''




import math
import random
from typing import Any

from .hill_climbing import SearchProblem



def    __lowerCamelCase						(      _lowercase			,       _lowercase = True			,       _lowercase = math.inf			,       _lowercase = -math.inf			,       _lowercase = math.inf			,       _lowercase = -math.inf			,       _lowercase = False			,       _lowercase = 1_0_0			,       _lowercase = 0.01			,       _lowercase = 1			,       )   ->    Any:
							UpperCAmelCase  :	Optional[int]							  =	False
							UpperCAmelCase  :	Any							  =	search_prob
							UpperCAmelCase  :	Any							  =	start_temperate
							UpperCAmelCase  :	Optional[Any]							  =	[]
							UpperCAmelCase  :	Optional[Any]							  =	0
							UpperCAmelCase  :	Optional[Any]							  =	None

							while not search_end:
														UpperCAmelCase  :	List[str]							  =	current_state.score()
														if best_state is None or current_score > best_state.score():
																					UpperCAmelCase  :	List[Any]							  =	current_state
														scores.append(_lowercase	)
														iterations += 1
														UpperCAmelCase  :	Dict							  =	None
														UpperCAmelCase  :	Union[str, Any]							  =	current_state.get_neighbors()
														while (
														    next_state is None and neighbors
														):  # till we do not find a neighbor that we can move to
																					UpperCAmelCase  :	int							  =	random.randint(0			,       len(_lowercase	) - 1	)  # picking a random neighbor
																					UpperCAmelCase  :	int							  =	neighbors.pop(_lowercase	)
																					UpperCAmelCase  :	Tuple							  =	picked_neighbor.score() - current_score

																					if (
																					    picked_neighbor.x > max_x
																					    or picked_neighbor.x < min_x
																					    or picked_neighbor.y > max_y
																					    or picked_neighbor.y < min_y
																					):
																												continue  # neighbor outside our bounds

																					if not find_max:
																												UpperCAmelCase  :	Union[str, Any]							  =	change * -1  # in case we are finding minimum
																					if change > 0:  # improves the solution
																												UpperCAmelCase  :	int							  =	picked_neighbor
																					else:
																												UpperCAmelCase  :	Optional[Any]							  =	(math.e) ** (
																												    change / current_temp
																												)  # probability generation function
																												if random.random() < probability:  # random number within probability
																																			UpperCAmelCase  :	Optional[int]							  =	picked_neighbor
														UpperCAmelCase  :	List[Any]							  =	current_temp - (current_temp * rate_of_decrease)

														if current_temp < threshold_temp or next_state is None:
																					# temperature below threshold, or could not find a suitable neighbor
																					UpperCAmelCase  :	Optional[int]							  =	True
														else:
																					UpperCAmelCase  :	Optional[int]							  =	next_state

							if visualization:
														from matplotlib import pyplot as plt

														plt.plot(range(_lowercase	)			,       _lowercase	)
														plt.xlabel("""Iterations"""	)
														plt.ylabel("""Function values"""	)
														plt.show()
							return best_state


if __name__ == "__main__":



							def    __lowerCamelCase						(      _lowercase			,       _lowercase	)   ->    str:
														return (x**2) + (y**2)

							# starting the problem with initial coordinates (12, 47)
							a     :    Dict      						=					SearchProblem(x=1_2, y=4_7, step_size=1, function_to_optimize=test_fa)
							a     :    Dict      						=					simulated_annealing(
							    prob, find_max=False, max_x=1_0_0, min_x=5, max_y=5_0, min_y=-5, visualization=True
							)
							print(
							    """The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 """
							    F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}'''
							)

							# starting the problem with initial coordinates (12, 47)
							a     :    List[str]      						=					SearchProblem(x=1_2, y=4_7, step_size=1, function_to_optimize=test_fa)
							a     :    Dict      						=					simulated_annealing(
							    prob, find_max=True, max_x=1_0_0, min_x=5, max_y=5_0, min_y=-5, visualization=True
							)
							print(
							    """The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 """
							    F'''and 50 > y > - 5 found via hill climbing: {local_min.score()}'''
							)



							def    __lowerCamelCase						(      _lowercase			,       _lowercase	)   ->    Optional[Any]:
														return (3 * x**2) - (6 * y)

							a     :    Tuple      						=					SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
							a     :    Any      						=					simulated_annealing(prob, find_max=False, visualization=True)
							print(
							    """The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: """
							    F'''{local_min.score()}'''
							)

							a     :    List[Any]      						=					SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
							a     :    Dict      						=					simulated_annealing(prob, find_max=True, visualization=True)


							print(
							    """The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: """
							    F'''{local_min.score()}'''
							)

