


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       _lowerCAmelCase    (				lowerCAmelCase_				:Optional[Any]				)->Optional[Any]:
   '''simple docstring'''






   snake_case_            =       [tensor.shape for tensor in tensor_list]
   return all(shape == shapes[0] for shape in shapes[1:]				)


class      __lowerCAmelCase  (			a			,	a			,	a			,	unittest.TestCase       ):






  """simple docstring"""





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

  _SCREAMING_SNAKE_CASE		     =							True



  @property
  def        lowerCAmelCase__	(  self							:							Tuple	) ->			Any:




     """simple docstring"""
     snake_case_            =       1
     snake_case_            =       4
     snake_case_            =       (1_6, 1_6)

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



  def        lowerCAmelCase__	(  self							:							str	) ->			Tuple:




     """simple docstring"""
     torch.manual_seed(0	)
     snake_case_            =       UNetaDConditionModel(
         act_fn="gelu"     ,					attention_head_dim=8     ,					norm_num_groups=_lowerCAmelCase     ,					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=_lowerCAmelCase     ,					only_cross_attention=_lowerCAmelCase     ,					out_channels=5     ,					resnet_time_scale_shift="scale_shift"     ,					time_embedding_type="fourier"     ,					timestep_post_act="gelu"     ,					up_block_types=("KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KUpBlock2D")     ,					)
     snake_case_            =       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     ,					)
     snake_case_            =       EulerDiscreteScheduler(prediction_type="sample"	)
     snake_case_            =       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     ,					hidden_act="quick_gelu"     ,					projection_dim=5_1_2     ,					)
     snake_case_            =       CLIPTextModel(_lowerCAmelCase	)
     snake_case_            =       CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	)

     snake_case_            =       {
         "unet": model.eval(),
         "vae": vae.eval(),
         "scheduler": scheduler,
         "text_encoder": text_encoder,
         "tokenizer": tokenizer,
     }

     return components



  def        lowerCAmelCase__	(  self							:							str     ,					_lowerCAmelCase							:							List[Any]     ,					_lowerCAmelCase							:							Tuple=0	) ->			Optional[int]:




     """simple docstring"""
     if str(_lowerCAmelCase	).startswith("mps"	):
        snake_case_            =       torch.manual_seed(_lowerCAmelCase	)
     else:
        snake_case_            =       torch.Generator(device=_lowerCAmelCase	).manual_seed(_lowerCAmelCase	)
     snake_case_            =       {
         "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        lowerCAmelCase__	(  self							:							Any	) ->			str:




     """simple docstring"""
     snake_case_            =       "cpu"

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

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

     self.assertEqual(image.shape     ,					(1, 2_5_6, 2_5_6, 3)	)
     snake_case_            =       np.array(
         [0.47_222_412, 0.41_921_633, 0.44_717_434, 0.46_874_192, 0.42_588_258, 0.46_150_726, 0.4_677_534, 0.45_583_832, 0.48_579_055]	)
     snake_case_            =       np.abs(image_slice.flatten() - expected_slice	).max()
     self.assertLessEqual(_lowerCAmelCase     ,					1e-3	)



  def        lowerCAmelCase__	(  self							:							int	) ->			Dict:




     """simple docstring"""
     super().test_attention_slicing_forward_pass(expected_max_diff=7e-3	)



  def        lowerCAmelCase__	(  self							:							int	) ->			List[str]:




     """simple docstring"""
     super().test_cpu_offload_forward_pass(expected_max_diff=3e-3	)



  def        lowerCAmelCase__	(  self							:							str	) ->			Optional[int]:




     """simple docstring"""
     super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]	) ->			int:




     """simple docstring"""
     super().test_inference_batch_single_identical(expected_max_diff=7e-3	)



  def        lowerCAmelCase__	(  self							:							Optional[int]	) ->			Tuple:




     """simple docstring"""
     super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3	)



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




     """simple docstring"""
     super().test_save_load_local(expected_max_difference=3e-3	)



  def        lowerCAmelCase__	(  self							:							List[Any]	) ->			Optional[int]:




     """simple docstring"""
     super().test_save_load_optional_components(expected_max_difference=3e-3	)



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




     """simple docstring"""
     snake_case_            =       [
         "DDIMScheduler",
         "DDPMScheduler",
         "PNDMScheduler",
         "HeunDiscreteScheduler",
         "EulerAncestralDiscreteScheduler",
         "KDPM2DiscreteScheduler",
         "KDPM2AncestralDiscreteScheduler",
         "DPMSolverSDEScheduler",
     ]
     snake_case_            =       self.get_dummy_components()
     snake_case_            =       self.pipeline_class(**_lowerCAmelCase	)

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

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

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

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

     assert check_same_shape(_lowerCAmelCase	)






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






  """simple docstring"""





  def        lowerCAmelCase__	(  self							:							str	) ->			Dict:




     """simple docstring"""
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()



  def        lowerCAmelCase__	(  self							:							int	) ->			Tuple:




     """simple docstring"""
     snake_case_            =       torch.manual_seed(3_3	)

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

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

     snake_case_            =       "a photo of an astronaut high resolution, unreal engine, ultra realistic"

     snake_case_            =       pipe(_lowerCAmelCase     ,					generator=_lowerCAmelCase     ,					output_type="latent"	).images

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

     snake_case_            =       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        lowerCAmelCase__	(  self							:							str	) ->			Tuple:




     """simple docstring"""
     snake_case_            =       torch.manual_seed(3_3	)

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

     snake_case_            =       "the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas"

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

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

     snake_case_            =       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 math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
   import torch_xla.core.xla_model as xm
   import torch_xla.debug.metrics as met


class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  def __init__(  self							:							Union[str, Any]     ,					*_lowerCAmelCase							:							Dict     ,					_lowerCAmelCase							:							List[Any]=None     ,					_lowerCAmelCase							:							str=None     ,					**_lowerCAmelCase							:							str	) ->			Dict:




     """simple docstring"""
     super().__init__(*_lowerCAmelCase     ,					**_lowerCAmelCase	)
     snake_case_            =       eval_examples
     snake_case_            =       post_process_function



  def        lowerCAmelCase__	(  self							:							Union[str, Any]     ,					_lowerCAmelCase							:							Optional[Dataset] = None     ,					_lowerCAmelCase							:							Any=None     ,					_lowerCAmelCase							:							Optional[List[str]] = None     ,					_lowerCAmelCase							:							str = "eval"     ,					**_lowerCAmelCase							:							Tuple     ,					) ->			Dict[str, float]:




     """simple docstring"""
     snake_case_            =       gen_kwargs.copy()
     snake_case_            =       (
         gen_kwargs["max_length"] if gen_kwargs.get("max_length"	) is not None else self.args.generation_max_length
     )
     snake_case_            =       (
         gen_kwargs["num_beams"] if gen_kwargs.get("num_beams"	) is not None else self.args.generation_num_beams
     )
     snake_case_            =       gen_kwargs

     snake_case_            =       self.eval_dataset if eval_dataset is None else eval_dataset
     snake_case_            =       self.get_eval_dataloader(_lowerCAmelCase	)
     snake_case_            =       self.eval_examples if eval_examples is None else eval_examples

     # Temporarily disable metric computation, we will do it in the loop here.
     snake_case_            =       self.compute_metrics
     snake_case_            =       None
     snake_case_            =       time.time()
     snake_case_            =       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
        snake_case_            =       eval_loop(
            _lowerCAmelCase     ,					description="Evaluation"     ,					prediction_loss_only=True if compute_metrics is None else None     ,					ignore_keys=_lowerCAmelCase     ,					metric_key_prefix=_lowerCAmelCase     ,					)
     finally:
        snake_case_            =       compute_metrics
     snake_case_            =       self.args.eval_batch_size * self.args.world_size
     if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
        start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
     output.metrics.update(
         speed_metrics(
             _lowerCAmelCase     ,					_lowerCAmelCase     ,					num_samples=output.num_samples     ,					num_steps=math.ceil(output.num_samples / total_batch_size	)     ,					)	)

     if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
        # Only the main node write the results by default
        snake_case_            =       self.post_process_function(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase	)
        snake_case_            =       self.compute_metrics(_lowerCAmelCase	)

        # Prefix all keys with metric_key_prefix + '_'
        for key in list(metrics.keys()	):
           if not key.startswith(F'''{metric_key_prefix}_'''	):
              snake_case_            =       metrics.pop(_lowerCAmelCase	)

        metrics.update(output.metrics	)
     else:
        snake_case_            =       output.metrics

     if self.args.should_log:
        # Only the main node log the results by default
        self.log(_lowerCAmelCase	)

     if self.args.tpu_metrics_debug or self.args.debug:
        # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
        xm.master_print(met.metrics_report()	)

     snake_case_            =       self.callback_handler.on_evaluate(self.args     ,					self.state     ,					self.control     ,					_lowerCAmelCase	)
     return metrics



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							Optional[Any]     ,					_lowerCAmelCase							:							Union[str, Any]     ,					_lowerCAmelCase							:							List[str]=None     ,					_lowerCAmelCase							:							str = "test"     ,					**_lowerCAmelCase							:							Optional[Any]	) ->			Union[str, Any]:




     """simple docstring"""
     snake_case_            =       gen_kwargs.copy()

     snake_case_            =       self.get_test_dataloader(_lowerCAmelCase	)

     # Temporarily disable metric computation, we will do it in the loop here.
     snake_case_            =       self.compute_metrics
     snake_case_            =       None
     snake_case_            =       time.time()
     snake_case_            =       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
        snake_case_            =       eval_loop(
            _lowerCAmelCase     ,					description="Prediction"     ,					prediction_loss_only=True if compute_metrics is None else None     ,					ignore_keys=_lowerCAmelCase     ,					metric_key_prefix=_lowerCAmelCase     ,					)
     finally:
        snake_case_            =       compute_metrics

     snake_case_            =       self.args.eval_batch_size * self.args.world_size
     if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
        start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
     output.metrics.update(
         speed_metrics(
             _lowerCAmelCase     ,					_lowerCAmelCase     ,					num_samples=output.num_samples     ,					num_steps=math.ceil(output.num_samples / total_batch_size	)     ,					)	)
     if self.post_process_function is None or self.compute_metrics is None:
        return output

     snake_case_            =       self.post_process_function(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase     ,					"predict"	)
     snake_case_            =       self.compute_metrics(_lowerCAmelCase	)

     # Prefix all keys with metric_key_prefix + '_'
     for key in list(metrics.keys()	):
        if not key.startswith(F'''{metric_key_prefix}_'''	):
           snake_case_            =       metrics.pop(_lowerCAmelCase	)
     metrics.update(output.metrics	)
     return PredictionOutput(predictions=predictions.predictions     ,					label_ids=predictions.label_ids     ,					metrics=_lowerCAmelCase	)




