





"""simple docstring"""






import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPanoramaPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

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()




@skip_mps
class 				__lowercase			(			_UpperCamelCase   ,      _UpperCamelCase   ,      unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  StableDiffusionPanoramaPipeline
					__lowerCAmelCase  =  TEXT_TO_IMAGE_PARAMS
					__lowerCAmelCase  =  TEXT_TO_IMAGE_BATCH_PARAMS
					__lowerCAmelCase  =  TEXT_TO_IMAGE_IMAGE_PARAMS
					__lowerCAmelCase  =  TEXT_TO_IMAGE_IMAGE_PARAMS







					def  _lowerCamelCase		(						self     ):
									torch.manual_seed(0     )
									__a :						List[Any]			       =  UNetaDConditionModel(
									    block_out_channels=(32, 64)	,			layers_per_block=1	,			sample_size=32	,			in_channels=4	,			out_channels=4	,			down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')	,			up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')	,			cross_attention_dim=32	,			)
									__a :						str			       =  DDIMScheduler()
									torch.manual_seed(0     )
									__a :						List[str]			       =  AutoencoderKL(
									    block_out_channels=[32, 64]	,			in_channels=3	,			out_channels=3	,			down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']	,			up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']	,			latent_channels=4	,			)
									torch.manual_seed(0     )
									__a :						Optional[int]			       =  CLIPTextConfig(
									    bos_token_id=0	,			eos_token_id=2	,			hidden_size=32	,			intermediate_size=37	,			layer_norm_eps=1e-0_5	,			num_attention_heads=4	,			num_hidden_layers=5	,			pad_token_id=1	,			vocab_size=1000	,			)
									__a :						List[Any]			       =  CLIPTextModel(_UpperCAmelCase     )
									__a :						int			       =  CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''     )

									__a :						Union[str, Any]			       =  {
									    '''unet''': unet,
									    '''scheduler''': scheduler,
									    '''vae''': vae,
									    '''text_encoder''': text_encoder,
									    '''tokenizer''': tokenizer,
									    '''safety_checker''': None,
									    '''feature_extractor''': None,
									}
									return components







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase=0     ):
									__a :						Optional[int]			       =  torch.manual_seed(_UpperCAmelCase     )
									__a :						Tuple			       =  {
									    '''prompt''': '''a photo of the dolomites''',
									    '''generator''': generator,
									    # Setting height and width to None to prevent OOMs on CPU.
									    '''height''': None,
									    '''width''': None,
									    '''num_inference_steps''': 1,
									    '''guidance_scale''': 6.0,
									    '''output_type''': '''numpy''',
									}
									return inputs







					def  _lowerCamelCase		(						self     ):
									__a :						Dict			       =  '''cpu'''  # ensure determinism for the device-dependent torch.Generator
									__a :						Union[str, Any]			       =  self.get_dummy_components()
									__a :						Optional[Any]			       =  StableDiffusionPanoramaPipeline(**_UpperCAmelCase     )
									__a :						Tuple			       =  sd_pipe.to(_UpperCAmelCase     )
									sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase     )

									__a :						Union[str, Any]			       =  self.get_dummy_inputs(_UpperCAmelCase     )
									__a :						Tuple			       =  sd_pipe(**_UpperCAmelCase     ).images
									__a :						Optional[Any]			       =  image[0, -3:, -3:, -1]
									assert image.shape == (1, 64, 64, 3)

									__a :						List[str]			       =  np.array([0.6_1_8_6, 0.5_3_7_4, 0.4_9_1_5, 0.4_1_3_5, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_7, 0.4_7_5_7]     )

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







					def  _lowerCamelCase		(						self     ):
									super().test_inference_batch_consistent(batch_sizes=[1, 2]     )







					def  _lowerCamelCase		(						self     ):
									super().test_inference_batch_single_identical(batch_size=2	,			expected_max_diff=3.2_5e-3     )







					def  _lowerCamelCase		(						self     ):
									__a :						str			       =  '''cpu'''  # ensure determinism for the device-dependent torch.Generator
									__a :						List[Any]			       =  self.get_dummy_components()
									__a :						int			       =  StableDiffusionPanoramaPipeline(**_UpperCAmelCase     )
									__a :						Union[str, Any]			       =  sd_pipe.to(_UpperCAmelCase     )
									sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase     )

									__a :						Optional[int]			       =  self.get_dummy_inputs(_UpperCAmelCase     )
									__a :						Dict			       =  '''french fries'''
									__a :						List[Any]			       =  sd_pipe(**_UpperCAmelCase	,			negative_prompt=_UpperCAmelCase     )
									__a :						List[str]			       =  output.images
									__a :						str			       =  image[0, -3:, -3:, -1]

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

									__a :						int			       =  np.array([0.6_1_8_7, 0.5_3_7_5, 0.4_9_1_5, 0.4_1_3_6, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_6, 0.4_7_5_7]     )

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







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  '''cpu'''  # ensure determinism for the device-dependent torch.Generator
									__a :						List[Any]			       =  self.get_dummy_components()
									__a :						Union[str, Any]			       =  StableDiffusionPanoramaPipeline(**_UpperCAmelCase     )
									__a :						Any			       =  sd_pipe.to(_UpperCAmelCase     )
									sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase     )

									__a :						str			       =  self.get_dummy_inputs(_UpperCAmelCase     )
									__a :						List[str]			       =  sd_pipe(**_UpperCAmelCase	,			view_batch_size=2     )
									__a :						Optional[Any]			       =  output.images
									__a :						Dict			       =  image[0, -3:, -3:, -1]

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

									__a :						Optional[int]			       =  np.array([0.6_1_8_7, 0.5_3_7_5, 0.4_9_1_5, 0.4_1_3_6, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_6, 0.4_7_5_7]     )

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







					def  _lowerCamelCase		(						self     ):
									__a :						int			       =  '''cpu'''  # ensure determinism for the device-dependent torch.Generator
									__a :						Any			       =  self.get_dummy_components()
									__a :						Optional[Any]			       =  EulerAncestralDiscreteScheduler(
									    beta_start=0.0_0_0_8_5	,			beta_end=0.0_1_2	,			beta_schedule='''scaled_linear'''     )
									__a :						List[Any]			       =  StableDiffusionPanoramaPipeline(**_UpperCAmelCase     )
									__a :						Optional[Any]			       =  sd_pipe.to(_UpperCAmelCase     )
									sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase     )

									__a :						int			       =  self.get_dummy_inputs(_UpperCAmelCase     )
									__a :						str			       =  sd_pipe(**_UpperCAmelCase     ).images
									__a :						Union[str, Any]			       =  image[0, -3:, -3:, -1]

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

									__a :						int			       =  np.array([0.4_0_2_4, 0.6_5_1_0, 0.4_9_0_1, 0.5_3_7_8, 0.5_8_1_3, 0.5_6_2_2, 0.4_7_9_5, 0.4_4_6_7, 0.4_9_5_2]     )

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







					def  _lowerCamelCase		(						self     ):
									__a :						List[str]			       =  '''cpu'''  # ensure determinism for the device-dependent torch.Generator
									__a :						Dict			       =  self.get_dummy_components()
									__a :						str			       =  PNDMScheduler(
									    beta_start=0.0_0_0_8_5	,			beta_end=0.0_1_2	,			beta_schedule='''scaled_linear'''	,			skip_prk_steps=_UpperCAmelCase     )
									__a :						Dict			       =  StableDiffusionPanoramaPipeline(**_UpperCAmelCase     )
									__a :						Dict			       =  sd_pipe.to(_UpperCAmelCase     )
									sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase     )

									__a :						str			       =  self.get_dummy_inputs(_UpperCAmelCase     )
									__a :						Union[str, Any]			       =  sd_pipe(**_UpperCAmelCase     ).images
									__a :						Dict			       =  image[0, -3:, -3:, -1]

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

									__a :						Union[str, Any]			       =  np.array([0.6_3_9_1, 0.6_2_9_1, 0.4_8_6_1, 0.5_1_3_4, 0.5_5_5_2, 0.4_5_7_8, 0.5_0_3_2, 0.5_0_2_3, 0.4_5_3_9]     )

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







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







					'''simple docstring'''




					def  _lowerCamelCase		(						self     ):
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase=0     ):
									__a :						List[Any]			       =  torch.manual_seed(_UpperCAmelCase     )
									__a :						Any			       =  {
									    '''prompt''': '''a photo of the dolomites''',
									    '''generator''': generator,
									    '''num_inference_steps''': 3,
									    '''guidance_scale''': 7.5,
									    '''output_type''': '''numpy''',
									}
									return inputs







					def  _lowerCamelCase		(						self     ):
									__a :						Union[str, Any]			       =  '''stabilityai/stable-diffusion-2-base'''
									__a :						List[str]			       =  DDIMScheduler.from_pretrained(_UpperCAmelCase	,			subfolder='''scheduler'''     )
									__a :						Optional[Any]			       =  StableDiffusionPanoramaPipeline.from_pretrained(_UpperCAmelCase	,			scheduler=_UpperCAmelCase	,			safety_checker=_UpperCAmelCase     )
									pipe.to(_UpperCAmelCase     )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase     )
									pipe.enable_attention_slicing()

									__a :						Dict			       =  self.get_inputs()
									__a :						Union[str, Any]			       =  pipe(**_UpperCAmelCase     ).images
									__a :						Tuple			       =  image[0, -3:, -3:, -1].flatten()

									assert image.shape == (1, 512, 2048, 3)

									__a :						Dict			       =  np.array(
									    [
									        0.3_6_9_6_8_3_9_2,
									        0.2_7_0_2_5_3_7_2,
									        0.3_2_4_4_6_7_6_6,
									        0.2_8_3_7_9_3_8_7,
									        0.3_6_3_6_3_2_7_4,
									        0.3_0_7_3_3_3_4_7,
									        0.2_7_1_0_0_0_2_7,
									        0.2_7_0_5_4_1_2_5,
									        0.2_5_5_3_6_0_9_6,
									    ]     )

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







					def  _lowerCamelCase		(						self     ):
									__a :						Dict			       =  StableDiffusionPanoramaPipeline.from_pretrained(
									    '''stabilityai/stable-diffusion-2-base'''	,			safety_checker=_UpperCAmelCase     )
									__a :						List[Any]			       =  LMSDiscreteScheduler.from_config(pipe.scheduler.config     )
									pipe.to(_UpperCAmelCase     )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase     )
									pipe.enable_attention_slicing()

									__a :						Union[str, Any]			       =  self.get_inputs()
									__a :						Union[str, Any]			       =  pipe(**_UpperCAmelCase     ).images
									__a :						List[Any]			       =  image[0, -3:, -3:, -1].flatten()

									assert image.shape == (1, 512, 2048, 3)

									__a :						int			       =  np.array(
									    [
									        [
									            0.0,
									            0.0,
									            0.0,
									            0.0,
									            0.0,
									            0.0,
									            0.0,
									            0.0,
									            0.0,
									        ]
									    ]     )

									assert np.abs(expected_slice - image_slice     ).max() < 1e-3







					def  _lowerCamelCase		(						self     ):
									__a :						List[str]			       =  0

									def callback_fn(_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ) -> None:
													__a :						List[Any]			       =  True
													nonlocal number_of_steps
													number_of_steps += 1
													if step == 1:
																	__a :						List[str]			       =  latents.detach().cpu().numpy()
																	assert latents.shape == (1, 4, 64, 256)
																	__a :						int			       =  latents[0, -3:, -3:, -1]

																	__a :						List[str]			       =  np.array(
																	    [
																	        0.1_8_6_8_1_8_6_9,
																	        0.3_3_9_0_7_8_1_6,
																	        0.5_3_6_1_2_7_6,
																	        0.1_4_4_3_2_8_6_5,
																	        -0.0_2_8_5_6_6_1_1,
																	        -0.7_3_9_4_1_1_2_3,
																	        0.2_3_3_9_7_9_8_7,
																	        0.4_7_3_2_2_6_8_2,
																	        -0.3_7_8_2_3_1_6_4,
																	    ]     )
																	assert np.abs(latents_slice.flatten() - expected_slice     ).max() < 5e-2
													elif step == 2:
																	__a :						str			       =  latents.detach().cpu().numpy()
																	assert latents.shape == (1, 4, 64, 256)
																	__a :						str			       =  latents[0, -3:, -3:, -1]

																	__a :						Union[str, Any]			       =  np.array(
																	    [
																	        0.1_8_5_3_9_6_4_5,
																	        0.3_3_9_8_7_2_4_8,
																	        0.5_3_7_8_5_5_9,
																	        0.1_4_4_3_7_1_4_2,
																	        -0.0_2_4_5_5_2_6_1,
																	        -0.7_3_3_8_3_1_7,
																	        0.2_3_9_9_0_7_5_5,
																	        0.4_7_3_5_6_2_7_2,
																	        -0.3_7_8_6_5_0_5,
																	    ]     )

																	assert np.abs(latents_slice.flatten() - expected_slice     ).max() < 5e-2

									__a :						Union[str, Any]			       =  False

									__a :						Optional[int]			       =  '''stabilityai/stable-diffusion-2-base'''
									__a :						Tuple			       =  DDIMScheduler.from_pretrained(_UpperCAmelCase	,			subfolder='''scheduler'''     )
									__a :						Union[str, Any]			       =  StableDiffusionPanoramaPipeline.from_pretrained(_UpperCAmelCase	,			scheduler=_UpperCAmelCase	,			safety_checker=_UpperCAmelCase     )
									__a :						Dict			       =  pipe.to(_UpperCAmelCase     )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase     )
									pipe.enable_attention_slicing()

									__a :						Optional[Any]			       =  self.get_inputs()
									pipe(**_UpperCAmelCase	,			callback=_UpperCAmelCase	,			callback_steps=1     )
									assert callback_fn.has_been_called
									assert number_of_steps == 3







					def  _lowerCamelCase		(						self     ):
									torch.cuda.empty_cache()
									torch.cuda.reset_max_memory_allocated()
									torch.cuda.reset_peak_memory_stats()

									__a :						str			       =  '''stabilityai/stable-diffusion-2-base'''
									__a :						Tuple			       =  DDIMScheduler.from_pretrained(_UpperCAmelCase	,			subfolder='''scheduler'''     )
									__a :						Optional[Any]			       =  StableDiffusionPanoramaPipeline.from_pretrained(_UpperCAmelCase	,			scheduler=_UpperCAmelCase	,			safety_checker=_UpperCAmelCase     )
									__a :						Optional[Any]			       =  pipe.to(_UpperCAmelCase     )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase     )
									pipe.enable_attention_slicing(1     )
									pipe.enable_sequential_cpu_offload()

									__a :						List[str]			       =  self.get_inputs()
									__a :						List[Any]			       =  pipe(**_UpperCAmelCase     )

									__a :						str			       =  torch.cuda.max_memory_allocated()
									# make sure that less than 5.2 GB is allocated
									assert mem_bytes < 5.5 * 10**9






"""simple docstring"""






import argparse

from torch import nn

# transformers_old should correspond to branch `save_old_prophetnet_model_structure` here
# original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively
from transformers_old.modeling_prophetnet import (
    ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld,
)
from transformers_old.modeling_xlm_prophetnet import (
    XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld,
)

from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging


A 			=  logging.get_logger(__name__)
logging.set_verbosity_info()

def     __A      (			a_						:str   ,       a_						:str)		->		List[Any]:
				if "xprophetnet" in prophetnet_checkpoint_path:
								__a :						List[str]			       =  XLMProphetNetForConditionalGenerationOld.from_pretrained(a_)
								__a       ,						__a :						int			       =  XLMProphetNetForConditionalGeneration.from_pretrained(
								    a_   ,       output_loading_info=a_)
				else:
								__a :						Optional[int]			       =  ProphetNetForConditionalGenerationOld.from_pretrained(a_)
								__a       ,						__a :						Optional[Any]			       =  ProphetNetForConditionalGeneration.from_pretrained(
								    a_   ,       output_loading_info=a_)

				__a :						Tuple			       =  ['''key_proj''', '''value_proj''', '''query_proj''']

				__a :						List[str]			       =  {
				    '''self_attn''': '''ngram_self_attn''',
				    '''cross_attn''': '''encoder_attn''',
				    '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''',
				    '''feed_forward_layer_norm''': '''final_layer_norm''',
				    '''feed_forward''': '''''',
				    '''intermediate''': '''fc1''',
				    '''output''': '''fc2''',
				    '''key_proj''': '''k_proj''',
				    '''query_proj''': '''q_proj''',
				    '''value_proj''': '''v_proj''',
				    '''word_embeddings''': '''embed_tokens''',
				    '''embeddings_layer_norm''': '''emb_layer_norm''',
				    '''relative_pos_embeddings''': '''relative_linear''',
				    '''ngram_embeddings''': '''ngram_input_embed''',
				    '''position_embeddings''': '''embed_positions''',
				}

				for key in loading_info["missing_keys"]:
								__a :						List[str]			       =  key.split('''.''')

								if attributes[0] == "lm_head":
												__a :						Dict			       =  prophet
												__a :						Optional[Any]			       =  prophet_old
								else:
												__a :						Union[str, Any]			       =  prophet.prophetnet
												__a :						int			       =  prophet_old.model

								__a :						Optional[int]			       =  False
								for attribute in attributes:
												if attribute in mapping:
																__a :						str			       =  mapping[attribute]
																if not hasattr(a_   ,       a_) and len(a_) > 0:
																				__a :						List[str]			       =  attribute
												elif hasattr(a_   ,       a_):
																__a :						int			       =  attribute

												if attribute == "weight":
																assert old_model.weight.shape == model.weight.shape, "Shapes have to match!"
																__a :						Optional[int]			       =  old_model.weight
																logger.info(F"""{attribute} is initialized.""")
																__a :						List[str]			       =  True
																break
												elif attribute == "bias":
																assert old_model.bias.shape == model.bias.shape, "Shapes have to match!"
																__a :						Dict			       =  old_model.bias
																logger.info(F"""{attribute} is initialized""")
																__a :						Tuple			       =  True
																break
												elif attribute in special_keys and hasattr(a_   ,       '''in_proj_weight'''):
																__a :						List[Any]			       =  old_model.in_proj_weight.shape[0] // 3
																__a :						Tuple			       =  getattr(a_   ,       a_)
																param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match"
																param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match"
																if attribute == "query_proj":
																				__a :						Any			       =  nn.Parameter(old_model.in_proj_weight[:embed_dim, :])
																				__a :						List[Any]			       =  nn.Parameter(old_model.in_proj_bias[:embed_dim])

																elif attribute == "key_proj":
																				__a :						Union[str, Any]			       =  nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :])
																				__a :						int			       =  nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim])
																elif attribute == "value_proj":
																				__a :						Dict			       =  nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :])
																				__a :						List[Any]			       =  nn.Parameter(old_model.in_proj_bias[2 * embed_dim :])
																__a :						int			       =  True
																break
												elif attribute == "position_embeddings":
																assert (
																    model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1]
																), "Hidden size has to match"
																assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings."
																__a :						Optional[Any]			       =  nn.Parameter(old_model.embed_positions.weight[:5_12, :])
																__a :						List[Any]			       =  True
																break

												if attribute.isdigit():
																__a :						Optional[Any]			       =  model[int(a_)]
																__a :						str			       =  old_model[int(a_)]
												else:
																__a :						Optional[int]			       =  getattr(a_   ,       a_)

																if old_attribute == "":
																				__a :						str			       =  old_model
																else:
																				if not hasattr(a_   ,       a_):
																								raise ValueError(F"""{old_model} does not have {old_attribute}""")
																				__a :						Union[str, Any]			       =  getattr(a_   ,       a_)

								if not is_key_init:
												raise ValueError(F"""{key} was not correctly initialized!""")

				print(F"""Saving model to {pytorch_dump_folder_path}""")
				prophet.save_pretrained(a_)


if __name__ == "__main__":
		A 			=  argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '''--prophetnet_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.'''
		)
		parser.add_argument(
		    '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
		)
		A 			=  parser.parse_args()
		convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)