


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


__snake_case    					=					False

@skip_mps
class 				_SCREAMING_SNAKE_CASE				(							__UpperCAmelCase     ,   __UpperCAmelCase     ,   __UpperCAmelCase     ,   unittest.TestCase  ):
   """simple docstring"""



   _a   :       int									=    StableDiffusionAttendAndExcitePipeline
   _a   :       Union[str, Any]									=    False
   _a   :       List[Any]									=    TEXT_TO_IMAGE_PARAMS
   _a   :       List[Any]									=    TEXT_TO_IMAGE_BATCH_PARAMS.union({'''token_indices'''}  )
   _a   :       Union[str, Any]									=    TEXT_TO_IMAGE_IMAGE_PARAMS
   _a   :       Any									=    TEXT_TO_IMAGE_IMAGE_PARAMS


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


   @classmethod
   def 					UpperCAmelCase__(						cls						)      ->							int:
    super().tearDownClass()
    torch.use_deterministic_algorithms(lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							List[Any]:
    torch.manual_seed(0						)
    lowercase__       :       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__ ,     )
    lowercase__       :       Any				=						DDIMScheduler(
        beta_start=0.0_0085 ,     beta_end=0.012 ,     beta_schedule="""scaled_linear""" ,     clip_sample=lowerCamelCase__ ,     set_alpha_to_one=lowerCamelCase__ ,     )
    torch.manual_seed(0						)
    lowercase__       :       Dict				=						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						)
    lowercase__       :       int				=						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 ,     )
    lowercase__       :       List[str]				=						CLIPTextModel(lowerCamelCase__						)
    lowercase__       :       Dict				=						CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""						)

    lowercase__       :       int				=						{
        """unet""": unet,
        """scheduler""": scheduler,
        """vae""": vae,
        """text_encoder""": text_encoder,
        """tokenizer""": tokenizer,
        """safety_checker""": None,
        """feature_extractor""": None,
    }

    return components


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__=0						)      ->							List[Any]:
    if str(lowerCamelCase__						).startswith("""mps"""						):
     lowercase__       :       Any				=						torch.manual_seed(lowerCamelCase__						)
    else:
     lowercase__       :       Tuple				=						torch.Generator(device=lowerCamelCase__						).manual_seed(lowerCamelCase__						)
    lowercase__       :       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 					UpperCAmelCase__(						self						)      ->							Any:
    lowercase__       :       Optional[Any]				=						"""cpu"""

    lowercase__       :       Optional[Any]				=						self.get_dummy_components()
    lowercase__       :       Optional[int]				=						self.pipeline_class(**lowerCamelCase__						)
    pipe.to(lowerCamelCase__						)
    pipe.set_progress_bar_config(disable=lowerCamelCase__						)

    lowercase__       :       List[Any]				=						self.get_dummy_inputs(lowerCamelCase__						)
    lowercase__       :       List[Any]				=						pipe(**lowerCamelCase__						).images
    lowercase__       :       Optional[int]				=						image[0, -3:, -3:, -1]

    self.assertEqual(image.shape ,     (1, 64, 64, 3)						)
    lowercase__       :       List[str]				=						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]						)
    lowercase__       :       Optional[int]				=						np.abs(image_slice.flatten() - expected_slice						).max()
    self.assertLessEqual(lowerCamelCase__ ,     1E-3						)


   def 					UpperCAmelCase__(						self						)      ->							Tuple:
    super().test_cpu_offload_forward_pass(expected_max_diff=5E-4						)


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


   def 					UpperCAmelCase__(						self						)      ->							str:
    self._test_inference_batch_single_identical(batch_size=2 ,     expected_max_diff=7E-4						)


   def 					UpperCAmelCase__(						self						)      ->							Any:
    super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3						)


   def 					UpperCAmelCase__(						self						)      ->							Any:
    super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5E-4						)


   def 					UpperCAmelCase__(						self						)      ->							str:
    super().test_save_load_local(expected_max_difference=5E-4						)







   def 					UpperCAmelCase__(						self						)      ->							List[str]:
    super().test_save_load_optional_components(expected_max_difference=4E-4						)

@require_torch_gpu
@slow
class 				_SCREAMING_SNAKE_CASE				(							unittest.TestCase  ):
   """simple docstring"""



   @classmethod
   def 					UpperCAmelCase__(						cls						)      ->							Optional[Any]:
    super().setUpClass()
    torch.use_deterministic_algorithms(lowerCamelCase__						)


   @classmethod
   def 					UpperCAmelCase__(						cls						)      ->							Dict:
    super().tearDownClass()
    torch.use_deterministic_algorithms(lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							str:
    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()







   def 					UpperCAmelCase__(						self						)      ->							Union[str, Any]:
    lowercase__       :       Tuple				=						torch.manual_seed(51						)

    lowercase__       :       Optional[Any]				=						StableDiffusionAttendAndExcitePipeline.from_pretrained(
        """CompVis/stable-diffusion-v1-4""" ,     safety_checker=lowerCamelCase__ ,     torch_dtype=torch.floataa						)
    pipe.to("""cuda"""						)

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

    lowercase__       :       Dict				=						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]

    lowercase__       :       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



"""simple docstring"""


import inspect
import unittest

from transformers import MobileNetVaConfig
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 transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
   from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import MobileNetVaImageProcessor

class 				_SCREAMING_SNAKE_CASE				(							__UpperCAmelCase  ):
   """simple docstring"""



   def 					UpperCAmelCase__(						self						)      ->							Union[str, Any]:
    lowercase__       :       Optional[Any]				=						self.config_class(**self.inputs_dict						)
    self.parent.assertTrue(hasattr(lowerCamelCase__ ,     """tf_padding"""						)						)
    self.parent.assertTrue(hasattr(lowerCamelCase__ ,     """depth_multiplier"""						)						)

class 				_SCREAMING_SNAKE_CASE				:
   """simple docstring"""



   def __init__(						self ,     lowerCamelCase__ ,     lowerCamelCase__=13 ,     lowerCamelCase__=3 ,     lowerCamelCase__=32 ,     lowerCamelCase__=0.25 ,     lowerCamelCase__=8 ,     lowerCamelCase__=8 ,     lowerCamelCase__=6 ,     lowerCamelCase__=32 ,     lowerCamelCase__=True ,     lowerCamelCase__=True ,     lowerCamelCase__=True ,     lowerCamelCase__="relu6" ,     lowerCamelCase__=1280 ,     lowerCamelCase__=0.1 ,     lowerCamelCase__=0.02 ,     lowerCamelCase__=True ,     lowerCamelCase__=True ,     lowerCamelCase__=10 ,     lowerCamelCase__=None ,     )      ->							List[Any]:
    lowercase__       :       int				=						parent
    lowercase__       :       Any				=						batch_size
    lowercase__       :       Optional[int]				=						num_channels
    lowercase__       :       int				=						image_size
    lowercase__       :       Tuple				=						depth_multiplier
    lowercase__       :       Tuple				=						depth_divisible_by
    lowercase__       :       str				=						min_depth
    lowercase__       :       int				=						expand_ratio
    lowercase__       :       List[str]				=						tf_padding
    lowercase__       :       Union[str, Any]				=						output_stride
    lowercase__       :       Optional[int]				=						first_layer_is_expansion
    lowercase__       :       Optional[Any]				=						finegrained_output
    lowercase__       :       Union[str, Any]				=						hidden_act
    lowercase__       :       List[Any]				=						last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier						)
    lowercase__       :       Union[str, Any]				=						classifier_dropout_prob
    lowercase__       :       str				=						use_labels
    lowercase__       :       Optional[Any]				=						is_training
    lowercase__       :       List[Any]				=						num_labels
    lowercase__       :       List[str]				=						initializer_range
    lowercase__       :       Optional[int]				=						scope


   def 					UpperCAmelCase__(						self						)      ->							Any:
    lowercase__       :       Dict				=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

    lowercase__       :       Dict				=						self.get_config()

    return config, pixel_values, labels, pixel_labels


   def 					UpperCAmelCase__(						self						)      ->							int:
    return MobileNetVaConfig(
        num_channels=self.num_channels ,     image_size=self.image_size ,     depth_multiplier=self.depth_multiplier ,     depth_divisible_by=self.depth_divisible_by ,     min_depth=self.min_depth ,     expand_ratio=self.expand_ratio ,     output_stride=self.output_stride ,     first_layer_is_expansion=self.first_layer_is_expansion ,     finegrained_output=self.finegrained_output ,     hidden_act=self.hidden_act ,     tf_padding=self.tf_padding ,     classifier_dropout_prob=self.classifier_dropout_prob ,     initializer_range=self.initializer_range ,     )


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							Dict:
    lowercase__       :       str				=						MobileNetVaModel(config=lowerCamelCase__						)
    model.to(lowerCamelCase__						)
    model.eval()
    lowercase__       :       List[Any]				=						model(lowerCamelCase__						)
    self.parent.assertEqual(
        result.last_hidden_state.shape ,     (
            self.batch_size,
            self.last_hidden_size,
            self.image_size // self.output_stride,
            self.image_size // self.output_stride,
        ) ,     )
    self.parent.assertEqual(
        result.pooler_output.shape ,     (self.batch_size, self.last_hidden_size) ,     )


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							List[Any]:
    lowercase__       :       Optional[int]				=						self.num_labels
    lowercase__       :       List[str]				=						MobileNetVaForImageClassification(lowerCamelCase__						)
    model.to(lowerCamelCase__						)
    model.eval()
    lowercase__       :       Dict				=						model(lowerCamelCase__ ,     labels=lowerCamelCase__						)
    self.parent.assertEqual(result.logits.shape ,     (self.batch_size, self.num_labels)						)


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							str:
    lowercase__       :       Dict				=						self.num_labels
    lowercase__       :       List[Any]				=						MobileNetVaForSemanticSegmentation(lowerCamelCase__						)
    model.to(lowerCamelCase__						)
    model.eval()
    lowercase__       :       List[Any]				=						model(lowerCamelCase__						)
    self.parent.assertEqual(
        result.logits.shape ,     (
            self.batch_size,
            self.num_labels,
            self.image_size // self.output_stride,
            self.image_size // self.output_stride,
        ) ,     )
    lowercase__       :       Tuple				=						model(lowerCamelCase__ ,     labels=lowerCamelCase__						)
    self.parent.assertEqual(
        result.logits.shape ,     (
            self.batch_size,
            self.num_labels,
            self.image_size // self.output_stride,
            self.image_size // self.output_stride,
        ) ,     )







   def 					UpperCAmelCase__(						self						)      ->							int:
    lowercase__       :       Optional[int]				=						self.prepare_config_and_inputs()
    lowercase__	,       lowercase__	,       lowercase__	,       lowercase__       :       Tuple				=						config_and_inputs
    lowercase__       :       List[Any]				=						{"""pixel_values""": pixel_values}
    return config, inputs_dict

@require_torch
class 				_SCREAMING_SNAKE_CASE				(							__UpperCAmelCase     ,   __UpperCAmelCase     ,   unittest.TestCase  ):
   """simple docstring"""



   _a   :       Optional[int]									=    (
       (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
       if is_torch_available()
       else ()
   )
   _a   :       Tuple									=    (
       {
           '''feature-extraction''': MobileNetVaModel,
           '''image-classification''': MobileNetVaForImageClassification,
           '''image-segmentation''': MobileNetVaForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   _a   :       List[str]									=    False
   _a   :       int									=    False
   _a   :       Optional[Any]									=    False
   _a   :       List[str]									=    False


   def 					UpperCAmelCase__(						self						)      ->							Optional[int]:
    lowercase__       :       List[Any]				=						MobileNetVaModelTester(self						)
    lowercase__       :       Any				=						MobileNetVaConfigTester(self ,     config_class=lowerCamelCase__ ,     has_text_modality=lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							Union[str, Any]:
    self.config_tester.run_common_tests()


   @unittest.skip(reason="""MobileNetV2 does not use inputs_embeds"""						)
   def 					UpperCAmelCase__(						self						)      ->							str:
    pass


   @unittest.skip(reason="""MobileNetV2 does not support input and output embeddings"""						)
   def 					UpperCAmelCase__(						self						)      ->							List[Any]:
    pass


   @unittest.skip(reason="""MobileNetV2 does not output attentions"""						)
   def 					UpperCAmelCase__(						self						)      ->							List[Any]:
    pass


   def 					UpperCAmelCase__(						self						)      ->							str:
    lowercase__	,       lowercase__       :       Optional[int]				=						self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     lowercase__       :       Union[str, Any]				=						model_class(lowerCamelCase__						)
     lowercase__       :       List[Any]				=						inspect.signature(model.forward						)
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     lowercase__       :       Union[str, Any]				=						[*signature.parameters.keys()]

     lowercase__       :       Union[str, Any]				=						["""pixel_values"""]
     self.assertListEqual(arg_names[:1] ,     lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							Union[str, Any]:
    lowercase__       :       Any				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							Any:
    def check_hidden_states_output(lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						):
     lowercase__       :       Optional[int]				=						model_class(lowerCamelCase__						)
     model.to(lowerCamelCase__						)
     model.eval()

     with torch.no_grad():
      lowercase__       :       str				=						model(**self._prepare_for_class(lowerCamelCase__ ,     lowerCamelCase__						)						)

     lowercase__       :       int				=						outputs.hidden_states

     lowercase__       :       Optional[int]				=						16
     self.assertEqual(len(lowerCamelCase__						) ,     lowerCamelCase__						)

    lowercase__	,       lowercase__       :       List[str]				=						self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     lowercase__       :       Tuple				=						True
     check_hidden_states_output(lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)

     # check that output_hidden_states also work using config
     del inputs_dict["output_hidden_states"]
     lowercase__       :       Union[str, Any]				=						True

     check_hidden_states_output(lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							str:
    lowercase__       :       Union[str, Any]				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							str:
    lowercase__       :       str				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase__						)







   @slow
   def 					UpperCAmelCase__(						self						)      ->							Optional[int]:
    for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowercase__       :       List[str]				=						MobileNetVaModel.from_pretrained(lowerCamelCase__						)
     self.assertIsNotNone(lowerCamelCase__						)


def    _lowerCamelCase   (     ):
 lowercase__       :       List[Any]				=						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
 return image






@require_torch
@require_vision
class 				_SCREAMING_SNAKE_CASE				(							unittest.TestCase  ):
   """simple docstring"""



   @cached_property
   def 					UpperCAmelCase__(						self						)      ->							Optional[Any]:
    return (
        MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224"""						) if is_vision_available() else None
    )


   @slow
   def 					UpperCAmelCase__(						self						)      ->							Optional[int]:
    lowercase__       :       Union[str, Any]				=						MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""						).to(lowerCamelCase__						)

    lowercase__       :       int				=						self.default_image_processor
    lowercase__       :       str				=						prepare_img()
    lowercase__       :       Optional[int]				=						image_processor(images=lowerCamelCase__ ,     return_tensors="""pt"""						).to(lowerCamelCase__						)

    # forward pass
    with torch.no_grad():
     lowercase__       :       Optional[Any]				=						model(**lowerCamelCase__						)

    # verify the logits
    lowercase__       :       List[Any]				=						torch.Size((1, 1001)						)
    self.assertEqual(outputs.logits.shape ,     lowerCamelCase__						)

    lowercase__       :       List[Any]				=						torch.tensor([0.2445, -1.1993, 0.1905]						).to(lowerCamelCase__						)

    self.assertTrue(torch.allclose(outputs.logits[0, :3] ,     lowerCamelCase__ ,     atol=1E-4						)						)







   @slow
   def 					UpperCAmelCase__(						self						)      ->							List[str]:
    lowercase__       :       Any				=						MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""						)
    lowercase__       :       Optional[int]				=						model.to(lowerCamelCase__						)

    lowercase__       :       int				=						MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""						)

    lowercase__       :       Tuple				=						prepare_img()
    lowercase__       :       int				=						image_processor(images=lowerCamelCase__ ,     return_tensors="""pt"""						).to(lowerCamelCase__						)

    # forward pass
    with torch.no_grad():
     lowercase__       :       List[Any]				=						model(**lowerCamelCase__						)
    lowercase__       :       int				=						outputs.logits

    # verify the logits
    lowercase__       :       Any				=						torch.Size((1, 21, 65, 65)						)
    self.assertEqual(logits.shape ,     lowerCamelCase__						)

    lowercase__       :       Union[str, Any]				=						torch.tensor(
        [
            [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
            [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
            [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
        ] ,     device=lowerCamelCase__ ,     )

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