code: 
  
  
  
  """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 			UpperCAmelCase_						(       unittest.TestCase		):
  						__SCREAMING_SNAKE_CASE		:     List[str]				  =				'hf-internal-testing/tiny-stable-diffusion-pipe'
  						__SCREAMING_SNAKE_CASE		:     Tuple				  =				1
  						__SCREAMING_SNAKE_CASE		:     Any				  =				7_7
  						__SCREAMING_SNAKE_CASE		:     Tuple				  =				3_2
  						__SCREAMING_SNAKE_CASE		:     Union[str, Any]				  =				4
  						__SCREAMING_SNAKE_CASE		:     Optional[int]				  =				6_4
  						__SCREAMING_SNAKE_CASE		:     Optional[int]				  =				torch.manual_seed(0		)
  
  
  
  						def 		snake_case_      (  self  :			List[Any]  ,			A  :			Optional[Any]=0.9_999		):
  											_UpperCAmelCase    :			List[Any]              =					UNetaDConditionModel.from_pretrained(self.model_id  ,			subfolder="unet"		)
  											_UpperCAmelCase    :			Union[str, Any]              =					unet.to(A		)
  											_UpperCAmelCase    :			Optional[int]              =					EMAModel(unet.parameters()  ,			decay=A  ,			model_cls=A  ,			model_config=unet.config		)
  											return unet, ema_unet
  
  
  
  						def 		snake_case_      (  self  :			int		):
  											_UpperCAmelCase    :			Optional[int]              =					torch.randn(
  											    self.batch_size  ,			self.num_in_channels  ,			self.latent_height  ,			self.latent_width  ,			generator=self.generator		).to(A		)
  											_UpperCAmelCase    :			Tuple              =					torch.randint(0  ,			1_0_0_0  ,			size=(self.batch_size,)  ,			generator=self.generator		).to(A		)
  											_UpperCAmelCase    :			Union[str, Any]              =					torch.randn(
  											    self.batch_size  ,			self.prompt_length  ,			self.text_encoder_hidden_dim  ,			generator=self.generator		).to(A		)
  											return noisy_latents, timesteps, encoder_hidden_states
  
  
  
  						def 		snake_case_      (  self  :			Tuple  ,			A  :			List[Any]		):
  											_UpperCAmelCase    :			List[Any]              =					{}
  											for k, param in unet.state_dict().items():
  																_UpperCAmelCase    :			Optional[Any]              =					torch.randn_like(A		) + (param * torch.randn_like(A		))
  																updated_state_dict.update({k: updated_param}		)
  											unet.load_state_dict(A		)
  											return unet
  
  
  
  						def 		snake_case_      (  self  :			Optional[Any]		):
  											_UpperCAmelCase       , _UpperCAmelCase    :			Tuple              =					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 		snake_case_      (  self  :			List[Any]		):
  											_UpperCAmelCase       , _UpperCAmelCase    :			Optional[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()		)
  											_UpperCAmelCase    :			Union[str, Any]              =					list(unet.parameters()		)
  											for s_param, param in zip(ema_unet.shadow_params  ,			A		):
  																assert torch.allclose(A  ,			A		)
  
  											# 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  ,			A		):
  																assert torch.allclose(A  ,			A		)
  
  
  
  						def 		snake_case_      (  self  :			Optional[int]		):
  											_UpperCAmelCase       , _UpperCAmelCase    :			Tuple              =					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.
  											_UpperCAmelCase    :			Dict              =					self.simulate_backprop(A		)
  
  											# 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.
  											_UpperCAmelCase    :			List[Any]              =					list(unet_pseudo_updated_step_one.parameters()		)
  											for s_param, param in zip(ema_unet.shadow_params  ,			A		):
  																assert ~torch.allclose(A  ,			A		)
  
  											# 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  ,			A		):
  																assert ~torch.allclose(A  ,			A		)
  
  
  
  						def 		snake_case_      (  self  :			Optional[int]		):
  											_UpperCAmelCase       , _UpperCAmelCase    :			List[Any]              =					self.get_models()
  
  											# First backprop + EMA
  											_UpperCAmelCase    :			str              =					self.simulate_backprop(A		)
  											ema_unet.step(unet_step_one.parameters()		)
  											_UpperCAmelCase    :			Optional[int]              =					ema_unet.shadow_params
  
  											# Second backprop + EMA
  											_UpperCAmelCase    :			Union[str, Any]              =					self.simulate_backprop(A		)
  											ema_unet.step(unet_step_two.parameters()		)
  											_UpperCAmelCase    :			int              =					ema_unet.shadow_params
  
  											for step_one, step_two in zip(A  ,			A		):
  																assert ~torch.allclose(A  ,			A		)
  
  
  
  						def 		snake_case_      (  self  :			Any		):
  											_UpperCAmelCase       , _UpperCAmelCase    :			Union[str, Any]              =					self.get_models(decay=0.0		)
  											_UpperCAmelCase    :			Tuple              =					self.simulate_backprop(A		)
  											ema_unet.step(unet_step_one.parameters()		)
  											_UpperCAmelCase    :			List[str]              =					ema_unet.shadow_params
  
  											_UpperCAmelCase    :			Optional[Any]              =					self.simulate_backprop(A		)
  											ema_unet.step(unet_step_two.parameters()		)
  											_UpperCAmelCase    :			int              =					ema_unet.shadow_params
  
  											for step_one, step_two in zip(A  ,			A		):
  																assert torch.allclose(A  ,			A		)
  
  
  
  						@skip_mps
  						def 		snake_case_      (  self  :			Dict		):
  											_UpperCAmelCase       , _UpperCAmelCase    :			List[Any]              =					self.get_models()
  											_UpperCAmelCase       , _UpperCAmelCase       , _UpperCAmelCase    :			Optional[int]              =					self.get_dummy_inputs()
  
  											with tempfile.TemporaryDirectory() as tmpdir:
  																ema_unet.save_pretrained(A		)
  																_UpperCAmelCase    :			Tuple              =					UNetaDConditionModel.from_pretrained(A  ,			model_cls=A		)
  																_UpperCAmelCase    :			Any              =					loaded_unet.to(unet.device		)
  
  											# Since no EMA step has been performed the outputs should match.
  											_UpperCAmelCase    :			Dict              =					unet(A  ,			A  ,			A		).sample
  											_UpperCAmelCase    :			Any              =					loaded_unet(A  ,			A  ,			A		).sample
  
  											assert torch.allclose(A  ,			A  ,			atol=1e-4		)
  
style_context: 
  
  
  
  """simple docstring"""
  
  
  
  
  import argparse
  
  import torch
  
  from transformers import ImageGPTConfig, ImageGPTForCausalLM, load_tf_weights_in_imagegpt
  from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging
  
  
  logging.set_verbosity_info()
  
  def 		__snake_case		(   SCREAMING_SNAKE_CASE__			:		Optional[Any]		,						SCREAMING_SNAKE_CASE__			:		Tuple		,						SCREAMING_SNAKE_CASE__			:		Any       )						->     Dict:
  
  
  
  
  
       '''simple docstring'''
  
       _UpperCAmelCase    :			Any              =					{"small": (512, 8, 24), "medium": (1_024, 8, 36), "large": (1_536, 16, 48)}
       _UpperCAmelCase       , _UpperCAmelCase       , _UpperCAmelCase    :			str              =					MODELS[model_size]  # set model hyperparameters
       _UpperCAmelCase    :			Tuple              =					ImageGPTConfig(n_embd=SCREAMING_SNAKE_CASE__		,						n_layer=SCREAMING_SNAKE_CASE__		,						n_head=SCREAMING_SNAKE_CASE__       )
       _UpperCAmelCase    :			int              =					ImageGPTForCausalLM(SCREAMING_SNAKE_CASE__       )
  
       # Load weights from numpy
       load_tf_weights_in_imagegpt(SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__       )
  
       # Save pytorch-model
       _UpperCAmelCase    :			Union[str, Any]              =					pytorch_dump_folder_path + "/" + WEIGHTS_NAME
       _UpperCAmelCase    :			int              =					pytorch_dump_folder_path + "/" + CONFIG_NAME
       print(f'Save PyTorch model to {pytorch_weights_dump_path}'       )
       torch.save(model.state_dict()		,						SCREAMING_SNAKE_CASE__       )
       print(f'Save configuration file to {pytorch_config_dump_path}'       )
       with open(SCREAMING_SNAKE_CASE__		,						"w"		,						encoding="utf-8"       ) as f:
            f.write(config.to_json_string()       )
  
  
  if __name__ == "__main__":
   _lowerCAmelCase       :      List[str]							=							argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       "--imagegpt_checkpoint_path",
       default=None,
       type=str,
       required=True,
       help="Path to the TensorFlow checkpoint path.",
   )
   parser.add_argument(
       "--model_size",
       default=None,
       type=str,
       required=True,
       help="Size of the model (can be either 'small', 'medium' or 'large').",
   )
   parser.add_argument(
       "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
   )
   _lowerCAmelCase       :      Optional[int]							=							parser.parse_args()
   convert_imagegpt_checkpoint_to_pytorch(
       args.imagegpt_checkpoint_path, args.model_size, args.pytorch_dump_folder_path
   )
  