'''simple docstring'''
import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


lowercase__  :      int        =					False







@skip_mps
class lowerCamelCase							(	lowerCamelCase     ,		lowerCamelCase     ,		lowerCamelCase     ,		unittest.TestCase					):




  '''simple docstring'''



  lowerCAmelCase__										=	StableDiffusionAttendAndExcitePipeline
  lowerCAmelCase__										=	False
  lowerCAmelCase__										=	TEXT_TO_IMAGE_PARAMS
  lowerCAmelCase__										=	TEXT_TO_IMAGE_BATCH_PARAMS.union({'''token_indices'''}					)
  lowerCAmelCase__										=	TEXT_TO_IMAGE_IMAGE_PARAMS
  lowerCAmelCase__										=	TEXT_TO_IMAGE_IMAGE_PARAMS






  @classmethod
  def  lowerCAmelCase__ (      cls			:       List[str]  )				->List[Any]:
        super().setUpClass()
        torch.use_deterministic_algorithms(UpperCAmelCase__  )






  @classmethod
  def  lowerCAmelCase__ (      cls			:       List[str]  )				->str:
        super().tearDownClass()
        torch.use_deterministic_algorithms(UpperCAmelCase__  )






  def  lowerCAmelCase__ (      self			:       Tuple  )				->str:
        torch.manual_seed(0  )
        UpperCAmelCase_            =  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  ,							attention_head_dim=(2, 4)  ,							use_linear_projection=UpperCAmelCase__  ,							)
        UpperCAmelCase_            =  DDIMScheduler(
            beta_start=0.0_0085  ,							beta_end=0.012  ,							beta_schedule='''scaled_linear'''  ,							clip_sample=UpperCAmelCase__  ,							set_alpha_to_one=UpperCAmelCase__  ,							)
        torch.manual_seed(0  )
        UpperCAmelCase_            =  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  ,							sample_size=128  ,							)
        torch.manual_seed(0  )
        UpperCAmelCase_            =  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=1000  ,							hidden_act='''gelu'''  ,							projection_dim=512  ,							)
        UpperCAmelCase_            =  CLIPTextModel(UpperCAmelCase__  )
        UpperCAmelCase_            =  CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''  )

        UpperCAmelCase_            =  {
            '''unet''': unet,
            '''scheduler''': scheduler,
            '''vae''': vae,
            '''text_encoder''': text_encoder,
            '''tokenizer''': tokenizer,
            '''safety_checker''': None,
            '''feature_extractor''': None,
        }

        return components






  def  lowerCAmelCase__ (      self			:       Union[str, Any]  ,							UpperCAmelCase__			:       Optional[int]  ,							UpperCAmelCase__			:       str=0  )				->List[str]:
        if str(UpperCAmelCase__  ).startswith('''mps'''  ):
              UpperCAmelCase_            =  torch.manual_seed(UpperCAmelCase__  )
        else:
              UpperCAmelCase_            =  torch.Generator(device=UpperCAmelCase__  ).manual_seed(UpperCAmelCase__  )
        UpperCAmelCase_            =  UpperCAmelCase_            =  {
            '''prompt''': '''a cat and a frog''',
            '''token_indices''': [2, 5],
            '''generator''': generator,
            '''num_inference_steps''': 1,
            '''guidance_scale''': 6.0,
            '''output_type''': '''numpy''',
            '''max_iter_to_alter''': 2,
            '''thresholds''': {0: 0.7},
        }
        return inputs






  def  lowerCAmelCase__ (      self			:       Dict  )				->Any:
        UpperCAmelCase_            =  '''cpu'''

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

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

        self.assertEqual(image.shape  ,							(1, 64, 64, 3)  )
        UpperCAmelCase_            =  np.array(
            [0.6390_5364, 0.6289_7307, 0.4859_9017, 0.513_3624, 0.555_0048, 0.4576_9516, 0.5032_6973, 0.502_3139, 0.4538_4496]  )
        UpperCAmelCase_            =  np.abs(image_slice.flatten() - expected_slice  ).max()
        self.assertLessEqual(UpperCAmelCase__  ,							1e-3  )






  def  lowerCAmelCase__ (      self			:       int  )				->str:
        super().test_cpu_offload_forward_pass(expected_max_diff=5e-4  )






  def  lowerCAmelCase__ (      self			:       Union[str, Any]  )				->Union[str, Any]:
        # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
        self._test_inference_batch_consistent(batch_sizes=[1, 2]  )






  def  lowerCAmelCase__ (      self			:       int  )				->List[str]:
        self._test_inference_batch_single_identical(batch_size=2  ,							expected_max_diff=7e-4  )






  def  lowerCAmelCase__ (      self			:       int  )				->Optional[int]:
        super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3  )






  def  lowerCAmelCase__ (      self			:       Optional[Any]  )				->Optional[Any]:
        super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4  )






  def  lowerCAmelCase__ (      self			:       Tuple  )				->Any:
        super().test_save_load_local(expected_max_difference=5e-4  )






  def  lowerCAmelCase__ (      self			:       Optional[int]  )				->Tuple:
        super().test_save_load_optional_components(expected_max_difference=4e-4  )







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




  '''simple docstring'''



  @classmethod
  def  lowerCAmelCase__ (      cls			:       Tuple  )				->Dict:
        super().setUpClass()
        torch.use_deterministic_algorithms(UpperCAmelCase__  )






  @classmethod
  def  lowerCAmelCase__ (      cls			:       Any  )				->Tuple:
        super().tearDownClass()
        torch.use_deterministic_algorithms(UpperCAmelCase__  )






  def  lowerCAmelCase__ (      self			:       Any  )				->Tuple:
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()






  def  lowerCAmelCase__ (      self			:       Dict  )				->List[str]:
        UpperCAmelCase_            =  torch.manual_seed(51  )

        UpperCAmelCase_            =  StableDiffusionAttendAndExcitePipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''  ,							safety_checker=UpperCAmelCase__  ,							torch_dtype=torch.floataa  )
        pipe.to('''cuda'''  )

        UpperCAmelCase_            =  '''a painting of an elephant with glasses'''
        UpperCAmelCase_            =  [5, 7]

        UpperCAmelCase_            =  pipe(
            prompt=UpperCAmelCase__  ,							token_indices=UpperCAmelCase__  ,							guidance_scale=7.5  ,							generator=UpperCAmelCase__  ,							num_inference_steps=5  ,							max_iter_to_alter=5  ,							output_type='''numpy'''  ,							).images[0]

        UpperCAmelCase_            =  load_numpy(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy'''  )
        assert np.abs((expected_image - image).max()  ) < 5e-1






'''simple docstring'''
import unittest
from typing import Dict, List, Optional, Union

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import BridgeTowerImageProcessor







class lowerCamelCase							(	unittest.TestCase					):




  '''simple docstring'''



  def __init__(      self			:       int  ,							UpperCAmelCase__			:       Dict  ,							UpperCAmelCase__			:       bool = True  ,							UpperCAmelCase__			:       Dict[str, int] = None  ,							UpperCAmelCase__			:       int = 32  ,							UpperCAmelCase__			:       bool = True  ,							UpperCAmelCase__			:       Union[int, float] = 1 / 255  ,							UpperCAmelCase__			:       bool = True  ,							UpperCAmelCase__			:       bool = True  ,							UpperCAmelCase__			:       Optional[Union[float, List[float]]] = [0.4814_5466, 0.457_8275, 0.4082_1073]  ,							UpperCAmelCase__			:       Optional[Union[float, List[float]]] = [0.2686_2954, 0.2613_0258, 0.2757_7711]  ,							UpperCAmelCase__			:       bool = True  ,							UpperCAmelCase__			:       Union[str, Any]=7  ,							UpperCAmelCase__			:       Optional[int]=30  ,							UpperCAmelCase__			:       Union[str, Any]=400  ,							UpperCAmelCase__			:       List[Any]=3  ,							)				->Dict:
        UpperCAmelCase_            =  parent
        UpperCAmelCase_            =  do_resize
        UpperCAmelCase_            =  size if size is not None else {'''shortest_edge''': 288}
        UpperCAmelCase_            =  size_divisor
        UpperCAmelCase_            =  do_rescale
        UpperCAmelCase_            =  rescale_factor
        UpperCAmelCase_            =  do_normalize
        UpperCAmelCase_            =  do_center_crop
        UpperCAmelCase_            =  image_mean
        UpperCAmelCase_            =  image_std
        UpperCAmelCase_            =  do_pad
        UpperCAmelCase_            =  batch_size
        UpperCAmelCase_            =  num_channels
        UpperCAmelCase_            =  min_resolution
        UpperCAmelCase_            =  max_resolution






  def  lowerCAmelCase__ (      self			:       Tuple  )				->List[Any]:
        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "size_divisor": self.size_divisor,
        }






  def  lowerCAmelCase__ (      self			:       Optional[int]  ,							UpperCAmelCase__			:       List[str]  ,							UpperCAmelCase__			:       Dict=False  )				->Any:
        if not batched:
              UpperCAmelCase_            =  self.size['''shortest_edge''']
              UpperCAmelCase_            =  image_inputs[0]
              if isinstance(UpperCAmelCase__  ,							Image.Image  ):
                    UpperCAmelCase_      ,		UpperCAmelCase_            =  image.size
              else:
                    UpperCAmelCase_      ,		UpperCAmelCase_            =  image.shape[1], image.shape[2]
              UpperCAmelCase_            =  size / min(UpperCAmelCase__  ,							UpperCAmelCase__  )
              if h < w:
                    UpperCAmelCase_      ,		UpperCAmelCase_            =  size, scale * w
              else:
                    UpperCAmelCase_      ,		UpperCAmelCase_            =  scale * h, size

              UpperCAmelCase_            =  int((1333 / 800) * size  )
              if max(UpperCAmelCase__  ,							UpperCAmelCase__  ) > max_size:
                    UpperCAmelCase_            =  max_size / max(UpperCAmelCase__  ,							UpperCAmelCase__  )
                    UpperCAmelCase_            =  newh * scale
                    UpperCAmelCase_            =  neww * scale

              UpperCAmelCase_      ,		UpperCAmelCase_            =  int(newh + 0.5  ), int(neww + 0.5  )
              UpperCAmelCase_      ,		UpperCAmelCase_            =  (
                  newh // self.size_divisor * self.size_divisor,
                  neww // self.size_divisor * self.size_divisor,
              )

        else:
              UpperCAmelCase_            =  []
              for image in image_inputs:
                    UpperCAmelCase_      ,		UpperCAmelCase_            =  self.get_expected_values([image]  )
                    expected_values.append((expected_height, expected_width)  )
              UpperCAmelCase_            =  max(UpperCAmelCase__  ,							key=lambda UpperCAmelCase__  : item[0]  )[0]
              UpperCAmelCase_            =  max(UpperCAmelCase__  ,							key=lambda UpperCAmelCase__  : item[1]  )[1]

        return expected_height, expected_width







@require_torch
@require_vision
class lowerCamelCase							(	lowerCamelCase     ,		unittest.TestCase					):




  '''simple docstring'''



  lowerCAmelCase__										=	BridgeTowerImageProcessor if is_vision_available() else None






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






  @property
  def  lowerCAmelCase__ (      self			:       List[str]  )				->Dict:
        return self.image_processor_tester.prepare_image_processor_dict()






  def  lowerCAmelCase__ (      self			:       Optional[int]  )				->int:
        UpperCAmelCase_            =  self.image_processing_class(**self.image_processor_dict  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''image_mean'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''image_std'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''do_normalize'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''do_resize'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''size'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''size_divisor'''  )  )






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






  def  lowerCAmelCase__ (      self			:       List[str]  )				->Union[str, Any]:
        # Initialize image processor
        UpperCAmelCase_            =  self.image_processing_class(**self.image_processor_dict  )
        # create random PIL images
        UpperCAmelCase_            =  prepare_image_inputs(self.image_processor_tester  ,							equal_resolution=UpperCAmelCase__  )
        for image in image_inputs:
              self.assertIsInstance(UpperCAmelCase__  ,							Image.Image  )

        # Test not batched input
        UpperCAmelCase_            =  image_processing(image_inputs[0]  ,							return_tensors='''pt'''  ).pixel_values

        UpperCAmelCase_      ,		UpperCAmelCase_            =  self.image_processor_tester.get_expected_values(UpperCAmelCase__  )
        self.assertEqual(
            encoded_images.shape  ,							(1, self.image_processor_tester.num_channels, expected_height, expected_width)  ,							)

        # Test batched
        UpperCAmelCase_            =  image_processing(UpperCAmelCase__  ,							return_tensors='''pt'''  ).pixel_values

        UpperCAmelCase_      ,		UpperCAmelCase_            =  self.image_processor_tester.get_expected_values(UpperCAmelCase__  ,							batched=UpperCAmelCase__  )
        self.assertEqual(
            encoded_images.shape  ,							(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )  ,							)






  def  lowerCAmelCase__ (      self			:       Any  )				->Optional[int]:
        # Initialize image processor
        UpperCAmelCase_            =  self.image_processing_class(**self.image_processor_dict  )
        # create random numpy tensors
        UpperCAmelCase_            =  prepare_image_inputs(self.image_processor_tester  ,							equal_resolution=UpperCAmelCase__  ,							numpify=UpperCAmelCase__  )
        for image in image_inputs:
              self.assertIsInstance(UpperCAmelCase__  ,							np.ndarray  )

        # Test not batched input
        UpperCAmelCase_            =  image_processing(image_inputs[0]  ,							return_tensors='''pt'''  ).pixel_values

        UpperCAmelCase_      ,		UpperCAmelCase_            =  self.image_processor_tester.get_expected_values(UpperCAmelCase__  )
        self.assertEqual(
            encoded_images.shape  ,							(1, self.image_processor_tester.num_channels, expected_height, expected_width)  ,							)

        # Test batched
        UpperCAmelCase_            =  image_processing(UpperCAmelCase__  ,							return_tensors='''pt'''  ).pixel_values

        UpperCAmelCase_      ,		UpperCAmelCase_            =  self.image_processor_tester.get_expected_values(UpperCAmelCase__  ,							batched=UpperCAmelCase__  )
        self.assertEqual(
            encoded_images.shape  ,							(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )  ,							)






  def  lowerCAmelCase__ (      self			:       int  )				->List[str]:
        # Initialize image processor
        UpperCAmelCase_            =  self.image_processing_class(**self.image_processor_dict  )
        # create random PyTorch tensors
        UpperCAmelCase_            =  prepare_image_inputs(self.image_processor_tester  ,							equal_resolution=UpperCAmelCase__  ,							torchify=UpperCAmelCase__  )
        for image in image_inputs:
              self.assertIsInstance(UpperCAmelCase__  ,							torch.Tensor  )

        # Test not batched input
        UpperCAmelCase_            =  image_processing(image_inputs[0]  ,							return_tensors='''pt'''  ).pixel_values

        UpperCAmelCase_      ,		UpperCAmelCase_            =  self.image_processor_tester.get_expected_values(UpperCAmelCase__  )
        self.assertEqual(
            encoded_images.shape  ,							(1, self.image_processor_tester.num_channels, expected_height, expected_width)  ,							)

        # Test batched
        UpperCAmelCase_            =  image_processing(UpperCAmelCase__  ,							return_tensors='''pt'''  ).pixel_values

        UpperCAmelCase_      ,		UpperCAmelCase_            =  self.image_processor_tester.get_expected_values(UpperCAmelCase__  ,							batched=UpperCAmelCase__  )
        self.assertEqual(
            encoded_images.shape  ,							(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )  ,							)





