





'''simple docstring'''






import unittest

from transformers import AlbertTokenizer, AlbertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


lowerCAmelCase       		=      get_tests_dir("""fixtures/spiece.model""")
@require_sentencepiece
@require_tokenizers
class 		lowerCamelCase  (      _A      ,   unittest.TestCase       ):
	snake_case_      =   AlbertTokenizer
	snake_case_      =   AlbertTokenizerFast
	snake_case_      =   True
	snake_case_      =   True
	snake_case_      =   True



	def   _lowerCamelCase    (   self	):
			super().setUp()

			# We have a SentencePiece fixture for testing
			lowerCAmelCase     :    Dict						=   AlbertTokenizer(a_	)
			tokenizer.save_pretrained(self.tmpdirname	)



	def   _lowerCamelCase    (   self		,      a_	):
			lowerCAmelCase     :    List[str]						=   'this is a test'
			lowerCAmelCase     :    List[str]						=   'this is a test'
			return input_text, output_text



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    int						=   '<pad>'
			lowerCAmelCase     :    Tuple						=   0

			self.assertEqual(self.get_tokenizer()._convert_token_to_id(a_	)		,      a_	)
			self.assertEqual(self.get_tokenizer()._convert_id_to_token(a_	)		,      a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Union[str, Any]						=   list(self.get_tokenizer().get_vocab().keys()	)

			self.assertEqual(vocab_keys[0]		,      "<pad>"	)
			self.assertEqual(vocab_keys[1]		,      "<unk>"	)
			self.assertEqual(vocab_keys[-1]		,      "▁eloquent"	)
			self.assertEqual(len(a_	)		,      30_000	)



	def   _lowerCamelCase    (   self	):
			self.assertEqual(self.get_tokenizer().vocab_size		,      30_000	)



	def   _lowerCamelCase    (   self	):







			if not self.test_rust_tokenizer:
					return

			lowerCAmelCase     :    List[str]						=   self.get_tokenizer()
			lowerCAmelCase     :    List[str]						=   self.get_rust_tokenizer()

			lowerCAmelCase     :    Optional[int]						=   'I was born in 92000, and this is falsé.'

			lowerCAmelCase     :    Dict						=   tokenizer.tokenize(a_	)
			lowerCAmelCase     :    Optional[Any]						=   rust_tokenizer.tokenize(a_	)
			self.assertListEqual(a_		,      a_	)

			lowerCAmelCase     :    Any						=   tokenizer.encode(a_		,      add_special_tokens=a_	)
			lowerCAmelCase     :    List[str]						=   rust_tokenizer.encode(a_		,      add_special_tokens=a_	)
			self.assertListEqual(a_		,      a_	)

			lowerCAmelCase     :    List[str]						=   self.get_rust_tokenizer()
			lowerCAmelCase     :    Dict						=   tokenizer.encode(a_	)
			lowerCAmelCase     :    List[Any]						=   rust_tokenizer.encode(a_	)
			self.assertListEqual(a_		,      a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[str]						=   AlbertTokenizer(a_		,      keep_accents=a_	)

			lowerCAmelCase     :    Optional[int]						=   tokenizer.tokenize("This is a test"	)
			self.assertListEqual(a_		,      ["▁this", "▁is", "▁a", "▁test"]	)

			self.assertListEqual(tokenizer.convert_tokens_to_ids(a_	)		,      [48, 25, 21, 1_289]	)

			lowerCAmelCase     :    Dict						=   tokenizer.tokenize("I was born in 92000, and this is falsé."	)
			self.assertListEqual(
			    a_		,      ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", "."]	)
			lowerCAmelCase     :    Any						=   tokenizer.convert_tokens_to_ids(a_	)
			self.assertListEqual(a_		,      [31, 23, 386, 19, 561, 3_050, 15, 17, 48, 25, 8_256, 18, 1, 9]	)

			lowerCAmelCase     :    Union[str, Any]						=   tokenizer.convert_ids_to_tokens(a_	)
			self.assertListEqual(
			    a_		,      ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "."]		,      )



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Optional[Any]						=   AlbertTokenizer(a_	)

			lowerCAmelCase     :    Tuple						=   tokenizer.encode("sequence builders"	)
			lowerCAmelCase     :    str						=   tokenizer.encode("multi-sequence build"	)

			lowerCAmelCase     :    Any						=   tokenizer.build_inputs_with_special_tokens(a_	)
			lowerCAmelCase     :    Dict						=   tokenizer.build_inputs_with_special_tokens(a_		,      a_	)

			assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
			assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
			    tokenizer.sep_token_id
			]





	@slow
	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[Any]						=   {'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'input_ids': [[2, 21_970, 13, 5, 6_092, 167, 28, 7_103, 2_153, 673, 8, 7_028, 12_051, 18, 17, 7_103, 2_153, 673, 8, 3_515, 18_684, 8, 4_461, 6, 1_927, 297, 8, 12_060, 2_607, 18, 13, 5, 4_461, 15, 10_538, 38, 8, 135, 15, 822, 58, 15, 993, 10_363, 15, 1_460, 8_005, 4_461, 15, 993, 255, 2_328, 9, 9, 9, 6, 26, 1_112, 816, 3_260, 13, 5, 103, 2_377, 6, 17, 1_112, 816, 2_782, 13, 5, 103, 10_641, 6, 29, 84, 2_512, 2_430, 782, 18_684, 2_761, 19, 808, 2_430, 2_556, 17, 855, 1_480, 9_477, 4_091, 128, 11_712, 15, 7_103, 2_153, 673, 17, 24_883, 9_990, 9, 3], [2, 11_502, 25, 1_006, 20, 782, 8, 11_809, 855, 1_732, 19_393, 18_667, 37, 367, 21_018, 69, 1_854, 34, 11_860, 19_124, 27, 156, 225, 17, 193, 4_141, 19, 65, 9_124, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 14, 2_231, 886, 2_385, 17_659, 84, 14, 16_792, 1_952, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
			# fmt: on

			self.tokenizer_integration_test_util(
			    expected_encoding=a_		,      model_name="albert-base-v2"		,      revision="6b6560eaf5ff2e250b00c50f380c5389a9c2d82e"		,      )










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



	def __init__(   self		,      a_		,      a_=2		,      a_=True		,      a_=False		,      a_=10		,      a_=3		,      a_=32 * 8		,      a_=32 * 8		,      a_=4		,      a_=64		,      ):
			lowerCAmelCase     :    Optional[int]						=   parent
			lowerCAmelCase     :    Optional[Any]						=   batch_size
			lowerCAmelCase     :    str						=   is_training
			lowerCAmelCase     :    Any						=   use_auxiliary_loss
			lowerCAmelCase     :    Dict						=   num_queries
			lowerCAmelCase     :    Dict						=   num_channels
			lowerCAmelCase     :    str						=   min_size
			lowerCAmelCase     :    Any						=   max_size
			lowerCAmelCase     :    int						=   num_labels
			lowerCAmelCase     :    Tuple						=   hidden_dim
			lowerCAmelCase     :    Optional[Any]						=   hidden_dim



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    int						=   floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]	).to(
			    a_	)

			lowerCAmelCase     :    Optional[int]						=   torch.ones([self.batch_size, self.min_size, self.max_size]		,      device=a_	)

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

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



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Optional[Any]						=   MaskaFormerConfig(
			    hidden_size=self.hidden_dim		,      )
			lowerCAmelCase     :    Optional[Any]						=   self.num_queries
			lowerCAmelCase     :    Optional[Any]						=   self.num_labels

			lowerCAmelCase     :    str						=   [1, 1, 1, 1]
			lowerCAmelCase     :    Tuple						=   self.num_channels

			lowerCAmelCase     :    str						=   64
			lowerCAmelCase     :    Any						=   128
			lowerCAmelCase     :    List[str]						=   self.hidden_dim
			lowerCAmelCase     :    Optional[Any]						=   self.hidden_dim
			lowerCAmelCase     :    int						=   self.hidden_dim
			return config



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase     :    int						=   self.prepare_config_and_inputs()
			lowerCAmelCase     :    Optional[int]						=   {"pixel_values": pixel_values, "pixel_mask": pixel_mask}
			return config, inputs_dict



	def   _lowerCamelCase    (   self		,      a_		,      a_	):
			lowerCAmelCase     :    str						=   output.encoder_hidden_states
			lowerCAmelCase     :    List[str]						=   output.pixel_decoder_hidden_states
			lowerCAmelCase     :    Any						=   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   _lowerCamelCase    (   self		,      a_		,      a_		,      a_		,      a_=False	):
			with torch.no_grad():
					lowerCAmelCase     :    Tuple						=   MaskaFormerModel(config=a_	)
					model.to(a_	)
					model.eval()

					lowerCAmelCase     :    Tuple						=   model(pixel_values=a_		,      pixel_mask=a_	)
					lowerCAmelCase     :    str						=   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   _lowerCamelCase    (   self		,      a_		,      a_		,      a_		,      a_		,      a_	):
			lowerCAmelCase     :    Union[str, Any]						=   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():
					lowerCAmelCase     :    List[str]						=   model(pixel_values=a_		,      pixel_mask=a_	)
					lowerCAmelCase     :    Union[str, Any]						=   model(a_	)

					comm_check_on_output(a_	)

					lowerCAmelCase     :    str						=   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 		lowerCamelCase  (      _A      ,   _A      ,   unittest.TestCase       ):
	snake_case_      =   (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
	snake_case_      =   {"feature-extraction": MaskaFormerModel} if is_torch_available() else {}

	snake_case_      =   False
	snake_case_      =   False
	snake_case_      =   False
	snake_case_      =   False



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[Any]						=   MaskaFormerModelTester(self	)
			lowerCAmelCase     :    Tuple						=   ConfigTester(self		,      config_class=a_		,      has_text_modality=a_	)



	def   _lowerCamelCase    (   self	):
			self.config_tester.run_common_tests()



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase	,		lowerCAmelCase     :    List[str]						=   self.model_tester.prepare_config_and_inputs_for_common()
			self.model_tester.create_and_check_maskaformer_model(a_		,      **a_		,      output_hidden_states=a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[str]						=   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   _lowerCamelCase    (   self	):
			pass



	@unittest.skip(reason="Mask2Former does not have a get_input_embeddings method"	)
	def   _lowerCamelCase    (   self	):
			pass



	@unittest.skip(reason="Mask2Former is not a generative model"	)
	def   _lowerCamelCase    (   self	):
			pass



	@unittest.skip(reason="Mask2Former does not use token embeddings"	)
	def   _lowerCamelCase    (   self	):
			pass



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



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



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase	,		lowerCAmelCase     :    Any						=   self.model_tester.prepare_config_and_inputs_for_common()

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

					lowerCAmelCase     :    Tuple						=   ["pixel_values"]
					self.assertListEqual(arg_names[:1]		,      a_	)



	@slow
	def   _lowerCamelCase    (   self	):
			for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
					lowerCAmelCase     :    int						=   MaskaFormerModel.from_pretrained(a_	)
					self.assertIsNotNone(a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[Any]						=   (self.model_tester.min_size,) * 2
			lowerCAmelCase     :    Union[str, Any]						=   {
			    "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(),
			}
			lowerCAmelCase     :    Optional[Any]						=   self.model_tester.get_config()

			lowerCAmelCase     :    List[Any]						=   MaskaFormerForUniversalSegmentation(a_	).to(a_	)
			lowerCAmelCase     :    Dict						=   model(**a_	)
			self.assertTrue(outputs.loss is not None	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase	,		lowerCAmelCase     :    int						=   self.model_tester.prepare_config_and_inputs_for_common()
			self.model_tester.create_and_check_maskaformer_model(a_		,      **a_		,      output_hidden_states=a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase	,		lowerCAmelCase     :    Optional[int]						=   self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					lowerCAmelCase     :    Optional[int]						=   model_class(a_	).to(a_	)
					lowerCAmelCase     :    Union[str, Any]						=   model(**a_		,      output_attentions=a_	)
					self.assertTrue(outputs.attentions is not None	)



	def   _lowerCamelCase    (   self	):
			if not self.model_tester.is_training:
					return

			lowerCAmelCase     :    List[Any]						=   self.all_model_classes[1]
			lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase     :    List[str]						=   self.model_tester.prepare_config_and_inputs()

			lowerCAmelCase     :    List[Any]						=   model_class(a_	)
			model.to(a_	)
			model.train()

			lowerCAmelCase     :    Union[str, Any]						=   model(a_		,      mask_labels=a_		,      class_labels=a_	).loss
			loss.backward()





	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Optional[int]						=   self.all_model_classes[1]
			lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase     :    Any						=   self.model_tester.prepare_config_and_inputs()
			lowerCAmelCase     :    Tuple						=   True
			lowerCAmelCase     :    int						=   True

			lowerCAmelCase     :    int						=   model_class(a_	).to(a_	)
			model.train()

			lowerCAmelCase     :    Any						=   model(a_		,      mask_labels=a_		,      class_labels=a_	)

			lowerCAmelCase     :    List[Any]						=   outputs.encoder_hidden_states[0]
			encoder_hidden_states.retain_grad()

			lowerCAmelCase     :    Optional[int]						=   outputs.pixel_decoder_hidden_states[0]
			pixel_decoder_hidden_states.retain_grad()

			lowerCAmelCase     :    List[Any]						=   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=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      __A						(							):
		lowerCAmelCase     :    str						=   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"	)
		return image

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



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



	@cached_property
	def   _lowerCamelCase    (   self	):
			return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints	) if is_vision_available() else None



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    int						=   MaskaFormerModel.from_pretrained(self.model_checkpoints	).to(a_	)
			lowerCAmelCase     :    Optional[int]						=   self.default_image_processor
			lowerCAmelCase     :    Dict						=   prepare_img()
			lowerCAmelCase     :    Any						=   image_processor(a_		,      return_tensors="pt"	).to(a_	)
			lowerCAmelCase     :    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(a_		,      (1, 3, 384, 384)	)

			with torch.no_grad():
					lowerCAmelCase     :    Dict						=   model(**a_	)

			lowerCAmelCase     :    Union[str, Any]						=   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_	)	)

			lowerCAmelCase     :    Optional[Any]						=   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_	)	)

			lowerCAmelCase     :    Optional[int]						=   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   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Any						=   MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints	).to(a_	).eval()
			lowerCAmelCase     :    Union[str, Any]						=   self.default_image_processor
			lowerCAmelCase     :    Dict						=   prepare_img()
			lowerCAmelCase     :    Tuple						=   image_processor(a_		,      return_tensors="pt"	).to(a_	)
			lowerCAmelCase     :    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(a_		,      (1, 3, 384, 384)	)

			with torch.no_grad():
					lowerCAmelCase     :    List[str]						=   model(**a_	)
			# masks_queries_logits
			lowerCAmelCase     :    Optional[Any]						=   outputs.masks_queries_logits
			self.assertEqual(
			    masks_queries_logits.shape		,      (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)	)
			lowerCAmelCase     :    Tuple						=   [
			    [-8.7839, -9.0056, -8.8121],
			    [-7.4104, -7.0313, -6.5401],
			    [-6.6105, -6.3427, -6.4675],
			]
			lowerCAmelCase     :    Tuple						=   torch.tensor(a_	).to(a_	)
			self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]		,      a_		,      atol=a_	)	)
			# class_queries_logits
			lowerCAmelCase     :    str						=   outputs.class_queries_logits
			self.assertEqual(class_queries_logits.shape		,      (1, model.config.num_queries, model.config.num_labels + 1)	)
			lowerCAmelCase     :    List[str]						=   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   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints	).to(a_	).eval()
			lowerCAmelCase     :    Optional[Any]						=   self.default_image_processor

			lowerCAmelCase     :    Optional[int]						=   image_processor(
			    [np.zeros((3, 800, 1_333)	), np.zeros((3, 800, 1_333)	)]		,      segmentation_maps=[np.zeros((384, 384)	).astype(np.floataa	), np.zeros((384, 384)	).astype(np.floataa	)]		,      return_tensors="pt"		,      )

			lowerCAmelCase     :    Optional[int]						=   inputs["pixel_values"].to(a_	)
			lowerCAmelCase     :    int						=   [el.to(a_	) for el in inputs["mask_labels"]]
			lowerCAmelCase     :    Union[str, Any]						=   [el.to(a_	) for el in inputs["class_labels"]]

			with torch.no_grad():
					lowerCAmelCase     :    Optional[Any]						=   model(**a_	)

			self.assertTrue(outputs.loss is not None	)



