code: 
  
  
  from collections import OrderedDict
  from typing import Any, Mapping, Optional
  
  from ... import PreTrainedTokenizer
  from ...configuration_utils import PretrainedConfig
  from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
  from ...onnx.utils import compute_effective_axis_dimension
  from ...utils import TensorType, is_torch_available, logging
  
  
  A_														=      logging.get_logger(__name__)
  
  A_														=      {
      "google/bigbird-pegasus-large-arxiv": (
          "https://huggingface.co/google/bigbird-pegasus-large-arxiv/resolve/main/config.json"
      ),
      "google/bigbird-pegasus-large-pubmed": (
          "https://huggingface.co/google/bigbird-pegasus-large-pubmed/resolve/main/config.json"
      ),
      "google/bigbird-pegasus-large-bigpatent": (
          "https://huggingface.co/google/bigbird-pegasus-large-bigpatent/resolve/main/config.json"
      ),
      # See all BigBirdPegasus models at https://huggingface.co/models?filter=bigbird_pegasus
  }
  
  class 						snake_case			(       lowerCAmelCase__	):
  			'''simple docstring'''
  
  			UpperCAmelCase       :       Tuple       = """bigbird_pegasus"""
  			UpperCAmelCase       :       int       = ["""past_key_values"""]
  			UpperCAmelCase       :       Union[str, Any]       = {
  			    """num_attention_heads""": """encoder_attention_heads""",
  			    """hidden_size""": """d_model""",
  			    """attention_probs_dropout_prob""": """attention_dropout""",
  			}
  
  			def __init__(	self     :     List[Any]							,							lowerCAmelCase_     :     Union[str, Any]=96_103							,							lowerCAmelCase_     :     Tuple=4_096							,							lowerCAmelCase_     :     Optional[int]=16							,							lowerCAmelCase_     :     Any=4_096							,							lowerCAmelCase_     :     int=16							,							lowerCAmelCase_     :     Optional[Any]=16							,							lowerCAmelCase_     :     List[str]=4_096							,							lowerCAmelCase_     :     List[str]=16							,							lowerCAmelCase_     :     int=0.0							,							lowerCAmelCase_     :     str=0.0							,							lowerCAmelCase_     :     Optional[Any]=True							,							lowerCAmelCase_     :     Union[str, Any]=True							,							lowerCAmelCase_     :     Any="gelu_new"							,							lowerCAmelCase_     :     str=1_024							,							lowerCAmelCase_     :     Tuple=0.1							,							lowerCAmelCase_     :     int=0.0							,							lowerCAmelCase_     :     str=0.0							,							lowerCAmelCase_     :     Optional[Any]=0.02							,							lowerCAmelCase_     :     List[str]=2							,							lowerCAmelCase_     :     Optional[Any]=0.0							,							lowerCAmelCase_     :     List[Any]=True							,							lowerCAmelCase_     :     Optional[int]=0							,							lowerCAmelCase_     :     List[str]=2							,							lowerCAmelCase_     :     Dict=1							,							lowerCAmelCase_     :     Optional[Any]="block_sparse"							,							lowerCAmelCase_     :     int=64							,							lowerCAmelCase_     :     List[Any]=3							,							lowerCAmelCase_     :     Optional[Any]=False							,							**lowerCAmelCase_     :     Dict							,							)					->					List[Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       vocab_size
  								SCREAMING_SNAKE_CASE_	      =       max_position_embeddings
  								SCREAMING_SNAKE_CASE_	      =       d_model
  								SCREAMING_SNAKE_CASE_	      =       encoder_ffn_dim
  								SCREAMING_SNAKE_CASE_	      =       encoder_layers
  								SCREAMING_SNAKE_CASE_	      =       encoder_attention_heads
  								SCREAMING_SNAKE_CASE_	      =       decoder_ffn_dim
  								SCREAMING_SNAKE_CASE_	      =       decoder_layers
  								SCREAMING_SNAKE_CASE_	      =       decoder_attention_heads
  								SCREAMING_SNAKE_CASE_	      =       dropout
  								SCREAMING_SNAKE_CASE_	      =       attention_dropout
  								SCREAMING_SNAKE_CASE_	      =       activation_dropout
  								SCREAMING_SNAKE_CASE_	      =       activation_function
  								SCREAMING_SNAKE_CASE_	      =       init_std
  								SCREAMING_SNAKE_CASE_	      =       encoder_layerdrop
  								SCREAMING_SNAKE_CASE_	      =       decoder_layerdrop
  								SCREAMING_SNAKE_CASE_	      =       classifier_dropout
  								SCREAMING_SNAKE_CASE_	      =       use_cache
  								SCREAMING_SNAKE_CASE_	      =       encoder_layers
  								SCREAMING_SNAKE_CASE_	      =       scale_embedding  # scale factor will be sqrt(d_model) if True
  
  								# extra config
  								SCREAMING_SNAKE_CASE_	      =       attention_type
  								SCREAMING_SNAKE_CASE_	      =       block_size
  								SCREAMING_SNAKE_CASE_	      =       num_random_blocks
  								SCREAMING_SNAKE_CASE_	      =       use_bias
  
  								super().__init__(
  								    pad_token_id=lowerCAmelCase_							,							bos_token_id=lowerCAmelCase_							,							eos_token_id=lowerCAmelCase_							,							is_encoder_decoder=lowerCAmelCase_							,							decoder_start_token_id=lowerCAmelCase_							,							**lowerCAmelCase_							,							)
  
  class 						snake_case			(       lowerCAmelCase__	):
  			'''simple docstring'''
  
  			@property
  			def _lowercase (	self     :     List[Any]					)					->					Mapping[str, Mapping[int, str]]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								if self.task in ["default", "seq2seq-lm"]:
  													SCREAMING_SNAKE_CASE_	      =       OrderedDict(
  													    [
  													        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
  													        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
  													    ]					)
  
  													if self.use_past:
  																		SCREAMING_SNAKE_CASE_	      =       {0: '''batch'''}
  																		SCREAMING_SNAKE_CASE_	      =       {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
  													else:
  																		SCREAMING_SNAKE_CASE_	      =       {0: '''batch''', 1: '''decoder_sequence'''}
  																		SCREAMING_SNAKE_CASE_	      =       {0: '''batch''', 1: '''decoder_sequence'''}
  
  													if self.use_past:
  																		self.fill_with_past_key_values_(lowerCAmelCase_							,							direction='''inputs'''					)
  								elif self.task == "causal-lm":
  													# TODO: figure this case out.
  													SCREAMING_SNAKE_CASE_	      =       OrderedDict(
  													    [
  													        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
  													        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
  													    ]					)
  													if self.use_past:
  																		SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.num_layers
  																		for i in range(lowerCAmelCase_					):
  																							SCREAMING_SNAKE_CASE_	      =       {0: '''batch''', 2: '''past_sequence + sequence'''}
  																							SCREAMING_SNAKE_CASE_	      =       {0: '''batch''', 2: '''past_sequence + sequence'''}
  								else:
  													SCREAMING_SNAKE_CASE_	      =       OrderedDict(
  													    [
  													        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
  													        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
  													        ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}),
  													        ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}),
  													    ]					)
  
  								return common_inputs
  
  			@property
  			def _lowercase (	self     :     Dict					)					->					Mapping[str, Mapping[int, str]]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								if self.task in ["default", "seq2seq-lm"]:
  													SCREAMING_SNAKE_CASE_	      =       super().outputs
  								else:
  													SCREAMING_SNAKE_CASE_	      =       super(lowerCAmelCase_							,							self					).outputs
  													if self.use_past:
  																		SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.num_layers
  																		for i in range(lowerCAmelCase_					):
  																							SCREAMING_SNAKE_CASE_	      =       {0: '''batch''', 2: '''past_sequence + sequence'''}
  																							SCREAMING_SNAKE_CASE_	      =       {0: '''batch''', 2: '''past_sequence + sequence'''}
  								return common_outputs
  
  			def _lowercase (	self     :     List[Any]							,							lowerCAmelCase_     :     PreTrainedTokenizer							,							lowerCAmelCase_     :     int = -1							,							lowerCAmelCase_     :     int = -1							,							lowerCAmelCase_     :     bool = False							,							lowerCAmelCase_     :     Optional[TensorType] = None							,							)					->					Mapping[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
  								    lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_					)
  
  								# Generate decoder inputs
  								SCREAMING_SNAKE_CASE_	      =       seq_length if not self.use_past else 1
  								SCREAMING_SNAKE_CASE_	      =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
  								    lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_					)
  								SCREAMING_SNAKE_CASE_	      =       {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
  								SCREAMING_SNAKE_CASE_	      =       dict(**lowerCAmelCase_							,							**lowerCAmelCase_					)
  
  								if self.use_past:
  													if not is_torch_available():
  																		raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''					)
  													else:
  																		import torch
  													SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       common_inputs['''input_ids'''].shape
  													SCREAMING_SNAKE_CASE_	      =       common_inputs['''decoder_input_ids'''].shape[1]
  													SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.num_attention_heads
  													SCREAMING_SNAKE_CASE_	      =       (
  													    batch,
  													    num_encoder_attention_heads,
  													    encoder_seq_length,
  													    self._config.hidden_size // num_encoder_attention_heads,
  													)
  													SCREAMING_SNAKE_CASE_	      =       decoder_seq_length + 3
  													SCREAMING_SNAKE_CASE_	      =       (
  													    batch,
  													    num_decoder_attention_heads,
  													    decoder_past_length,
  													    self._config.hidden_size // num_decoder_attention_heads,
  													)
  
  													SCREAMING_SNAKE_CASE_	      =       torch.cat(
  													    [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCAmelCase_							,							lowerCAmelCase_					)]							,							dim=1					)
  
  													SCREAMING_SNAKE_CASE_	      =       []
  													# If the number of encoder and decoder layers are present in the model configuration, both are considered
  													SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.num_layers
  													SCREAMING_SNAKE_CASE_	      =       min(lowerCAmelCase_							,							lowerCAmelCase_					)
  													SCREAMING_SNAKE_CASE_	      =       max(lowerCAmelCase_							,							lowerCAmelCase_					) - min_num_layers
  													SCREAMING_SNAKE_CASE_	      =       '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''
  
  													for _ in range(lowerCAmelCase_					):
  																		common_inputs["past_key_values"].append(
  																		    (
  																		        torch.zeros(lowerCAmelCase_					),
  																		        torch.zeros(lowerCAmelCase_					),
  																		        torch.zeros(lowerCAmelCase_					),
  																		        torch.zeros(lowerCAmelCase_					),
  																		    )					)
  													# TODO: test this.
  													SCREAMING_SNAKE_CASE_	      =       encoder_shape if remaining_side_name == '''encoder''' else decoder_shape
  													for _ in range(lowerCAmelCase_							,							lowerCAmelCase_					):
  																		common_inputs["past_key_values"].append((torch.zeros(lowerCAmelCase_					), torch.zeros(lowerCAmelCase_					))					)
  								return common_inputs
  
  			def _lowercase (	self     :     Union[str, Any]							,							lowerCAmelCase_     :     PreTrainedTokenizer							,							lowerCAmelCase_     :     int = -1							,							lowerCAmelCase_     :     int = -1							,							lowerCAmelCase_     :     bool = False							,							lowerCAmelCase_     :     Optional[TensorType] = None							,							)					->					Mapping[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
  								    lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_					)
  
  								if self.use_past:
  													if not is_torch_available():
  																		raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''					)
  													else:
  																		import torch
  													SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       common_inputs['''input_ids'''].shape
  													# Not using the same length for past_key_values
  													SCREAMING_SNAKE_CASE_	      =       seqlen + 2
  													SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.num_layers
  													SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.num_attention_heads
  													SCREAMING_SNAKE_CASE_	      =       (
  													    batch,
  													    num_encoder_attention_heads,
  													    past_key_values_length,
  													    self._config.hidden_size // num_encoder_attention_heads,
  													)
  
  													SCREAMING_SNAKE_CASE_	      =       common_inputs['''attention_mask'''].dtype
  													SCREAMING_SNAKE_CASE_	      =       torch.cat(
  													    [common_inputs['''attention_mask'''], torch.ones(lowerCAmelCase_							,							lowerCAmelCase_							,							dtype=lowerCAmelCase_					)]							,							dim=1					)
  													SCREAMING_SNAKE_CASE_	      =       [
  													    (torch.zeros(lowerCAmelCase_					), torch.zeros(lowerCAmelCase_					)) for _ in range(lowerCAmelCase_					)
  													]
  								return common_inputs
  
  			def _lowercase (	self     :     Dict							,							lowerCAmelCase_     :     PreTrainedTokenizer							,							lowerCAmelCase_     :     int = -1							,							lowerCAmelCase_     :     int = -1							,							lowerCAmelCase_     :     bool = False							,							lowerCAmelCase_     :     Optional[TensorType] = None							,							)					->					Mapping[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       compute_effective_axis_dimension(
  								    lowerCAmelCase_							,							fixed_dimension=OnnxConfig.default_fixed_batch							,							num_token_to_add=0					)
  
  								# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
  								SCREAMING_SNAKE_CASE_	      =       tokenizer.num_special_tokens_to_add(lowerCAmelCase_					)
  								SCREAMING_SNAKE_CASE_	      =       compute_effective_axis_dimension(
  								    lowerCAmelCase_							,							fixed_dimension=OnnxConfig.default_fixed_sequence							,							num_token_to_add=lowerCAmelCase_					)
  
  								# Generate dummy inputs according to compute batch and sequence
  								SCREAMING_SNAKE_CASE_	      =       [''' '''.join([tokenizer.unk_token]					) * seq_length] * batch_size
  								SCREAMING_SNAKE_CASE_	      =       dict(tokenizer(lowerCAmelCase_							,							return_tensors=lowerCAmelCase_					)					)
  								return common_inputs
  
  			def _lowercase (	self     :     List[str]							,							lowerCAmelCase_     :     PreTrainedTokenizer							,							lowerCAmelCase_     :     int = -1							,							lowerCAmelCase_     :     int = -1							,							lowerCAmelCase_     :     bool = False							,							lowerCAmelCase_     :     Optional[TensorType] = None							,							)					->					Mapping[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								if self.task in ["default", "seq2seq-lm"]:
  													SCREAMING_SNAKE_CASE_	      =       self._generate_dummy_inputs_for_default_and_seqaseq_lm(
  													    lowerCAmelCase_							,							batch_size=lowerCAmelCase_							,							seq_length=lowerCAmelCase_							,							is_pair=lowerCAmelCase_							,							framework=lowerCAmelCase_					)
  
  								elif self.task == "causal-lm":
  													SCREAMING_SNAKE_CASE_	      =       self._generate_dummy_inputs_for_causal_lm(
  													    lowerCAmelCase_							,							batch_size=lowerCAmelCase_							,							seq_length=lowerCAmelCase_							,							is_pair=lowerCAmelCase_							,							framework=lowerCAmelCase_					)
  								else:
  													SCREAMING_SNAKE_CASE_	      =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
  													    lowerCAmelCase_							,							batch_size=lowerCAmelCase_							,							seq_length=lowerCAmelCase_							,							is_pair=lowerCAmelCase_							,							framework=lowerCAmelCase_					)
  
  								return common_inputs
  
  
  
  
  			def _lowercase (	self     :     Optional[Any]							,							lowerCAmelCase_     :     Dict							,							lowerCAmelCase_     :     Optional[Any]							,							lowerCAmelCase_     :     str							,							lowerCAmelCase_     :     List[Any]					)					->					Optional[Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								if self.task in ["default", "seq2seq-lm"]:
  													SCREAMING_SNAKE_CASE_	      =       super()._flatten_past_key_values_(lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_					)
  								else:
  													SCREAMING_SNAKE_CASE_	      =       super(lowerCAmelCase_							,							self					)._flatten_past_key_values_(
  													    lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_					)
  
  
  
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 						snake_case			(       unittest.TestCase	):
  			'''simple docstring'''
  
  			UpperCAmelCase       :       Union[str, Any]       = """hf-internal-testing/tiny-stable-diffusion-pipe"""
  			UpperCAmelCase       :       int       = 1
  			UpperCAmelCase       :       Optional[Any]       = 77
  			UpperCAmelCase       :       Any       = 32
  			UpperCAmelCase       :       str       = 4
  			UpperCAmelCase       :       Optional[int]       = 64
  			UpperCAmelCase       :       Tuple       = torch.manual_seed(0	)
  
  			def _lowercase (	self     :     str							,							lowerCAmelCase_     :     str=0.9_999					)					->					int:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       UNetaDConditionModel.from_pretrained(self.model_id							,							subfolder='''unet'''					)
  								SCREAMING_SNAKE_CASE_	      =       unet.to(lowerCAmelCase_					)
  								SCREAMING_SNAKE_CASE_	      =       EMAModel(unet.parameters()							,							decay=lowerCAmelCase_							,							model_cls=lowerCAmelCase_							,							model_config=unet.config					)
  								return unet, ema_unet
  
  			def _lowercase (	self     :     str					)					->					List[str]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       torch.randn(
  								    self.batch_size							,							self.num_in_channels							,							self.latent_height							,							self.latent_width							,							generator=self.generator					).to(lowerCAmelCase_					)
  								SCREAMING_SNAKE_CASE_	      =       torch.randint(0							,							1_000							,							size=(self.batch_size,)							,							generator=self.generator					).to(lowerCAmelCase_					)
  								SCREAMING_SNAKE_CASE_	      =       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     :     str							,							lowerCAmelCase_     :     Tuple					)					->					str:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       {}
  								for k, param in unet.state_dict().items():
  													SCREAMING_SNAKE_CASE_	      =       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     :     Dict					)					->					str:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       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     :     Union[str, Any]					)					->					List[str]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       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()					)
  								SCREAMING_SNAKE_CASE_	      =       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     :     Any					)					->					Union[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       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.
  								SCREAMING_SNAKE_CASE_	      =       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.
  								SCREAMING_SNAKE_CASE_	      =       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     :     Any					)					->					Tuple:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_models()
  
  								# First backprop + EMA
  								SCREAMING_SNAKE_CASE_	      =       self.simulate_backprop(lowerCAmelCase_					)
  								ema_unet.step(unet_step_one.parameters()					)
  								SCREAMING_SNAKE_CASE_	      =       ema_unet.shadow_params
  
  								# Second backprop + EMA
  								SCREAMING_SNAKE_CASE_	      =       self.simulate_backprop(lowerCAmelCase_					)
  								ema_unet.step(unet_step_two.parameters()					)
  								SCREAMING_SNAKE_CASE_	      =       ema_unet.shadow_params
  
  								for step_one, step_two in zip(lowerCAmelCase_							,							lowerCAmelCase_					):
  													assert ~torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_					)
  
  			def _lowercase (	self     :     str					)					->					Optional[Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_models(decay=0.0					)
  								SCREAMING_SNAKE_CASE_	      =       self.simulate_backprop(lowerCAmelCase_					)
  								ema_unet.step(unet_step_one.parameters()					)
  								SCREAMING_SNAKE_CASE_	      =       ema_unet.shadow_params
  
  								SCREAMING_SNAKE_CASE_	      =       self.simulate_backprop(lowerCAmelCase_					)
  								ema_unet.step(unet_step_two.parameters()					)
  								SCREAMING_SNAKE_CASE_	      =       ema_unet.shadow_params
  
  								for step_one, step_two in zip(lowerCAmelCase_							,							lowerCAmelCase_					):
  													assert torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_					)
  
  
  
  
  			@skip_mps
  			def _lowercase (	self     :     str					)					->					List[Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_models()
  								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_dummy_inputs()
  
  								with tempfile.TemporaryDirectory() as tmpdir:
  													ema_unet.save_pretrained(lowerCAmelCase_					)
  													SCREAMING_SNAKE_CASE_	      =       UNetaDConditionModel.from_pretrained(lowerCAmelCase_							,							model_cls=lowerCAmelCase_					)
  													SCREAMING_SNAKE_CASE_	      =       loaded_unet.to(unet.device					)
  
  								# Since no EMA step has been performed the outputs should match.
  								SCREAMING_SNAKE_CASE_	      =       unet(lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_					).sample
  								SCREAMING_SNAKE_CASE_	      =       loaded_unet(lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_					).sample
  
  								assert torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_							,							atol=1e-4					)
  
  
  