


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


  '''simple docstring'''







  def __init__(							self :       Optional[int]	,_a :       Tuple	,_a :       Dict=13	,_a :       Tuple=30	,_a :       Dict=2	,_a :       List[Any]=3	,_a :       List[Any]=True	,_a :       Dict=True	,_a :       Optional[Any]=32	,_a :       List[str]=5	,_a :       List[Any]=4	,_a :       str=37	,_a :       List[Any]="gelu"	,_a :       Dict=0.1	,_a :       Optional[int]=0.1	,_a :       Tuple=10	,_a :       Optional[Any]=0.02	,_a :       Dict=3	,_a :       List[str]=0.6	,_a :       Optional[Any]=None	,):

        '''simple docstring'''




        A_						:  List[str]          =				parent
        A_						:  Optional[int]          =				batch_size
        A_						:  Any          =				image_size
        A_						:  Tuple          =				patch_size
        A_						:  Any          =				num_channels
        A_						:  Dict          =				is_training
        A_						:  str          =				use_labels
        A_						:  List[str]          =				hidden_size
        A_						:  Optional[Any]          =				num_hidden_layers
        A_						:  Optional[int]          =				num_attention_heads
        A_						:  Optional[Any]          =				intermediate_size
        A_						:  str          =				hidden_act
        A_						:  Union[str, Any]          =				hidden_dropout_prob
        A_						:  Optional[int]          =				attention_probs_dropout_prob
        A_						:  List[str]          =				type_sequence_label_size
        A_						:  Optional[int]          =				initializer_range
        A_						:  Optional[Any]          =				mask_ratio
        A_						:  Optional[Any]          =				scope

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






  def        _a							(							self :       str ):

        '''simple docstring'''




        A_						:  Dict          =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

        A_						:  Any          =				self.get_config()

        return config, pixel_values, labels






  def        _a							(							self :       List[Any] ):

        '''simple docstring'''




        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=_a	,initializer_range=self.initializer_range	,mask_ratio=self.mask_ratio	,)






  def        _a							(							self :       Dict	,_a :       List[Any]	,_a :       str	,_a :       List[Any] ):

        '''simple docstring'''




        A_						:  int          =				ViTMAEModel(config=_a )
        model.to(_a )
        model.eval()
        A_						:  Tuple          =				model(_a )
        self.parent.assertEqual(result.last_hidden_state.shape	,(self.batch_size, self.seq_length, self.hidden_size) )






  def        _a							(							self :       Union[str, Any]	,_a :       Any	,_a :       int	,_a :       List[Any] ):

        '''simple docstring'''




        A_						:  Union[str, Any]          =				ViTMAEForPreTraining(_a )
        model.to(_a )
        model.eval()
        A_						:  Optional[Any]          =				model(_a )
        A_						:  Any          =				(self.image_size // self.patch_size) ** 2
        A_						:  str          =				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_						:  int          =				1
        A_						:  Tuple          =				ViTMAEForPreTraining(_a )
        model.to(_a )
        model.eval()
        A_						:  str          =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
        A_						:  str          =				model(_a )
        A_						:  Any          =				self.patch_size**2
        self.parent.assertEqual(result.logits.shape	,(self.batch_size, num_patches, expected_num_channels) )






  def        _a							(							self :       Any ):

        '''simple docstring'''




        A_						:  List[str]          =				self.prepare_config_and_inputs()
        A_   ,					A_   ,					A_						:  Dict          =				config_and_inputs
        A_						:  List[str]          =				{"""pixel_values""": pixel_values}
        return config, inputs_dict







@require_torch
class 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE  , __SCREAMING_SNAKE_CASE  , unittest.TestCase    ):


  '''simple docstring'''







  a_  					=							(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
  a_  					=							{"""feature-extraction""": ViTMAEModel} if is_torch_available() else {}

  a_  					=							False
  a_  					=							False
  a_  					=							False
  a_  					=							False






  def        _a							(							self :       Dict ):

        '''simple docstring'''




        A_						:  List[Any]          =				ViTMAEModelTester(self )
        A_						:  str          =				ConfigTester(self	,config_class=_a	,has_text_modality=_a	,hidden_size=37 )






  def        _a							(							self :       Union[str, Any] ):

        '''simple docstring'''




        self.config_tester.run_common_tests()






  @unittest.skip(reason="""ViTMAE does not use inputs_embeds""" )
  def        _a							(							self :       Any ):

        '''simple docstring'''




        pass






  def        _a							(							self :       Optional[Any] ):

        '''simple docstring'''




        A_   ,					A_						:  Dict          =				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              A_						:  List[str]          =				model_class(_a )
              self.assertIsInstance(model.get_input_embeddings()	,(nn.Module) )
              A_						:  Dict          =				model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(_a	,nn.Linear ) )






  def        _a							(							self :       str ):

        '''simple docstring'''




        A_   ,					A_						:  int          =				self.model_tester.prepare_config_and_inputs_for_common()

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

              A_						:  Optional[int]          =				["""pixel_values"""]
              self.assertListEqual(arg_names[:1]	,_a )






  def        _a							(							self :       List[str] ):

        '''simple docstring'''




        A_						:  Optional[Any]          =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_a )






  def        _a							(							self :       str ):

        '''simple docstring'''




        A_						:  Union[str, Any]          =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*_a )






  def        _a							(							self :       str	,_a :       str	,_a :       str	,_a :       Any ):

        '''simple docstring'''




        np.random.seed(2 )

        A_						:  str          =				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_						:  Optional[Any]          =				torch.from_numpy(_a )

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

        super().check_pt_tf_models(_a	,_a	,_a )






  def        _a							(							self :       int ):

        '''simple docstring'''




        A_   ,					A_						:  Any          =				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              A_						:  List[str]          =				model_class(_a )
              model.to(_a )
              model.eval()
              # make random mask reproducible
              torch.manual_seed(2 )
              with torch.no_grad():
                    A_						:  Dict          =				model(**self._prepare_for_class(_a	,_a ) )

              A_						:  Tuple          =				outputs[0].cpu().numpy()
              A_						:  Optional[Any]          =				0

              with tempfile.TemporaryDirectory() as tmpdirname:
                    model.save_pretrained(_a )
                    A_						:  Union[str, Any]          =				model_class.from_pretrained(_a )
                    model.to(_a )
                    # make random mask reproducible
                    torch.manual_seed(2 )
                    with torch.no_grad():
                          A_						:  Union[str, Any]          =				model(**self._prepare_for_class(_a	,_a ) )

                    # Make sure we don't have nans
                    A_						:  Optional[int]          =				after_outputs[0].cpu().numpy()
                    A_						:  Tuple          =				0
                    A_						:  int          =				np.amax(np.abs(out_a - out_a ) )
                    self.assertLessEqual(_a	,1e-5 )






  @unittest.skip(
      reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load
    to get deterministic results.""" )
  def        _a							(							self :       Union[str, Any] ):

        '''simple docstring'''




        pass






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

        '''simple docstring'''




        pass






  @unittest.skip(
      reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load
    to get deterministic results.""" )
  def        _a							(							self :       Optional[int] ):

        '''simple docstring'''




        pass






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

        '''simple docstring'''




        pass






  @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" )
  def        _a							(							self :       Tuple ):

        '''simple docstring'''




        pass






  @slow
  def        _a							(							self :       int ):

        '''simple docstring'''




        for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              A_						:  Optional[int]          =				ViTMAEModel.from_pretrained(_a )
              self.assertIsNotNone(_a )







def      lowerCamelCase					(      ):
      A_						:  Optional[int]          =				Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
      return image






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


  '''simple docstring'''







  @cached_property
  def        _a							(							self :       Union[str, Any] ):

        '''simple docstring'''




        return ViTImageProcessor.from_pretrained("""facebook/vit-mae-base""" ) if is_vision_available() else None






  @slow
  def        _a							(							self :       int ):

        '''simple docstring'''




        np.random.seed(2 )

        A_						:  Optional[Any]          =				ViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base""" ).to(_a )

        A_						:  Dict          =				self.default_image_processor
        A_						:  Union[str, Any]          =				prepare_img()
        A_						:  List[Any]          =				image_processor(images=_a	,return_tensors="""pt""" ).to(_a )

        # 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_						:  Optional[int]          =				ViTMAEConfig()
        A_						:  int          =				int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 )
        A_						:  Dict          =				np.random.uniform(size=(1, num_patches) )

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

        # verify the logits
        A_						:  Any          =				torch.Size((1, 196, 768) )
        self.assertEqual(outputs.logits.shape	,_a )

        A_						:  int          =				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(_a )	,atol=1e-4 ) )




'''simple docstring'''


import gc
import random
import unittest

import numpy as np
import torch

from diffusers import (
    DDIMScheduler,
    KandinskyVaaControlnetPipeline,
    KandinskyVaaPriorPipeline,
    UNetaDConditionModel,
    VQModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()



class 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE  , unittest.TestCase    ):


  '''simple docstring'''







  a_  					=							KandinskyVaaControlnetPipeline
  a_  					=							["""image_embeds""", """negative_image_embeds""", """hint"""]
  a_  					=							["""image_embeds""", """negative_image_embeds""", """hint"""]
  a_  					=							[
      """generator""",
      """height""",
      """width""",
      """latents""",
      """guidance_scale""",
      """num_inference_steps""",
      """return_dict""",
      """guidance_scale""",
      """num_images_per_prompt""",
      """output_type""",
      """return_dict""",
  ]
  a_  					=							False






  @property
  def        _a							(							self :       Any ):

        '''simple docstring'''




        return 32






  @property
  def        _a							(							self :       Tuple ):

        '''simple docstring'''




        return 32






  @property
  def        _a							(							self :       Tuple ):

        '''simple docstring'''




        return self.time_input_dim






  @property
  def        _a							(							self :       str ):

        '''simple docstring'''




        return self.time_input_dim * 4






  @property
  def        _a							(							self :       Optional[Any] ):

        '''simple docstring'''




        return 100






  @property
  def        _a							(							self :       List[Any] ):

        '''simple docstring'''




        torch.manual_seed(0 )

        A_						:  List[Any]          =				{
            """in_channels""": 8,
            # Out channels is double in channels because predicts mean and variance
            """out_channels""": 8,
            """addition_embed_type""": """image_hint""",
            """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""),
            """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""),
            """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""",
            """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2),
            """layers_per_block""": 1,
            """encoder_hid_dim""": self.text_embedder_hidden_size,
            """encoder_hid_dim_type""": """image_proj""",
            """cross_attention_dim""": self.cross_attention_dim,
            """attention_head_dim""": 4,
            """resnet_time_scale_shift""": """scale_shift""",
            """class_embed_type""": None,
        }

        A_						:  Tuple          =				UNetaDConditionModel(**_a )
        return model






  @property
  def        _a							(							self :       List[str] ):

        '''simple docstring'''




        return {
            "block_out_channels": [32, 32, 64, 64],
            "down_block_types": [
                "DownEncoderBlock2D",
                "DownEncoderBlock2D",
                "DownEncoderBlock2D",
                "AttnDownEncoderBlock2D",
            ],
            "in_channels": 3,
            "latent_channels": 4,
            "layers_per_block": 1,
            "norm_num_groups": 8,
            "norm_type": "spatial",
            "num_vq_embeddings": 12,
            "out_channels": 3,
            "up_block_types": ["AttnUpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"],
            "vq_embed_dim": 4,
        }






  @property
  def        _a							(							self :       Optional[int] ):

        '''simple docstring'''




        torch.manual_seed(0 )
        A_						:  int          =				VQModel(**self.dummy_movq_kwargs )
        return model






  def        _a							(							self :       List[str] ):

        '''simple docstring'''




        A_						:  Optional[Any]          =				self.dummy_unet
        A_						:  int          =				self.dummy_movq

        A_						:  Tuple          =				DDIMScheduler(
            num_train_timesteps=1000	,beta_schedule="""linear"""	,beta_start=0.00085	,beta_end=0.012	,clip_sample=_a	,set_alpha_to_one=_a	,steps_offset=1	,prediction_type="""epsilon"""	,thresholding=_a	,)

        A_						:  int          =				{
            """unet""": unet,
            """scheduler""": scheduler,
            """movq""": movq,
        }
        return components






  def        _a							(							self :       Dict	,_a :       str	,_a :       Union[str, Any]=0 ):

        '''simple docstring'''




        A_						:  Dict          =				floats_tensor((1, self.text_embedder_hidden_size)	,rng=random.Random(_a ) ).to(_a )
        A_						:  int          =				floats_tensor((1, self.text_embedder_hidden_size)	,rng=random.Random(seed + 1 ) ).to(
            _a )

        # create hint
        A_						:  List[Any]          =				floats_tensor((1, 3, 64, 64)	,rng=random.Random(_a ) ).to(_a )

        if str(_a ).startswith("""mps""" ):
              A_						:  Optional[Any]          =				torch.manual_seed(_a )
        else:
              A_						:  str          =				torch.Generator(device=_a ).manual_seed(_a )
        A_						:  List[Any]          =				{
            """image_embeds""": image_embeds,
            """negative_image_embeds""": negative_image_embeds,
            """hint""": hint,
            """generator""": generator,
            """height""": 64,
            """width""": 64,
            """guidance_scale""": 4.0,
            """num_inference_steps""": 2,
            """output_type""": """np""",
        }
        return inputs






  def        _a							(							self :       Dict ):

        '''simple docstring'''




        A_						:  List[Any]          =				"""cpu"""

        A_						:  List[str]          =				self.get_dummy_components()

        A_						:  Tuple          =				self.pipeline_class(**_a )
        A_						:  Dict          =				pipe.to(_a )

        pipe.set_progress_bar_config(disable=_a )

        A_						:  Tuple          =				pipe(**self.get_dummy_inputs(_a ) )
        A_						:  Tuple          =				output.images

        A_						:  Optional[Any]          =				pipe(
            **self.get_dummy_inputs(_a )	,return_dict=_a	,)[0]

        A_						:  Tuple          =				image[0, -3:, -3:, -1]
        A_						:  Any          =				image_from_tuple[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)

        A_						:  List[Any]          =				np.array(
            [0.6959826, 0.868279, 0.7558092, 0.68769467, 0.85805804, 0.65977496, 0.44885302, 0.5959111, 0.4251595] )

        assert (
            np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
        ), f' expected_slice {expected_slice}, but got {image_slice.flatten()}'

        assert (
            np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2
        ), f' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}'







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


  '''simple docstring'''







  def        _a							(							self :       Tuple ):

        '''simple docstring'''




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






  def        _a							(							self :       Any ):

        '''simple docstring'''




        A_						:  Tuple          =				load_numpy(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
            """/kandinskyv22/kandinskyv22_controlnet_robotcat_fp16.npy""" )

        A_						:  Optional[int]          =				load_image(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
            """/kandinskyv22/hint_image_cat.png""" )
        A_						:  Optional[int]          =				torch.from_numpy(np.array(_a ) ).float() / 255.0
        A_						:  List[Any]          =				hint.permute(2	,0	,1 ).unsqueeze(0 )

        A_						:  List[Any]          =				KandinskyVaaPriorPipeline.from_pretrained(
            """kandinsky-community/kandinsky-2-2-prior"""	,torch_dtype=torch.floataa )
        pipe_prior.to(_a )

        A_						:  Union[str, Any]          =				KandinskyVaaControlnetPipeline.from_pretrained(
            """kandinsky-community/kandinsky-2-2-controlnet-depth"""	,torch_dtype=torch.floataa )
        A_						:  Union[str, Any]          =				pipeline.to(_a )
        pipeline.set_progress_bar_config(disable=_a )

        A_						:  Optional[Any]          =				"""A robot, 4k photo"""

        A_						:  Any          =				torch.Generator(device="""cuda""" ).manual_seed(0 )
        A_   ,					A_						:  List[str]          =				pipe_prior(
            _a	,generator=_a	,num_inference_steps=5	,negative_prompt=""""""	,).to_tuple()

        A_						:  int          =				torch.Generator(device="""cuda""" ).manual_seed(0 )
        A_						:  List[Any]          =				pipeline(
            image_embeds=_a	,negative_image_embeds=_a	,hint=_a	,generator=_a	,num_inference_steps=100	,output_type="""np"""	,)

        A_						:  Dict          =				output.images[0]

        assert image.shape == (512, 512, 3)

        assert_mean_pixel_difference(_a	,_a )
