


'''simple docstring'''






import gc
import random
import unittest

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

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
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 ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()







def     UpperCamelCase_      (   A__ :       Union[str, Any] ):
		'''simple docstring'''





		lowerCAmelCase_      :				Union[str, Any]      =       [tensor.shape for tensor in tensor_list]
		return all(shape == shapes[0] for shape in shapes[1:] )






class        __snake_case	(       _SCREAMING_SNAKE_CASE     ,_SCREAMING_SNAKE_CASE     ,_SCREAMING_SNAKE_CASE     ,unittest.TestCase):







				"""simple docstring"""



				lowercase       			= StableDiffusionLatentUpscalePipeline
				lowercase       			= TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
				    'height',
				    'width',
				    'cross_attention_kwargs',
				    'negative_prompt_embeds',
				    'prompt_embeds',
				}
				lowercase       			= PipelineTesterMixin.required_optional_params - {'num_images_per_prompt'}
				lowercase       			= TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
				lowercase       			= frozenset(
				    [])  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
				lowercase       			= frozenset([])

				lowercase       			= True

				@property
				def __lowercase       (       self   :  Any		)       ->  List[str]:
						lowerCAmelCase_      :				Tuple      =       1
						lowerCAmelCase_      :				Tuple      =       4
						lowerCAmelCase_      :				List[str]      =       (16, 16)

						lowerCAmelCase_      :				Dict      =       floats_tensor((batch_size, num_channels) + sizes   ,						rng=random.Random(0		)		).to(lowerCamelCase		)
						return image

				def __lowercase       (       self   :  str		)       ->  Any:
						torch.manual_seed(0		)
						lowerCAmelCase_      :				Optional[Any]      =       UNetaDConditionModel(
						    act_fn="""gelu"""   ,						attention_head_dim=8   ,						norm_num_groups=lowerCamelCase   ,						block_out_channels=[32, 32, 64, 64]   ,						time_cond_proj_dim=1_60   ,						conv_in_kernel=1   ,						conv_out_kernel=1   ,						cross_attention_dim=32   ,						down_block_types=(
						        """KDownBlock2D""",
						        """KCrossAttnDownBlock2D""",
						        """KCrossAttnDownBlock2D""",
						        """KCrossAttnDownBlock2D""",
						    )   ,						in_channels=8   ,						mid_block_type=lowerCamelCase   ,						only_cross_attention=lowerCamelCase   ,						out_channels=5   ,						resnet_time_scale_shift="""scale_shift"""   ,						time_embedding_type="""fourier"""   ,						timestep_post_act="""gelu"""   ,						up_block_types=("""KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KUpBlock2D""")   ,						)
						lowerCAmelCase_      :				Any      =       AutoencoderKL(
						    block_out_channels=[32, 32, 64, 64]   ,						in_channels=3   ,						out_channels=3   ,						down_block_types=[
						        """DownEncoderBlock2D""",
						        """DownEncoderBlock2D""",
						        """DownEncoderBlock2D""",
						        """DownEncoderBlock2D""",
						    ]   ,						up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D"""]   ,						latent_channels=4   ,						)
						lowerCAmelCase_      :				Tuple      =       EulerDiscreteScheduler(prediction_type="""sample"""		)
						lowerCAmelCase_      :				Any      =       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=10_00   ,						hidden_act="""quick_gelu"""   ,						projection_dim=5_12   ,						)
						lowerCAmelCase_      :				Optional[int]      =       CLIPTextModel(lowerCamelCase		)
						lowerCAmelCase_      :				Tuple      =       CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""		)

						lowerCAmelCase_      :				Any      =       {
						    """unet""": model.eval(),
						    """vae""": vae.eval(),
						    """scheduler""": scheduler,
						    """text_encoder""": text_encoder,
						    """tokenizer""": tokenizer,
						}

						return components

				def __lowercase       (       self   :  Optional[Any]   ,						lowerCamelCase   :  Tuple   ,						lowerCamelCase   :  List[Any]=0		)       ->  Tuple:
						if str(lowerCamelCase		).startswith("""mps"""		):
								lowerCAmelCase_      :				List[str]      =       torch.manual_seed(lowerCamelCase		)
						else:
								lowerCAmelCase_      :				Optional[Any]      =       torch.Generator(device=lowerCamelCase		).manual_seed(lowerCamelCase		)
						lowerCAmelCase_      :				Tuple      =       {
						    """prompt""": """A painting of a squirrel eating a burger""",
						    """image""": self.dummy_image.cpu(),
						    """generator""": generator,
						    """num_inference_steps""": 2,
						    """output_type""": """numpy""",
						}
						return inputs

				def __lowercase       (       self   :  Optional[Any]		)       ->  str:
						lowerCAmelCase_      :				Dict      =       """cpu"""

						lowerCAmelCase_      :				List[str]      =       self.get_dummy_components()
						lowerCAmelCase_      :				Tuple      =       self.pipeline_class(**lowerCamelCase		)
						pipe.to(lowerCamelCase		)
						pipe.set_progress_bar_config(disable=lowerCamelCase		)

						lowerCAmelCase_      :				Union[str, Any]      =       self.get_dummy_inputs(lowerCamelCase		)
						lowerCAmelCase_      :				Union[str, Any]      =       pipe(**lowerCamelCase		).images
						lowerCAmelCase_      :				str      =       image[0, -3:, -3:, -1]

						self.assertEqual(image.shape   ,						(1, 2_56, 2_56, 3)		)
						lowerCAmelCase_      :				List[str]      =       np.array(
						    [0.47_222_412, 0.41_921_633, 0.44_717_434, 0.46_874_192, 0.42_588_258, 0.46_150_726, 0.4_677_534, 0.45_583_832, 0.48_579_055]		)
						lowerCAmelCase_      :				List[Any]      =       np.abs(image_slice.flatten() - expected_slice		).max()
						self.assertLessEqual(lowerCamelCase   ,						1E-3		)

				def __lowercase       (       self   :  Tuple		)       ->  Tuple:
						super().test_attention_slicing_forward_pass(expected_max_diff=7E-3		)

				def __lowercase       (       self   :  Optional[Any]		)       ->  List[str]:
						super().test_cpu_offload_forward_pass(expected_max_diff=3E-3		)

				def __lowercase       (       self   :  Dict		)       ->  List[str]:
						super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3		)

				def __lowercase       (       self   :  Optional[int]		)       ->  Dict:
						super().test_inference_batch_single_identical(expected_max_diff=7E-3		)

				def __lowercase       (       self   :  Any		)       ->  Any:
						super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3E-3		)

				def __lowercase       (       self   :  List[Any]		)       ->  str:
						super().test_save_load_local(expected_max_difference=3E-3		)

				def __lowercase       (       self   :  List[str]		)       ->  Optional[int]:
						super().test_save_load_optional_components(expected_max_difference=3E-3		)





				def __lowercase       (       self   :  Any		)       ->  Union[str, Any]:
						lowerCAmelCase_      :				Tuple      =       [
						    """DDIMScheduler""",
						    """DDPMScheduler""",
						    """PNDMScheduler""",
						    """HeunDiscreteScheduler""",
						    """EulerAncestralDiscreteScheduler""",
						    """KDPM2DiscreteScheduler""",
						    """KDPM2AncestralDiscreteScheduler""",
						    """DPMSolverSDEScheduler""",
						]
						lowerCAmelCase_      :				Union[str, Any]      =       self.get_dummy_components()
						lowerCAmelCase_      :				List[str]      =       self.pipeline_class(**lowerCamelCase		)

						# make sure that PNDM does not need warm-up
						pipe.scheduler.register_to_config(skip_prk_steps=lowerCamelCase		)

						pipe.to(lowerCamelCase		)
						pipe.set_progress_bar_config(disable=lowerCamelCase		)
						lowerCAmelCase_      :				int      =       self.get_dummy_inputs(lowerCamelCase		)
						lowerCAmelCase_      :				List[Any]      =       2

						lowerCAmelCase_      :				int      =       []
						for scheduler_enum in KarrasDiffusionSchedulers:
								if scheduler_enum.name in skip_schedulers:
										# no sigma schedulers are not supported
										# no schedulers
										continue

								lowerCAmelCase_      :				Optional[int]      =       getattr(lowerCamelCase   ,						scheduler_enum.name		)
								lowerCAmelCase_      :				Any      =       scheduler_cls.from_config(pipe.scheduler.config		)
								lowerCAmelCase_      :				List[str]      =       pipe(**lowerCamelCase		)[0]
								outputs.append(lowerCamelCase		)

						assert check_same_shape(lowerCamelCase		)







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







				"""simple docstring"""



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

				def __lowercase       (       self   :  List[Any]		)       ->  str:
						lowerCAmelCase_      :				Optional[Any]      =       torch.manual_seed(33		)

						lowerCAmelCase_      :				List[str]      =       StableDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""   ,						torch_dtype=torch.floataa		)
						pipe.to("""cuda"""		)

						lowerCAmelCase_      :				Union[str, Any]      =       StableDiffusionLatentUpscalePipeline.from_pretrained(
						    """stabilityai/sd-x2-latent-upscaler"""   ,						torch_dtype=torch.floataa		)
						upscaler.to("""cuda"""		)

						lowerCAmelCase_      :				Dict      =       """a photo of an astronaut high resolution, unreal engine, ultra realistic"""

						lowerCAmelCase_      :				Dict      =       pipe(lowerCamelCase   ,						generator=lowerCamelCase   ,						output_type="""latent"""		).images

						lowerCAmelCase_      :				Tuple      =       upscaler(
						    prompt=lowerCamelCase   ,						image=lowerCamelCase   ,						num_inference_steps=20   ,						guidance_scale=0   ,						generator=lowerCamelCase   ,						output_type="""np"""   ,						).images[0]

						lowerCAmelCase_      :				List[Any]      =       load_numpy(
						    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy"""		)
						assert np.abs((expected_image - image).mean()		) < 5E-2





				def __lowercase       (       self   :  Union[str, Any]		)       ->  Union[str, Any]:
						lowerCAmelCase_      :				Dict      =       torch.manual_seed(33		)

						lowerCAmelCase_      :				str      =       StableDiffusionLatentUpscalePipeline.from_pretrained(
						    """stabilityai/sd-x2-latent-upscaler"""   ,						torch_dtype=torch.floataa		)
						upscaler.to("""cuda"""		)

						lowerCAmelCase_      :				Any      =       """the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas"""

						lowerCAmelCase_      :				List[str]      =       load_image(
						    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png"""		)

						lowerCAmelCase_      :				Dict      =       upscaler(
						    prompt=lowerCamelCase   ,						image=lowerCamelCase   ,						num_inference_steps=20   ,						guidance_scale=0   ,						generator=lowerCamelCase   ,						output_type="""np"""   ,						).images[0]

						lowerCAmelCase_      :				int      =       load_numpy(
						    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy"""		)
						assert np.abs((expected_image - image).max()		) < 5E-2







'''simple docstring'''






import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

  if is_vision_available():
    from transformers import MaskFormerImageProcessor

if is_vision_available():
  from PIL import Image






class        __snake_case	:







    """simple docstring"""



    def __init__(       self   :  Union[str, Any]   ,						lowerCamelCase   :  List[Any]   ,						lowerCamelCase   :  List[Any]=2   ,						lowerCamelCase   :  int=True   ,						lowerCamelCase   :  str=False   ,						lowerCamelCase   :  List[str]=10   ,						lowerCamelCase   :  Dict=3   ,						lowerCamelCase   :  str=32 * 4   ,						lowerCamelCase   :  Tuple=32 * 6   ,						lowerCamelCase   :  int=4   ,						lowerCamelCase   :  Optional[int]=32   ,						)       ->  List[Any]:
      lowerCAmelCase_      :				Tuple      =       parent
      lowerCAmelCase_      :				int      =       batch_size
      lowerCAmelCase_      :				Tuple      =       is_training
      lowerCAmelCase_      :				str      =       use_auxiliary_loss
      lowerCAmelCase_      :				Optional[Any]      =       num_queries
      lowerCAmelCase_      :				List[str]      =       num_channels
      lowerCAmelCase_      :				Optional[Any]      =       min_size
      lowerCAmelCase_      :				Dict      =       max_size
      lowerCAmelCase_      :				List[Any]      =       num_labels
      lowerCAmelCase_      :				List[Any]      =       mask_feature_size

    def __lowercase       (       self   :  str		)       ->  List[Any]:
      lowerCAmelCase_      :				Union[str, Any]      =       floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]		).to(
          lowerCamelCase		)

      lowerCAmelCase_      :				int      =       torch.ones([self.batch_size, self.min_size, self.max_size]   ,						device=lowerCamelCase		)

      lowerCAmelCase_      :				List[Any]      =       (
          torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]   ,						device=lowerCamelCase		) > 0.5
      ).float()
      lowerCAmelCase_      :				Union[str, Any]      =       (torch.rand((self.batch_size, self.num_labels)   ,						device=lowerCamelCase		) > 0.5).long()

      lowerCAmelCase_      :				List[Any]      =       self.get_config()
      return config, pixel_values, pixel_mask, mask_labels, class_labels

    def __lowercase       (       self   :  Optional[int]		)       ->  Optional[int]:
      return MaskFormerConfig.from_backbone_and_decoder_configs(
          backbone_config=SwinConfig(
              depths=[1, 1, 1, 1]   ,						)   ,						decoder_config=DetrConfig(
              decoder_ffn_dim=1_28   ,						num_queries=self.num_queries   ,						decoder_attention_heads=2   ,						d_model=self.mask_feature_size   ,						)   ,						mask_feature_size=self.mask_feature_size   ,						fpn_feature_size=self.mask_feature_size   ,						num_channels=self.num_channels   ,						num_labels=self.num_labels   ,						)

    def __lowercase       (       self   :  Tuple		)       ->  Optional[Any]:
      lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_      :				List[Any]      =       self.prepare_config_and_inputs()
      lowerCAmelCase_      :				Union[str, Any]      =       {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
      return config, inputs_dict

    def __lowercase       (       self   :  Tuple   ,						lowerCamelCase   :  Tuple   ,						lowerCamelCase   :  Optional[Any]		)       ->  Union[str, Any]:
      lowerCAmelCase_      :				Any      =       output.encoder_hidden_states
      lowerCAmelCase_      :				Dict      =       output.pixel_decoder_hidden_states
      lowerCAmelCase_      :				str      =       output.transformer_decoder_hidden_states

      self.parent.assertTrue(len(lowerCamelCase		)   ,						len(config.backbone_config.depths		)		)
      self.parent.assertTrue(len(lowerCamelCase		)   ,						len(config.backbone_config.depths		)		)
      self.parent.assertTrue(len(lowerCamelCase		)   ,						config.decoder_config.decoder_layers		)

    def __lowercase       (       self   :  List[Any]   ,						lowerCamelCase   :  Any   ,						lowerCamelCase   :  int   ,						lowerCamelCase   :  Optional[int]   ,						lowerCamelCase   :  Tuple=False		)       ->  List[Any]:
      with torch.no_grad():
        lowerCAmelCase_      :				int      =       MaskFormerModel(config=lowerCamelCase		)
        model.to(lowerCamelCase		)
        model.eval()

        lowerCAmelCase_      :				List[Any]      =       model(pixel_values=lowerCamelCase   ,						pixel_mask=lowerCamelCase		)
        lowerCAmelCase_      :				Union[str, Any]      =       model(lowerCamelCase   ,						output_hidden_states=lowerCamelCase		)
      # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
      # encoder and pixel decoder
      self.parent.assertEqual(
          output.transformer_decoder_last_hidden_state.shape   ,						(self.batch_size, self.num_queries, self.mask_feature_size)   ,						)
      # let's ensure the other two hidden state exists
      self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None		)
      self.parent.assertTrue(output.encoder_last_hidden_state is not None		)

      if output_hidden_states:
        self.check_output_hidden_state(lowerCamelCase   ,						lowerCamelCase		)





    def __lowercase       (       self   :  Any   ,						lowerCamelCase   :  Dict   ,						lowerCamelCase   :  Any   ,						lowerCamelCase   :  Optional[int]   ,						lowerCamelCase   :  Tuple   ,						lowerCamelCase   :  Optional[Any]		)       ->  Dict:
      lowerCAmelCase_      :				Tuple      =       MaskFormerForInstanceSegmentation(config=lowerCamelCase		)
      model.to(lowerCamelCase		)
      model.eval()

      def comm_check_on_output(lowerCamelCase   :  Optional[Any]		):
        # let's still check that all the required stuff is there
        self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None		)
        self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None		)
        self.parent.assertTrue(result.encoder_last_hidden_state is not None		)
        # okay, now we need to check the logits shape
        # due to the encoder compression, masks have a //4 spatial size
        self.parent.assertEqual(
            result.masks_queries_logits.shape   ,						(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)   ,						)
        # + 1 for null class
        self.parent.assertEqual(
            result.class_queries_logits.shape   ,						(self.batch_size, self.num_queries, self.num_labels + 1)		)

      with torch.no_grad():
        lowerCAmelCase_      :				Dict      =       model(pixel_values=lowerCamelCase   ,						pixel_mask=lowerCamelCase		)
        lowerCAmelCase_      :				List[str]      =       model(lowerCamelCase		)

        comm_check_on_output(lowerCamelCase		)

        lowerCAmelCase_      :				Union[str, Any]      =       model(
            pixel_values=lowerCamelCase   ,						pixel_mask=lowerCamelCase   ,						mask_labels=lowerCamelCase   ,						class_labels=lowerCamelCase		)

      comm_check_on_output(lowerCamelCase		)

      self.parent.assertTrue(result.loss is not None		)
      self.parent.assertEqual(result.loss.shape   ,						torch.Size([1]		)		)







@require_torch
class        __snake_case	(       _SCREAMING_SNAKE_CASE     ,_SCREAMING_SNAKE_CASE     ,unittest.TestCase):







    """simple docstring"""



    lowercase       			= (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
    lowercase       			= (
        {'feature-extraction': MaskFormerModel, 'image-segmentation': MaskFormerForInstanceSegmentation}
        if is_torch_available()
        else {}
    )

    lowercase       			= False
    lowercase       			= False
    lowercase       			= False
    lowercase       			= False

    def __lowercase       (       self   :  Dict		)       ->  Optional[Any]:
      lowerCAmelCase_      :				Tuple      =       MaskFormerModelTester(self		)
      lowerCAmelCase_      :				str      =       ConfigTester(self   ,						config_class=lowerCamelCase   ,						has_text_modality=lowerCamelCase		)

    def __lowercase       (       self   :  List[Any]		)       ->  Optional[Any]:
      self.config_tester.run_common_tests()

    def __lowercase       (       self   :  Tuple		)       ->  List[Any]:
      lowerCAmelCase_,							lowerCAmelCase_      :				List[str]      =       self.model_tester.prepare_config_and_inputs_for_common()
      self.model_tester.create_and_check_maskformer_model(lowerCamelCase   ,						**lowerCamelCase   ,						output_hidden_states=lowerCamelCase		)

    def __lowercase       (       self   :  Union[str, Any]		)       ->  str:
      lowerCAmelCase_      :				Dict      =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*lowerCamelCase		)

    @unittest.skip(reason="""MaskFormer does not use inputs_embeds"""		)
    def __lowercase       (       self   :  Any		)       ->  Optional[Any]:
      pass

    @unittest.skip(reason="""MaskFormer does not have a get_input_embeddings method"""		)
    def __lowercase       (       self   :  Union[str, Any]		)       ->  Optional[Any]:
      pass

    @unittest.skip(reason="""MaskFormer is not a generative model"""		)
    def __lowercase       (       self   :  Any		)       ->  str:
      pass

    @unittest.skip(reason="""MaskFormer does not use token embeddings"""		)
    def __lowercase       (       self   :  List[Any]		)       ->  Any:
      pass

    @require_torch_multi_gpu
    @unittest.skip(
        reason="""MaskFormer has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""		)
    def __lowercase       (       self   :  List[str]		)       ->  Optional[int]:
      pass

    @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""		)
    def __lowercase       (       self   :  str		)       ->  Optional[int]:
      pass

    def __lowercase       (       self   :  Tuple		)       ->  List[str]:
      lowerCAmelCase_,							lowerCAmelCase_      :				Any      =       self.model_tester.prepare_config_and_inputs_for_common()

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

        lowerCAmelCase_      :				int      =       ["""pixel_values"""]
        self.assertListEqual(arg_names[:1]   ,						lowerCamelCase		)

    @slow
    def __lowercase       (       self   :  Optional[int]		)       ->  List[str]:
      for model_name in ["facebook/maskformer-swin-small-coco"]:
        lowerCAmelCase_      :				List[str]      =       MaskFormerModel.from_pretrained(lowerCamelCase		)
        self.assertIsNotNone(lowerCamelCase		)

    def __lowercase       (       self   :  Dict		)       ->  int:
      lowerCAmelCase_      :				Any      =       (self.model_tester.min_size,) * 2
      lowerCAmelCase_      :				Dict      =       {
          """pixel_values""": torch.randn((2, 3, *size)   ,						device=lowerCamelCase		),
          """mask_labels""": torch.randn((2, 10, *size)   ,						device=lowerCamelCase		),
          """class_labels""": torch.zeros(2   ,						10   ,						device=lowerCamelCase		).long(),
      }

      lowerCAmelCase_      :				Dict      =       MaskFormerForInstanceSegmentation(MaskFormerConfig()		).to(lowerCamelCase		)
      lowerCAmelCase_      :				str      =       model(**lowerCamelCase		)
      self.assertTrue(outputs.loss is not None		)

    def __lowercase       (       self   :  int		)       ->  int:
      lowerCAmelCase_,							lowerCAmelCase_      :				Union[str, Any]      =       self.model_tester.prepare_config_and_inputs_for_common()
      self.model_tester.create_and_check_maskformer_model(lowerCamelCase   ,						**lowerCamelCase   ,						output_hidden_states=lowerCamelCase		)

    def __lowercase       (       self   :  Dict		)       ->  List[str]:
      lowerCAmelCase_,							lowerCAmelCase_      :				Union[str, Any]      =       self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        lowerCAmelCase_      :				List[str]      =       model_class(lowerCamelCase		).to(lowerCamelCase		)
        lowerCAmelCase_      :				Tuple      =       model(**lowerCamelCase   ,						output_attentions=lowerCamelCase		)
        self.assertTrue(outputs.attentions is not None		)

    def __lowercase       (       self   :  int		)       ->  Union[str, Any]:
      if not self.model_tester.is_training:
        return
      # only MaskFormerForInstanceSegmentation has the loss
      lowerCAmelCase_      :				Optional[int]      =       self.all_model_classes[1]
      lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_      :				str      =       self.model_tester.prepare_config_and_inputs()

      lowerCAmelCase_      :				Union[str, Any]      =       model_class(lowerCamelCase		)
      model.to(lowerCamelCase		)
      model.train()

      lowerCAmelCase_      :				str      =       model(lowerCamelCase   ,						mask_labels=lowerCamelCase   ,						class_labels=lowerCamelCase		).loss
      loss.backward()





    def __lowercase       (       self   :  Any		)       ->  Union[str, Any]:
      # only MaskFormerForInstanceSegmentation has the loss
      lowerCAmelCase_      :				List[str]      =       self.all_model_classes[1]
      lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_      :				Any      =       self.model_tester.prepare_config_and_inputs()
      lowerCAmelCase_      :				List[str]      =       True
      lowerCAmelCase_      :				List[str]      =       True

      lowerCAmelCase_      :				Dict      =       model_class(lowerCamelCase		)
      model.to(lowerCamelCase		)
      model.train()

      lowerCAmelCase_      :				Union[str, Any]      =       model(lowerCamelCase   ,						mask_labels=lowerCamelCase   ,						class_labels=lowerCamelCase		)

      lowerCAmelCase_      :				Optional[Any]      =       outputs.encoder_hidden_states[0]
      encoder_hidden_states.retain_grad()

      lowerCAmelCase_      :				List[str]      =       outputs.pixel_decoder_hidden_states[0]
      pixel_decoder_hidden_states.retain_grad()
      # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
      lowerCAmelCase_      :				Optional[int]      =       outputs.transformer_decoder_hidden_states[0]
      transformer_decoder_hidden_states.retain_grad()

      lowerCAmelCase_      :				Tuple      =       outputs.attentions[0]
      attentions.retain_grad()

      outputs.loss.backward(retain_graph=lowerCamelCase		)

      self.assertIsNotNone(encoder_hidden_states.grad		)
      self.assertIsNotNone(pixel_decoder_hidden_states.grad		)
      self.assertIsNotNone(transformer_decoder_hidden_states.grad		)
      self.assertIsNotNone(attentions.grad		)







__A :  List[Any]    							=				1E-4







def     UpperCamelCase_      (   ):
  '''simple docstring'''





  lowerCAmelCase_      :				int      =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
  return image






@require_vision
@slow
class        __snake_case	(       unittest.TestCase):







    """simple docstring"""



    @cached_property
    def __lowercase       (       self   :  Union[str, Any]		)       ->  List[Any]:
      return (
          MaskFormerImageProcessor.from_pretrained("""facebook/maskformer-swin-small-coco"""		)
          if is_vision_available()
          else None
      )

    def __lowercase       (       self   :  List[Any]		)       ->  Union[str, Any]:
      lowerCAmelCase_      :				Optional[int]      =       MaskFormerModel.from_pretrained("""facebook/maskformer-swin-small-coco"""		).to(lowerCamelCase		)
      lowerCAmelCase_      :				Dict      =       self.default_image_processor
      lowerCAmelCase_      :				Optional[int]      =       prepare_img()
      lowerCAmelCase_      :				Dict      =       image_processor(lowerCamelCase   ,						return_tensors="""pt"""		).to(lowerCamelCase		)
      lowerCAmelCase_      :				Union[str, Any]      =       inputs["""pixel_values"""].shape
      # check size is divisible by 32
      self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0		)
      # check size
      self.assertEqual(lowerCamelCase   ,						(1, 3, 8_00, 10_88)		)

      with torch.no_grad():
        lowerCAmelCase_      :				List[str]      =       model(**lowerCamelCase		)

      lowerCAmelCase_      :				Any      =       torch.tensor(
          [[-0.0_482, 0.9_228, 0.4_951], [-0.2_547, 0.8_017, 0.8_527], [-0.0_069, 0.3_385, -0.0_089]]		).to(lowerCamelCase		)
      self.assertTrue(
          torch.allclose(
              outputs.encoder_last_hidden_state[0, 0, :3, :3]   ,						lowerCamelCase   ,						atol=lowerCamelCase		)		)

      lowerCAmelCase_      :				Union[str, Any]      =       torch.tensor(
          [[-0.8_422, -0.8_434, -0.9_718], [-1.0_144, -0.5_565, -0.4_195], [-1.0_038, -0.4_484, -0.1_961]]		).to(lowerCamelCase		)
      self.assertTrue(
          torch.allclose(
              outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]   ,						lowerCamelCase   ,						atol=lowerCamelCase		)		)

      lowerCAmelCase_      :				Optional[Any]      =       torch.tensor(
          [[0.2_852, -0.0_159, 0.9_735], [0.6_254, 0.1_858, 0.8_529], [-0.0_680, -0.4_116, 1.8_413]]		).to(lowerCamelCase		)
      self.assertTrue(
          torch.allclose(
              outputs.transformer_decoder_last_hidden_state[0, :3, :3]   ,						lowerCamelCase   ,						atol=lowerCamelCase		)		)

    def __lowercase       (       self   :  Optional[int]		)       ->  Optional[int]:
      lowerCAmelCase_      :				Union[str, Any]      =       (
          MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""		)
          .to(lowerCamelCase		)
          .eval()
      )
      lowerCAmelCase_      :				Union[str, Any]      =       self.default_image_processor
      lowerCAmelCase_      :				Optional[Any]      =       prepare_img()
      lowerCAmelCase_      :				Optional[int]      =       image_processor(lowerCamelCase   ,						return_tensors="""pt"""		).to(lowerCamelCase		)
      lowerCAmelCase_      :				Optional[int]      =       inputs["""pixel_values"""].shape
      # check size is divisible by 32
      self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0		)
      # check size
      self.assertEqual(lowerCamelCase   ,						(1, 3, 8_00, 10_88)		)

      with torch.no_grad():
        lowerCAmelCase_      :				List[str]      =       model(**lowerCamelCase		)
      # masks_queries_logits
      lowerCAmelCase_      :				Dict      =       outputs.masks_queries_logits
      self.assertEqual(
          masks_queries_logits.shape   ,						(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)   ,						)
      lowerCAmelCase_      :				Any      =       [
          [-1.3_737_124, -1.7_724_937, -1.9_364_233],
          [-1.5_977_281, -1.9_867_939, -2.1_523_695],
          [-1.5_795_398, -1.9_269_832, -2.093_942],
      ]
      lowerCAmelCase_      :				Optional[int]      =       torch.tensor(lowerCamelCase		).to(lowerCamelCase		)
      self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]   ,						lowerCamelCase   ,						atol=lowerCamelCase		)		)
      # class_queries_logits
      lowerCAmelCase_      :				Optional[Any]      =       outputs.class_queries_logits
      self.assertEqual(
          class_queries_logits.shape   ,						(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)		)
      lowerCAmelCase_      :				Tuple      =       torch.tensor(
          [
              [1.6_512E00, -5.2_572E00, -3.3_519E00],
              [3.6_169E-02, -5.9_025E00, -2.9_313E00],
              [1.0_766E-04, -7.7_630E00, -5.1_263E00],
          ]		).to(lowerCamelCase		)
      self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]   ,						lowerCamelCase   ,						atol=lowerCamelCase		)		)

    def __lowercase       (       self   :  Optional[int]		)       ->  List[str]:
      lowerCAmelCase_      :				List[str]      =       (
          MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-resnet101-coco-stuff"""		)
          .to(lowerCamelCase		)
          .eval()
      )
      lowerCAmelCase_      :				Tuple      =       self.default_image_processor
      lowerCAmelCase_      :				int      =       prepare_img()
      lowerCAmelCase_      :				Any      =       image_processor(lowerCamelCase   ,						return_tensors="""pt"""		).to(lowerCamelCase		)
      lowerCAmelCase_      :				List[str]      =       inputs["""pixel_values"""].shape
      # check size is divisible by 32
      self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0		)
      # check size
      self.assertEqual(lowerCamelCase   ,						(1, 3, 8_00, 10_88)		)

      with torch.no_grad():
        lowerCAmelCase_      :				Optional[Any]      =       model(**lowerCamelCase		)
      # masks_queries_logits
      lowerCAmelCase_      :				List[Any]      =       outputs.masks_queries_logits
      self.assertEqual(
          masks_queries_logits.shape   ,						(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)   ,						)
      lowerCAmelCase_      :				Optional[Any]      =       [[-0.9_046, -2.6_366, -4.6_062], [-3.4_179, -5.7_890, -8.8_057], [-4.9_179, -7.6_560, -10.7_711]]
      lowerCAmelCase_      :				Union[str, Any]      =       torch.tensor(lowerCamelCase		).to(lowerCamelCase		)
      self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]   ,						lowerCamelCase   ,						atol=lowerCamelCase		)		)
      # class_queries_logits
      lowerCAmelCase_      :				Dict      =       outputs.class_queries_logits
      self.assertEqual(
          class_queries_logits.shape   ,						(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)		)
      lowerCAmelCase_      :				int      =       torch.tensor(
          [[4.7_188, -3.2_585, -2.8_857], [6.6_871, -2.9_181, -1.2_487], [7.2_449, -2.2_764, -2.1_874]]		).to(lowerCamelCase		)
      self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]   ,						lowerCamelCase   ,						atol=lowerCamelCase		)		)





    def __lowercase       (       self   :  Union[str, Any]		)       ->  List[str]:
      lowerCAmelCase_      :				Dict      =       (
          MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""		)
          .to(lowerCamelCase		)
          .eval()
      )
      lowerCAmelCase_      :				List[str]      =       self.default_image_processor

      lowerCAmelCase_      :				int      =       image_processor(
          [np.zeros((3, 8_00, 13_33)		), np.zeros((3, 8_00, 13_33)		)]   ,						segmentation_maps=[np.zeros((3_84, 3_84)		).astype(np.floataa		), np.zeros((3_84, 3_84)		).astype(np.floataa		)]   ,						return_tensors="""pt"""   ,						)

      lowerCAmelCase_      :				List[str]      =       inputs["""pixel_values"""].to(lowerCamelCase		)
      lowerCAmelCase_      :				Tuple      =       [el.to(lowerCamelCase		) for el in inputs["""mask_labels"""]]
      lowerCAmelCase_      :				Union[str, Any]      =       [el.to(lowerCamelCase		) for el in inputs["""class_labels"""]]

      with torch.no_grad():
        lowerCAmelCase_      :				Any      =       model(**lowerCamelCase		)

      self.assertTrue(outputs.loss is not None		)



