

'''simple docstring'''







import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, 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 MaskaFormerForUniversalSegmentation, MaskaFormerModel

      if is_vision_available():
            from transformers import MaskaFormerImageProcessor

if is_vision_available():
      from PIL import Image


class 					A__      :



    def __init__(					self		,				UpperCamelCase__		,				UpperCamelCase__=2		,				UpperCamelCase__=True		,				UpperCamelCase__=False		,				UpperCamelCase__=10		,				UpperCamelCase__=3		,				UpperCamelCase__=32 * 8		,				UpperCamelCase__=32 * 8		,				UpperCamelCase__=4		,				UpperCamelCase__=64		,				)						->      Dict:





           '''simple docstring'''
           A_						=      parent
           A_						=      batch_size
           A_						=      is_training
           A_						=      use_auxiliary_loss
           A_						=      num_queries
           A_						=      num_channels
           A_						=      min_size
           A_						=      max_size
           A_						=      num_labels
           A_						=      hidden_dim
           A_						=      hidden_dim



    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           A_						=      floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]       ).to(
               _a       )

           A_						=      torch.ones([self.batch_size, self.min_size, self.max_size]		,				device=_a       )

           A_						=      (
               torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]		,				device=_a       ) > 0.5
           ).float()
           A_						=      (torch.rand((self.batch_size, self.num_labels)		,				device=_a       ) > 0.5).long()

           A_						=      self.get_config()
           return config, pixel_values, pixel_mask, mask_labels, class_labels



    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           A_						=      MaskaFormerConfig(
               hidden_size=self.hidden_dim		,				)
           A_						=      self.num_queries
           A_						=      self.num_labels

           A_						=      [1, 1, 1, 1]
           A_						=      self.num_channels

           A_						=      64
           A_						=      128
           A_						=      self.hidden_dim
           A_						=      self.hidden_dim
           A_						=      self.hidden_dim
           return config



    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           A_						=      self.prepare_config_and_inputs()
           A_						=      {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
           return config, inputs_dict



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__       )						->      Any:





           '''simple docstring'''
           A_						=      output.encoder_hidden_states
           A_						=      output.pixel_decoder_hidden_states
           A_						=      output.transformer_decoder_hidden_states

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



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__=False       )						->      Optional[Any]:





           '''simple docstring'''




           with torch.no_grad():
                  A_						=      MaskaFormerModel(config=_a       )
                  model.to(_a       )
                  model.eval()

                  A_						=      model(pixel_values=_a		,				pixel_mask=_a       )
                  A_						=      model(_a		,				output_hidden_states=_a       )

           self.parent.assertEqual(
               output.transformer_decoder_last_hidden_state.shape		,				(self.batch_size, self.num_queries, self.hidden_dim)		,				)
           # 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(_a		,				_a       )







    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       )						->      Dict:





           '''simple docstring'''
           A_						=      MaskaFormerForUniversalSegmentation(config=_a       )
           model.to(_a       )
           model.eval()

           def comm_check_on_output(UpperCamelCase__       ):
                  # 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():
                  A_						=      model(pixel_values=_a		,				pixel_mask=_a       )
                  A_						=      model(_a       )

                  comm_check_on_output(_a       )

                  A_						=      model(
                      pixel_values=_a		,				pixel_mask=_a		,				mask_labels=_a		,				class_labels=_a       )

           comm_check_on_output(_a       )

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


@require_torch
class 					A__      (      __SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						unittest.TestCase			):
    lowercase						=		(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
    lowercase						=		{"feature-extraction": MaskaFormerModel} if is_torch_available() else {}

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



    def 					snake_case_			(					self       )						->      Union[str, Any]:





           '''simple docstring'''
           A_						=      MaskaFormerModelTester(self       )
           A_						=      ConfigTester(self		,				config_class=_a		,				has_text_modality=_a       )



    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           self.config_tester.run_common_tests()



    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.create_and_check_maskaformer_model(_a		,				**_a		,				output_hidden_states=_a       )



    def 					snake_case_			(					self       )						->      List[str]:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*_a       )



    @unittest.skip(reason="""Mask2Former does not use inputs_embeds"""       )
    def 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''
           pass



    @unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method"""       )
    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           pass



    @unittest.skip(reason="""Mask2Former is not a generative model"""       )
    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''
           pass



    @unittest.skip(reason="""Mask2Former does not use token embeddings"""       )
    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           pass



    @require_torch_multi_gpu
    @unittest.skip(
        reason="""Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""       )
    def 					snake_case_			(					self       )						->      Dict:





           '''simple docstring'''
           pass



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





           '''simple docstring'''
           pass



    def 					snake_case_			(					self       )						->      Optional[int]:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs_for_common()

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

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



    @slow
    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''




           for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
                  A_						=      MaskaFormerModel.from_pretrained(_a       )
                  self.assertIsNotNone(_a       )



    def 					snake_case_			(					self       )						->      List[str]:





           '''simple docstring'''
           A_						=      (self.model_tester.min_size,) * 2
           A_						=      {
               """pixel_values""": torch.randn((2, 3, *size)		,				device=_a       ),
               """mask_labels""": torch.randn((2, 10, *size)		,				device=_a       ),
               """class_labels""": torch.zeros(2		,				10		,				device=_a       ).long(),
           }
           A_						=      self.model_tester.get_config()

           A_						=      MaskaFormerForUniversalSegmentation(_a       ).to(_a       )
           A_						=      model(**_a       )
           self.assertTrue(outputs.loss is not None       )



    def 					snake_case_			(					self       )						->      List[str]:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.create_and_check_maskaformer_model(_a		,				**_a		,				output_hidden_states=_a       )



    def 					snake_case_			(					self       )						->      List[str]:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A_						=      model_class(_a       ).to(_a       )
                  A_						=      model(**_a		,				output_attentions=_a       )
                  self.assertTrue(outputs.attentions is not None       )



    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''




           if not self.model_tester.is_training:
                  return

           A_						=      self.all_model_classes[1]
           A_						=      self.model_tester.prepare_config_and_inputs()

           A_						=      model_class(_a       )
           model.to(_a       )
           model.train()

           A_						=      model(_a		,				mask_labels=_a		,				class_labels=_a       ).loss
           loss.backward()







    def 					snake_case_			(					self       )						->      Optional[int]:





           '''simple docstring'''
           A_						=      self.all_model_classes[1]
           A_						=      self.model_tester.prepare_config_and_inputs()
           A_						=      True
           A_						=      True

           A_						=      model_class(_a       ).to(_a       )
           model.train()

           A_						=      model(_a		,				mask_labels=_a		,				class_labels=_a       )

           A_						=      outputs.encoder_hidden_states[0]
           encoder_hidden_states.retain_grad()

           A_						=      outputs.pixel_decoder_hidden_states[0]
           pixel_decoder_hidden_states.retain_grad()

           A_						=      outputs.transformer_decoder_hidden_states[0]
           transformer_decoder_hidden_states.retain_grad()

           A_						=      outputs.attentions[0]
           attentions.retain_grad()

           outputs.loss.backward(retain_graph=_a       )

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

__lowerCamelCase		       =    1e-4
def       UpperCAmelCase__    (      )			->			Optional[Any]:
       A_						=      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""				)
       return image







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



    @cached_property
    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''
           return "facebook/mask2former-swin-small-coco-instance"



    @cached_property
    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints       ) if is_vision_available() else None



    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           A_						=      MaskaFormerModel.from_pretrained(self.model_checkpoints       ).to(_a       )
           A_						=      self.default_image_processor
           A_						=      prepare_img()
           A_						=      image_processor(_a		,				return_tensors="""pt"""       ).to(_a       )
           A_						=      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(_a		,				(1, 3, 384, 384)       )

           with torch.no_grad():
                  A_						=      model(**_a       )

           A_						=      torch.tensor(
               [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]]       ).to(_a       )
           self.assertTrue(
               torch.allclose(
                   outputs.encoder_last_hidden_state[0, 0, :3, :3]		,				_a		,				atol=_a       )       )

           A_						=      torch.tensor(
               [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]]       ).to(_a       )
           self.assertTrue(
               torch.allclose(
                   outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]		,				_a		,				atol=_a       )       )

           A_						=      torch.tensor(
               [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]]       ).to(_a       )
           self.assertTrue(
               torch.allclose(
                   outputs.transformer_decoder_last_hidden_state[0, :3, :3]		,				_a		,				atol=_a       )       )



    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           A_						=      MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints       ).to(_a       ).eval()
           A_						=      self.default_image_processor
           A_						=      prepare_img()
           A_						=      image_processor(_a		,				return_tensors="""pt"""       ).to(_a       )
           A_						=      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(_a		,				(1, 3, 384, 384)       )

           with torch.no_grad():
                  A_						=      model(**_a       )
           # masks_queries_logits
           A_						=      outputs.masks_queries_logits
           self.assertEqual(
               masks_queries_logits.shape		,				(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)       )
           A_						=      [
               [-8.7839, -9.0056, -8.8121],
               [-7.4104, -7.0313, -6.5401],
               [-6.6105, -6.3427, -6.4675],
           ]
           A_						=      torch.tensor(_a       ).to(_a       )
           self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]		,				_a		,				atol=_a       )       )
           # class_queries_logits
           A_						=      outputs.class_queries_logits
           self.assertEqual(class_queries_logits.shape		,				(1, model.config.num_queries, model.config.num_labels + 1)       )
           A_						=      torch.tensor(
               [
                   [1.8324, -8.0835, -4.1922],
                   [0.8450, -9.0050, -3.6053],
                   [0.3045, -7.7293, -3.0275],
               ]       ).to(_a       )
           self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		,				_a		,				atol=_a       )       )







    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''
           A_						=      MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints       ).to(_a       ).eval()
           A_						=      self.default_image_processor

           A_						=      image_processor(
               [np.zeros((3, 800, 1333)       ), np.zeros((3, 800, 1333)       )]		,				segmentation_maps=[np.zeros((384, 384)       ).astype(np.floataa       ), np.zeros((384, 384)       ).astype(np.floataa       )]		,				return_tensors="""pt"""		,				)

           A_						=      inputs["""pixel_values"""].to(_a       )
           A_						=      [el.to(_a       ) for el in inputs["""mask_labels"""]]
           A_						=      [el.to(_a       ) for el in inputs["""class_labels"""]]

           with torch.no_grad():
                  A_						=      model(**_a       )

           self.assertTrue(outputs.loss is not None       )



'''simple docstring'''







import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class 					A__      (      _snake_case						,						unittest.TestCase			):
    lowercase						=		KandinskyVaaPriorPipeline
    lowercase						=		["prompt"]
    lowercase						=		["prompt", "negative_prompt"]
    lowercase						=		[
        "num_images_per_prompt",
        "generator",
        "num_inference_steps",
        "latents",
        "negative_prompt",
        "guidance_scale",
        "output_type",
        "return_dict",
    ]
    lowercase						=		False



    @property
    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           return 32



    @property
    def 					snake_case_			(					self       )						->      Optional[int]:





           '''simple docstring'''
           return 32



    @property
    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''
           return self.time_input_dim



    @property
    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           return self.time_input_dim * 4



    @property
    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           return 100



    @property
    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''
           A_						=      CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""       )
           return tokenizer



    @property
    def 					snake_case_			(					self       )						->      Union[str, Any]:





           '''simple docstring'''
           torch.manual_seed(0       )
           A_						=      CLIPTextConfig(
               bos_token_id=0		,				eos_token_id=2		,				hidden_size=self.text_embedder_hidden_size		,				projection_dim=self.text_embedder_hidden_size		,				intermediate_size=37		,				layer_norm_eps=1e-0_5		,				num_attention_heads=4		,				num_hidden_layers=5		,				pad_token_id=1		,				vocab_size=1000		,				)
           return CLIPTextModelWithProjection(UpperCamelCase__       )



    @property
    def 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''
           torch.manual_seed(0       )

           A_						=      {
               """num_attention_heads""": 2,
               """attention_head_dim""": 12,
               """embedding_dim""": self.text_embedder_hidden_size,
               """num_layers""": 1,
           }

           A_						=      PriorTransformer(**UpperCamelCase__       )
           # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
           A_						=      nn.Parameter(torch.ones(model.clip_std.shape       )       )
           return model



    @property
    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           torch.manual_seed(0       )
           A_						=      CLIPVisionConfig(
               hidden_size=self.text_embedder_hidden_size		,				image_size=224		,				projection_dim=self.text_embedder_hidden_size		,				intermediate_size=37		,				num_attention_heads=4		,				num_channels=3		,				num_hidden_layers=5		,				patch_size=14		,				)

           A_						=      CLIPVisionModelWithProjection(UpperCamelCase__       )
           return model



    @property
    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           A_						=      CLIPImageProcessor(
               crop_size=224		,				do_center_crop=UpperCamelCase__		,				do_normalize=UpperCamelCase__		,				do_resize=UpperCamelCase__		,				image_mean=[0.48145466, 0.4578275, 0.40821073]		,				image_std=[0.26862954, 0.26130258, 0.27577711]		,				resample=3		,				size=224		,				)

           return image_processor



    def 					snake_case_			(					self       )						->      Union[str, Any]:





           '''simple docstring'''
           A_						=      self.dummy_prior
           A_						=      self.dummy_image_encoder
           A_						=      self.dummy_text_encoder
           A_						=      self.dummy_tokenizer
           A_						=      self.dummy_image_processor

           A_						=      UnCLIPScheduler(
               variance_type="""fixed_small_log"""		,				prediction_type="""sample"""		,				num_train_timesteps=1000		,				clip_sample=UpperCamelCase__		,				clip_sample_range=10.0		,				)

           A_						=      {
               """prior""": prior,
               """image_encoder""": image_encoder,
               """text_encoder""": text_encoder,
               """tokenizer""": tokenizer,
               """scheduler""": scheduler,
               """image_processor""": image_processor,
           }

           return components



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__=0       )						->      Optional[int]:





           '''simple docstring'''
           if str(UpperCamelCase__       ).startswith("""mps"""       ):
                  A_						=      torch.manual_seed(UpperCamelCase__       )
           else:
                  A_						=      torch.Generator(device=UpperCamelCase__       ).manual_seed(UpperCamelCase__       )
           A_						=      {
               """prompt""": """horse""",
               """generator""": generator,
               """guidance_scale""": 4.0,
               """num_inference_steps""": 2,
               """output_type""": """np""",
           }
           return inputs



    def 					snake_case_			(					self       )						->      List[str]:





           '''simple docstring'''
           A_						=      """cpu"""

           A_						=      self.get_dummy_components()

           A_						=      self.pipeline_class(**UpperCamelCase__       )
           A_						=      pipe.to(UpperCamelCase__       )

           pipe.set_progress_bar_config(disable=UpperCamelCase__       )

           A_						=      pipe(**self.get_dummy_inputs(UpperCamelCase__       )       )
           A_						=      output.image_embeds

           A_						=      pipe(
               **self.get_dummy_inputs(UpperCamelCase__       )		,				return_dict=UpperCamelCase__		,				)[0]

           A_						=      image[0, -10:]
           A_						=      image_from_tuple[0, -10:]

           assert image.shape == (1, 32)

           A_						=      np.array(
               [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156]       )

           assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2
           assert np.abs(image_from_tuple_slice.flatten() - expected_slice       ).max() < 1e-2



    @skip_mps
    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           A_						=      torch_device == """cpu"""
           A_						=      True
           A_						=      False

           self._test_inference_batch_single_identical(
               test_max_difference=UpperCamelCase__		,				relax_max_difference=UpperCamelCase__		,				test_mean_pixel_difference=UpperCamelCase__		,				)







    @skip_mps
    def 					snake_case_			(					self       )						->      Union[str, Any]:





           '''simple docstring'''
           A_						=      torch_device == """cpu"""
           A_						=      False

           self._test_attention_slicing_forward_pass(
               test_max_difference=UpperCamelCase__		,				test_mean_pixel_difference=UpperCamelCase__		,				)
