






import inspect
from typing import Callable, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import DiffusionPipeline
from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
from diffusers.utils import logging


__A      =				logging.get_logger(__name__)  # pylint: disable=invalid-name





class 						SCREAMING_SNAKE_CASE				( snake_case					):





							"""simple docstring"""

							def __init__(  self:   Optional[Any]		,     __A:   AutoencoderKL		,     __A:   CLIPTextModel		,     __A:   CLIPTokenizer		,     __A:   UNetaDConditionModel		,     __A:   Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]		,     __A:   StableDiffusionSafetyChecker		,     __A:   CLIPImageProcessor		,     )    ->				Optional[Any]:
												super().__init__()
												self.register_modules(
												    vae=__A		,     text_encoder=__A		,     tokenizer=__A		,     unet=__A		,     scheduler=__A		,     safety_checker=__A		,     feature_extractor=__A		,     )

							def       __A (  self:   List[str]		,     __A:   Optional[Union[str, int]] = "auto"   )    ->				Union[str, Any]:
												if slice_size == "auto":
																	# half the attention head size is usually a good trade-off between
																	# speed and memory
																	_A								= self.unet.config.attention_head_dim // 2
												self.unet.set_attention_slice(__A   )

							def       __A (  self:   List[str]   )    ->				Tuple:
												self.enable_attention_slicing(__A   )



							@torch.no_grad()
							def __call__(  self:   Dict		,     __A:   Union[str, List[str]]		,     __A:   int = 5_12		,     __A:   int = 5_12		,     __A:   int = 50		,     __A:   float = 7.5		,     __A:   Optional[Union[str, List[str]]] = None		,     __A:   Optional[int] = 1		,     __A:   float = 0.0		,     __A:   Optional[torch.Generator] = None		,     __A:   Optional[torch.FloatTensor] = None		,     __A:   Optional[str] = "pil"		,     __A:   bool = True		,     __A:   Optional[Callable[[int, int, torch.FloatTensor], None]] = None		,     __A:   int = 1		,     __A:   Optional[torch.FloatTensor] = None		,     **__A:   List[str]		,     )    ->				int:

												if isinstance(__A		,     __A   ):
																	_A								= 1
												elif isinstance(__A		,     __A   ):
																	_A								= len(__A   )
												else:
																	raise ValueError(f"""`prompt` has to be of type `str` or `list` but is {type(__A   )}"""   )

												if height % 8 != 0 or width % 8 != 0:
																	raise ValueError(f"""`height` and `width` have to be divisible by 8 but are {height} and {width}."""   )

												if (callback_steps is None) or (
												    callback_steps is not None and (not isinstance(__A		,     __A   ) or callback_steps <= 0)
												):
																	raise ValueError(
																	    f"""`callback_steps` has to be a positive integer but is {callback_steps} of type"""
																	    f""" {type(__A   )}."""   )

												# get prompt text embeddings
												_A								= self.tokenizer(
												    __A		,     padding='''max_length'''		,     max_length=self.tokenizer.model_max_length		,     return_tensors='''pt'''		,     )
												_A								= text_inputs.input_ids

												if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
																	_A								= self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]   )
																	logger.warning(
																	    '''The following part of your input was truncated because CLIP can only handle sequences up to'''
																	    f""" {self.tokenizer.model_max_length} tokens: {removed_text}"""   )
																	_A								= text_input_ids[:, : self.tokenizer.model_max_length]

												if text_embeddings is None:
																	_A								= self.text_encoder(text_input_ids.to(self.device   )   )[0]

												# duplicate text embeddings for each generation per prompt, using mps friendly method
												_A							,_A							,_A								= text_embeddings.shape
												_A								= text_embeddings.repeat(1		,     __A		,     1   )
												_A								= text_embeddings.view(bs_embed * num_images_per_prompt		,     __A		,     -1   )

												# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
												# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
												# corresponds to doing no classifier free guidance.
												_A								= guidance_scale > 1.0
												# get unconditional embeddings for classifier free guidance
												if do_classifier_free_guidance:
																	_A								= 42
																	if negative_prompt is None:
																						_A								= ['''''']
																	elif type(__A   ) is not type(__A   ):
																						raise TypeError(
																						    f"""`negative_prompt` should be the same type to `prompt`, but got {type(__A   )} !="""
																						    f""" {type(__A   )}."""   )
																	elif isinstance(__A		,     __A   ):
																						_A								= [negative_prompt]
																	elif batch_size != len(__A   ):
																						raise ValueError(
																						    f"""`negative_prompt`: {negative_prompt} has batch size {len(__A   )}, but `prompt`:"""
																						    f""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"""
																						    ''' the batch size of `prompt`.'''   )
																	else:
																						_A								= negative_prompt

																	_A								= text_input_ids.shape[-1]
																	_A								= self.tokenizer(
																	    __A		,     padding='''max_length'''		,     max_length=__A		,     truncation=__A		,     return_tensors='''pt'''		,     )
																	_A								= self.text_encoder(uncond_input.input_ids.to(self.device   )   )[0]

																	# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
																	_A								= uncond_embeddings.shape[1]
																	_A								= uncond_embeddings.repeat(__A		,     __A		,     1   )
																	_A								= uncond_embeddings.view(batch_size * num_images_per_prompt		,     __A		,     -1   )

																	# For classifier free guidance, we need to do two forward passes.
																	# Here we concatenate the unconditional and text embeddings into a single batch
																	# to avoid doing two forward passes
																	_A								= torch.cat([uncond_embeddings, text_embeddings]   )

												# get the initial random noise unless the user supplied it

												# Unlike in other pipelines, latents need to be generated in the target device
												# for 1-to-1 results reproducibility with the CompVis implementation.
												# However this currently doesn't work in `mps`.
												_A								= (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
												_A								= (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64)
												_A								= text_embeddings.dtype
												if latents is None:
																	if self.device.type == "mps":
																						# randn does not exist on mps
																						_A								= torch.randn(
																						    __A		,     generator=__A		,     device='''cpu'''		,     dtype=__A   ).to(self.device   )
																						_A								= torch.randn(__A		,     generator=__A		,     device='''cpu'''		,     dtype=__A   ).to(
																						    self.device   )
																	else:
																						_A								= torch.randn(
																						    __A		,     generator=__A		,     device=self.device		,     dtype=__A   )
																						_A								= torch.randn(__A		,     generator=__A		,     device=self.device		,     dtype=__A   )
												else:
																	if latents_reference.shape != latents_shape:
																						raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}"""   )
																	_A								= latents_reference.to(self.device   )
																	_A								= latents.to(self.device   )

												# This is the key part of the pipeline where we
												# try to ensure that the generated images w/ the same seed
												# but different sizes actually result in similar images
												_A								= (latents_shape[3] - latents_shape_reference[3]) // 2
												_A								= (latents_shape[2] - latents_shape_reference[2]) // 2
												_A								= latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx
												_A								= latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy
												_A								= 0 if dx < 0 else dx
												_A								= 0 if dy < 0 else dy
												_A								= max(-dx		,     0   )
												_A								= max(-dy		,     0   )
												# import pdb
												# pdb.set_trace()
												_A								= latents_reference[:, :, dy : dy + h, dx : dx + w]

												# set timesteps
												self.scheduler.set_timesteps(__A   )

												# Some schedulers like PNDM have timesteps as arrays
												# It's more optimized to move all timesteps to correct device beforehand
												_A								= self.scheduler.timesteps.to(self.device   )

												# scale the initial noise by the standard deviation required by the scheduler
												_A								= latents * self.scheduler.init_noise_sigma

												# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
												# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
												# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
												# and should be between [0, 1]
												_A								= '''eta''' in set(inspect.signature(self.scheduler.step   ).parameters.keys()   )
												_A								= {}
												if accepts_eta:
																	_A								= eta

												for i, t in enumerate(self.progress_bar(__A   )   ):
																	# expand the latents if we are doing classifier free guidance
																	_A								= torch.cat([latents] * 2   ) if do_classifier_free_guidance else latents
																	_A								= self.scheduler.scale_model_input(__A		,     __A   )

																	# predict the noise residual
																	_A								= self.unet(__A		,     __A		,     encoder_hidden_states=__A   ).sample

																	# perform guidance
																	if do_classifier_free_guidance:
																						_A							,_A								= noise_pred.chunk(2   )
																						_A								= noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

																	# compute the previous noisy sample x_t -> x_t-1
																	_A								= self.scheduler.step(__A		,     __A		,     __A		,     **__A   ).prev_sample

																	# call the callback, if provided
																	if callback is not None and i % callback_steps == 0:
																						callback(__A		,     __A		,     __A   )

												_A								= 1 / 0.18_215 * latents
												_A								= self.vae.decode(__A   ).sample

												_A								= (image / 2 + 0.5).clamp(0		,     1   )

												# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
												_A								= image.cpu().permute(0		,     2		,     3		,     1   ).float().numpy()

												if self.safety_checker is not None:
																	_A								= self.feature_extractor(self.numpy_to_pil(__A   )		,     return_tensors='''pt'''   ).to(
																	    self.device   )
																	_A							,_A								= self.safety_checker(
																	    images=__A		,     clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype   )   )
												else:
																	_A								= None

												if output_type == "pil":
																	_A								= self.numpy_to_pil(__A   )

												if not return_dict:
																	return (image, has_nsfw_concept)

												return StableDiffusionPipelineOutput(images=__A		,     nsfw_content_detected=__A   )











import inspect
import unittest

import numpy as np

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

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


if is_torch_available():
				import torch

				from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

				if is_vision_available():
								from transformers import MaskFormerImageProcessor

if is_vision_available():
				from PIL import Image





class 						SCREAMING_SNAKE_CASE				:





							"""simple docstring"""

							def __init__(  self:   Optional[int]		,     __A:   Union[str, Any]		,     __A:   int=2		,     __A:   List[str]=True		,     __A:   List[Any]=False		,     __A:   Union[str, Any]=10		,     __A:   Optional[int]=3		,     __A:   List[Any]=32 * 4		,     __A:   Dict=32 * 6		,     __A:   Optional[Any]=4		,     __A:   Any=32		,     )    ->				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								= mask_feature_size

							def       __A (  self:   Dict   )    ->				Optional[int]:
												_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       __A (  self:   Optional[Any]   )    ->				Tuple:
												return MaskFormerConfig.from_backbone_and_decoder_configs(
												    backbone_config=SwinConfig(
												        depths=[1, 1, 1, 1]		,     )		,     decoder_config=DetrConfig(
												        decoder_ffn_dim=1_28		,     num_queries=self.num_queries		,     decoder_attention_heads=2		,     d_model=self.mask_feature_size		,     )		,     mask_feature_size=self.mask_feature_size		,     fpn_feature_size=self.mask_feature_size		,     num_channels=self.num_channels		,     num_labels=self.num_labels		,     )

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

							def       __A (  self:   Optional[int]		,     __A:   Union[str, Any]		,     __A:   Dict   )    ->				int:
												_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_config.decoder_layers   )

							def       __A (  self:   Optional[Any]		,     __A:   Union[str, Any]		,     __A:   Optional[Any]		,     __A:   Any		,     __A:   Dict=False   )    ->				Any:
												with torch.no_grad():
																	_A								= MaskFormerModel(config=__A   )
																	model.to(__A   )
																	model.eval()

																	_A								= model(pixel_values=__A		,     pixel_mask=__A   )
																	_A								= model(__A		,     output_hidden_states=__A   )
												# the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
												# encoder and pixel decoder
												self.parent.assertEqual(
												    output.transformer_decoder_last_hidden_state.shape		,     (self.batch_size, self.num_queries, self.mask_feature_size)		,     )
												# let's ensure the other two hidden state exists
												self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None   )
												self.parent.assertTrue(output.encoder_last_hidden_state is not None   )

												if output_hidden_states:
																	self.check_output_hidden_state(__A		,     __A   )



							def       __A (  self:   Optional[Any]		,     __A:   Union[str, Any]		,     __A:   Optional[Any]		,     __A:   Union[str, Any]		,     __A:   Union[str, Any]		,     __A:   List[Any]   )    ->				int:
												_A								= MaskFormerForInstanceSegmentation(config=__A   )
												model.to(__A   )
												model.eval()

												def comm_check_on_output(__A:   int   ):
																	# 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 						SCREAMING_SNAKE_CASE				( snake_case  ,	snake_case  ,	unittest.TestCase					):





							"""simple docstring"""

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

							A_						  =					False
							A_						  =					False
							A_						  =					False
							A_						  =					False

							def       __A (  self:   int   )    ->				Tuple:
												_A								= MaskFormerModelTester(self   )
												_A								= ConfigTester(self		,     config_class=__A		,     has_text_modality=__A   )

							def       __A (  self:   List[Any]   )    ->				Dict:
												self.config_tester.run_common_tests()

							def       __A (  self:   Optional[Any]   )    ->				int:
												_A							,_A								= self.model_tester.prepare_config_and_inputs_for_common()
												self.model_tester.create_and_check_maskformer_model(__A		,     **__A		,     output_hidden_states=__A   )

							def       __A (  self:   Dict   )    ->				Optional[Any]:
												_A								= self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*__A   )

							@unittest.skip(reason='''MaskFormer does not use inputs_embeds'''   )
							def       __A (  self:   int   )    ->				Tuple:
												pass

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

							@unittest.skip(reason='''MaskFormer is not a generative model'''   )
							def       __A (  self:   Union[str, Any]   )    ->				Optional[int]:
												pass

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

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

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

							def       __A (  self:   Dict   )    ->				Optional[Any]:
												_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       __A (  self:   int   )    ->				Optional[Any]:
												for model_name in ["facebook/maskformer-swin-small-coco"]:
																	_A								= MaskFormerModel.from_pretrained(__A   )
																	self.assertIsNotNone(__A   )

							def       __A (  self:   Optional[Any]   )    ->				Optional[int]:
												_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								= MaskFormerForInstanceSegmentation(MaskFormerConfig()   ).to(__A   )
												_A								= model(**__A   )
												self.assertTrue(outputs.loss is not None   )

							def       __A (  self:   Optional[Any]   )    ->				List[Any]:
												_A							,_A								= self.model_tester.prepare_config_and_inputs_for_common()
												self.model_tester.create_and_check_maskformer_model(__A		,     **__A		,     output_hidden_states=__A   )

							def       __A (  self:   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   ).to(__A   )
																	_A								= model(**__A		,     output_attentions=__A   )
																	self.assertTrue(outputs.attentions is not None   )

							def       __A (  self:   Dict   )    ->				Union[str, Any]:
												if not self.model_tester.is_training:
																	return
												# only MaskFormerForInstanceSegmentation has the loss
												_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       __A (  self:   Tuple   )    ->				Optional[Any]:
												# only MaskFormerForInstanceSegmentation has the loss
												_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   )
												model.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()
												# we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
												_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   )



__A      =				1e-4




def      __A					(     ):

					'''simple docstring'''







					_A								= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
					return image





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





							"""simple docstring"""

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

							def       __A (  self:   List[Any]   )    ->				Any:
												_A								= MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco'''   ).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, 8_00, 10_88)   )

												with torch.no_grad():
																	_A								= model(**__A   )

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

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

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

							def       __A (  self:   Dict   )    ->				Dict:
												_A								= (
												    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''   )
												    .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, 8_00, 10_88)   )

												with torch.no_grad():
																	_A								= model(**__A   )
												# masks_queries_logits
												_A								= outputs.masks_queries_logits
												self.assertEqual(
												    masks_queries_logits.shape		,     (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)		,     )
												_A								= [
												    [-1.3_737_124, -1.7_724_937, -1.9_364_233],
												    [-1.5_977_281, -1.9_867_939, -2.1_523_695],
												    [-1.5_795_398, -1.9_269_832, -2.093_942],
												]
												_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.decoder_config.num_queries, model.config.num_labels + 1)   )
												_A								= torch.tensor(
												    [
												        [1.65_12e00, -5.25_72e00, -3.35_19e00],
												        [3.61_69e-02, -5.90_25e00, -2.93_13e00],
												        [1.07_66e-04, -7.76_30e00, -5.12_63e00],
												    ]   ).to(__A   )
												self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		,     __A		,     atol=__A   )   )

							def       __A (  self:   List[Any]   )    ->				Dict:
												_A								= (
												    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff'''   )
												    .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, 8_00, 10_88)   )

												with torch.no_grad():
																	_A								= model(**__A   )
												# masks_queries_logits
												_A								= outputs.masks_queries_logits
												self.assertEqual(
												    masks_queries_logits.shape		,     (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)		,     )
												_A								= [[-0.9_046, -2.6_366, -4.6_062], [-3.4_179, -5.7_890, -8.8_057], [-4.9_179, -7.6_560, -10.7_711]]
												_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.decoder_config.num_queries, model.config.num_labels + 1)   )
												_A								= torch.tensor(
												    [[4.7_188, -3.2_585, -2.8_857], [6.6_871, -2.9_181, -1.2_487], [7.2_449, -2.2_764, -2.1_874]]   ).to(__A   )
												self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		,     __A		,     atol=__A   )   )



							def       __A (  self:   Optional[Any]   )    ->				str:
												_A								= (
												    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''   )
												    .to(__A   )
												    .eval()
												)
												_A								= self.default_image_processor

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

												_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   )



