






import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()



class   lowerCamelCase      (_snake_case ,  _snake_case ,  unittest.TestCase			):







		'''simple docstring'''



		_snake_case    :    Dict       						=  StableDiffusionSAGPipeline
		_snake_case    :    Optional[int]       						=  TEXT_TO_IMAGE_PARAMS
		_snake_case    :    str       						=  TEXT_TO_IMAGE_BATCH_PARAMS
		_snake_case    :    Optional[Any]       						=  TEXT_TO_IMAGE_IMAGE_PARAMS
		_snake_case    :    Optional[Any]       						=  TEXT_TO_IMAGE_IMAGE_PARAMS
		_snake_case    :    str       						=  False



		def 					__UpperCAmelCase			(							self   )	->							Optional[int]:
							torch.manual_seed(0   )
							UpperCAmelCase_    :      List[Any]            =						UNetaDConditionModel(
							    block_out_channels=(3_2, 6_4)				,		layers_per_block=2				,		sample_size=3_2				,		in_channels=4				,		out_channels=4				,		down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')				,		up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')				,		cross_attention_dim=3_2				,		)
							UpperCAmelCase_    :      Any            =						DDIMScheduler(
							    beta_start=0.0_00_85				,		beta_end=0.0_12				,		beta_schedule='scaled_linear'				,		clip_sample=_UpperCamelCase				,		set_alpha_to_one=_UpperCamelCase				,		)
							torch.manual_seed(0   )
							UpperCAmelCase_    :      List[str]            =						AutoencoderKL(
							    block_out_channels=[3_2, 6_4]				,		in_channels=3				,		out_channels=3				,		down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']				,		up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']				,		latent_channels=4				,		)
							torch.manual_seed(0   )
							UpperCAmelCase_    :      Optional[int]            =						CLIPTextConfig(
							    bos_token_id=0				,		eos_token_id=2				,		hidden_size=3_2				,		intermediate_size=3_7				,		layer_norm_eps=1E-05				,		num_attention_heads=4				,		num_hidden_layers=5				,		pad_token_id=1				,		vocab_size=1_0_0_0				,		)
							UpperCAmelCase_    :      int            =						CLIPTextModel(_UpperCamelCase   )
							UpperCAmelCase_    :      Dict            =						CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'   )

							UpperCAmelCase_    :      List[str]            =						{
							    'unet': unet,
							    'scheduler': scheduler,
							    'vae': vae,
							    'text_encoder': text_encoder,
							    'tokenizer': tokenizer,
							    'safety_checker': None,
							    'feature_extractor': None,
							}
							return components



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=0   )	->							Dict:
							if str(_UpperCamelCase   ).startswith('mps'   ):
												UpperCAmelCase_    :      Optional[int]            =						torch.manual_seed(_UpperCamelCase   )
							else:
												UpperCAmelCase_    :      List[Any]            =						torch.Generator(device=_UpperCamelCase   ).manual_seed(_UpperCamelCase   )
							UpperCAmelCase_    :      Tuple            =						{
							    'prompt': '.',
							    'generator': generator,
							    'num_inference_steps': 2,
							    'guidance_scale': 1.0,
							    'sag_scale': 1.0,
							    'output_type': 'numpy',
							}
							return inputs



		def 					__UpperCAmelCase			(							self   )	->							Any:
							super().test_inference_batch_single_identical(expected_max_diff=3E-3   )





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







		'''simple docstring'''



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



		def 					__UpperCAmelCase			(							self   )	->							int:
							UpperCAmelCase_    :      Any            =						StableDiffusionSAGPipeline.from_pretrained('CompVis/stable-diffusion-v1-4'   )
							UpperCAmelCase_    :      str            =						sag_pipe.to(_UpperCamelCase   )
							sag_pipe.set_progress_bar_config(disable=_UpperCamelCase   )

							UpperCAmelCase_    :      Dict            =						'.'
							UpperCAmelCase_    :      Optional[int]            =						torch.manual_seed(0   )
							UpperCAmelCase_    :      List[str]            =						sag_pipe(
							    [prompt]				,		generator=_UpperCamelCase				,		guidance_scale=7.5				,		sag_scale=1.0				,		num_inference_steps=2_0				,		output_type='np'   )

							UpperCAmelCase_    :      Tuple            =						output.images

							UpperCAmelCase_    :      Optional[Any]            =						image[0, -3:, -3:, -1]

							assert image.shape == (1, 5_1_2, 5_1_2, 3)
							UpperCAmelCase_    :      List[str]            =						np.array([0.15_68, 0.17_38, 0.16_95, 0.16_93, 0.15_07, 0.17_05, 0.15_47, 0.17_51, 0.19_49]   )

							assert np.abs(image_slice.flatten() - expected_slice   ).max() < 5E-2



		def 					__UpperCAmelCase			(							self   )	->							Optional[Any]:
							UpperCAmelCase_    :      int            =						StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base'   )
							UpperCAmelCase_    :      int            =						sag_pipe.to(_UpperCamelCase   )
							sag_pipe.set_progress_bar_config(disable=_UpperCamelCase   )

							UpperCAmelCase_    :      List[Any]            =						'.'
							UpperCAmelCase_    :      Union[str, Any]            =						torch.manual_seed(0   )
							UpperCAmelCase_    :      str            =						sag_pipe(
							    [prompt]				,		generator=_UpperCamelCase				,		guidance_scale=7.5				,		sag_scale=1.0				,		num_inference_steps=2_0				,		output_type='np'   )

							UpperCAmelCase_    :      Any            =						output.images

							UpperCAmelCase_    :      str            =						image[0, -3:, -3:, -1]

							assert image.shape == (1, 5_1_2, 5_1_2, 3)
							UpperCAmelCase_    :      Optional[Any]            =						np.array([0.34_59, 0.28_76, 0.25_37, 0.30_02, 0.26_71, 0.21_60, 0.30_26, 0.22_62, 0.23_71]   )

							assert np.abs(image_slice.flatten() - expected_slice   ).max() < 5E-2



		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							UpperCAmelCase_    :      Tuple            =						StableDiffusionSAGPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base'   )
							UpperCAmelCase_    :      Optional[int]            =						sag_pipe.to(_UpperCamelCase   )
							sag_pipe.set_progress_bar_config(disable=_UpperCamelCase   )

							UpperCAmelCase_    :      Any            =						'.'
							UpperCAmelCase_    :      int            =						torch.manual_seed(0   )
							UpperCAmelCase_    :      str            =						sag_pipe(
							    [prompt]				,		width=7_6_8				,		height=5_1_2				,		generator=_UpperCamelCase				,		guidance_scale=7.5				,		sag_scale=1.0				,		num_inference_steps=2_0				,		output_type='np'				,		)

							UpperCAmelCase_    :      Dict            =						output.images

							assert image.shape == (1, 5_1_2, 7_6_8, 3)












import argparse
import os
import pickle
import sys

import torch

from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl
from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils
from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()

# We do this to be able to load python 2 datasets pickles
# See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918
__A        =    data_utils.TransfoXLTokenizer
__A        =    data_utils.TransfoXLCorpus
__A        =    data_utils
__A        =    data_utils







def 			lowerCamelCase_		(       UpperCamelCase__				:      List[str] ,     UpperCamelCase__				:      Optional[int] ,     UpperCamelCase__				:      Union[str, Any] ,     UpperCamelCase__				:      Optional[Any]      )     ->       Union[str, Any]:





				"""simple docstring"""


				if transfo_xl_dataset_file:
								# Convert a pre-processed corpus (see original TensorFlow repo)
								with open(UpperCamelCase__ ,     'rb'      ) as fp:
												__lowerCamelCase		       =    pickle.load(UpperCamelCase__ ,     encoding='latin1'      )
								# Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term)
								__lowerCamelCase		       =    pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['pretrained_vocab_file']
								print(F"""Save vocabulary to {pytorch_vocab_dump_path}"""      )
								__lowerCamelCase		       =    corpus.vocab.__dict__
								torch.save(UpperCamelCase__ ,     UpperCamelCase__      )

								__lowerCamelCase		       =    corpus.__dict__
								corpus_dict_no_vocab.pop('vocab' ,     UpperCamelCase__      )
								__lowerCamelCase		       =    pytorch_dump_folder_path + '/' + CORPUS_NAME
								print(F"""Save dataset to {pytorch_dataset_dump_path}"""      )
								torch.save(UpperCamelCase__ ,     UpperCamelCase__      )

				if tf_checkpoint_path:
								# Convert a pre-trained TensorFlow model
								__lowerCamelCase		       =    os.path.abspath(UpperCamelCase__      )
								__lowerCamelCase		       =    os.path.abspath(UpperCamelCase__      )

								print(F"""Converting Transformer XL checkpoint from {tf_path} with config at {config_path}."""      )
								# Initialise PyTorch model
								if transfo_xl_config_file == "":
												__lowerCamelCase		       =    TransfoXLConfig()
								else:
												__lowerCamelCase		       =    TransfoXLConfig.from_json_file(UpperCamelCase__      )
								print(F"""Building PyTorch model from configuration: {config}"""      )
								__lowerCamelCase		       =    TransfoXLLMHeadModel(UpperCamelCase__      )

								__lowerCamelCase		       =    load_tf_weights_in_transfo_xl(UpperCamelCase__ ,     UpperCamelCase__ ,     UpperCamelCase__      )
								# Save pytorch-model
								__lowerCamelCase		       =    os.path.join(UpperCamelCase__ ,     UpperCamelCase__      )
								__lowerCamelCase		       =    os.path.join(UpperCamelCase__ ,     UpperCamelCase__      )
								print(F"""Save PyTorch model to {os.path.abspath(UpperCamelCase__      )}"""      )
								torch.save(model.state_dict() ,     UpperCamelCase__      )
								print(F"""Save configuration file to {os.path.abspath(UpperCamelCase__      )}"""      )
								with open(UpperCamelCase__ ,     'w' ,     encoding='utf-8'      ) as f:
												f.write(config.to_json_string()      )


if __name__ == "__main__":
					__A        =    argparse.ArgumentParser()
					parser.add_argument(
					    "--pytorch_dump_folder_path",
					    default=None,
					    type=str,
					    required=True,
					    help="Path to the folder to store the PyTorch model or dataset/vocab.",
					)
					parser.add_argument(
					    "--tf_checkpoint_path",
					    default="",
					    type=str,
					    help="An optional path to a TensorFlow checkpoint path to be converted.",
					)
					parser.add_argument(
					    "--transfo_xl_config_file",
					    default="",
					    type=str,
					    help=(
					        "An optional config json file corresponding to the pre-trained BERT model. \n"
					        "This specifies the model architecture."
					    ),
					)
					parser.add_argument(
					    "--transfo_xl_dataset_file",
					    default="",
					    type=str,
					    help="An optional dataset file to be converted in a vocabulary.",
					)
					__A        =    parser.parse_args()
					convert_transfo_xl_checkpoint_to_pytorch(
					    args.tf_checkpoint_path,
					    args.transfo_xl_config_file,
					    args.pytorch_dump_folder_path,
					    args.transfo_xl_dataset_file,
					)


