


import gc
import random
import unittest

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

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
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

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


def 		lowercase_	(  _UpperCamelCase	):


			'''simple docstring'''







			__lowercase			       = [tensor.shape for tensor in tensor_list]
			return all(shape == shapes[0] for shape in shapes[1:]	)
class 						lowerCamelCase_							(					lowerCAmelCase__  ,  lowerCAmelCase__  ,  lowerCAmelCase__  ,  unittest.TestCase					):



					'''simple docstring'''







					__UpperCAmelCase							  =					StableDiffusionLatentUpscalePipeline
					__UpperCAmelCase							  =					TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
					    "height",
					    "width",
					    "cross_attention_kwargs",
					    "negative_prompt_embeds",
					    "prompt_embeds",
					}
					__UpperCAmelCase							  =					PipelineTesterMixin.required_optional_params - {"num_images_per_prompt"}
					__UpperCAmelCase							  =					TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
					__UpperCAmelCase							  =					frozenset(
					    []					)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
					__UpperCAmelCase							  =					frozenset([]					)

					__UpperCAmelCase							  =					True






					@property
					def A   (					self    ) ->      Tuple:







								'''simple docstring'''

								__lowercase			       = 1
								__lowercase			       = 4
								__lowercase			       = (1_6, 1_6)

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






					def A   (					self    ) ->      List[str]:







								'''simple docstring'''

								torch.manual_seed(0    )
								__lowercase			       = UNetaDConditionModel(
								    act_fn='''gelu'''			,							attention_head_dim=8			,							norm_num_groups=snake_case_			,							block_out_channels=[3_2, 3_2, 6_4, 6_4]			,							time_cond_proj_dim=1_6_0			,							conv_in_kernel=1			,							conv_out_kernel=1			,							cross_attention_dim=3_2			,							down_block_types=(
								        '''KDownBlock2D''',
								        '''KCrossAttnDownBlock2D''',
								        '''KCrossAttnDownBlock2D''',
								        '''KCrossAttnDownBlock2D''',
								    )			,							in_channels=8			,							mid_block_type=snake_case_			,							only_cross_attention=snake_case_			,							out_channels=5			,							resnet_time_scale_shift='''scale_shift'''			,							time_embedding_type='''fourier'''			,							timestep_post_act='''gelu'''			,							up_block_types=('''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KUpBlock2D''')			,							)
								__lowercase			       = AutoencoderKL(
								    block_out_channels=[3_2, 3_2, 6_4, 6_4]			,							in_channels=3			,							out_channels=3			,							down_block_types=[
								        '''DownEncoderBlock2D''',
								        '''DownEncoderBlock2D''',
								        '''DownEncoderBlock2D''',
								        '''DownEncoderBlock2D''',
								    ]			,							up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''']			,							latent_channels=4			,							)
								__lowercase			       = EulerDiscreteScheduler(prediction_type='''sample'''    )
								__lowercase			       = CLIPTextConfig(
								    bos_token_id=0			,							eos_token_id=2			,							hidden_size=3_2			,							intermediate_size=3_7			,							layer_norm_eps=1e-0_5			,							num_attention_heads=4			,							num_hidden_layers=5			,							pad_token_id=1			,							vocab_size=1_0_0_0			,							hidden_act='''quick_gelu'''			,							projection_dim=5_1_2			,							)
								__lowercase			       = CLIPTextModel(snake_case_    )
								__lowercase			       = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''    )

								__lowercase			       = {
								    '''unet''': model.eval(),
								    '''vae''': vae.eval(),
								    '''scheduler''': scheduler,
								    '''text_encoder''': text_encoder,
								    '''tokenizer''': tokenizer,
								}

								return components






					def A   (					self			,							snake_case_			,							snake_case_=0    ) ->      int:







								'''simple docstring'''

								if str(snake_case_    ).startswith('''mps'''    ):
											__lowercase			       = torch.manual_seed(snake_case_    )
								else:
											__lowercase			       = torch.Generator(device=snake_case_    ).manual_seed(snake_case_    )
								__lowercase			       = {
								    '''prompt''': '''A painting of a squirrel eating a burger''',
								    '''image''': self.dummy_image.cpu(),
								    '''generator''': generator,
								    '''num_inference_steps''': 2,
								    '''output_type''': '''numpy''',
								}
								return inputs






					def A   (					self    ) ->      List[str]:







								'''simple docstring'''

								__lowercase			       = '''cpu'''

								__lowercase			       = self.get_dummy_components()
								__lowercase			       = self.pipeline_class(**snake_case_    )
								pipe.to(snake_case_    )
								pipe.set_progress_bar_config(disable=snake_case_    )

								__lowercase			       = self.get_dummy_inputs(snake_case_    )
								__lowercase			       = pipe(**snake_case_    ).images
								__lowercase			       = image[0, -3:, -3:, -1]

								self.assertEqual(image.shape			,							(1, 2_5_6, 2_5_6, 3)    )
								__lowercase			       = np.array(
								    [0.4_7_2_2_2_4_1_2, 0.4_1_9_2_1_6_3_3, 0.4_4_7_1_7_4_3_4, 0.4_6_8_7_4_1_9_2, 0.4_2_5_8_8_2_5_8, 0.4_6_1_5_0_7_2_6, 0.4_6_7_7_5_3_4, 0.4_5_5_8_3_8_3_2, 0.4_8_5_7_9_0_5_5]    )
								__lowercase			       = np.abs(image_slice.flatten() - expected_slice    ).max()
								self.assertLessEqual(snake_case_			,							1e-3    )






					def A   (					self    ) ->      Dict:







								'''simple docstring'''

								super().test_attention_slicing_forward_pass(expected_max_diff=7e-3    )






					def A   (					self    ) ->      Dict:







								'''simple docstring'''

								super().test_cpu_offload_forward_pass(expected_max_diff=3e-3    )






					def A   (					self    ) ->      Optional[Any]:







								'''simple docstring'''

								super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3    )






					def A   (					self    ) ->      Dict:







								'''simple docstring'''

								super().test_inference_batch_single_identical(expected_max_diff=7e-3    )






					def A   (					self    ) ->      Union[str, Any]:







								'''simple docstring'''

								super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3    )






					def A   (					self    ) ->      Dict:







								'''simple docstring'''

								super().test_save_load_local(expected_max_difference=3e-3    )






					def A   (					self    ) ->      str:







								'''simple docstring'''

								super().test_save_load_optional_components(expected_max_difference=3e-3    )






					def A   (					self    ) ->      Tuple:







								'''simple docstring'''

								__lowercase			       = [
								    '''DDIMScheduler''',
								    '''DDPMScheduler''',
								    '''PNDMScheduler''',
								    '''HeunDiscreteScheduler''',
								    '''EulerAncestralDiscreteScheduler''',
								    '''KDPM2DiscreteScheduler''',
								    '''KDPM2AncestralDiscreteScheduler''',
								    '''DPMSolverSDEScheduler''',
								]
								__lowercase			       = self.get_dummy_components()
								__lowercase			       = self.pipeline_class(**snake_case_    )

								# make sure that PNDM does not need warm-up
								pipe.scheduler.register_to_config(skip_prk_steps=snake_case_    )

								pipe.to(snake_case_    )
								pipe.set_progress_bar_config(disable=snake_case_    )
								__lowercase			       = self.get_dummy_inputs(snake_case_    )
								__lowercase			       = 2

								__lowercase			       = []
								for scheduler_enum in KarrasDiffusionSchedulers:
											if scheduler_enum.name in skip_schedulers:
														# no sigma schedulers are not supported
														# no schedulers
														continue

											__lowercase			       = getattr(snake_case_			,							scheduler_enum.name    )
											__lowercase			       = scheduler_cls.from_config(pipe.scheduler.config    )
											__lowercase			       = pipe(**snake_case_    )[0]
											outputs.append(snake_case_    )

								assert check_same_shape(snake_case_    )




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



					'''simple docstring'''







					def A   (					self    ) ->      List[str]:







								'''simple docstring'''

								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()






					def A   (					self    ) ->      str:







								'''simple docstring'''

								__lowercase			       = torch.manual_seed(3_3    )

								__lowercase			       = StableDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''			,							torch_dtype=torch.floataa    )
								pipe.to('''cuda'''    )

								__lowercase			       = StableDiffusionLatentUpscalePipeline.from_pretrained(
								    '''stabilityai/sd-x2-latent-upscaler'''			,							torch_dtype=torch.floataa    )
								upscaler.to('''cuda'''    )

								__lowercase			       = '''a photo of an astronaut high resolution, unreal engine, ultra realistic'''

								__lowercase			       = pipe(snake_case_			,							generator=snake_case_			,							output_type='''latent'''    ).images

								__lowercase			       = upscaler(
								    prompt=snake_case_			,							image=snake_case_			,							num_inference_steps=2_0			,							guidance_scale=0			,							generator=snake_case_			,							output_type='''np'''			,							).images[0]

								__lowercase			       = load_numpy(
								    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy'''    )
								assert np.abs((expected_image - image).mean()    ) < 5e-2






					def A   (					self    ) ->      Optional[Any]:







								'''simple docstring'''

								__lowercase			       = torch.manual_seed(3_3    )

								__lowercase			       = StableDiffusionLatentUpscalePipeline.from_pretrained(
								    '''stabilityai/sd-x2-latent-upscaler'''			,							torch_dtype=torch.floataa    )
								upscaler.to('''cuda'''    )

								__lowercase			       = '''the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas'''

								__lowercase			       = load_image(
								    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png'''    )

								__lowercase			       = upscaler(
								    prompt=snake_case_			,							image=snake_case_			,							num_inference_steps=2_0			,							guidance_scale=0			,							generator=snake_case_			,							output_type='''np'''			,							).images[0]

								__lowercase			       = load_numpy(
								    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy'''    )
								assert np.abs((expected_image - image).max()    ) < 5e-2






import argparse
from pathlib import Path

import torch

from transformers import OPTConfig, OPTModel
from transformers.utils import logging


logging.set_verbosity_info()
a			:			List[str]												=					logging.get_logger(__name__)


def 		lowercase_	(  _UpperCamelCase	):


			'''simple docstring'''







			__lowercase			       = torch.load(_UpperCamelCase							,      map_location='''cpu'''	)
			if "model" in sd.keys():
						__lowercase			       = torch.load(_UpperCamelCase							,      map_location='''cpu'''	)['''model''']

			# pop unnecessary weights
			__lowercase			       = [
			    '''decoder.version''',
			    '''decoder.output_projection.weight''',
			]
			for key in keys_to_delete:
						if key in sd:
									sd.pop(_UpperCamelCase	)

			__lowercase			       = {
			    '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''',
			    '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''',
			    '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''',
			    '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''',
			}
			for old_key, new_key in keys_to_rename.items():
						if old_key in sd:
									__lowercase			       = sd.pop(_UpperCamelCase	)

			__lowercase			       = list(sd.keys()	)
			for key in keys:
						if ".qkv_proj." in key:
									__lowercase			       = sd[key]
									# We split QKV in separate Q,K,V

									__lowercase			       = key.replace('''.qkv_proj.'''							,      '''.q_proj.'''	)
									__lowercase			       = key.replace('''.qkv_proj.'''							,      '''.k_proj.'''	)
									__lowercase			       = key.replace('''.qkv_proj.'''							,      '''.v_proj.'''	)

									__lowercase			       = value.shape[0]
									assert depth % 3 == 0
									# `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming:
									# https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97
									__lowercase       ,			__lowercase       ,			__lowercase			       = torch.split(_UpperCamelCase							,      depth // 3							,      dim=0	)

									__lowercase			       = q
									__lowercase			       = k
									__lowercase			       = v
									del sd[key]

			return sd


@torch.no_grad()
def 		lowercase_	(  _UpperCamelCase							,      _UpperCamelCase							,      _UpperCamelCase=None	):


			'''simple docstring'''







			__lowercase			       = load_checkpoint(_UpperCamelCase	)

			if config is not None:
						__lowercase			       = OPTConfig.from_pretrained(_UpperCamelCase	)
			else:
						__lowercase			       = OPTConfig()

			__lowercase			       = OPTModel(_UpperCamelCase	).half().eval()
			model.load_state_dict(_UpperCamelCase	)

			# Check results
			Path(_UpperCamelCase	).mkdir(exist_ok=_UpperCamelCase	)
			model.save_pretrained(_UpperCamelCase	)


if __name__ == "__main__":
	a			:			List[Any]												=					argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    '''--fairseq_path''',
	    type=str,
	    help=(
	        '''path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:'''
	        ''' https://huggingface.co/models?other=opt_metasq'''
	    ),
	)
	parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
	parser.add_argument('''--hf_config''', default=None, type=str, help='''Define HF config.''')
	a			:			Any												=					parser.parse_args()
	convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)


