code: 
  
  
  
  
  
  from typing import Dict, Optional, Tuple
  
  import torch
  def   _A (	SCREAMING_SNAKE_CASE  :     torch.Tensor		):
  
  
  
  
  
    """simple docstring"""
  
  
  
    a__			:      Any            =boundaries[1] - boundaries[0]
    a__			:      str            =boundaries + step / 2
    a__			:      List[str]            =torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1		)] , dim=0		)
    return bin_centers
  def   _A (	SCREAMING_SNAKE_CASE  :     torch.Tensor , SCREAMING_SNAKE_CASE  :     torch.Tensor , ):
  
  
  
  
  
    """simple docstring"""
  
  
  
    a__			:      int            =_calculate_bin_centers(SCREAMING_SNAKE_CASE		)
    return (
        torch.sum(aligned_distance_error_probs * bin_centers , dim=-1		),
        bin_centers[-1],
    )
  def   _A (	SCREAMING_SNAKE_CASE  :     torch.Tensor , SCREAMING_SNAKE_CASE  :     int = 31 , SCREAMING_SNAKE_CASE  :     int = 64 , **SCREAMING_SNAKE_CASE  :     List[Any] , ):
  
  
  
  
  
    """simple docstring"""
  
  
  
    a__			:      int            =torch.linspace(0 , SCREAMING_SNAKE_CASE , steps=(no_bins - 1) , device=logits.device		)
  
    a__			:      str            =torch.nn.functional.softmax(SCREAMING_SNAKE_CASE , dim=-1		)
    a__      ,							a__			:      List[Any]            =_calculate_expected_aligned_error(
        alignment_confidence_breaks=SCREAMING_SNAKE_CASE , aligned_distance_error_probs=SCREAMING_SNAKE_CASE , )
  
    return {
        "aligned_confidence_probs": aligned_confidence_probs,
        "predicted_aligned_error": predicted_aligned_error,
        "max_predicted_aligned_error": max_predicted_aligned_error,
    }
  
  
  
  
  
  def   _A (	SCREAMING_SNAKE_CASE  :     torch.Tensor , SCREAMING_SNAKE_CASE  :     Optional[torch.Tensor] = None , SCREAMING_SNAKE_CASE  :     int = 31 , SCREAMING_SNAKE_CASE  :     int = 64 , SCREAMING_SNAKE_CASE  :     float = 1e-8 , **SCREAMING_SNAKE_CASE  :     Optional[Any] , ):
  
  
  
  
  
    """simple docstring"""
  
  
  
    if residue_weights is None:
      a__			:      Dict            =logits.new_ones(logits.shape[-2]		)
  
    a__			:      Tuple            =torch.linspace(0 , SCREAMING_SNAKE_CASE , steps=(no_bins - 1) , device=logits.device		)
  
    a__			:      Dict            =_calculate_bin_centers(SCREAMING_SNAKE_CASE		)
    torch.sum(SCREAMING_SNAKE_CASE		)
    a__			:      List[str]            =logits.shape[-2]
    a__			:      Optional[Any]            =max(SCREAMING_SNAKE_CASE , 19		)
  
    a__			:      Optional[int]            =1.2_4 * (clipped_n - 15) ** (1.0 / 3) - 1.8
  
    a__			:      Optional[int]            =torch.nn.functional.softmax(SCREAMING_SNAKE_CASE , dim=-1		)
  
    a__			:      Union[str, Any]            =1.0 / (1 + (bin_centers**2) / (da**2))
    a__			:      List[str]            =torch.sum(probs * tm_per_bin , dim=-1		)
  
    a__			:      str            =residue_weights / (eps + residue_weights.sum())
    a__			:      Optional[int]            =torch.sum(predicted_tm_term * normed_residue_mask , dim=-1		)
  
    a__			:      Dict            =per_alignment * residue_weights
  
    a__			:      Union[str, Any]            =(weighted == torch.max(SCREAMING_SNAKE_CASE		)).nonzero()[0]
    return per_alignment[tuple(SCREAMING_SNAKE_CASE		)]
  
  
  
  
  
  
  
style_context: 
  
  
  
  
  
  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        __lowerCAmelCase (					unittest.TestCase):
        _lowercase       :      str        =    """hf-internal-testing/tiny-stable-diffusion-pipe"""
        _lowercase       :      Optional[int]        =    1
        _lowercase       :      List[str]        =    77
        _lowercase       :      int        =    32
        _lowercase       :      Dict        =    4
        _lowercase       :      str        =    64
        _lowercase       :      Optional[Any]        =    torch.manual_seed(0)
  
  
  
  
  
  
  
        def      _lowercase						(					self	,   lowerCAmelCase__=0.99_99							)     ->		Tuple:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      int            =UNetaDConditionModel.from_pretrained(self.model_id	,   subfolder="unet"							)
          a__			:      Any            =unet.to(lowerCAmelCase__							)
          a__			:      int            =EMAModel(unet.parameters()	,   decay=lowerCAmelCase__	,   model_cls=lowerCAmelCase__	,   model_config=unet.config							)
          return unet, ema_unet
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		Dict:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      str            =torch.randn(
              self.batch_size	,   self.num_in_channels	,   self.latent_height	,   self.latent_width	,   generator=self.generator							).to(lowerCAmelCase__							)
          a__			:      Any            =torch.randint(0	,   1_0_0_0	,   size=(self.batch_size,)	,   generator=self.generator							).to(lowerCAmelCase__							)
          a__			:      Tuple            =torch.randn(
              self.batch_size	,   self.prompt_length	,   self.text_encoder_hidden_dim	,   generator=self.generator							).to(lowerCAmelCase__							)
          return noisy_latents, timesteps, encoder_hidden_states
  
  
  
  
  
  
  
        def      _lowercase						(					self	,   lowerCAmelCase__							)     ->		Any:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      Any            ={}
          for k, param in unet.state_dict().items():
            a__			:      Tuple            =torch.randn_like(lowerCAmelCase__							) + (param * torch.randn_like(lowerCAmelCase__							))
            updated_state_dict.update({k: updated_param}							)
          unet.load_state_dict(lowerCAmelCase__							)
          return unet
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		Tuple:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      List[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      _lowercase						(					self							)     ->		List[str]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      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()							)
          a__			:      str            =list(unet.parameters()							)
          for s_param, param in zip(ema_unet.shadow_params	,   lowerCAmelCase__							):
            assert torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
          # 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	,   lowerCAmelCase__							):
            assert torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		List[Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      Optional[Any]            =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__			:      List[str]            =self.simulate_backprop(lowerCAmelCase__							)
  
          # 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__			:      Optional[int]            =list(unet_pseudo_updated_step_one.parameters()							)
          for s_param, param in zip(ema_unet.shadow_params	,   lowerCAmelCase__							):
            assert ~torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
          # 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	,   lowerCAmelCase__							):
            assert ~torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		Union[str, Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      Dict            =self.get_models()
  
          # First backprop + EMA
          a__			:      Tuple            =self.simulate_backprop(lowerCAmelCase__							)
          ema_unet.step(unet_step_one.parameters()							)
          a__			:      Dict            =ema_unet.shadow_params
  
          # Second backprop + EMA
          a__			:      str            =self.simulate_backprop(lowerCAmelCase__							)
          ema_unet.step(unet_step_two.parameters()							)
          a__			:      List[str]            =ema_unet.shadow_params
  
          for step_one, step_two in zip(lowerCAmelCase__	,   lowerCAmelCase__							):
            assert ~torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		List[str]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      int            =self.get_models(decay=0.0							)
          a__			:      Optional[int]            =self.simulate_backprop(lowerCAmelCase__							)
          ema_unet.step(unet_step_one.parameters()							)
          a__			:      List[Any]            =ema_unet.shadow_params
  
          a__			:      Tuple            =self.simulate_backprop(lowerCAmelCase__							)
          ema_unet.step(unet_step_two.parameters()							)
          a__			:      List[Any]            =ema_unet.shadow_params
  
          for step_one, step_two in zip(lowerCAmelCase__	,   lowerCAmelCase__							):
            assert torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
  
  
  
  
  
  
        @skip_mps
        def      _lowercase						(					self							)     ->		Optional[Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      Union[str, Any]            =self.get_models()
          a__      ,							a__      ,							a__			:      Tuple            =self.get_dummy_inputs()
  
          with tempfile.TemporaryDirectory() as tmpdir:
            ema_unet.save_pretrained(lowerCAmelCase__							)
            a__			:      List[str]            =UNetaDConditionModel.from_pretrained(lowerCAmelCase__	,   model_cls=lowerCAmelCase__							)
            a__			:      Tuple            =loaded_unet.to(unet.device							)
  
          # Since no EMA step has been performed the outputs should match.
          a__			:      Optional[Any]            =unet(lowerCAmelCase__	,   lowerCAmelCase__	,   lowerCAmelCase__							).sample
          a__			:      Optional[int]            =loaded_unet(lowerCAmelCase__	,   lowerCAmelCase__	,   lowerCAmelCase__							).sample
  
          assert torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__	,   atol=1E-4							)
  
  
  
  
  
  
  