






'''simple docstring'''
import tempfile
import unittest

import torch

from diffusers import UNetaDConditionModel
from diffusers.training_utils import EMAModel
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps, torch_device


enable_full_determinism()


class    _snake_case   (				unittest.TestCase					):

					"""simple docstring"""





					_UpperCamelCase		 =      "hf-internal-testing/tiny-stable-diffusion-pipe"
					_UpperCamelCase		 =      1
					_UpperCamelCase		 =      77
					_UpperCamelCase		 =      32
					_UpperCamelCase		 =      4
					_UpperCamelCase		 =      _UpperCamelCase		 =      64
					_UpperCamelCase		 =      torch.manual_seed(0					)




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__=0.9_9_9_9     )      ->	List[str]:
										a_									=   UNetaDConditionModel.from_pretrained(self.model_id   , subfolder='unet'     )
										a_									=   unet.to(UpperCAmelCase__     )
										a_									=   EMAModel(unet.parameters()   , decay=UpperCAmelCase__   , model_cls=UpperCAmelCase__   , model_config=unet.config     )
										return unet, ema_unet




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										a_									=   torch.randn(
										    self.batch_size   , self.num_in_channels   , self.latent_height   , self.latent_width   , generator=self.generator     ).to(UpperCAmelCase__     )
										a_									=   torch.randint(0   , 1000   , size=(self.batch_size,)   , generator=self.generator     ).to(UpperCAmelCase__     )
										a_									=   torch.randn(
										    self.batch_size   , self.prompt_length   , self.text_encoder_hidden_dim   , generator=self.generator     ).to(UpperCAmelCase__     )
										return noisy_latents, timesteps, encoder_hidden_states




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__     )      ->	str:
										a_									=   {}
										for k, param in unet.state_dict().items():
															a_									=   torch.randn_like(UpperCAmelCase__     ) + (param * torch.randn_like(UpperCAmelCase__     ))
															updated_state_dict.update({k: updated_param}     )
										unet.load_state_dict(UpperCAmelCase__     )
										return unet




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Any:
										a_							,					a_									=   self.get_models()
										# Take the first (hypothetical) EMA step.
										ema_unet.step(unet.parameters()     )
										assert ema_unet.optimization_step == 1

										# Take two more.
										for _ in range(2     ):
															ema_unet.step(unet.parameters()     )
										assert ema_unet.optimization_step == 3




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										a_							,					a_									=   self.get_models()
										# Since the `unet` is not being updated (i.e., backprop'd)
										# there won't be any difference between the `params` of `unet`
										# and `ema_unet` even if we call `ema_unet.step(unet.parameters())`.
										ema_unet.step(unet.parameters()     )
										a_									=   list(unet.parameters()     )
										for s_param, param in zip(ema_unet.shadow_params   , UpperCAmelCase__     ):
															assert torch.allclose(UpperCAmelCase__   , UpperCAmelCase__     )

										# The above holds true even if we call `ema.step()` multiple times since
										# `unet` params are still not being updated.
										for _ in range(4     ):
															ema_unet.step(unet.parameters()     )
										for s_param, param in zip(ema_unet.shadow_params   , UpperCAmelCase__     ):
															assert torch.allclose(UpperCAmelCase__   , UpperCAmelCase__     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										a_							,					a_									=   self.get_models()
										# Here we simulate the parameter updates for `unet`. Since there might
										# be some parameters which are initialized to zero we take extra care to
										# initialize their values to something non-zero before the multiplication.
										a_									=   self.simulate_backprop(UpperCAmelCase__     )

										# Take the EMA step.
										ema_unet.step(unet_pseudo_updated_step_one.parameters()     )

										# Now the EMA'd parameters won't be equal to the original model parameters.
										a_									=   list(unet_pseudo_updated_step_one.parameters()     )
										for s_param, param in zip(ema_unet.shadow_params   , UpperCAmelCase__     ):
															assert ~torch.allclose(UpperCAmelCase__   , UpperCAmelCase__     )

										# Ensure this is the case when we take multiple EMA steps.
										for _ in range(4     ):
															ema_unet.step(unet.parameters()     )
										for s_param, param in zip(ema_unet.shadow_params   , UpperCAmelCase__     ):
															assert ~torch.allclose(UpperCAmelCase__   , UpperCAmelCase__     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										# If we call EMA step after a backpropagation consecutively for two times,
										# the shadow params from those two steps should be different.
										a_							,					a_									=   self.get_models()

										# First backprop + EMA
										a_									=   self.simulate_backprop(UpperCAmelCase__     )
										ema_unet.step(unet_step_one.parameters()     )
										a_									=   ema_unet.shadow_params

										# Second backprop + EMA
										a_									=   self.simulate_backprop(UpperCAmelCase__     )
										ema_unet.step(unet_step_two.parameters()     )
										a_									=   ema_unet.shadow_params

										for step_one, step_two in zip(UpperCAmelCase__   , UpperCAmelCase__     ):
															assert ~torch.allclose(UpperCAmelCase__   , UpperCAmelCase__     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	str:
										# If there's no decay even if there are backprops, EMA steps
										# won't take any effect i.e., the shadow params would remain the
										# same.
										a_							,					a_									=   self.get_models(decay=0.0     )
										a_									=   self.simulate_backprop(UpperCAmelCase__     )
										ema_unet.step(unet_step_one.parameters()     )
										a_									=   ema_unet.shadow_params

										a_									=   self.simulate_backprop(UpperCAmelCase__     )
										ema_unet.step(unet_step_two.parameters()     )
										a_									=   ema_unet.shadow_params

										for step_one, step_two in zip(UpperCAmelCase__   , UpperCAmelCase__     ):
															assert torch.allclose(UpperCAmelCase__   , UpperCAmelCase__     )




					@skip_mps
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Optional[int]:
										a_							,					a_									=   self.get_models()
										a_							,					a_							,					a_									=   self.get_dummy_inputs()

										with tempfile.TemporaryDirectory() as tmpdir:
															ema_unet.save_pretrained(UpperCAmelCase__     )
															a_									=   UNetaDConditionModel.from_pretrained(UpperCAmelCase__   , model_cls=UpperCAmelCase__     )
															a_									=   loaded_unet.to(unet.device     )

										# Since no EMA step has been performed the outputs should match.
										a_									=   unet(UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     ).sample
										a_									=   loaded_unet(UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     ).sample

										assert torch.allclose(UpperCAmelCase__   , UpperCAmelCase__   , atol=1e-4     )









'''simple docstring'''
def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				list[int]:






					"""simple docstring"""







					if hue < 0 or hue > 3_6_0:
										raise Exception('hue should be between 0 and 360' )

					if saturation < 0 or saturation > 1:
										raise Exception('saturation should be between 0 and 1' )

					if value < 0 or value > 1:
										raise Exception('value should be between 0 and 1' )

					a_									=   value * saturation
					a_									=   hue / 6_0
					a_									=   chroma * (1 - abs(hue_section % 2 - 1 ))
					a_									=   value - chroma

					if hue_section >= 0 and hue_section <= 1:
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (match_value) )
					elif hue_section > 1 and hue_section <= 2:
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (match_value) )
					elif hue_section > 2 and hue_section <= 3:
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
					elif hue_section > 3 and hue_section <= 4:
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
					elif hue_section > 4 and hue_section <= 5:
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
					else:
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )

					return [red, green, blue]

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				list[float]:






					"""simple docstring"""







					if red < 0 or red > 2_5_5:
										raise Exception('red should be between 0 and 255' )

					if green < 0 or green > 2_5_5:
										raise Exception('green should be between 0 and 255' )

					if blue < 0 or blue > 2_5_5:
										raise Exception('blue should be between 0 and 255' )

					a_									=   red / 2_5_5
					a_									=   green / 2_5_5
					a_									=   blue / 2_5_5
					a_									=   max(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
					a_									=   value - min(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
					a_									=   0 if value == 0 else chroma / value

					if chroma == 0:
										a_									=   0.0
					elif value == float_red:
										a_									=   6_0 * (0 + (float_green - float_blue) / chroma)
					elif value == float_green:
										a_									=   6_0 * (2 + (float_blue - float_red) / chroma)
					else:
										a_									=   6_0 * (4 + (float_red - float_green) / chroma)

					a_									=   (hue + 3_6_0) % 3_6_0

					return [hue, saturation, value]





def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase )				->				bool:






					"""simple docstring"""







					a_									=   abs(hsv_a[0] - hsv_a[0] ) < 0.2
					a_									=   abs(hsv_a[1] - hsv_a[1] ) < 0.0_0_2
					a_									=   abs(hsv_a[2] - hsv_a[2] ) < 0.0_0_2

					return check_hue and check_saturation and check_value

