




import gc
import unittest

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

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


class 							UpperCamelCase_       (					UpperCAmelCase__						,   unittest.TestCase	):

    '''simple docstring'''






    UpperCAmelCase__				    = ShapEPipeline
    UpperCAmelCase__				    = ['''prompt''']
    UpperCAmelCase__				    = ['''prompt''']
    UpperCAmelCase__				    = [
        '''num_images_per_prompt''',
        '''num_inference_steps''',
        '''generator''',
        '''latents''',
        '''guidance_scale''',
        '''frame_size''',
        '''output_type''',
        '''return_dict''',
    ]
    UpperCAmelCase__				    = False


    @property
    def 			SCREAMING_SNAKE_CASE				(     self :      Dict)		->List[Any]:





           '''simple docstring'''

           return 32


    @property
    def 			SCREAMING_SNAKE_CASE				(     self :      List[str])		->int:





           '''simple docstring'''

           return 32


    @property
    def 			SCREAMING_SNAKE_CASE				(     self :      Tuple)		->Optional[Any]:





           '''simple docstring'''

           return self.time_input_dim * 4


    @property
    def 			SCREAMING_SNAKE_CASE				(     self :      Optional[Any])		->str:





           '''simple docstring'''

           return 8


    @property
    def 			SCREAMING_SNAKE_CASE				(     self :      List[str])		->Optional[int]:





           '''simple docstring'''

           A__ 					=       CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')
           return tokenizer


    @property
    def 			SCREAMING_SNAKE_CASE				(     self :      List[Any])		->List[Any]:





           '''simple docstring'''

           torch.manual_seed(0)
           A__ 					=       CLIPTextConfig(
               bos_token_id=0   ,					eos_token_id=2   ,					hidden_size=self.text_embedder_hidden_size   ,					projection_dim=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 CLIPTextModelWithProjection(UpperCAmelCase__)


    @property
    def 			SCREAMING_SNAKE_CASE				(     self :      Tuple)		->Union[str, Any]:





           '''simple docstring'''

           torch.manual_seed(0)

           A__ 					=       {
               '''num_attention_heads''': 2,
               '''attention_head_dim''': 16,
               '''embedding_dim''': self.time_input_dim,
               '''num_embeddings''': 32,
               '''embedding_proj_dim''': self.text_embedder_hidden_size,
               '''time_embed_dim''': self.time_embed_dim,
               '''num_layers''': 1,
               '''clip_embed_dim''': self.time_input_dim * 2,
               '''additional_embeddings''': 0,
               '''time_embed_act_fn''': '''gelu''',
               '''norm_in_type''': '''layer''',
               '''encoder_hid_proj_type''': None,
               '''added_emb_type''': None,
           }

           A__ 					=       PriorTransformer(**UpperCAmelCase__)
           return model


    @property
    def 			SCREAMING_SNAKE_CASE				(     self :      str)		->List[str]:





           '''simple docstring'''

           torch.manual_seed(0)

           A__ 					=       {
               '''param_shapes''': (
                   (self.renderer_dim, 93),
                   (self.renderer_dim, 8),
                   (self.renderer_dim, 8),
                   (self.renderer_dim, 8),
               ),
               '''d_latent''': self.time_input_dim,
               '''d_hidden''': self.renderer_dim,
               '''n_output''': 12,
               '''background''': (
                   0.1,
                   0.1,
                   0.1,
               ),
           }
           A__ 					=       ShapERenderer(**UpperCAmelCase__)
           return model


    def 			SCREAMING_SNAKE_CASE				(     self :      List[Any])		->List[Any]:





           '''simple docstring'''

           A__ 					=       self.dummy_prior
           A__ 					=       self.dummy_text_encoder
           A__ 					=       self.dummy_tokenizer
           A__ 					=       self.dummy_renderer

           A__ 					=       HeunDiscreteScheduler(
               beta_schedule='''exp'''   ,					num_train_timesteps=1_024   ,					prediction_type='''sample'''   ,					use_karras_sigmas=UpperCAmelCase__   ,					clip_sample=UpperCAmelCase__   ,					clip_sample_range=1.0   ,					)
           A__ 					=       {
               '''prior''': prior,
               '''text_encoder''': text_encoder,
               '''tokenizer''': tokenizer,
               '''renderer''': renderer,
               '''scheduler''': scheduler,
           }

           return components


    def 			SCREAMING_SNAKE_CASE				(     self :      Optional[int]   ,					UpperCAmelCase__ :      int   ,					UpperCAmelCase__ :      List[Any]=0)		->Union[str, Any]:





           '''simple docstring'''

           if str(UpperCAmelCase__).startswith('''mps'''):
                  A__ 					=       torch.manual_seed(UpperCAmelCase__)
           else:
                  A__ 					=       torch.Generator(device=UpperCAmelCase__).manual_seed(UpperCAmelCase__)
           A__ 					=       {
               '''prompt''': '''horse''',
               '''generator''': generator,
               '''num_inference_steps''': 1,
               '''frame_size''': 32,
               '''output_type''': '''np''',
           }
           return inputs


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





           '''simple docstring'''

           A__ 					=       '''cpu'''

           A__ 					=       self.get_dummy_components()

           A__ 					=       self.pipeline_class(**UpperCAmelCase__)
           A__ 					=       pipe.to(UpperCAmelCase__)

           pipe.set_progress_bar_config(disable=UpperCAmelCase__)

           A__ 					=       pipe(**self.get_dummy_inputs(UpperCAmelCase__))
           A__ 					=       output.images[0]
           A__ 					=       image[0, -3:, -3:, -1]

           assert image.shape == (20, 32, 32, 3)

           A__ 					=       np.array(
               [
                   0.00039216,
                   0.00039216,
                   0.00039216,
                   0.00039216,
                   0.00039216,
                   0.00039216,
                   0.00039216,
                   0.00039216,
                   0.00039216,
               ])

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


    def 			SCREAMING_SNAKE_CASE				(     self :      Any)		->int:





           '''simple docstring'''

           self._test_inference_batch_consistent(batch_sizes=[1, 2])


    def 			SCREAMING_SNAKE_CASE				(     self :      List[Any])		->List[str]:





           '''simple docstring'''

           A__ 					=       torch_device == '''cpu'''
           A__ 					=       True

           self._test_inference_batch_single_identical(
               batch_size=2   ,					test_max_difference=UpperCAmelCase__   ,					relax_max_difference=UpperCAmelCase__   ,					)





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





           '''simple docstring'''

           A__ 					=       self.get_dummy_components()
           A__ 					=       self.pipeline_class(**UpperCAmelCase__)
           A__ 					=       pipe.to(UpperCAmelCase__)
           pipe.set_progress_bar_config(disable=UpperCAmelCase__)

           A__ 					=       1
           A__ 					=       2

           A__ 					=       self.get_dummy_inputs(UpperCAmelCase__)

           for key in inputs.keys():
                  if key in self.batch_params:
                         A__ 					=       batch_size * [inputs[key]]

           A__ 					=       pipe(**UpperCAmelCase__   ,					num_images_per_prompt=UpperCAmelCase__)[0]

           assert images.shape[0] == batch_size * num_images_per_prompt


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

    '''simple docstring'''






    def 			SCREAMING_SNAKE_CASE				(     self :      str)		->List[Any]:





           '''simple docstring'''

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





    def 			SCREAMING_SNAKE_CASE				(     self :      Dict)		->Optional[int]:





           '''simple docstring'''

           A__ 					=       load_numpy(
               '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
               '''/shap_e/test_shap_e_np_out.npy''')
           A__ 					=       ShapEPipeline.from_pretrained('''openai/shap-e''')
           A__ 					=       pipe.to(UpperCAmelCase__)
           pipe.set_progress_bar_config(disable=UpperCAmelCase__)

           A__ 					=       torch.Generator(device=UpperCAmelCase__).manual_seed(0)

           A__ 					=       pipe(
               '''a shark'''   ,					generator=UpperCAmelCase__   ,					guidance_scale=15.0   ,					num_inference_steps=64   ,					frame_size=64   ,					output_type='''np'''   ,					).images[0]

           assert images.shape == (20, 64, 64, 3)

           assert_mean_pixel_difference(UpperCAmelCase__   ,					UpperCAmelCase__)




import logging
from pathlib import Path

import numpy as np
import pytorch_lightning as pl
import torch
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.utilities import rank_zero_only
from utils_rag import save_json




def 							a		(				snake_case__:   Any   ):


				'''simple docstring'''
				lowercase_	    =   filter(lambda snake_case__   : p.requires_grad		,  model.parameters()   )
				lowercase_	    =   sum([np.prod(p.size()   ) for p in model_parameters]   )
				return params


__a         =   logging.getLogger(__name__)




def 							a		(				snake_case__:   int		,  snake_case__:   Any   ):


				'''simple docstring'''
				if metric == "rouge2":
								lowercase_	    =   '''{val_avg_rouge2:.4f}-{step_count}'''
				elif metric == "bleu":
								lowercase_	    =   '''{val_avg_bleu:.4f}-{step_count}'''
				elif metric == "em":
								lowercase_	    =   '''{val_avg_em:.4f}-{step_count}'''
				elif metric == "loss":
								lowercase_	    =   '''{val_avg_loss:.4f}-{step_count}'''
				else:
								raise NotImplementedError(
								    F'''seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this'''
								    ''' function.'''   )

				lowercase_	    =   ModelCheckpoint(
				    dirpath=snake_case__		,  filename=snake_case__		,  monitor=F'''val_{metric}'''		,  mode='''max'''		,  save_top_k=1		,  every_n_epochs=1		,  )

				return checkpoint_callback




def 							a		(				snake_case__:   str		,  snake_case__:   List[str]   ):


				'''simple docstring'''
				return EarlyStopping(
				    monitor=F'''val_{metric}'''		,  mode='''min''' if '''loss''' in metric else '''max'''		,  patience=snake_case__		,  verbose=snake_case__		,  )


class 					lowercase__(							pl.Callback				):



	"""simple docstring"""




	def 		_lowercase    (			self    :     List[str]			,				SCREAMING_SNAKE_CASE_    :     Optional[int]			,				SCREAMING_SNAKE_CASE_    :     Any			)			->	Optional[Any]:
					lowercase_	    =   {f'''lr_group_{i}''': param['''lr'''] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups			)}
					pl_module.logger.log_metrics(SCREAMING_SNAKE_CASE_			)
	@rank_zero_only
	def 		_lowercase    (			self    :     Optional[int]			,				SCREAMING_SNAKE_CASE_    :     pl.Trainer			,				SCREAMING_SNAKE_CASE_    :     pl.LightningModule			,				SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     Tuple=True			)			->	None:
					logger.info(f'''***** {type_path} results at step {trainer.global_step:05d} *****'''			)
					lowercase_	    =   trainer.callback_metrics
					trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['''log''', '''progress_bar''', '''preds''']}			)
					# Log results
					lowercase_	    =   Path(pl_module.hparams.output_dir			)
					if type_path == "test":
									lowercase_	    =   od / '''test_results.txt'''
									lowercase_	    =   od / '''test_generations.txt'''
					else:
									# this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json
									# If people want this it will be easy enough to add back.
									lowercase_	    =   od / f'''{type_path}_results/{trainer.global_step:05d}.txt'''
									lowercase_	    =   od / f'''{type_path}_generations/{trainer.global_step:05d}.txt'''
									results_file.parent.mkdir(exist_ok=SCREAMING_SNAKE_CASE_			)
									generations_file.parent.mkdir(exist_ok=SCREAMING_SNAKE_CASE_			)
					with open(SCREAMING_SNAKE_CASE_			,				'''a+'''			) as writer:
									for key in sorted(SCREAMING_SNAKE_CASE_			):
													if key in ["log", "progress_bar", "preds"]:
																	continue
													lowercase_	    =   metrics[key]
													if isinstance(SCREAMING_SNAKE_CASE_			,				torch.Tensor			):
																	lowercase_	    =   val.item()
													lowercase_	    =   f'''{key}: {val:.6f}\n'''
													writer.write(SCREAMING_SNAKE_CASE_			)

					if not save_generations:
									return

					if "preds" in metrics:
									lowercase_	    =   '''\n'''.join(metrics['''preds''']			)
									generations_file.open('''w+'''			).write(SCREAMING_SNAKE_CASE_			)
	@rank_zero_only
	def 		_lowercase    (			self    :     Any			,				SCREAMING_SNAKE_CASE_    :     int			,				SCREAMING_SNAKE_CASE_    :     Any			)			->	Union[str, Any]:
					try:
									lowercase_	    =   pl_module.model.model.num_parameters()
					except AttributeError:
									lowercase_	    =   pl_module.model.num_parameters()

					lowercase_	    =   count_trainable_parameters(SCREAMING_SNAKE_CASE_			)
					# mp stands for million parameters
					trainer.logger.log_metrics({'''n_params''': npars, '''mp''': npars / 1e6, '''grad_mp''': n_trainable_pars / 1e6}			)
	@rank_zero_only
	def 		_lowercase    (			self    :     Tuple			,				SCREAMING_SNAKE_CASE_    :     pl.Trainer			,				SCREAMING_SNAKE_CASE_    :     pl.LightningModule			)			->	Any:
					save_json(pl_module.metrics			,				pl_module.metrics_save_path			)
					return self._write_logs(SCREAMING_SNAKE_CASE_			,				SCREAMING_SNAKE_CASE_			,				'''test'''			)




	@rank_zero_only
	def 		_lowercase    (			self    :     str			,				SCREAMING_SNAKE_CASE_    :     pl.Trainer			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]			)			->	Dict:
					save_json(pl_module.metrics			,				pl_module.metrics_save_path			)
					# Uncommenting this will save val generations
					# return self._write_logs(trainer, pl_module, "valid")




