


'''simple docstring'''

import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()





class      snake_case    (    unittest.TestCase			):





  """simple docstring"""




  def   snake_case		(    self     ):







   """simple docstring"""
   # clean up the VRAM after each test
   super().tearDown()
   gc.collect()
   torch.cuda.empty_cache()






  @property
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  1
   lowerCamelCase_      =  3
   lowerCamelCase_      =  (32, 32)

   lowerCamelCase_      =  floats_tensor((batch_size, num_channels) + sizes			,  rng=random.Random(0     )     ).to(UpperCamelCase     )
   return image






  @property
  def   snake_case		(    self     ):







   """simple docstring"""
   torch.manual_seed(0     )
   lowerCamelCase_      =  UNetaDConditionModel(
       block_out_channels=(32, 32, 64)			,  layers_per_block=2			,  sample_size=32			,  in_channels=7			,  out_channels=4			,  down_block_types=("DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D")			,  up_block_types=("CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D")			,  cross_attention_dim=32			,  attention_head_dim=8			,  use_linear_projection=UpperCamelCase			,  only_cross_attention=(True, True, False)			,  num_class_embeds=100			,  )
   return model






  @property
  def   snake_case		(    self     ):







   """simple docstring"""
   torch.manual_seed(0     )
   lowerCamelCase_      =  AutoencoderKL(
       block_out_channels=[32, 32, 64]			,  in_channels=3			,  out_channels=3			,  down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D"]			,  up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"]			,  latent_channels=4			,  )
   return model






  @property
  def   snake_case		(    self     ):







   """simple docstring"""
   torch.manual_seed(0     )
   lowerCamelCase_      =  CLIPTextConfig(
       bos_token_id=0			,  eos_token_id=2			,  hidden_size=32			,  intermediate_size=37			,  layer_norm_eps=1e-05			,  num_attention_heads=4			,  num_hidden_layers=5			,  pad_token_id=1			,  vocab_size=1000			,  hidden_act="gelu"			,  projection_dim=512			,  )
   return CLIPTextModel(UpperCamelCase     )






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  "cpu"  # ensure determinism for the device-dependent torch.Generator
   lowerCamelCase_      =  self.dummy_cond_unet_upscale
   lowerCamelCase_      =  DDPMScheduler()
   lowerCamelCase_      =  DDIMScheduler(prediction_type="v_prediction"     )
   lowerCamelCase_      =  self.dummy_vae
   lowerCamelCase_      =  self.dummy_text_encoder
   lowerCamelCase_      =  CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"     )

   lowerCamelCase_      =  self.dummy_image.cpu().permute(0			,  2			,  3			,  1     )[0]
   lowerCamelCase_      =  Image.fromarray(np.uinta(UpperCamelCase     )     ).convert("RGB"     ).resize((64, 64)     )

   # make sure here that pndm scheduler skips prk
   lowerCamelCase_      =  StableDiffusionUpscalePipeline(
       unet=UpperCamelCase			,  low_res_scheduler=UpperCamelCase			,  scheduler=UpperCamelCase			,  vae=UpperCamelCase			,  text_encoder=UpperCamelCase			,  tokenizer=UpperCamelCase			,  max_noise_level=350			,  )
   lowerCamelCase_      =  sd_pipe.to(UpperCamelCase     )
   sd_pipe.set_progress_bar_config(disable=UpperCamelCase     )

   lowerCamelCase_      =  "A painting of a squirrel eating a burger"
   lowerCamelCase_      =  torch.Generator(device=UpperCamelCase     ).manual_seed(0     )
   lowerCamelCase_      =  sd_pipe(
       [prompt]			,  image=UpperCamelCase			,  generator=UpperCamelCase			,  guidance_scale=6.0			,  noise_level=20			,  num_inference_steps=2			,  output_type="np"			,  )

   lowerCamelCase_      =  output.images

   lowerCamelCase_      =  torch.Generator(device=UpperCamelCase     ).manual_seed(0     )
   lowerCamelCase_      =  sd_pipe(
       [prompt]			,  image=UpperCamelCase			,  generator=UpperCamelCase			,  guidance_scale=6.0			,  noise_level=20			,  num_inference_steps=2			,  output_type="np"			,  return_dict=UpperCamelCase			,  )[0]

   lowerCamelCase_      =  image[0, -3:, -3:, -1]
   lowerCamelCase_      =  image_from_tuple[0, -3:, -3:, -1]

   lowerCamelCase_      =  low_res_image.size[0] * 4
   assert image.shape == (1, expected_height_width, expected_height_width, 3)
   lowerCamelCase_      =  np.array([0.3_113, 0.3_910, 0.4_272, 0.4_859, 0.5_061, 0.4_652, 0.5_362, 0.5_715, 0.5_661]     )

   assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1e-2
   assert np.abs(image_from_tuple_slice.flatten() - expected_slice     ).max() < 1e-2






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  "cpu"  # ensure determinism for the device-dependent torch.Generator
   lowerCamelCase_      =  self.dummy_cond_unet_upscale
   lowerCamelCase_      =  DDPMScheduler()
   lowerCamelCase_      =  DDIMScheduler(prediction_type="v_prediction"     )
   lowerCamelCase_      =  self.dummy_vae
   lowerCamelCase_      =  self.dummy_text_encoder
   lowerCamelCase_      =  CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"     )

   lowerCamelCase_      =  self.dummy_image.cpu().permute(0			,  2			,  3			,  1     )[0]
   lowerCamelCase_      =  Image.fromarray(np.uinta(UpperCamelCase     )     ).convert("RGB"     ).resize((64, 64)     )

   # make sure here that pndm scheduler skips prk
   lowerCamelCase_      =  StableDiffusionUpscalePipeline(
       unet=UpperCamelCase			,  low_res_scheduler=UpperCamelCase			,  scheduler=UpperCamelCase			,  vae=UpperCamelCase			,  text_encoder=UpperCamelCase			,  tokenizer=UpperCamelCase			,  max_noise_level=350			,  )
   lowerCamelCase_      =  sd_pipe.to(UpperCamelCase     )
   sd_pipe.set_progress_bar_config(disable=UpperCamelCase     )

   lowerCamelCase_      =  "A painting of a squirrel eating a burger"
   lowerCamelCase_      =  sd_pipe(
       2 * [prompt]			,  image=2 * [low_res_image]			,  guidance_scale=6.0			,  noise_level=20			,  num_inference_steps=2			,  output_type="np"			,  )
   lowerCamelCase_      =  output.images
   assert image.shape[0] == 2

   lowerCamelCase_      =  torch.Generator(device=UpperCamelCase     ).manual_seed(0     )
   lowerCamelCase_      =  sd_pipe(
       [prompt]			,  image=UpperCamelCase			,  generator=UpperCamelCase			,  num_images_per_prompt=2			,  guidance_scale=6.0			,  noise_level=20			,  num_inference_steps=2			,  output_type="np"			,  )
   lowerCamelCase_      =  output.images
   assert image.shape[0] == 2







  @unittest.skipIf(torch_device != "cuda"			,  "This test requires a GPU"     )
  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  self.dummy_cond_unet_upscale
   lowerCamelCase_      =  DDPMScheduler()
   lowerCamelCase_      =  DDIMScheduler(prediction_type="v_prediction"     )
   lowerCamelCase_      =  self.dummy_vae
   lowerCamelCase_      =  self.dummy_text_encoder
   lowerCamelCase_      =  CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"     )

   lowerCamelCase_      =  self.dummy_image.cpu().permute(0			,  2			,  3			,  1     )[0]
   lowerCamelCase_      =  Image.fromarray(np.uinta(UpperCamelCase     )     ).convert("RGB"     ).resize((64, 64)     )

   # put models in fp16, except vae as it overflows in fp16
   lowerCamelCase_      =  unet.half()
   lowerCamelCase_      =  text_encoder.half()

   # make sure here that pndm scheduler skips prk
   lowerCamelCase_      =  StableDiffusionUpscalePipeline(
       unet=UpperCamelCase			,  low_res_scheduler=UpperCamelCase			,  scheduler=UpperCamelCase			,  vae=UpperCamelCase			,  text_encoder=UpperCamelCase			,  tokenizer=UpperCamelCase			,  max_noise_level=350			,  )
   lowerCamelCase_      =  sd_pipe.to(UpperCamelCase     )
   sd_pipe.set_progress_bar_config(disable=UpperCamelCase     )

   lowerCamelCase_      =  "A painting of a squirrel eating a burger"
   lowerCamelCase_      =  torch.manual_seed(0     )
   lowerCamelCase_      =  sd_pipe(
       [prompt]			,  image=UpperCamelCase			,  generator=UpperCamelCase			,  num_inference_steps=2			,  output_type="np"			,  ).images

   lowerCamelCase_      =  low_res_image.size[0] * 4
   assert image.shape == (1, expected_height_width, expected_height_width, 3)





@slow
@require_torch_gpu
class      snake_case    (    unittest.TestCase			):





  """simple docstring"""




  def   snake_case		(    self     ):







   """simple docstring"""
   # clean up the VRAM after each test
   super().tearDown()
   gc.collect()
   torch.cuda.empty_cache()






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  load_image(
       "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
       "/sd2-upscale/low_res_cat.png"     )
   lowerCamelCase_      =  load_numpy(
       "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"
       "/upsampled_cat.npy"     )

   lowerCamelCase_      =  "stabilityai/stable-diffusion-x4-upscaler"
   lowerCamelCase_      =  StableDiffusionUpscalePipeline.from_pretrained(UpperCamelCase     )
   pipe.to(UpperCamelCase     )
   pipe.set_progress_bar_config(disable=UpperCamelCase     )
   pipe.enable_attention_slicing()

   lowerCamelCase_      =  "a cat sitting on a park bench"

   lowerCamelCase_      =  torch.manual_seed(0     )
   lowerCamelCase_      =  pipe(
       prompt=UpperCamelCase			,  image=UpperCamelCase			,  generator=UpperCamelCase			,  output_type="np"			,  )
   lowerCamelCase_      =  output.images[0]

   assert image.shape == (512, 512, 3)
   assert np.abs(expected_image - image     ).max() < 1e-3






  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  load_image(
       "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
       "/sd2-upscale/low_res_cat.png"     )
   lowerCamelCase_      =  load_numpy(
       "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"
       "/upsampled_cat_fp16.npy"     )

   lowerCamelCase_      =  "stabilityai/stable-diffusion-x4-upscaler"
   lowerCamelCase_      =  StableDiffusionUpscalePipeline.from_pretrained(
       UpperCamelCase			,  torch_dtype=torch.floataa			,  )
   pipe.to(UpperCamelCase     )
   pipe.set_progress_bar_config(disable=UpperCamelCase     )
   pipe.enable_attention_slicing()

   lowerCamelCase_      =  "a cat sitting on a park bench"

   lowerCamelCase_      =  torch.manual_seed(0     )
   lowerCamelCase_      =  pipe(
       prompt=UpperCamelCase			,  image=UpperCamelCase			,  generator=UpperCamelCase			,  output_type="np"			,  )
   lowerCamelCase_      =  output.images[0]

   assert image.shape == (512, 512, 3)
   assert np.abs(expected_image - image     ).max() < 5e-1







  def   snake_case		(    self     ):







   """simple docstring"""
   torch.cuda.empty_cache()
   torch.cuda.reset_max_memory_allocated()
   torch.cuda.reset_peak_memory_stats()

   lowerCamelCase_      =  load_image(
       "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
       "/sd2-upscale/low_res_cat.png"     )

   lowerCamelCase_      =  "stabilityai/stable-diffusion-x4-upscaler"
   lowerCamelCase_      =  StableDiffusionUpscalePipeline.from_pretrained(
       UpperCamelCase			,  torch_dtype=torch.floataa			,  )
   pipe.to(UpperCamelCase     )
   pipe.set_progress_bar_config(disable=UpperCamelCase     )
   pipe.enable_attention_slicing(1     )
   pipe.enable_sequential_cpu_offload()

   lowerCamelCase_      =  "a cat sitting on a park bench"

   lowerCamelCase_      =  torch.manual_seed(0     )
   lowerCamelCase_      =  pipe(
       prompt=UpperCamelCase			,  image=UpperCamelCase			,  generator=UpperCamelCase			,  num_inference_steps=5			,  output_type="np"			,  )

   lowerCamelCase_      =  torch.cuda.max_memory_allocated()
   # make sure that less than 2.9 GB is allocated
   assert mem_bytes < 2.9 * 10**9






import argparse

import torch

from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()

def 						UpperCamelCase					(lowercase_: int    ,      lowercase_: Dict    ,      lowercase_: Tuple					)     ->      Any:
	# Construct model
	if gpta_config_file == "":
		A__							:    Dict        =		GPTaConfig()
	else:
		A__							:    List[Any]        =		GPTaConfig.from_json_file(lowercase_					)
	A__							:    Tuple        =		GPTaModel(lowercase_					)

	# Load weights from numpy
	load_tf_weights_in_gpta(lowercase_    ,      lowercase_    ,      lowercase_					)

	# Save pytorch-model
	A__							:    Optional[Any]        =		pytorch_dump_folder_path + """/""" + WEIGHTS_NAME
	A__							:    Optional[Any]        =		pytorch_dump_folder_path + """/""" + CONFIG_NAME
	print(f"""Save PyTorch model to {pytorch_weights_dump_path}"""					)
	torch.save(model.state_dict()    ,      lowercase_					)
	print(f"""Save configuration file to {pytorch_config_dump_path}"""					)
	with open(lowercase_    ,      """w"""    ,      encoding="""utf-8"""					) as f:
		f.write(config.to_json_string()					)


if __name__ == "__main__":
	A_       : Union[str, Any]         =			argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    '--gpt2_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
	)
	parser.add_argument(
	    '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
	)
	parser.add_argument(
	    '--gpt2_config_file',
	    default='',
	    type=str,
	    help=(
	        'An optional config json file corresponding to the pre-trained OpenAI model. \n'
	        'This specifies the model architecture.'
	    ),
	)
	A_       : str         =			parser.parse_args()
	convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)



