




'''simple docstring'''
import gc
import unittest

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

from diffusers import TransformeraDModel, VQDiffusionPipeline, VQDiffusionScheduler, VQModel
from diffusers.pipelines.vq_diffusion.pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings
from diffusers.utils import load_numpy, slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu


_SCREAMING_SNAKE_CASE			=						False
class   __UpperCAmelCase				(     unittest.TestCase				):



					'''simple docstring'''





					def     __snake_case						(	self     :						Optional[Any])    ->							Optional[int]:
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()



					@property
					def     __snake_case						(	self     :						Any)    ->							int:
								return 12



					@property
					def     __snake_case						(	self     :						Optional[int])    ->							str:
								return 12



					@property
					def     __snake_case						(	self     :						int)    ->							Tuple:
								return 32



					@property
					def     __snake_case						(	self     :						Optional[int])    ->							List[Any]:
								torch.manual_seed(0)
								A_									= VQModel(
								    block_out_channels=[32, 64]      ,       in_channels=3      ,       out_channels=3      ,       down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']      ,       up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']      ,       latent_channels=3      ,       num_vq_embeddings=self.num_embed      ,       vq_embed_dim=3      ,       )
								return model



					@property
					def     __snake_case						(	self     :						Tuple)    ->							str:
								A_									= CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip')
								return tokenizer



					@property
					def     __snake_case						(	self     :						List[Any])    ->							List[str]:
								torch.manual_seed(0)
								A_									= CLIPTextConfig(
								    bos_token_id=0      ,       eos_token_id=2      ,       hidden_size=self.text_embedder_hidden_size      ,       intermediate_size=37      ,       layer_norm_eps=1E-05      ,       num_attention_heads=4      ,       num_hidden_layers=5      ,       pad_token_id=1      ,       vocab_size=1_000      ,       )
								return CLIPTextModel(_lowercase)



					@property
					def     __snake_case						(	self     :						Tuple)    ->							List[Any]:
								torch.manual_seed(0)

								A_									= 12
								A_									= 12

								A_									= {
								    'attention_bias': True,
								    'cross_attention_dim': 32,
								    'attention_head_dim': height * width,
								    'num_attention_heads': 1,
								    'num_vector_embeds': self.num_embed,
								    'num_embeds_ada_norm': self.num_embeds_ada_norm,
								    'norm_num_groups': 32,
								    'sample_size': width,
								    'activation_fn': 'geglu-approximate',
								}

								A_									= TransformeraDModel(**_lowercase)
								return model



					def     __snake_case						(	self     :						Optional[Any])    ->							Dict:
								A_									= 'cpu'

								A_									= self.dummy_vqvae
								A_									= self.dummy_text_encoder
								A_									= self.dummy_tokenizer
								A_									= self.dummy_transformer
								A_									= VQDiffusionScheduler(self.num_embed)
								A_									= LearnedClassifierFreeSamplingEmbeddings(learnable=_lowercase)

								A_									= VQDiffusionPipeline(
								    vqvae=_lowercase      ,       text_encoder=_lowercase      ,       tokenizer=_lowercase      ,       transformer=_lowercase      ,       scheduler=_lowercase      ,       learned_classifier_free_sampling_embeddings=_lowercase      ,       )
								A_									= pipe.to(_lowercase)
								pipe.set_progress_bar_config(disable=_lowercase)

								A_									= 'teddy bear playing in the pool'

								A_									= torch.Generator(device=_lowercase).manual_seed(0)
								A_									= pipe([prompt]      ,       generator=_lowercase      ,       num_inference_steps=2      ,       output_type='np')
								A_									= output.images

								A_									= torch.Generator(device=_lowercase).manual_seed(0)
								A_									= pipe(
								    [prompt]      ,       generator=_lowercase      ,       output_type='np'      ,       return_dict=_lowercase      ,       num_inference_steps=2)[0]

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

								assert image.shape == (1, 24, 24, 3)

								A_									= np.array([0.65_51, 0.61_68, 0.50_08, 0.56_76, 0.56_59, 0.42_95, 0.60_73, 0.55_99, 0.49_92])

								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     :						Tuple)    ->							Any:
								A_									= 'cpu'

								A_									= self.dummy_vqvae
								A_									= self.dummy_text_encoder
								A_									= self.dummy_tokenizer
								A_									= self.dummy_transformer
								A_									= VQDiffusionScheduler(self.num_embed)
								A_									= LearnedClassifierFreeSamplingEmbeddings(
								    learnable=_lowercase      ,       hidden_size=self.text_embedder_hidden_size      ,       length=tokenizer.model_max_length)

								A_									= VQDiffusionPipeline(
								    vqvae=_lowercase      ,       text_encoder=_lowercase      ,       tokenizer=_lowercase      ,       transformer=_lowercase      ,       scheduler=_lowercase      ,       learned_classifier_free_sampling_embeddings=_lowercase      ,       )
								A_									= pipe.to(_lowercase)
								pipe.set_progress_bar_config(disable=_lowercase)

								A_									= 'teddy bear playing in the pool'

								A_									= torch.Generator(device=_lowercase).manual_seed(0)
								A_									= pipe([prompt]      ,       generator=_lowercase      ,       num_inference_steps=2      ,       output_type='np')
								A_									= output.images

								A_									= torch.Generator(device=_lowercase).manual_seed(0)
								A_									= pipe(
								    [prompt]      ,       generator=_lowercase      ,       output_type='np'      ,       return_dict=_lowercase      ,       num_inference_steps=2)[0]

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

								assert image.shape == (1, 24, 24, 3)

								A_									= np.array([0.66_93, 0.60_75, 0.49_59, 0.57_01, 0.55_83, 0.43_33, 0.61_71, 0.56_84, 0.49_88])

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





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



					'''simple docstring'''





					def     __snake_case						(	self     :						str)    ->							str:
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()



					def     __snake_case						(	self     :						List[Any])    ->							Tuple:
								A_									= load_numpy(
								    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
								    '/vq_diffusion/teddy_bear_pool_classifier_free_sampling.npy')

								A_									= VQDiffusionPipeline.from_pretrained('microsoft/vq-diffusion-ithq')
								A_									= pipeline.to(_lowercase)
								pipeline.set_progress_bar_config(disable=_lowercase)

								# requires GPU generator for gumbel softmax
								# don't use GPU generator in tests though
								A_									= torch.Generator(device=_lowercase).manual_seed(0)
								A_									= pipeline(
								    'teddy bear playing in the pool'      ,       num_images_per_prompt=1      ,       generator=_lowercase      ,       output_type='np'      ,       )

								A_									= output.images[0]

								assert image.shape == (256, 256, 3)
								assert np.abs(expected_image - image).max() < 2.0










'''simple docstring'''
import argparse

import torch

from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()





def 			lowerCamelCase(							SCREAMING_SNAKE_CASE_       ,SCREAMING_SNAKE_CASE_       ,SCREAMING_SNAKE_CASE_     )		->		str:
			# Construct model
			if openai_config_file == "":
						A_									= OpenAIGPTConfig()
			else:
						A_									= OpenAIGPTConfig.from_json_file(SCREAMING_SNAKE_CASE_     )
			A_									= OpenAIGPTModel(SCREAMING_SNAKE_CASE_     )

			# Load weights from numpy
			load_tf_weights_in_openai_gpt(SCREAMING_SNAKE_CASE_       ,SCREAMING_SNAKE_CASE_       ,SCREAMING_SNAKE_CASE_     )

			# Save pytorch-model
			A_									= pytorch_dump_folder_path + '/' + WEIGHTS_NAME
			A_									= 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__":
							_SCREAMING_SNAKE_CASE			=						argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    "--openai_checkpoint_folder_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(
							    "--openai_config_file",
							    default="",
							    type=str,
							    help=(
							        "An optional config json file corresponding to the pre-trained OpenAI model. \n"
							        "This specifies the model architecture."
							    ),
							)
							_SCREAMING_SNAKE_CASE			=						parser.parse_args()
							convert_openai_checkpoint_to_pytorch(
							    args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path
							)




