





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







     def __init__( self    ,   A__    ,   A__=2    ,   A__=True    ,   A__=False    ,   A__=10    ,   A__=3    ,   A__=32 * 8    ,   A__=32 * 8    ,   A__=4    ,   A__=64    ,   ) ->	Union[str, Any]:
      snake_case				=			parent
      snake_case				=			batch_size
      snake_case				=			is_training
      snake_case				=			use_auxiliary_loss
      snake_case				=			num_queries
      snake_case				=			num_channels
      snake_case				=			min_size
      snake_case				=			max_size
      snake_case				=			num_labels
      snake_case				=			hidden_dim
      snake_case				=			hidden_dim







     def     UpperCamelCase			( self    ) ->	Optional[int]:
      snake_case				=			floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]    ).to(
          A__    )

      snake_case				=			torch.ones([self.batch_size, self.min_size, self.max_size]    ,   device=A__    )

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

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







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      snake_case				=			MaskaFormerConfig(
          hidden_size=self.hidden_dim    ,   )
      snake_case				=			self.num_queries
      snake_case				=			self.num_labels

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

      snake_case				=			64
      snake_case				=			1_28
      snake_case				=			self.hidden_dim
      snake_case				=			self.hidden_dim
      snake_case				=			self.hidden_dim
      return config







     def     UpperCamelCase			( self    ) ->	Dict:
      snake_case				,		snake_case				,		snake_case				,		snake_case				,		snake_case				=			self.prepare_config_and_inputs()
      snake_case				=			{'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
      return config, inputs_dict







     def     UpperCamelCase			( self    ,   A__    ,   A__    ) ->	Union[str, Any]:
      snake_case				=			output.encoder_hidden_states
      snake_case				=			output.pixel_decoder_hidden_states
      snake_case				=			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     UpperCamelCase			( self    ,   A__    ,   A__    ,   A__    ,   A__=False    ) ->	str:
      with torch.no_grad():
       snake_case				=			MaskaFormerModel(config=A__    )
       model.to(A__    )
       model.eval()

       snake_case				=			model(pixel_values=A__    ,   pixel_mask=A__    )
       snake_case				=			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     UpperCamelCase			( self    ,   A__    ,   A__    ,   A__    ,   A__    ,   A__    ) ->	List[str]:
      snake_case				=			MaskaFormerForUniversalSegmentation(config=A__    )
      model.to(A__    )
      model.eval()

      def comm_check_on_output(A__    ):
       # 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():
       snake_case				=			model(pixel_values=A__    ,   pixel_mask=A__    )
       snake_case				=			model(A__    )

       comm_check_on_output(A__    )

       snake_case				=			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 			_lowercase						(       __a				,		__a				,		unittest.TestCase					):
     _UpperCAmelCase										=					(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
     _UpperCAmelCase										=					{'''feature-extraction''': MaskaFormerModel} if is_torch_available() else {}

     _UpperCAmelCase										=					False
     _UpperCAmelCase										=					False
     _UpperCAmelCase										=					False
     _UpperCAmelCase										=					False







     def     UpperCamelCase			( self    ) ->	str:
      snake_case				=			MaskaFormerModelTester(self    )
      snake_case				=			ConfigTester(self    ,   config_class=A__    ,   has_text_modality=A__    )







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







     def     UpperCamelCase			( self    ) ->	str:
      snake_case				,		snake_case				=			self.model_tester.prepare_config_and_inputs_for_common()
      self.model_tester.create_and_check_maskaformer_model(A__    ,   **A__    ,   output_hidden_states=A__    )







     def     UpperCamelCase			( self    ) ->	str:
      snake_case				=			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     UpperCamelCase			( self    ) ->	Dict:
      pass







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







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







     @unittest.skip(reason='''Mask2Former does not use token embeddings'''    )
     def     UpperCamelCase			( self    ) ->	List[str]:
      pass







     @require_torch_multi_gpu
     @unittest.skip(
         reason='''Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''    )
     def     UpperCamelCase			( self    ) ->	List[Any]:
      pass







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







     def     UpperCamelCase			( self    ) ->	str:
      snake_case				,		snake_case				=			self.model_tester.prepare_config_and_inputs_for_common()

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

       snake_case				=			['''pixel_values''']
       self.assertListEqual(arg_names[:1]    ,   A__    )







     @slow
     def     UpperCamelCase			( self    ) ->	List[str]:
      for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
       snake_case				=			MaskaFormerModel.from_pretrained(A__    )
       self.assertIsNotNone(A__    )







     def     UpperCamelCase			( self    ) ->	Dict:
      snake_case				=			(self.model_tester.min_size,) * 2
      snake_case				=			{
          '''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(),
      }
      snake_case				=			self.model_tester.get_config()

      snake_case				=			MaskaFormerForUniversalSegmentation(A__    ).to(A__    )
      snake_case				=			model(**A__    )
      self.assertTrue(outputs.loss is not None    )







     def     UpperCamelCase			( self    ) ->	Optional[int]:
      snake_case				,		snake_case				=			self.model_tester.prepare_config_and_inputs_for_common()
      self.model_tester.create_and_check_maskaformer_model(A__    ,   **A__    ,   output_hidden_states=A__    )







     def     UpperCamelCase			( self    ) ->	List[Any]:
      snake_case				,		snake_case				=			self.model_tester.prepare_config_and_inputs_for_common()

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







     def     UpperCamelCase			( self    ) ->	int:
      if not self.model_tester.is_training:
       return

      snake_case				=			self.all_model_classes[1]
      snake_case				,		snake_case				,		snake_case				,		snake_case				,		snake_case				=			self.model_tester.prepare_config_and_inputs()

      snake_case				=			model_class(A__    )
      model.to(A__    )
      model.train()

      snake_case				=			model(A__    ,   mask_labels=A__    ,   class_labels=A__    ).loss
      loss.backward()







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      snake_case				=			self.all_model_classes[1]
      snake_case				,		snake_case				,		snake_case				,		snake_case				,		snake_case				=			self.model_tester.prepare_config_and_inputs()
      snake_case				=			True
      snake_case				=			True

      snake_case				=			model_class(A__    ).to(A__    )
      model.train()

      snake_case				=			model(A__    ,   mask_labels=A__    ,   class_labels=A__    )

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

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

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

      snake_case				=			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    )




_lowercase       						=			1E-4

def    __UpperCamelCase	(	) ->Union[str, Any]:
 snake_case				=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
 return image






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







     @cached_property
     def     UpperCamelCase			( self    ) ->	Tuple:
      return "facebook/mask2former-swin-small-coco-instance"







     @cached_property
     def     UpperCamelCase			( self    ) ->	str:
      return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints    ) if is_vision_available() else None







     def     UpperCamelCase			( self    ) ->	Optional[int]:
      snake_case				=			MaskaFormerModel.from_pretrained(self.model_checkpoints    ).to(A__    )
      snake_case				=			self.default_image_processor
      snake_case				=			prepare_img()
      snake_case				=			image_processor(A__    ,   return_tensors='''pt'''    ).to(A__    )
      snake_case				=			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, 3_84, 3_84)    )

      with torch.no_grad():
       snake_case				=			model(**A__    )

      snake_case				=			torch.tensor(
          [[-0.2_7_9_0, -1.0_7_1_7, -1.1_6_6_8], [-0.5_1_2_8, -0.3_1_2_8, -0.4_9_8_7], [-0.5_8_3_2, 0.1_9_7_1, -0.0_1_9_7]]    ).to(A__    )
      self.assertTrue(
          torch.allclose(
              outputs.encoder_last_hidden_state[0, 0, :3, :3]    ,   A__    ,   atol=A__    )    )

      snake_case				=			torch.tensor(
          [[0.8_9_7_3, 1.1_8_4_7, 1.1_7_7_6], [1.1_9_3_4, 1.5_0_4_0, 1.5_1_2_8], [1.1_1_5_3, 1.4_4_8_6, 1.4_9_5_1]]    ).to(A__    )
      self.assertTrue(
          torch.allclose(
              outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]    ,   A__    ,   atol=A__    )    )

      snake_case				=			torch.tensor(
          [[2.1_1_5_2, 1.7_0_0_0, -0.8_6_0_3], [1.5_8_0_8, 1.8_0_0_4, -0.9_3_5_3], [1.6_0_4_3, 1.7_4_9_5, -0.5_9_9_9]]    ).to(A__    )
      self.assertTrue(
          torch.allclose(
              outputs.transformer_decoder_last_hidden_state[0, :3, :3]    ,   A__    ,   atol=A__    )    )







     def     UpperCamelCase			( self    ) ->	str:
      snake_case				=			MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints    ).to(A__    ).eval()
      snake_case				=			self.default_image_processor
      snake_case				=			prepare_img()
      snake_case				=			image_processor(A__    ,   return_tensors='''pt'''    ).to(A__    )
      snake_case				=			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, 3_84, 3_84)    )

      with torch.no_grad():
       snake_case				=			model(**A__    )
      # masks_queries_logits
      snake_case				=			outputs.masks_queries_logits
      self.assertEqual(
          masks_queries_logits.shape    ,   (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)    )
      snake_case				=			[
          [-8.7_8_3_9, -9.0_0_5_6, -8.8_1_2_1],
          [-7.4_1_0_4, -7.0_3_1_3, -6.5_4_0_1],
          [-6.6_1_0_5, -6.3_4_2_7, -6.4_6_7_5],
      ]
      snake_case				=			torch.tensor(A__    ).to(A__    )
      self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]    ,   A__    ,   atol=A__    )    )
      # class_queries_logits
      snake_case				=			outputs.class_queries_logits
      self.assertEqual(class_queries_logits.shape    ,   (1, model.config.num_queries, model.config.num_labels + 1)    )
      snake_case				=			torch.tensor(
          [
              [1.8_3_2_4, -8.0_8_3_5, -4.1_9_2_2],
              [0.8_4_5_0, -9.0_0_5_0, -3.6_0_5_3],
              [0.3_0_4_5, -7.7_2_9_3, -3.0_2_7_5],
          ]    ).to(A__    )
      self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]    ,   A__    ,   atol=A__    )    )







     def     UpperCamelCase			( self    ) ->	List[str]:
      snake_case				=			MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints    ).to(A__    ).eval()
      snake_case				=			self.default_image_processor

      snake_case				=			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'''    ,   )

      snake_case				=			inputs['''pixel_values'''].to(A__    )
      snake_case				=			[el.to(A__    ) for el in inputs['''mask_labels''']]
      snake_case				=			[el.to(A__    ) for el in inputs['''class_labels''']]

      with torch.no_grad():
       snake_case				=			model(**A__    )

      self.assertTrue(outputs.loss is not None    )








'''simple docstring'''







from ...processing_utils import ProcessorMixin
class 			_lowercase						(       __a					):
     _UpperCAmelCase										=					'''WhisperFeatureExtractor'''
     _UpperCAmelCase										=					'''WhisperTokenizer'''







     def __init__( self    ,   A__    ,   A__    ) ->	Optional[Any]:
      super().__init__(A__    ,   A__    )
      snake_case				=			self.feature_extractor
      snake_case				=			False







     def     UpperCamelCase			( self    ,   A__=None    ,   A__=None    ,   A__=True    ) ->	Union[str, Any]:
      return self.tokenizer.get_decoder_prompt_ids(task=A__    ,   language=A__    ,   no_timestamps=A__    )







     def __call__( self    ,   *A__    ,   **A__    ) ->	Dict:
      # For backward compatibility
      if self._in_target_context_manager:
       return self.current_processor(*A__    ,   **A__    )

      snake_case				=			kwargs.pop('''audio'''    ,   A__    )
      snake_case				=			kwargs.pop('''sampling_rate'''    ,   A__    )
      snake_case				=			kwargs.pop('''text'''    ,   A__    )
      if len(A__    ) > 0:
       snake_case				=			args[0]
       snake_case				=			args[1:]

      if audio is None and text is None:
       raise ValueError('''You need to specify either an `audio` or `text` input to process.'''    )

      if audio is not None:
       snake_case				=			self.feature_extractor(A__    ,   *A__    ,   sampling_rate=A__    ,   **A__    )
      if text is not None:
       snake_case				=			self.tokenizer(A__    ,   **A__    )

      if text is None:
       return inputs

      elif audio is None:
       return encodings
      else:
       snake_case				=			encodings['''input_ids''']
       return inputs







     def     UpperCamelCase			( self    ,   *A__    ,   **A__    ) ->	Optional[Any]:
      return self.tokenizer.batch_decode(*A__    ,   **A__    )







     def     UpperCamelCase			( self    ,   *A__    ,   **A__    ) ->	str:
      return self.tokenizer.decode(*A__    ,   **A__    )







     def     UpperCamelCase			( self    ,   A__    ,   A__="np"    ) ->	Optional[Any]:
      return self.tokenizer.get_prompt_ids(A__    ,   return_tensors=A__    )

