



'''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


lowerCAmelCase:      List[Any]		 =		False

@skip_mps
class 							a__(      lowercase_	,				lowercase_	,				lowercase_	,				unittest.TestCase			):
    lowercase__				=					StableDiffusionAttendAndExcitePipeline
    lowercase__				=					False
    lowercase__				=					TEXT_TO_IMAGE_PARAMS
    lowercase__				=					TEXT_TO_IMAGE_BATCH_PARAMS.union({"""token_indices"""}			)
    lowercase__				=					TEXT_TO_IMAGE_IMAGE_PARAMS
    lowercase__				=					TEXT_TO_IMAGE_IMAGE_PARAMS







    @classmethod
    def     lowercase_ (   cls				:						Optional[int]			):

      '''simple docstring'''





      super().setUpClass()
      torch.use_deterministic_algorithms(lowerCamelCase_			)







    @classmethod
    def     lowercase_ (   cls				:						List[str]			):

      '''simple docstring'''





      super().tearDownClass()
      torch.use_deterministic_algorithms(lowerCamelCase_			)







    def     lowercase_ (   self				:						Dict			):

      '''simple docstring'''





      torch.manual_seed(0			)
      a		:      str       =  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=lowerCamelCase_		,    )
      a		:      Dict       =  DDIMScheduler(
          beta_start=0.00085		,    beta_end=0.012		,    beta_schedule='scaled_linear'		,    clip_sample=lowerCamelCase_		,    set_alpha_to_one=lowerCamelCase_		,    )
      torch.manual_seed(0			)
      a		:      int       =  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=1_28		,    )
      torch.manual_seed(0			)
      a		:      List[str]       =  CLIPTextConfig(
          bos_token_id=0		,    eos_token_id=2		,    hidden_size=32		,    intermediate_size=37		,    layer_norm_eps=1e-0_5		,    num_attention_heads=4		,    num_hidden_layers=5		,    pad_token_id=1		,    vocab_size=10_00		,    hidden_act='gelu'		,    projection_dim=5_12		,    )
      a		:      List[Any]       =  CLIPTextModel(lowerCamelCase_			)
      a		:      Optional[int]       =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'			)

      a		:      List[Any]       =  {
          """unet""": unet,
          """scheduler""": scheduler,
          """vae""": vae,
          """text_encoder""": text_encoder,
          """tokenizer""": tokenizer,
          """safety_checker""": None,
          """feature_extractor""": None,
      }

      return components







    def     lowercase_ (   self				:						Dict		,    __snake_case				:						List[str]		,    __snake_case				:						Dict=0			):

      '''simple docstring'''





      if str(lowerCamelCase_			).startswith('mps'			):
        a		:      Optional[Any]       =  torch.manual_seed(lowerCamelCase_			)
      else:
        a		:      Optional[int]       =  torch.Generator(device=lowerCamelCase_			).manual_seed(lowerCamelCase_			)
      a		:      List[Any]       =  {
          """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     lowercase_ (   self				:						Optional[int]			):

      '''simple docstring'''





      a		:      Optional[Any]       =  """cpu"""

      a		:      Any       =  self.get_dummy_components()
      a		:      Tuple       =  self.pipeline_class(**lowerCamelCase_			)
      pipe.to(lowerCamelCase_			)
      pipe.set_progress_bar_config(disable=lowerCamelCase_			)

      a		:      List[str]       =  self.get_dummy_inputs(lowerCamelCase_			)
      a		:      Tuple       =  pipe(**lowerCamelCase_			).images
      a		:      Any       =  image[0, -3:, -3:, -1]

      self.assertEqual(image.shape		,    (1, 64, 64, 3)			)
      a		:      Optional[int]       =  np.array(
          [0.63905364, 0.62897307, 0.48599017, 0.5133624, 0.5550048, 0.45769516, 0.50326973, 0.5023139, 0.45384496]			)
      a		:      Any       =  np.abs(image_slice.flatten() - expected_slice			).max()
      self.assertLessEqual(lowerCamelCase_		,    1e-3			)







    def     lowercase_ (   self				:						List[str]			):

      '''simple docstring'''





      super().test_cpu_offload_forward_pass(expected_max_diff=5e-4			)







    def     lowercase_ (   self				:						Any			):

      '''simple docstring'''





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







    def     lowercase_ (   self				:						Tuple			):

      '''simple docstring'''





      self._test_inference_batch_single_identical(batch_size=2		,    expected_max_diff=7e-4			)







    def     lowercase_ (   self				:						List[str]			):

      '''simple docstring'''





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







    def     lowercase_ (   self				:						str			):

      '''simple docstring'''





      super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4			)







    def     lowercase_ (   self				:						Any			):

      '''simple docstring'''





      super().test_save_load_local(expected_max_difference=5e-4			)







    def     lowercase_ (   self				:						int			):

      '''simple docstring'''





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

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







    @classmethod
    def     lowercase_ (   cls				:						List[Any]			):

      '''simple docstring'''





      super().setUpClass()
      torch.use_deterministic_algorithms(lowerCamelCase_			)







    @classmethod
    def     lowercase_ (   cls				:						int			):

      '''simple docstring'''





      super().tearDownClass()
      torch.use_deterministic_algorithms(lowerCamelCase_			)







    def     lowercase_ (   self				:						Tuple			):

      '''simple docstring'''





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







    def     lowercase_ (   self				:						int			):

      '''simple docstring'''





      a		:      Dict       =  torch.manual_seed(51			)

      a		:      List[Any]       =  StableDiffusionAttendAndExcitePipeline.from_pretrained(
          'CompVis/stable-diffusion-v1-4'		,    safety_checker=lowerCamelCase_		,    torch_dtype=torch.floataa			)
      pipe.to('cuda'			)

      a		:      List[Any]       =  """a painting of an elephant with glasses"""
      a		:      Optional[Any]       =  [5, 7]

      a		:      Optional[Any]       =  pipe(
          prompt=lowerCamelCase_		,    token_indices=lowerCamelCase_		,    guidance_scale=7.5		,    generator=lowerCamelCase_		,    num_inference_steps=5		,    max_iter_to_alter=5		,    output_type='numpy'		,    ).images[0]

      a		:      Dict       =  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 inspect
import math
import tempfile
import unittest

import numpy as np

from transformers import ViTMAEConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch
     from torch import nn

     from transformers import ViTMAEForPreTraining, ViTMAEModel
     from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import ViTImageProcessor

class 							a__:







    def __init__(   self				:						List[str]		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Any=13		,    __snake_case				:						int=30		,    __snake_case				:						Union[str, Any]=2		,    __snake_case				:						Optional[int]=3		,    __snake_case				:						Dict=True		,    __snake_case				:						List[str]=True		,    __snake_case				:						Any=32		,    __snake_case				:						List[Any]=5		,    __snake_case				:						List[Any]=4		,    __snake_case				:						Optional[int]=37		,    __snake_case				:						int="gelu"		,    __snake_case				:						Optional[Any]=0.1		,    __snake_case				:						List[str]=0.1		,    __snake_case				:						List[str]=10		,    __snake_case				:						int=0.02		,    __snake_case				:						Optional[int]=3		,    __snake_case				:						Tuple=0.6		,    __snake_case				:						Union[str, Any]=None		,    ):
      a		:      List[str]       =  parent
      a		:      Tuple       =  batch_size
      a		:      Union[str, Any]       =  image_size
      a		:      List[str]       =  patch_size
      a		:      Optional[Any]       =  num_channels
      a		:      Optional[Any]       =  is_training
      a		:      List[Any]       =  use_labels
      a		:      Union[str, Any]       =  hidden_size
      a		:      Dict       =  num_hidden_layers
      a		:      Optional[Any]       =  num_attention_heads
      a		:      Optional[Any]       =  intermediate_size
      a		:      int       =  hidden_act
      a		:      Dict       =  hidden_dropout_prob
      a		:      Optional[Any]       =  attention_probs_dropout_prob
      a		:      Optional[Any]       =  type_sequence_label_size
      a		:      Optional[Any]       =  initializer_range
      a		:      Union[str, Any]       =  mask_ratio
      a		:      Optional[int]       =  scope

      # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
      # (we add 1 for the [CLS] token)
      a		:      Tuple       =  (image_size // patch_size) ** 2
      a		:      List[Any]       =  int(math.ceil((1 - mask_ratio) * (num_patches + 1)			)			)







    def     lowercase_ (   self				:						List[str]			):
      a		:      str       =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

      a		:      Tuple       =  None
      if self.use_labels:
        a		:      List[str]       =  ids_tensor([self.batch_size]		,    self.type_sequence_label_size			)

      a		:      Optional[int]       =  self.get_config()

      return config, pixel_values, labels







    def     lowercase_ (   self				:						str			):
      return ViTMAEConfig(
          image_size=self.image_size		,    patch_size=self.patch_size		,    num_channels=self.num_channels		,    hidden_size=self.hidden_size		,    num_hidden_layers=self.num_hidden_layers		,    num_attention_heads=self.num_attention_heads		,    intermediate_size=self.intermediate_size		,    hidden_act=self.hidden_act		,    hidden_dropout_prob=self.hidden_dropout_prob		,    attention_probs_dropout_prob=self.attention_probs_dropout_prob		,    is_decoder=__snake_case		,    initializer_range=self.initializer_range		,    mask_ratio=self.mask_ratio		,    )







    def     lowercase_ (   self				:						Dict		,    __snake_case				:						Dict		,    __snake_case				:						List[Any]		,    __snake_case				:						Union[str, Any]			):
      a		:      Union[str, Any]       =  ViTMAEModel(config=__snake_case			)
      model.to(__snake_case			)
      model.eval()
      a		:      Optional[Any]       =  model(__snake_case			)
      self.parent.assertEqual(result.last_hidden_state.shape		,    (self.batch_size, self.seq_length, self.hidden_size)			)







    def     lowercase_ (   self				:						Optional[Any]		,    __snake_case				:						List[str]		,    __snake_case				:						Optional[int]		,    __snake_case				:						Dict			):
      a		:      Dict       =  ViTMAEForPreTraining(__snake_case			)
      model.to(__snake_case			)
      model.eval()
      a		:      Dict       =  model(__snake_case			)
      a		:      str       =  (self.image_size // self.patch_size) ** 2
      a		:      int       =  self.patch_size**2 * self.num_channels
      self.parent.assertEqual(result.logits.shape		,    (self.batch_size, num_patches, expected_num_channels)			)

      # test greyscale images
      a		:      Any       =  1
      a		:      Optional[int]       =  ViTMAEForPreTraining(__snake_case			)
      model.to(__snake_case			)
      model.eval()
      a		:      int       =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
      a		:      Optional[Any]       =  model(__snake_case			)
      a		:      Dict       =  self.patch_size**2
      self.parent.assertEqual(result.logits.shape		,    (self.batch_size, num_patches, expected_num_channels)			)







    def     lowercase_ (   self				:						Dict			):
      a		:      List[str]       =  self.prepare_config_and_inputs()
      a     ,			a     ,			a		:      Any       =  config_and_inputs
      a		:      Optional[Any]       =  {'pixel_values': pixel_values}
      return config, inputs_dict

@require_torch
class 							a__(      lowerCamelCase__	,				lowerCamelCase__	,				unittest.TestCase			):
    lowercase__				=					(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
    lowercase__				=					{"""feature-extraction""": ViTMAEModel} if is_torch_available() else {}

    lowercase__				=					False
    lowercase__				=					False
    lowercase__				=					False
    lowercase__				=					False







    def     lowercase_ (   self				:						Tuple			):
      a		:      List[str]       =  ViTMAEModelTester(self			)
      a		:      List[str]       =  ConfigTester(self		,    config_class=__snake_case		,    has_text_modality=__snake_case		,    hidden_size=37			)







    def     lowercase_ (   self				:						List[str]			):
      self.config_tester.run_common_tests()







    @unittest.skip(reason='ViTMAE does not use inputs_embeds'			)
    def     lowercase_ (   self				:						List[str]			):
      pass







    def     lowercase_ (   self				:						Dict			):
      a     ,			a		:      List[Any]       =  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        a		:      Optional[Any]       =  model_class(__snake_case			)
        self.assertIsInstance(model.get_input_embeddings()		,    (nn.Module)			)
        a		:      Optional[int]       =  model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(__snake_case		,    nn.Linear			)			)







    def     lowercase_ (   self				:						List[str]			):
      a     ,			a		:      Dict       =  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        a		:      Any       =  model_class(__snake_case			)
        a		:      Any       =  inspect.signature(model.forward			)
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        a		:      int       =  [*signature.parameters.keys()]

        a		:      List[Any]       =  ['pixel_values']
        self.assertListEqual(arg_names[:1]		,    __snake_case			)







    def     lowercase_ (   self				:						Any			):
      a		:      Optional[int]       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__snake_case			)







    def     lowercase_ (   self				:						List[Any]			):
      a		:      Dict       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*__snake_case			)







    def     lowercase_ (   self				:						List[str]		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Union[str, Any]		,    __snake_case				:						Tuple			):
      # make masks reproducible
      np.random.seed(2			)

      a		:      Union[str, Any]       =  int((pt_model.config.image_size // pt_model.config.patch_size) ** 2			)
      a		:      int       =  np.random.uniform(size=(self.model_tester.batch_size, num_patches)			)
      a		:      List[Any]       =  torch.from_numpy(__snake_case			)

      # Add `noise` argument.
      # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
      a		:      List[str]       =  pt_noise

      super().check_pt_tf_models(__snake_case		,    __snake_case		,    __snake_case			)







    def     lowercase_ (   self				:						Optional[int]			):
      a     ,			a		:      List[str]       =  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        a		:      Any       =  model_class(__snake_case			)
        model.to(__snake_case			)
        model.eval()
        # make random mask reproducible
        torch.manual_seed(2			)
        with torch.no_grad():
          a		:      Tuple       =  model(**self._prepare_for_class(__snake_case		,    __snake_case			)			)

        a		:      List[str]       =  outputs[0].cpu().numpy()
        a		:      str       =  0

        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_pretrained(__snake_case			)
          a		:      Dict       =  model_class.from_pretrained(__snake_case			)
          model.to(__snake_case			)
          # make random mask reproducible
          torch.manual_seed(2			)
          with torch.no_grad():
            a		:      int       =  model(**self._prepare_for_class(__snake_case		,    __snake_case			)			)

          # Make sure we don't have nans
          a		:      List[Any]       =  after_outputs[0].cpu().numpy()
          a		:      Any       =  0
          a		:      Any       =  np.amax(np.abs(out_a - out_a			)			)
          self.assertLessEqual(__snake_case		,    1e-5			)







    @unittest.skip(
        reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'			)
    def     lowercase_ (   self				:						List[Any]			):
      pass







    @unittest.skip(
        reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'			)
    def     lowercase_ (   self				:						Any			):
      pass







    @unittest.skip(
        reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'			)
    def     lowercase_ (   self				:						int			):
      pass







    @unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load'			)
    def     lowercase_ (   self				:						int			):
      pass







    @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'			)
    def     lowercase_ (   self				:						Any			):
      pass







    @slow
    def     lowercase_ (   self				:						Dict			):
      for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a		:      str       =  ViTMAEModel.from_pretrained(__snake_case			)
        self.assertIsNotNone(__snake_case			)
def    lowerCamelCase__ (		):
  a		:      Tuple       =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
  return image

@require_torch
@require_vision
class 							a__(      unittest.TestCase			):







    @cached_property
    def     lowercase_ (   self				:						Union[str, Any]			):
      return ViTImageProcessor.from_pretrained('facebook/vit-mae-base'			) if is_vision_available() else None







    @slow
    def     lowercase_ (   self				:						Union[str, Any]			):
      # make random mask reproducible across the PT and TF model
      np.random.seed(2			)

      a		:      Union[str, Any]       =  ViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base'			).to(__snake_case			)

      a		:      str       =  self.default_image_processor
      a		:      Dict       =  prepare_img()
      a		:      Union[str, Any]       =  image_processor(images=__snake_case		,    return_tensors='pt'			).to(__snake_case			)

      # prepare a noise vector that will be also used for testing the TF model
      # (this way we can ensure that the PT and TF models operate on the same inputs)
      a		:      Tuple       =  ViTMAEConfig()
      a		:      Optional[Any]       =  int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2			)
      a		:      List[str]       =  np.random.uniform(size=(1, num_patches)			)

      # forward pass
      with torch.no_grad():
        a		:      Tuple       =  model(**__snake_case		,    noise=torch.from_numpy(__snake_case			).to(device=__snake_case			)			)

      # verify the logits
      a		:      int       =  torch.Size((1, 1_96, 7_68)			)
      self.assertEqual(outputs.logits.shape		,    __snake_case			)

      a		:      Optional[Any]       =  torch.tensor(
          [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]]			)

      self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		,    expected_slice.to(__snake_case			)		,    atol=1e-4			)			)