


'''simple docstring'''
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    a__			(   lowerCAmelCase__		)       ->      str:
    UpperCAmelCase__     :						List[str]						=							[tensor.shape for tensor in tensor_list]
    return all(shape == shapes[0] for shape in shapes[1:]		)




class      lowerCamelCase_  (      __a	,			__a	,			__a	,			unittest.TestCase ):
      lowerCAmelCase__							=	StableDiffusionLatentUpscalePipeline
      lowerCAmelCase__							=	TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
          'height',
          'width',
          'cross_attention_kwargs',
          'negative_prompt_embeds',
          'prompt_embeds',
      }
      lowerCAmelCase__							=	PipelineTesterMixin.required_optional_params - {'num_images_per_prompt'}
      lowerCAmelCase__							=	TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
      lowerCAmelCase__							=	frozenset(
          [] )  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
      lowerCAmelCase__							=	frozenset([] )

      lowerCAmelCase__							=	True






      @property
      def lowercase_				(   self							:       Dict					):


          '''simple docstring'''


          UpperCAmelCase__     :						List[str]						=							1
          UpperCAmelCase__     :						int						=							4
          UpperCAmelCase__     :						Optional[int]						=							(16, 16)

          UpperCAmelCase__     :						int						=							floats_tensor((batch_size, num_channels) + sizes		,		rng=random.Random(0					)					).to(UpperCamelCase_					)
          return image






      def lowercase_				(   self							:       Optional[int]					):


          '''simple docstring'''


          torch.manual_seed(0					)
          UpperCAmelCase__     :						Tuple						=							UNetaDConditionModel(
              act_fn='''gelu'''		,		attention_head_dim=8		,		norm_num_groups=UpperCamelCase_		,		block_out_channels=[32, 32, 64, 64]		,		time_cond_proj_dim=160		,		conv_in_kernel=1		,		conv_out_kernel=1		,		cross_attention_dim=32		,		down_block_types=(
                  '''KDownBlock2D''',
                  '''KCrossAttnDownBlock2D''',
                  '''KCrossAttnDownBlock2D''',
                  '''KCrossAttnDownBlock2D''',
              )		,		in_channels=8		,		mid_block_type=UpperCamelCase_		,		only_cross_attention=UpperCamelCase_		,		out_channels=5		,		resnet_time_scale_shift='''scale_shift'''		,		time_embedding_type='''fourier'''		,		timestep_post_act='''gelu'''		,		up_block_types=('''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KUpBlock2D''')		,		)
          UpperCAmelCase__     :						Any						=							AutoencoderKL(
              block_out_channels=[32, 32, 64, 64]		,		in_channels=3		,		out_channels=3		,		down_block_types=[
                  '''DownEncoderBlock2D''',
                  '''DownEncoderBlock2D''',
                  '''DownEncoderBlock2D''',
                  '''DownEncoderBlock2D''',
              ]		,		up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''']		,		latent_channels=4		,		)
          UpperCAmelCase__     :						Union[str, Any]						=							EulerDiscreteScheduler(prediction_type='''sample'''					)
          UpperCAmelCase__     :						Union[str, Any]						=							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=1_000		,		hidden_act='''quick_gelu'''		,		projection_dim=512		,		)
          UpperCAmelCase__     :						Union[str, Any]						=							CLIPTextModel(UpperCamelCase_					)
          UpperCAmelCase__     :						Optional[int]						=							CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

          UpperCAmelCase__     :						List[str]						=							{
              '''unet''': model.eval(),
              '''vae''': vae.eval(),
              '''scheduler''': scheduler,
              '''text_encoder''': text_encoder,
              '''tokenizer''': tokenizer,
          }

          return components






      def lowercase_				(   self							:       int		,		_A							:       List[str]		,		_A							:       Tuple=0					):


          '''simple docstring'''


          if str(UpperCamelCase_					).startswith('''mps'''					):
              UpperCAmelCase__     :						Union[str, Any]						=							torch.manual_seed(UpperCamelCase_					)
          else:
              UpperCAmelCase__     :						Union[str, Any]						=							torch.Generator(device=UpperCamelCase_					).manual_seed(UpperCamelCase_					)
          UpperCAmelCase__     :						Any						=							{
              '''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 lowercase_				(   self							:       Tuple					):


          '''simple docstring'''


          UpperCAmelCase__     :						int						=							'''cpu'''

          UpperCAmelCase__     :						Any						=							self.get_dummy_components()
          UpperCAmelCase__     :						Dict						=							self.pipeline_class(**UpperCamelCase_					)
          pipe.to(UpperCamelCase_					)
          pipe.set_progress_bar_config(disable=UpperCamelCase_					)

          UpperCAmelCase__     :						List[str]						=							self.get_dummy_inputs(UpperCamelCase_					)
          UpperCAmelCase__     :						int						=							pipe(**UpperCamelCase_					).images
          UpperCAmelCase__     :						int						=							image[0, -3:, -3:, -1]

          self.assertEqual(image.shape		,		(1, 256, 256, 3)					)
          UpperCAmelCase__     :						Tuple						=							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]					)
          UpperCAmelCase__     :						Tuple						=							np.abs(image_slice.flatten() - expected_slice					).max()
          self.assertLessEqual(UpperCamelCase_		,		1e-3					)






      def lowercase_				(   self							:       List[str]					):


          '''simple docstring'''


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






      def lowercase_				(   self							:       List[str]					):


          '''simple docstring'''


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






      def lowercase_				(   self							:       Union[str, Any]					):


          '''simple docstring'''


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






      def lowercase_				(   self							:       Any					):


          '''simple docstring'''


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






      def lowercase_				(   self							:       Union[str, Any]					):


          '''simple docstring'''


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






      def lowercase_				(   self							:       List[Any]					):


          '''simple docstring'''


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






      def lowercase_				(   self							:       str					):


          '''simple docstring'''


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






      def lowercase_				(   self							:       Optional[Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Dict						=							[
              '''DDIMScheduler''',
              '''DDPMScheduler''',
              '''PNDMScheduler''',
              '''HeunDiscreteScheduler''',
              '''EulerAncestralDiscreteScheduler''',
              '''KDPM2DiscreteScheduler''',
              '''KDPM2AncestralDiscreteScheduler''',
              '''DPMSolverSDEScheduler''',
          ]
          UpperCAmelCase__     :						Optional[Any]						=							self.get_dummy_components()
          UpperCAmelCase__     :						List[str]						=							self.pipeline_class(**UpperCamelCase_					)

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

          pipe.to(UpperCamelCase_					)
          pipe.set_progress_bar_config(disable=UpperCamelCase_					)
          UpperCAmelCase__     :						List[Any]						=							self.get_dummy_inputs(UpperCamelCase_					)
          UpperCAmelCase__     :						Tuple						=							2

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

              UpperCAmelCase__     :						List[str]						=							getattr(UpperCamelCase_		,		scheduler_enum.name					)
              UpperCAmelCase__     :						Optional[int]						=							scheduler_cls.from_config(pipe.scheduler.config					)
              UpperCAmelCase__     :						Optional[Any]						=							pipe(**UpperCamelCase_					)[0]
              outputs.append(UpperCamelCase_					)

          assert check_same_shape(UpperCamelCase_					)




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






      def lowercase_				(   self							:       Optional[int]					):


          '''simple docstring'''


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






      def lowercase_				(   self							:       Optional[Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						List[Any]						=							torch.manual_seed(33					)

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

          UpperCAmelCase__     :						Optional[Any]						=							StableDiffusionLatentUpscalePipeline.from_pretrained(
              '''stabilityai/sd-x2-latent-upscaler'''		,		torch_dtype=torch.floataa					)
          upscaler.to('''cuda'''					)

          UpperCAmelCase__     :						List[Any]						=							'''a photo of an astronaut high resolution, unreal engine, ultra realistic'''

          UpperCAmelCase__     :						List[Any]						=							pipe(UpperCamelCase_		,		generator=UpperCamelCase_		,		output_type='''latent'''					).images

          UpperCAmelCase__     :						Dict						=							upscaler(
              prompt=UpperCamelCase_		,		image=UpperCamelCase_		,		num_inference_steps=20		,		guidance_scale=0		,		generator=UpperCamelCase_		,		output_type='''np'''		,		).images[0]

          UpperCAmelCase__     :						Union[str, Any]						=							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 lowercase_				(   self							:       str					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							torch.manual_seed(33					)

          UpperCAmelCase__     :						Union[str, Any]						=							StableDiffusionLatentUpscalePipeline.from_pretrained(
              '''stabilityai/sd-x2-latent-upscaler'''		,		torch_dtype=torch.floataa					)
          upscaler.to('''cuda'''					)

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

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

          UpperCAmelCase__     :						int						=							upscaler(
              prompt=UpperCamelCase_		,		image=UpperCamelCase_		,		num_inference_steps=20		,		guidance_scale=0		,		generator=UpperCamelCase_		,		output_type='''np'''		,		).images[0]

          UpperCAmelCase__     :						Union[str, Any]						=							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







'''simple docstring'''




import argparse
import pickle

import numpy as np
import torch
from torch import nn

from transformers import ReformerConfig, ReformerModelWithLMHead
from transformers.utils import logging


logging.set_verbosity_info()




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_					,				UpperCAmelCase_=None							)   ->List[str]:
	# set parameter of one layer
	assert torch_layer.weight.shape == weight.shape, f'''{torch_layer} layer.weight does not match'''
	snake_case__	       =   nn.Parameter(UpperCAmelCase_							)
	if bias is not None:
		assert torch_layer.bias.shape == bias.shape, f'''{torch_layer} layer.bias does not match'''
		snake_case__	       =   nn.Parameter(UpperCAmelCase_							)




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_					,				UpperCAmelCase_							)   ->List[Any]:
	# set torch weights for 1-to-1 comparison
	snake_case__	       =   np.asarray(weights[0]							)
	snake_case__	       =   np.asarray(weights[1]							)
	snake_case__	       =   np.asarray(weights[2]							)

	set_param(
	    torch_layer.self_attention.query_key					,				torch.tensor(UpperCAmelCase_							).transpose(1					,				2							).contiguous().view(-1					,				UpperCAmelCase_							)					,				)
	set_param(
	    torch_layer.self_attention.value					,				torch.tensor(UpperCAmelCase_							).transpose(1					,				2							).contiguous().view(-1					,				UpperCAmelCase_							)					,				)
	set_param(
	    torch_layer.output.dense					,				torch.tensor(UpperCAmelCase_							).view(-1					,				UpperCAmelCase_							).contiguous().transpose(0					,				1							)					,				)




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_					,				UpperCAmelCase_							)   ->List[Any]:
	# set torch weights for 1-to-1 comparison
	snake_case__	       =   np.asarray(weights[0]							)
	snake_case__	       =   np.asarray(weights[1]							)
	snake_case__	       =   np.asarray(weights[2]							)
	snake_case__	       =   np.asarray(weights[3]							)

	set_param(
	    torch_layer.self_attention.query					,				torch.tensor(UpperCAmelCase_							).transpose(1					,				2							).contiguous().view(-1					,				UpperCAmelCase_							)					,				)
	set_param(
	    torch_layer.self_attention.key					,				torch.tensor(UpperCAmelCase_							).transpose(1					,				2							).contiguous().view(-1					,				UpperCAmelCase_							)					,				)
	set_param(
	    torch_layer.self_attention.value					,				torch.tensor(UpperCAmelCase_							).transpose(1					,				2							).contiguous().view(-1					,				UpperCAmelCase_							)					,				)
	set_param(
	    torch_layer.output.dense					,				torch.tensor(UpperCAmelCase_							).view(-1					,				UpperCAmelCase_							).contiguous().transpose(0					,				1							)					,				)




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_					,				UpperCAmelCase_							)   ->Dict:
	# layernorm 1
	snake_case__	       =   weights[0][0][0]
	snake_case__	       =   np.asarray(layer_norm_a[0]							)
	snake_case__	       =   np.asarray(layer_norm_a[1]							)
	set_param(
	    torch_block.attention.layer_norm					,				torch.tensor(UpperCAmelCase_							)					,				torch.tensor(UpperCAmelCase_							)					,				)

	# lsh weights + output
	snake_case__	       =   weights[0][1]
	if len(UpperCAmelCase_							) < 4:
		set_layer_weights_in_torch_lsh(UpperCAmelCase_					,				torch_block.attention					,				UpperCAmelCase_							)
	else:
		set_layer_weights_in_torch_local(UpperCAmelCase_					,				torch_block.attention					,				UpperCAmelCase_							)

	# intermediate weighs
	snake_case__	       =   weights[2][0][1][2]

	# Chunked Feed Forward
	if len(UpperCAmelCase_							) == 4:
		snake_case__	       =   intermediate_weights[2]

	# layernorm 2
	snake_case__	       =   np.asarray(intermediate_weights[0][0]							)
	snake_case__	       =   np.asarray(intermediate_weights[0][1]							)
	set_param(
	    torch_block.feed_forward.layer_norm					,				torch.tensor(UpperCAmelCase_							)					,				torch.tensor(UpperCAmelCase_							)					,				)

	# intermediate dense
	snake_case__	       =   np.asarray(intermediate_weights[1][0]							)
	snake_case__	       =   np.asarray(intermediate_weights[1][1]							)
	set_param(
	    torch_block.feed_forward.dense.dense					,				torch.tensor(UpperCAmelCase_							).transpose(0					,				1							).contiguous()					,				torch.tensor(UpperCAmelCase_							)					,				)

	# intermediate out
	snake_case__	       =   np.asarray(intermediate_weights[4][0]							)
	snake_case__	       =   np.asarray(intermediate_weights[4][1]							)
	set_param(
	    torch_block.feed_forward.output.dense					,				torch.tensor(UpperCAmelCase_							).transpose(0					,				1							).contiguous()					,				torch.tensor(UpperCAmelCase_							)					,				)




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_					,				UpperCAmelCase_							)   ->Union[str, Any]:
	# reformer model
	snake_case__	       =   torch_model.reformer

	# word embeds
	snake_case__	       =   np.asarray(weights[1]							)
	set_param(
	    torch_model_reformer.embeddings.word_embeddings					,				torch.tensor(UpperCAmelCase_							)					,				)

	if isinstance(weights[3]					,				UpperCAmelCase_							):
		snake_case__	       =   torch_model_reformer.embeddings.position_embeddings
		for emb_idx in range(len(position_embeddings.weights							)							):
			snake_case__	       =   np.asarray(weights[3][emb_idx][0]							)
			assert (
			    position_embeddings.weights[emb_idx].shape == emb_weights.shape
			), f'''{position_embeddings[emb_idx]} emb does not match'''
			snake_case__	       =   nn.Parameter(torch.tensor(UpperCAmelCase_							)							)

	snake_case__	       =   weights[5]
	assert len(torch_model_reformer.encoder.layers							) * 4 == len(
	    UpperCAmelCase_							), "HF and trax model do not have the same number of layers"
	for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers							):
		snake_case__	       =   trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)]
		set_block_weights_in_torch(UpperCAmelCase_					,				UpperCAmelCase_					,				UpperCAmelCase_							)

	# output layer norm
	snake_case__	       =   np.asarray(weights[7][0]							)
	snake_case__	       =   np.asarray(weights[7][1]							)
	set_param(
	    torch_model_reformer.encoder.layer_norm					,				torch.tensor(UpperCAmelCase_							)					,				torch.tensor(UpperCAmelCase_							)					,				)

	# output embeddings
	snake_case__	       =   np.asarray(weights[9][0]							)
	snake_case__	       =   np.asarray(weights[9][1]							)
	set_param(
	    torch_model.lm_head.decoder					,				torch.tensor(UpperCAmelCase_							).transpose(0					,				1							).contiguous()					,				torch.tensor(UpperCAmelCase_							)					,				)




def 						__lowerCamelCase			(	UpperCAmelCase_					,				UpperCAmelCase_					,				UpperCAmelCase_							)   ->List[Any]:
	# Initialise PyTorch model
	snake_case__	       =   ReformerConfig.from_json_file(UpperCAmelCase_							)
	print(f'''Building PyTorch model from configuration: {config}'''							)
	snake_case__	       =   ReformerModelWithLMHead(UpperCAmelCase_							)

	with open(UpperCAmelCase_					,				'rb'							) as f:
		snake_case__	       =   pickle.load(UpperCAmelCase_							)['weights']

	set_model_weights_in_torch(UpperCAmelCase_					,				UpperCAmelCase_					,				config.hidden_size							)

	# Save pytorch-model
	print(f'''Save PyTorch model to {pytorch_dump_path}'''							)
	torch.save(model.state_dict()					,				UpperCAmelCase_							)


if __name__ == "__main__":
			a__					:			Optional[int]          =     argparse.ArgumentParser()
			# Required parameters
			parser.add_argument(
			    '''--trax_model_pkl_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
			)
			parser.add_argument(
			    '''--config_file''',
			    default=None,
			    type=str,
			    required=True,
			    help=(
			        '''The config json file corresponding to the pre-trained Reformer model. \n'''
			        '''This specifies the model architecture.'''
			    ),
			)
			parser.add_argument(
			    '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
			)
			a__					:			Dict          =     parser.parse_args()
			convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)



