
"""simple docstring"""


import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
from torchvision import transforms

from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling






def       _snake_case				(		snake_case__   :     List[Any]     ):
	A						   =   [2, 2, 6, 2] if 'tiny' in model_name else [2, 2, 18, 2]
	A						   =   True if 'large' in model_name or 'huge' in model_name else False
	A						   =   True if 'large' in model_name or 'huge' in model_name else False
	A						   =   True if 'large' in model_name or 'huge' in model_name else False

	if "large" in model_name or "xlarge" in model_name or "huge" in model_name:
		if "fl3" in model_name:
			A						   =   [3, 3, 3, 3]
			A						   =   [5, 5, 5, 5]
		elif "fl4" in model_name:
			A						   =   [4, 4, 4, 4]
			A						   =   [3, 3, 3, 3]

	if "tiny" in model_name or "small" in model_name or "base" in model_name:
		A						   =   [3, 3, 3, 3]
		if "lrf" in model_name:
			A						   =   [3, 3, 3, 3]
		else:
			A						   =   [2, 2, 2, 2]

	if "tiny" in model_name:
		A						   =   96
	elif "small" in model_name:
		A						   =   96
	elif "base" in model_name:
		A						   =   128
	elif "large" in model_name:
		A						   =   192
	elif "xlarge" in model_name:
		A						   =   256
	elif "huge" in model_name:
		A						   =   352

	# set label information
	A						   =   'huggingface/label-files'
	if "large" in model_name or "huge" in model_name:
		A						   =   'imagenet-22k-id2label.json'
	else:
		A						   =   'imagenet-1k-id2label.json'

	A						   =   json.load(open(hf_hub_download(snake_case__	,      snake_case__	,      repo_type='dataset'     )	,      'r'     )     )
	A						   =   {int(snake_case__     ): v for k, v in idalabel.items()}
	A						   =   {v: k for k, v in idalabel.items()}

	A						   =   FocalNetConfig(
	    embed_dim=snake_case__	,      depths=snake_case__	,      focal_levels=snake_case__	,      focal_windows=snake_case__	,      use_conv_embed=snake_case__	,      idalabel=snake_case__	,      labelaid=snake_case__	,      use_post_layernorm=snake_case__	,      use_layerscale=snake_case__	,      )

	return config






def       _snake_case				(		snake_case__   :     int     ):
	if "patch_embed.proj" in name:
		A						   =   name.replace('patch_embed.proj'	,      'embeddings.patch_embeddings.projection'     )
	if "patch_embed.norm" in name:
		A						   =   name.replace('patch_embed.norm'	,      'embeddings.norm'     )
	if "layers" in name:
		A						   =   'encoder.' + name
	if "encoder.layers" in name:
		A						   =   name.replace('encoder.layers'	,      'encoder.stages'     )
	if "downsample.proj" in name:
		A						   =   name.replace('downsample.proj'	,      'downsample.projection'     )
	if "blocks" in name:
		A						   =   name.replace('blocks'	,      'layers'     )
	if "modulation.f.weight" in name or "modulation.f.bias" in name:
		A						   =   name.replace('modulation.f'	,      'modulation.projection_in'     )
	if "modulation.h.weight" in name or "modulation.h.bias" in name:
		A						   =   name.replace('modulation.h'	,      'modulation.projection_context'     )
	if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
		A						   =   name.replace('modulation.proj'	,      'modulation.projection_out'     )

	if name == "norm.weight":
		A						   =   'layernorm.weight'
	if name == "norm.bias":
		A						   =   'layernorm.bias'

	if "head" in name:
		A						   =   name.replace('head'	,      'classifier'     )
	else:
		A						   =   'focalnet.' + name

	return name






def       _snake_case				(		snake_case__   :     str	,      snake_case__   :     Any	,      snake_case__   :     Dict=False     ):
	# fmt: off
	A						   =   {
	    'focalnet-tiny': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth',
	    'focalnet-tiny-lrf': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth',
	    'focalnet-small': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth',
	    'focalnet-small-lrf': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth',
	    'focalnet-base': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth',
	    'focalnet-base-lrf': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth',
	    'focalnet-large-lrf-fl3': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth',
	    'focalnet-large-lrf-fl4': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth',
	    'focalnet-xlarge-lrf-fl3': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth',
	    'focalnet-xlarge-lrf-fl4': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth',
	}
	# fmt: on

	A						   =   model_name_to_url[model_name]
	print('Checkpoint URL: '	,      snake_case__     )
	A						   =   torch.hub.load_state_dict_from_url(snake_case__	,      map_location='cpu'     )['model']

	# rename keys
	for key in state_dict.copy().keys():
		A						   =   state_dict.pop(snake_case__     )
		A						   =   val

	A						   =   get_focalnet_config(snake_case__     )
	A						   =   FocalNetForImageClassification(snake_case__     )
	model.eval()

	# load state dict
	model.load_state_dict(snake_case__     )

	# verify conversion
	A						   =   'http://images.cocodataset.org/val2017/000000039769.jpg'

	A						   =   BitImageProcessor(
	    do_resize=snake_case__	,      size={'shortest_edge': 256}	,      resample=PILImageResampling.BILINEAR	,      do_center_crop=snake_case__	,      crop_size=224	,      do_normalize=snake_case__	,      image_mean=snake_case__	,      image_std=snake_case__	,      )
	A						   =   Image.open(requests.get(snake_case__	,      stream=snake_case__     ).raw     )
	A						   =   processor(images=snake_case__	,      return_tensors='pt'     )

	A						   =   transforms.Compose(
	    [
	        transforms.Resize(256     ),
	        transforms.CenterCrop(224     ),
	        transforms.ToTensor(),
	        transforms.Normalize(mean=[0.485, 0.456, 0.406]	,      std=[0.229, 0.224, 0.225]     ),
	    ]     )

	A						   =   image_transforms(snake_case__     ).unsqueeze(0     )

	# verify pixel_values
	assert torch.allclose(inputs.pixel_values	,      snake_case__	,      atol=1e-4     )

	A						   =   model(**snake_case__     )

	A						   =   outputs.logits.argmax(-1     ).item()
	print('Predicted class:'	,      model.config.idalabel[predicted_class_idx]     )

	print('First values of logits:'	,      outputs.logits[0, :3]     )

	if model_name == "focalnet-tiny":
		A						   =   torch.tensor([0.2166, -0.4368, 0.2191]     )
	elif model_name == "focalnet-tiny-lrf":
		A						   =   torch.tensor([1.1669, 0.0125, -0.1695]     )
	elif model_name == "focalnet-small":
		A						   =   torch.tensor([0.4917, -0.0430, 0.1341]     )
	elif model_name == "focalnet-small-lrf":
		A						   =   torch.tensor([-0.2588, -0.5342, -0.2331]     )
	elif model_name == "focalnet-base":
		A						   =   torch.tensor([-0.1655, -0.4090, -0.1730]     )
	elif model_name == "focalnet-base-lrf":
		A						   =   torch.tensor([0.5306, -0.0483, -0.3928]     )
	assert torch.allclose(outputs.logits[0, :3]	,      snake_case__	,      atol=1e-4     )
	print('Looks ok!'     )

	if pytorch_dump_folder_path is not None:
		print(F'Saving model and processor of {model_name} to {pytorch_dump_folder_path}'     )
		model.save_pretrained(snake_case__     )
		processor.save_pretrained(snake_case__     )

	if push_to_hub:
		print(F'Pushing model and processor of {model_name} to the hub...'     )
		model.push_to_hub(F'{model_name}'     )
		processor.push_to_hub(F'{model_name}'     )


if __name__ == "__main__":
		_lowercase							   =				argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '''--model_name''',
		    default='''focalnet-tiny''',
		    type=str,
		    help='''Name of the FocalNet model you\'d like to convert.''',
		)
		parser.add_argument(
		    '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.'''
		)
		parser.add_argument(
		    '''--push_to_hub''',
		    action='''store_true''',
		    help='''Whether to push the model and processor to the hub.''',
		)

		_lowercase							   =				parser.parse_args()
		convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)

"""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_				:



						'''simple docstring'''


						def __init__( self     :      Optional[Any]  ,A_     :      Optional[Any]  ,A_     :      Optional[int]=2  ,A_     :      Any=True  ,A_     :      List[str]=False  ,A_     :      Tuple=10  ,A_     :      List[Any]=3  ,A_     :      Any=32 * 8  ,A_     :      Dict=32 * 8  ,A_     :      List[Any]=4  ,A_     :      Tuple=64  ,)     ->   List[str]:
							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 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]      )     ->   List[str]:
							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 						_SCREAMING_SNAKE_CASE      ( self     :      Union[str, Any]      )     ->   Optional[int]:
							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 						_SCREAMING_SNAKE_CASE      ( self     :      List[Any]      )     ->   Any:
							A   ,      A   ,      A   ,      A   ,      A						   =   self.prepare_config_and_inputs()
							A						   =   {'pixel_values': pixel_values, 'pixel_mask': pixel_mask}
							return config, inputs_dict


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]  ,A_     :      Union[str, Any]  ,A_     :      Optional[int]      )     ->   Union[str, Any]:
							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 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]  ,A_     :      List[Any]  ,A_     :      Dict  ,A_     :      List[str]  ,A_     :      Union[str, Any]=False      )     ->   str:
							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 						_SCREAMING_SNAKE_CASE      ( self     :      List[Any]  ,A_     :      List[Any]  ,A_     :      Any  ,A_     :      Dict  ,A_     :      Any  ,A_     :      Dict      )     ->   Optional[Any]:
							A						   =   MaskaFormerForUniversalSegmentation(config=A_      )
							model.to(A_      )
							model.eval()

							def comm_check_on_output(A_     :      str      ):
								# 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 				lowerCAmelCase_				(      _lowercase		,					_lowercase		,					unittest.TestCase    ):



						'''simple docstring'''


						_lowerCamelCase:    Union[str, Any]      =    (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
						_lowerCamelCase:    Optional[Any]      =    {'''feature-extraction''': MaskaFormerModel} if is_torch_available() else {}

						_lowerCamelCase:    int      =    False
						_lowerCamelCase:    Dict      =    False
						_lowerCamelCase:    List[str]      =    False
						_lowerCamelCase:    int      =    False


						def 						_SCREAMING_SNAKE_CASE      ( self     :      int      )     ->   Dict:
							A						   =   MaskaFormerModelTester(self      )
							A						   =   ConfigTester(self  ,config_class=A_  ,has_text_modality=A_      )


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


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Any      )     ->   Dict:
							A   ,      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 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]      )     ->   str:
							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 						_SCREAMING_SNAKE_CASE      ( self     :      List[Any]      )     ->   Tuple:
							pass


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


						@unittest.skip(reason='Mask2Former is not a generative model'      )
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Dict      )     ->   Dict:
							pass


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


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


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


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]      )     ->   Tuple:
							A   ,      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 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]      )     ->   Any:
							for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
								A						   =   MaskaFormerModel.from_pretrained(A_      )
								self.assertIsNotNone(A_      )


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Any      )     ->   List[str]:
							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 						_SCREAMING_SNAKE_CASE      ( self     :      List[str]      )     ->   Optional[Any]:
							A   ,      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 						_SCREAMING_SNAKE_CASE      ( self     :      Tuple      )     ->   List[str]:
							A   ,      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 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]      )     ->   Tuple:
							if not self.model_tester.is_training:
								return

							A						   =   self.all_model_classes[1]
							A   ,      A   ,      A   ,      A   ,      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 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]      )     ->   Tuple:
							A						   =   self.all_model_classes[1]
							A   ,      A   ,      A   ,      A   ,      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      )

_lowercase							   =				1e-4






def       _snake_case				(		):
	A						   =   Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
	return image







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



						'''simple docstring'''


						@cached_property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Dict      )     ->   int:
							return "facebook/mask2former-swin-small-coco-instance"


						@cached_property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      int      )     ->   List[str]:
							return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints      ) if is_vision_available() else None


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]      )     ->   Optional[int]:
							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.27_90, -1.07_17, -1.16_68], [-0.51_28, -0.31_28, -0.49_87], [-0.58_32, 0.19_71, -0.01_97]]      ).to(A_      )
							self.assertTrue(
							    torch.allclose(
							        outputs.encoder_last_hidden_state[0, 0, :3, :3]  ,A_  ,atol=A_      )      )

							A						   =   torch.tensor(
							    [[0.89_73, 1.18_47, 1.17_76], [1.19_34, 1.50_40, 1.51_28], [1.11_53, 1.44_86, 1.49_51]]      ).to(A_      )
							self.assertTrue(
							    torch.allclose(
							        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]  ,A_  ,atol=A_      )      )

							A						   =   torch.tensor(
							    [[2.11_52, 1.70_00, -0.86_03], [1.58_08, 1.80_04, -0.93_53], [1.60_43, 1.74_95, -0.59_99]]      ).to(A_      )
							self.assertTrue(
							    torch.allclose(
							        outputs.transformer_decoder_last_hidden_state[0, :3, :3]  ,A_  ,atol=A_      )      )


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]      )     ->   Union[str, Any]:
							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.78_39, -9.00_56, -8.81_21],
							    [-7.41_04, -7.03_13, -6.54_01],
							    [-6.61_05, -6.34_27, -6.46_75],
							]
							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.83_24, -8.08_35, -4.19_22],
							        [0.84_50, -9.00_50, -3.60_53],
							        [0.30_45, -7.72_93, -3.02_75],
							    ]      ).to(A_      )
							self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]  ,A_  ,atol=A_      )      )




						def 						_SCREAMING_SNAKE_CASE      ( self     :      List[str]      )     ->   int:
							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      )