code: 
  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 						UpperCAmelCase__   (      unittest.TestCase		):
  
  
  
       '''simple docstring'''
       UpperCAmelCase_         =  'hf-internal-testing/tiny-stable-diffusion-pipe'
       UpperCAmelCase_         =  1
       UpperCAmelCase_         =  7_7
       UpperCAmelCase_         =  3_2
       UpperCAmelCase_         =  4
       UpperCAmelCase_         =  6_4
       UpperCAmelCase_         =  torch.manual_seed(0		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Any	,      UpperCamelCase       :       Union[str, Any]=0.9999		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				int           =			UNetaDConditionModel.from_pretrained(self.model_id	,      subfolder='''unet'''		)
              _lowercase					:				Any           =			unet.to(UpperCamelCase		)
              _lowercase					:				Union[str, Any]           =			EMAModel(unet.parameters()	,      decay=UpperCamelCase	,      model_cls=UpperCamelCase	,      model_config=unet.config		)
              return unet, ema_unet
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Tuple		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Tuple           =			torch.randn(
                  self.batch_size	,      self.num_in_channels	,      self.latent_height	,      self.latent_width	,      generator=self.generator		).to(UpperCamelCase		)
              _lowercase					:				Union[str, Any]           =			torch.randint(0	,      10_00	,      size=(self.batch_size,)	,      generator=self.generator		).to(UpperCamelCase		)
              _lowercase					:				int           =			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   lowerCAmelCase_   ( self       :       str	,      UpperCamelCase       :       List[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				int           =			{}
              for k, param in unet.state_dict().items():
                     _lowercase					:				Optional[Any]           =			torch.randn_like(UpperCamelCase		) + (param * torch.randn_like(UpperCamelCase		))
                     updated_state_dict.update({k: updated_param}		)
              unet.load_state_dict(UpperCamelCase		)
              return unet
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Union[str, Any]           =			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   lowerCAmelCase_   ( self       :       int		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Any           =			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()		)
              _lowercase					:				List[Any]           =			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   lowerCAmelCase_   ( self       :       Dict		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Dict           =			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.
              _lowercase					:				str           =			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.
              _lowercase					:				Optional[Any]           =			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   lowerCAmelCase_   ( self       :       Union[str, Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Dict           =			self.get_models()
  
              # First backprop + EMA
              _lowercase					:				Optional[Any]           =			self.simulate_backprop(UpperCamelCase		)
              ema_unet.step(unet_step_one.parameters()		)
              _lowercase					:				Optional[Any]           =			ema_unet.shadow_params
  
              # Second backprop + EMA
              _lowercase					:				Optional[int]           =			self.simulate_backprop(UpperCamelCase		)
              ema_unet.step(unet_step_two.parameters()		)
              _lowercase					:				List[str]           =			ema_unet.shadow_params
  
              for step_one, step_two in zip(UpperCamelCase	,      UpperCamelCase		):
                     assert ~torch.allclose(UpperCamelCase	,      UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       str		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				List[str]           =			self.get_models(decay=0.0		)
              _lowercase					:				Optional[Any]           =			self.simulate_backprop(UpperCamelCase		)
              ema_unet.step(unet_step_one.parameters()		)
              _lowercase					:				Dict           =			ema_unet.shadow_params
  
              _lowercase					:				List[str]           =			self.simulate_backprop(UpperCamelCase		)
              ema_unet.step(unet_step_two.parameters()		)
              _lowercase					:				Union[str, Any]           =			ema_unet.shadow_params
  
              for step_one, step_two in zip(UpperCamelCase	,      UpperCamelCase		):
                     assert torch.allclose(UpperCamelCase	,      UpperCamelCase		)
  
  
  
  
       @skip_mps
       def   lowerCAmelCase_   ( self       :       Dict		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Optional[Any]           =			self.get_models()
              _lowercase					:				Any           =			self.get_dummy_inputs()
  
              with tempfile.TemporaryDirectory() as tmpdir:
                     ema_unet.save_pretrained(UpperCamelCase		)
                     _lowercase					:				List[str]           =			UNetaDConditionModel.from_pretrained(UpperCamelCase	,      model_cls=UpperCamelCase		)
                     _lowercase					:				Dict           =			loaded_unet.to(unet.device		)
  
              # Since no EMA step has been performed the outputs should match.
              _lowercase					:				Optional[Any]           =			unet(UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase		).sample
              _lowercase					:				List[str]           =			loaded_unet(UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase		).sample
  
              assert torch.allclose(UpperCamelCase	,      UpperCamelCase	,      atol=1E-4		)
style_context: 
  
  
  import torch
  
  from diffusers import DDIMScheduler
  
  from .test_schedulers import SchedulerCommonTest
  
  class 						UpperCAmelCase__   (      A_		):
  
  
  
       '''simple docstring'''
       UpperCAmelCase_         =  (DDIMScheduler,)
       UpperCAmelCase_         =  (('''eta''', 0.0), ('''num_inference_steps''', 5_0))
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[Any]	,      **UpperCamelCase       :       str		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Any           =			{
                  '''num_train_timesteps''': 10_00,
                  '''beta_start''': 0.0001,
                  '''beta_end''': 0.02,
                  '''beta_schedule''': '''linear''',
                  '''clip_sample''': True,
              }
  
              config.update(**UpperCamelCase		)
              return config
  
  
  
  
       def   lowerCAmelCase_   ( self       :       str	,      **UpperCamelCase       :       int		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				str           =			self.scheduler_classes[0]
              _lowercase					:				Dict           =			self.get_scheduler_config(**UpperCamelCase		)
              _lowercase					:				List[str]           =			scheduler_class(**UpperCamelCase		)
  
              _lowercase       ,	_lowercase					:				int           =			10, 0.0
  
              _lowercase					:				List[str]           =			self.dummy_model()
              _lowercase					:				int           =			self.dummy_sample_deter
  
              scheduler.set_timesteps(UpperCamelCase		)
  
              for t in scheduler.timesteps:
                     _lowercase					:				Union[str, Any]           =			model(UpperCamelCase	,      UpperCamelCase		)
                     _lowercase					:				List[Any]           =			scheduler.step(UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase		).prev_sample
  
              return sample
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Dict		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for timesteps in [1_00, 5_00, 10_00]:
                     self.check_over_configs(num_train_timesteps=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for steps_offset in [0, 1]:
                     self.check_over_configs(steps_offset=UpperCamelCase		)
  
              _lowercase					:				List[Any]           =			self.scheduler_classes[0]
              _lowercase					:				Tuple           =			self.get_scheduler_config(steps_offset=1		)
              _lowercase					:				Union[str, Any]           =			scheduler_class(**UpperCamelCase		)
              scheduler.set_timesteps(5		)
              assert torch.equal(scheduler.timesteps	,      torch.LongTensor([8_01, 6_01, 4_01, 2_01, 1]		)		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1]	,      [0.002, 0.02, 0.2, 2]		):
                     self.check_over_configs(beta_start=UpperCamelCase	,      beta_end=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Any		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for schedule in ["linear", "squaredcos_cap_v2"]:
                     self.check_over_configs(beta_schedule=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[int]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for prediction_type in ["epsilon", "v_prediction"]:
                     self.check_over_configs(prediction_type=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for clip_sample in [True, False]:
                     self.check_over_configs(clip_sample=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Any		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for timestep_spacing in ["trailing", "leading"]:
                     self.check_over_configs(timestep_spacing=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[str]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for rescale_betas_zero_snr in [True, False]:
                     self.check_over_configs(rescale_betas_zero_snr=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              self.check_over_configs(thresholding=UpperCamelCase		)
              for threshold in [0.5, 1.0, 2.0]:
                     for prediction_type in ["epsilon", "v_prediction"]:
                            self.check_over_configs(
                                thresholding=UpperCamelCase	,      prediction_type=UpperCamelCase	,      sample_max_value=UpperCamelCase	,      )
  
  
  
  
       def   lowerCAmelCase_   ( self       :       str		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for t in [1, 10, 49]:
                     self.check_over_forward(time_step=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[int]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for t, num_inference_steps in zip([1, 10, 50]	,      [10, 50, 5_00]		):
                     self.check_over_forward(time_step=UpperCamelCase	,      num_inference_steps=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for t, eta in zip([1, 10, 49]	,      [0.0, 0.5, 1.0]		):
                     self.check_over_forward(time_step=UpperCamelCase	,      eta=UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				int           =			self.scheduler_classes[0]
              _lowercase					:				str           =			self.get_scheduler_config()
              _lowercase					:				int           =			scheduler_class(**UpperCamelCase		)
  
              assert torch.sum(torch.abs(scheduler._get_variance(0	,      0		) - 0.0		)		) < 1E-5
              assert torch.sum(torch.abs(scheduler._get_variance(4_20	,      4_00		) - 0.14771		)		) < 1E-5
              assert torch.sum(torch.abs(scheduler._get_variance(9_80	,      9_60		) - 0.32460		)		) < 1E-5
              assert torch.sum(torch.abs(scheduler._get_variance(0	,      0		) - 0.0		)		) < 1E-5
              assert torch.sum(torch.abs(scheduler._get_variance(4_87	,      4_86		) - 0.00979		)		) < 1E-5
              assert torch.sum(torch.abs(scheduler._get_variance(9_99	,      9_98		) - 0.02		)		) < 1E-5
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Any           =			self.full_loop()
  
              _lowercase					:				Optional[Any]           =			torch.sum(torch.abs(UpperCamelCase		)		)
              _lowercase					:				Union[str, Any]           =			torch.mean(torch.abs(UpperCamelCase		)		)
  
              assert abs(result_sum.item() - 172.0067		) < 1E-2
              assert abs(result_mean.item() - 0.223967		) < 1E-3
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Union[str, Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				List[Any]           =			self.full_loop(prediction_type='''v_prediction'''		)
  
              _lowercase					:				int           =			torch.sum(torch.abs(UpperCamelCase		)		)
              _lowercase					:				Union[str, Any]           =			torch.mean(torch.abs(UpperCamelCase		)		)
  
              assert abs(result_sum.item() - 52.5302		) < 1E-2
              assert abs(result_mean.item() - 0.0684		) < 1E-3
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Any		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Tuple           =			self.full_loop(set_alpha_to_one=UpperCamelCase	,      beta_start=0.01		)
              _lowercase					:				List[Any]           =			torch.sum(torch.abs(UpperCamelCase		)		)
              _lowercase					:				Any           =			torch.mean(torch.abs(UpperCamelCase		)		)
  
              assert abs(result_sum.item() - 149.8295		) < 1E-2
              assert abs(result_mean.item() - 0.1951		) < 1E-3
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Any		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				int           =			self.full_loop(set_alpha_to_one=UpperCamelCase	,      beta_start=0.01		)
              _lowercase					:				Optional[int]           =			torch.sum(torch.abs(UpperCamelCase		)		)
              _lowercase					:				Dict           =			torch.mean(torch.abs(UpperCamelCase		)		)
  
              assert abs(result_sum.item() - 149.0784		) < 1E-2
              assert abs(result_mean.item() - 0.1941		) < 1E-3