



'''simple docstring'''






import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch
     from torch import nn

     from transformers import (
         MODEL_MAPPING,
         BeitForImageClassification,
         BeitForMaskedImageModeling,
         BeitForSemanticSegmentation,
         BeitModel,
     )
     from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     import PIL
     from PIL import Image

     from transformers import BeitImageProcessor

class 							a__:







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

      # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
      a		:      List[str]       =  (image_size // patch_size) ** 2
      a		:      Any       =  num_patches + 1







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

      a		:      List[Any]       =  None
      a		:      Optional[Any]       =  None
      if self.use_labels:
        a		:      Dict       =  ids_tensor([self.batch_size]		,    self.type_sequence_label_size			)
        a		:      Any       =  ids_tensor([self.batch_size, self.image_size, self.image_size]		,    self.num_labels			)

      a		:      Tuple       =  self.get_config()

      return config, pixel_values, labels, pixel_labels







    def     lowercase_ (   self				:						Any			):
      return BeitConfig(
          vocab_size=self.vocab_size		,    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		,    out_indices=self.out_indices		,    )







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







    def     lowercase_ (   self				:						Dict		,    __snake_case				:						List[Any]		,    __snake_case				:						int		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Any			):
      a		:      Dict       =  BeitForMaskedImageModeling(config=__snake_case			)
      model.to(__snake_case			)
      model.eval()
      a		:      List[str]       =  model(__snake_case			)
      self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.seq_length - 1, self.vocab_size)			)







    def     lowercase_ (   self				:						Optional[Any]		,    __snake_case				:						Tuple		,    __snake_case				:						Union[str, Any]		,    __snake_case				:						Any		,    __snake_case				:						Union[str, Any]			):
      a		:      Dict       =  self.type_sequence_label_size
      a		:      Tuple       =  BeitForImageClassification(__snake_case			)
      model.to(__snake_case			)
      model.eval()
      a		:      List[str]       =  model(__snake_case		,    labels=__snake_case			)
      self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.type_sequence_label_size)			)

      # test greyscale images
      a		:      Optional[int]       =  1
      a		:      Tuple       =  BeitForImageClassification(__snake_case			)
      model.to(__snake_case			)
      model.eval()

      a		:      List[str]       =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
      a		:      Dict       =  model(__snake_case		,    labels=__snake_case			)
      self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.type_sequence_label_size)			)







    def     lowercase_ (   self				:						Tuple		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Any		,    __snake_case				:						Any		,    __snake_case				:						str			):
      a		:      Optional[Any]       =  self.num_labels
      a		:      str       =  BeitForSemanticSegmentation(__snake_case			)
      model.to(__snake_case			)
      model.eval()
      a		:      List[str]       =  model(__snake_case			)
      self.parent.assertEqual(
          result.logits.shape		,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)			)
      a		:      List[str]       =  model(__snake_case		,    labels=__snake_case			)
      self.parent.assertEqual(
          result.logits.shape		,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)			)







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

@require_torch
class 							a__(      lowerCamelCase__	,				lowerCamelCase__	,				unittest.TestCase			):
    lowercase__				=					(
        (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
        if is_torch_available()
        else ()
    )
    lowercase__				=					(
        {
            """feature-extraction""": BeitModel,
            """image-classification""": BeitForImageClassification,
            """image-segmentation""": BeitForSemanticSegmentation,
        }
        if is_torch_available()
        else {}
    )

    lowercase__				=					False
    lowercase__				=					False
    lowercase__				=					False







    def     lowercase_ (   self				:						Optional[Any]			):
      a		:      int       =  BeitModelTester(self			)
      a		:      Union[str, Any]       =  ConfigTester(self		,    config_class=__snake_case		,    has_text_modality=__snake_case		,    hidden_size=37			)







    def     lowercase_ (   self				:						Optional[Any]			):
      self.config_tester.run_common_tests()







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







    @require_torch_multi_gpu
    @unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'			)
    def     lowercase_ (   self				:						str			):
      pass







    def     lowercase_ (   self				:						int			):
      a     ,			a		:      Any       =  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        a		:      str       =  model_class(__snake_case			)
        self.assertIsInstance(model.get_input_embeddings()		,    (nn.Module)			)
        a		:      Tuple       =  model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(__snake_case		,    nn.Linear			)			)







    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		:      Optional[int]       =  model_class(__snake_case			)
        a		:      Optional[int]       =  inspect.signature(model.forward			)
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        a		:      Optional[Any]       =  [*signature.parameters.keys()]

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







    def     lowercase_ (   self				:						Union[str, Any]			):
      a		:      Any       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__snake_case			)







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







    def     lowercase_ (   self				:						Tuple			):
      a		:      int       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__snake_case			)







    def     lowercase_ (   self				:						Union[str, Any]			):
      a		:      str       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_semantic_segmentation(*__snake_case			)







    def     lowercase_ (   self				:						Tuple			):
      if not self.model_tester.is_training:
        return

      a     ,			a		:      List[Any]       =  self.model_tester.prepare_config_and_inputs_for_common()
      a		:      str       =  True

      for model_class in self.all_model_classes:
        # we don't test BeitForMaskedImageModeling
        if model_class in [*get_values(__snake_case			), BeitForMaskedImageModeling]:
          continue

        a		:      int       =  model_class(__snake_case			)
        model.to(__snake_case			)
        model.train()
        a		:      Any       =  self._prepare_for_class(__snake_case		,    __snake_case		,    return_labels=__snake_case			)
        a		:      str       =  model(**__snake_case			).loss
        loss.backward()







    def     lowercase_ (   self				:						Optional[Any]			):
      a     ,			a		:      Optional[Any]       =  self.model_tester.prepare_config_and_inputs_for_common()
      if not self.model_tester.is_training:
        return

      a		:      Union[str, Any]       =  False
      a		:      str       =  True

      for model_class in self.all_model_classes:
        # we don't test BeitForMaskedImageModeling
        if (
            model_class in [*get_values(__snake_case			), BeitForMaskedImageModeling]
            or not model_class.supports_gradient_checkpointing
        ):
          continue

        a		:      int       =  model_class(__snake_case			)
        model.gradient_checkpointing_enable()
        model.to(__snake_case			)
        model.train()
        a		:      Tuple       =  self._prepare_for_class(__snake_case		,    __snake_case		,    return_labels=__snake_case			)
        a		:      Union[str, Any]       =  model(**__snake_case			).loss
        loss.backward()







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

      a		:      Union[str, Any]       =  _config_zero_init(__snake_case			)
      for model_class in self.all_model_classes:
        a		:      List[str]       =  model_class(config=__snake_case			)
        for name, param in model.named_parameters():
          # we skip lambda parameters as these require special initial values
          # determined by config.layer_scale_init_value
          if "lambda" in name:
            continue
          if param.requires_grad:
            self.assertIn(
                ((param.data.mean() * 1e9).round() / 1e9).item()		,    [0.0, 1.0]		,    msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""		,    )







    @slow
    def     lowercase_ (   self				:						Any			):
      for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a		:      Tuple       =  BeitModel.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				:						str			):
      return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224'			) if is_vision_available() else None







    @slow
    def     lowercase_ (   self				:						Optional[int]			):
      a		:      int       =  BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k'			).to(__snake_case			)

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

      # prepare bool_masked_pos
      a		:      Any       =  torch.ones((1, 1_96)		,    dtype=torch.bool			).to(__snake_case			)

      # forward pass
      with torch.no_grad():
        a		:      List[str]       =  model(pixel_values=__snake_case		,    bool_masked_pos=__snake_case			)
      a		:      int       =  outputs.logits

      # verify the logits
      a		:      str       =  torch.Size((1, 1_96, 81_92)			)
      self.assertEqual(logits.shape		,    __snake_case			)

      a		:      Dict       =  torch.tensor(
          [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]			).to(__snake_case			)

      self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]		,    __snake_case		,    atol=1e-2			)			)







    @slow
    def     lowercase_ (   self				:						Optional[int]			):
      a		:      List[Any]       =  BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'			).to(__snake_case			)

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

      # forward pass
      with torch.no_grad():
        a		:      Optional[int]       =  model(**__snake_case			)
      a		:      List[str]       =  outputs.logits

      # verify the logits
      a		:      str       =  torch.Size((1, 10_00)			)
      self.assertEqual(logits.shape		,    __snake_case			)

      a		:      Tuple       =  torch.tensor([-1.2385, -1.0987, -1.0108]			).to(__snake_case			)

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

      a		:      int       =  2_81
      self.assertEqual(logits.argmax(-1			).item()		,    __snake_case			)







    @slow
    def     lowercase_ (   self				:						Tuple			):
      a		:      List[str]       =  BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k'			).to(
          __snake_case			)

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

      # forward pass
      with torch.no_grad():
        a		:      str       =  model(**__snake_case			)
      a		:      int       =  outputs.logits

      # verify the logits
      a		:      Any       =  torch.Size((1, 2_18_41)			)
      self.assertEqual(logits.shape		,    __snake_case			)

      a		:      Union[str, Any]       =  torch.tensor([1.6881, -0.2787, 0.5901]			).to(__snake_case			)

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

      a		:      Dict       =  23_96
      self.assertEqual(logits.argmax(-1			).item()		,    __snake_case			)







    @slow
    def     lowercase_ (   self				:						List[Any]			):
      a		:      Union[str, Any]       =  BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'			)
      a		:      Tuple       =  model.to(__snake_case			)

      a		:      List[Any]       =  BeitImageProcessor(do_resize=__snake_case		,    size=6_40		,    do_center_crop=__snake_case			)

      a		:      str       =  load_dataset('hf-internal-testing/fixtures_ade20k'		,    split='test'			)
      a		:      List[Any]       =  Image.open(ds[0]['file']			)
      a		:      int       =  image_processor(images=__snake_case		,    return_tensors='pt'			).to(__snake_case			)

      # forward pass
      with torch.no_grad():
        a		:      Dict       =  model(**__snake_case			)
      a		:      str       =  outputs.logits

      # verify the logits
      a		:      Optional[int]       =  torch.Size((1, 1_50, 1_60, 1_60)			)
      self.assertEqual(logits.shape		,    __snake_case			)

      a		:      List[str]       =  version.parse(PIL.__version__			) < version.parse('9.0.0'			)

      if is_pillow_less_than_a:
        a		:      str       =  torch.tensor(
            [
                [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]],
                [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]],
                [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]],
            ]		,    device=__snake_case		,    )
      else:
        a		:      Union[str, Any]       =  torch.tensor(
            [
                [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]],
                [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]],
                [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]],
            ]		,    device=__snake_case		,    )

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







    @slow
    def     lowercase_ (   self				:						List[Any]			):
      a		:      Tuple       =  BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'			)
      a		:      List[Any]       =  model.to(__snake_case			)

      a		:      List[Any]       =  BeitImageProcessor(do_resize=__snake_case		,    size=6_40		,    do_center_crop=__snake_case			)

      a		:      Optional[int]       =  load_dataset('hf-internal-testing/fixtures_ade20k'		,    split='test'			)
      a		:      Optional[int]       =  Image.open(ds[0]['file']			)
      a		:      Union[str, Any]       =  image_processor(images=__snake_case		,    return_tensors='pt'			).to(__snake_case			)

      # forward pass
      with torch.no_grad():
        a		:      int       =  model(**__snake_case			)

      a		:      Dict       =  outputs.logits.detach().cpu()

      a		:      List[Any]       =  image_processor.post_process_semantic_segmentation(outputs=__snake_case		,    target_sizes=[(5_00, 3_00)]			)
      a		:      Union[str, Any]       =  torch.Size((5_00, 3_00)			)
      self.assertEqual(segmentation[0].shape		,    __snake_case			)

      a		:      Optional[Any]       =  image_processor.post_process_semantic_segmentation(outputs=__snake_case			)
      a		:      Optional[int]       =  torch.Size((1_60, 1_60)			)
      self.assertEqual(segmentation[0].shape		,    __snake_case			)




'''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:      Any		 =		False

@skip_mps
class 							a__(      lowerCamelCase__	,				lowerCamelCase__	,				lowerCamelCase__	,				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				:						Union[str, Any]			):
      super().setUpClass()
      torch.use_deterministic_algorithms(__snake_case			)







    @classmethod
    def     lowercase_ (   cls				:						Optional[Any]			):
      super().tearDownClass()
      torch.use_deterministic_algorithms(__snake_case			)







    def     lowercase_ (   self				:						List[Any]			):
      torch.manual_seed(0			)
      a		:      List[Any]       =  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=__snake_case		,    )
      a		:      Optional[int]       =  DDIMScheduler(
          beta_start=0.00085		,    beta_end=0.012		,    beta_schedule='scaled_linear'		,    clip_sample=__snake_case		,    set_alpha_to_one=__snake_case		,    )
      torch.manual_seed(0			)
      a		:      str       =  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		:      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		:      Any       =  CLIPTextModel(__snake_case			)
      a		:      Tuple       =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'			)

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

      return components







    def     lowercase_ (   self				:						str		,    __snake_case				:						Tuple		,    __snake_case				:						Optional[int]=0			):
      if str(__snake_case			).startswith('mps'			):
        a		:      Any       =  torch.manual_seed(__snake_case			)
      else:
        a		:      Any       =  torch.Generator(device=__snake_case			).manual_seed(__snake_case			)
      a		:      int       =  {
          '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				:						List[Any]			):
      a		:      Union[str, Any]       =  'cpu'

      a		:      Any       =  self.get_dummy_components()
      a		:      List[str]       =  self.pipeline_class(**__snake_case			)
      pipe.to(__snake_case			)
      pipe.set_progress_bar_config(disable=__snake_case			)

      a		:      Any       =  self.get_dummy_inputs(__snake_case			)
      a		:      int       =  pipe(**__snake_case			).images
      a		:      int       =  image[0, -3:, -3:, -1]

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







    def     lowercase_ (   self				:						Dict			):
      super().test_cpu_offload_forward_pass(expected_max_diff=5e-4			)







    def     lowercase_ (   self				:						Tuple			):
      # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
      self._test_inference_batch_consistent(batch_sizes=[1, 2]			)







    def     lowercase_ (   self				:						Union[str, Any]			):
      self._test_inference_batch_single_identical(batch_size=2		,    expected_max_diff=7e-4			)







    def     lowercase_ (   self				:						Tuple			):
      super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3			)







    def     lowercase_ (   self				:						str			):
      super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4			)







    def     lowercase_ (   self				:						Any			):
      super().test_save_load_local(expected_max_difference=5e-4			)







    def     lowercase_ (   self				:						List[Any]			):
      super().test_save_load_optional_components(expected_max_difference=4e-4			)

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







    @classmethod
    def     lowercase_ (   cls				:						Union[str, Any]			):
      super().setUpClass()
      torch.use_deterministic_algorithms(__snake_case			)







    @classmethod
    def     lowercase_ (   cls				:						Union[str, Any]			):
      super().tearDownClass()
      torch.use_deterministic_algorithms(__snake_case			)







    def     lowercase_ (   self				:						Union[str, Any]			):
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()







    def     lowercase_ (   self				:						Optional[Any]			):
      a		:      List[Any]       =  torch.manual_seed(51			)

      a		:      Dict       =  StableDiffusionAttendAndExcitePipeline.from_pretrained(
          'CompVis/stable-diffusion-v1-4'		,    safety_checker=__snake_case		,    torch_dtype=torch.floataa			)
      pipe.to('cuda'			)

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

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

      a		:      str       =  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