


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 	snake_case_       :
					def __init__(				self       :Optional[Any]      ,__snake_case       :Union[str, Any]      ,__snake_case       :Union[str, Any]=2      ,__snake_case       :Union[str, Any]=True      ,__snake_case       :int=False      ,__snake_case       :Optional[Any]=10      ,__snake_case       :Optional[Any]=3      ,__snake_case       :Optional[Any]=32 * 4      ,__snake_case       :Optional[Any]=32 * 6      ,__snake_case       :int=4      ,__snake_case       :str=32      ,) ->   Union[str, Any]:
												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 			lowerCamelCase__(				self       :List[Any]				) ->   Dict:
												a__					=					floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]				).to(
												    __snake_case				)

												a__					=					torch.ones([self.batch_size, self.min_size, self.max_size]      ,device=__snake_case				)

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

												a__					=					self.get_config()
												return config, pixel_values, pixel_mask, mask_labels, class_labels
					def 			lowerCamelCase__(				self       :List[Any]				) ->   List[Any]:
												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 			lowerCamelCase__(				self       :str				) ->   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 			lowerCamelCase__(				self       :List[Any]      ,__snake_case       :Optional[Any]      ,__snake_case       :Any				) ->   List[Any]:
												a__					=					output.encoder_hidden_states
												a__					=					output.pixel_decoder_hidden_states
												a__					=					output.transformer_decoder_hidden_states

												self.parent.assertTrue(len(__snake_case				)      ,len(config.backbone_config.depths				)				)
												self.parent.assertTrue(len(__snake_case				)      ,len(config.backbone_config.depths				)				)
												self.parent.assertTrue(len(__snake_case				)      ,config.decoder_config.decoder_layers				)
					def 			lowerCamelCase__(				self       :List[str]      ,__snake_case       :Any      ,__snake_case       :Dict      ,__snake_case       :Union[str, Any]      ,__snake_case       :Optional[int]=False				) ->   Optional[int]:
												with torch.no_grad():
																			a__					=					MaskFormerModel(config=__snake_case				)
																			model.to(__snake_case				)
																			model.eval()

																			a__					=					model(pixel_values=__snake_case      ,pixel_mask=__snake_case				)
																			a__					=					model(__snake_case      ,output_hidden_states=__snake_case				)
												# 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(__snake_case      ,__snake_case				)



					def 			lowerCamelCase__(				self       :List[str]      ,__snake_case       :str      ,__snake_case       :Optional[Any]      ,__snake_case       :str      ,__snake_case       :Dict      ,__snake_case       :Dict				) ->   Tuple:
												a__					=					MaskFormerForInstanceSegmentation(config=__snake_case				)
												model.to(__snake_case				)
												model.eval()

												def comm_check_on_output(__snake_case       :List[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=__snake_case      ,pixel_mask=__snake_case				)
																			a__					=					model(__snake_case				)

																			comm_check_on_output(__snake_case				)

																			a__					=					model(
																			    pixel_values=__snake_case      ,pixel_mask=__snake_case      ,mask_labels=__snake_case      ,class_labels=__snake_case				)

												comm_check_on_output(__snake_case				)

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







@require_torch
class 	snake_case_       (lowerCamelCase_     ,						lowerCamelCase_     ,						unittest.TestCase				):
					UpperCAmelCase__ :   List[str]   				=      (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
					UpperCAmelCase__ :   Union[str, Any]   				=      (
					    {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation}
					    if is_torch_available()
					    else {}
					)

					UpperCAmelCase__ :   Dict   				=      False
					UpperCAmelCase__ :   Union[str, Any]   				=      False
					UpperCAmelCase__ :   int   				=      False
					UpperCAmelCase__ :   int   				=      False
					def 			lowerCamelCase__(				self       :List[str]				) ->   Optional[int]:
												a__					=					MaskFormerModelTester(self				)
												a__					=					ConfigTester(self      ,config_class=__snake_case      ,has_text_modality=__snake_case				)
					def 			lowerCamelCase__(				self       :str				) ->   Optional[Any]:
												self.config_tester.run_common_tests()
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   Tuple:
												a__				, a__					=					self.model_tester.prepare_config_and_inputs_for_common()
												self.model_tester.create_and_check_maskformer_model(__snake_case      ,**__snake_case      ,output_hidden_states=__snake_case				)
					def 			lowerCamelCase__(				self       :Dict				) ->   Dict:
												a__					=					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*__snake_case				)
					@unittest.skip(reason='MaskFormer does not use inputs_embeds'				)
					def 			lowerCamelCase__(				self       :Tuple				) ->   List[Any]:
												pass
					@unittest.skip(reason='MaskFormer does not have a get_input_embeddings method'				)
					def 			lowerCamelCase__(				self       :Dict				) ->   List[Any]:
												pass
					@unittest.skip(reason='MaskFormer is not a generative model'				)
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   Tuple:
												pass
					@unittest.skip(reason='MaskFormer does not use token embeddings'				)
					def 			lowerCamelCase__(				self       :List[str]				) ->   Optional[int]:
												pass
					@require_torch_multi_gpu
					@unittest.skip(
					    reason='MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'				)
					def 			lowerCamelCase__(				self       :Dict				) ->   Optional[Any]:
												pass
					@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'				)
					def 			lowerCamelCase__(				self       :List[str]				) ->   Dict:
												pass
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   Any:
												a__				, a__					=					self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			a__					=					model_class(__snake_case				)
																			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]      ,__snake_case				)
					@slow
					def 			lowerCamelCase__(				self       :int				) ->   Optional[Any]:
												for model_name in ["facebook/maskformer-swin-small-coco"]:
																			a__					=					MaskFormerModel.from_pretrained(__snake_case				)
																			self.assertIsNotNone(__snake_case				)
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   Tuple:
												a__					=					(self.model_tester.min_size,) * 2
												a__					=					{
												    'pixel_values': torch.randn((2, 3, *size)      ,device=__snake_case				),
												    'mask_labels': torch.randn((2, 10, *size)      ,device=__snake_case				),
												    'class_labels': torch.zeros(2      ,10      ,device=__snake_case				).long(),
												}

												a__					=					MaskFormerForInstanceSegmentation(MaskFormerConfig()				).to(__snake_case				)
												a__					=					model(**__snake_case				)
												self.assertTrue(outputs.loss is not None				)
					def 			lowerCamelCase__(				self       :List[Any]				) ->   int:
												a__				, a__					=					self.model_tester.prepare_config_and_inputs_for_common()
												self.model_tester.create_and_check_maskformer_model(__snake_case      ,**__snake_case      ,output_hidden_states=__snake_case				)
					def 			lowerCamelCase__(				self       :Tuple				) ->   List[Any]:
												a__				, a__					=					self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			a__					=					model_class(__snake_case				).to(__snake_case				)
																			a__					=					model(**__snake_case      ,output_attentions=__snake_case				)
																			self.assertTrue(outputs.attentions is not None				)
					def 			lowerCamelCase__(				self       :Tuple				) ->   List[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(__snake_case				)
												model.to(__snake_case				)
												model.train()

												a__					=					model(__snake_case      ,mask_labels=__snake_case      ,class_labels=__snake_case				).loss
												loss.backward()



					def 			lowerCamelCase__(				self       :Any				) ->   Dict:
												# 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(__snake_case				)
												model.to(__snake_case				)
												model.train()

												a__					=					model(__snake_case      ,mask_labels=__snake_case      ,class_labels=__snake_case				)

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

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





snake_case							:	int					      =				1e-4






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







@require_vision
@slow
class 	snake_case_       (unittest.TestCase				):
					@cached_property
					def 			lowerCamelCase__(				self       :int				) ->   str:
												return (
												    MaskFormerImageProcessor.from_pretrained('facebook/maskformer-swin-small-coco'				)
												    if is_vision_available()
												    else None
												)
					def 			lowerCamelCase__(				self       :Dict				) ->   Any:
												a__					=					MaskFormerModel.from_pretrained('facebook/maskformer-swin-small-coco'				).to(__snake_case				)
												a__					=					self.default_image_processor
												a__					=					prepare_img()
												a__					=					image_processor(__snake_case      ,return_tensors='pt'				).to(__snake_case				)
												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(__snake_case      ,(1, 3, 8_00, 10_88)				)

												with torch.no_grad():
																			a__					=					model(**__snake_case				)

												a__					=					torch.tensor(
												    [[-0.04_82, 0.92_28, 0.49_51], [-0.25_47, 0.80_17, 0.85_27], [-0.00_69, 0.33_85, -0.00_89]]				).to(__snake_case				)
												self.assertTrue(
												    torch.allclose(
												        outputs.encoder_last_hidden_state[0, 0, :3, :3]      ,__snake_case      ,atol=__snake_case				)				)

												a__					=					torch.tensor(
												    [[-0.84_22, -0.84_34, -0.97_18], [-1.01_44, -0.55_65, -0.41_95], [-1.00_38, -0.44_84, -0.19_61]]				).to(__snake_case				)
												self.assertTrue(
												    torch.allclose(
												        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]      ,__snake_case      ,atol=__snake_case				)				)

												a__					=					torch.tensor(
												    [[0.28_52, -0.01_59, 0.97_35], [0.62_54, 0.18_58, 0.85_29], [-0.06_80, -0.41_16, 1.84_13]]				).to(__snake_case				)
												self.assertTrue(
												    torch.allclose(
												        outputs.transformer_decoder_last_hidden_state[0, :3, :3]      ,__snake_case      ,atol=__snake_case				)				)
					def 			lowerCamelCase__(				self       :List[str]				) ->   Dict:
												a__					=					(
												    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'				)
												    .to(__snake_case				)
												    .eval()
												)
												a__					=					self.default_image_processor
												a__					=					prepare_img()
												a__					=					image_processor(__snake_case      ,return_tensors='pt'				).to(__snake_case				)
												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(__snake_case      ,(1, 3, 8_00, 10_88)				)

												with torch.no_grad():
																			a__					=					model(**__snake_case				)
												# 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_73_71_24, -1.7_72_49_37, -1.9_36_42_33],
												    [-1.5_97_72_81, -1.9_86_79_39, -2.1_52_36_95],
												    [-1.5_79_53_98, -1.9_26_98_32, -2.09_39_42],
												]
												a__					=					torch.tensor(__snake_case				).to(__snake_case				)
												self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]      ,__snake_case      ,atol=__snake_case				)				)
												# 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.6512E00, -5.2572E00, -3.3519E00],
												        [3.6169E-02, -5.9025E00, -2.9313E00],
												        [1.0766E-04, -7.7630E00, -5.1263E00],
												    ]				).to(__snake_case				)
												self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]      ,__snake_case      ,atol=__snake_case				)				)
					def 			lowerCamelCase__(				self       :str				) ->   Optional[int]:
												a__					=					(
												    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-resnet101-coco-stuff'				)
												    .to(__snake_case				)
												    .eval()
												)
												a__					=					self.default_image_processor
												a__					=					prepare_img()
												a__					=					image_processor(__snake_case      ,return_tensors='pt'				).to(__snake_case				)
												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(__snake_case      ,(1, 3, 8_00, 10_88)				)

												with torch.no_grad():
																			a__					=					model(**__snake_case				)
												# 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.90_46, -2.63_66, -4.60_62], [-3.41_79, -5.78_90, -8.80_57], [-4.91_79, -7.65_60, -10.77_11]]
												a__					=					torch.tensor(__snake_case				).to(__snake_case				)
												self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]      ,__snake_case      ,atol=__snake_case				)				)
												# 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.71_88, -3.25_85, -2.88_57], [6.68_71, -2.91_81, -1.24_87], [7.24_49, -2.27_64, -2.18_74]]				).to(__snake_case				)
												self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]      ,__snake_case      ,atol=__snake_case				)				)



					def 			lowerCamelCase__(				self       :List[str]				) ->   Any:
												a__					=					(
												    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'				)
												    .to(__snake_case				)
												    .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(__snake_case				)
												a__					=					[el.to(__snake_case				) for el in inputs['mask_labels']]
												a__					=					[el.to(__snake_case				) for el in inputs['class_labels']]

												with torch.no_grad():
																			a__					=					model(**__snake_case				)

												self.assertTrue(outputs.loss is not None				)








from collections import defaultdict
from math import ceil, sqrt






def 							__lowercase       (			__lowerCAmelCase							:    int = 1_0_0_0_0_0_0 ,   __lowerCAmelCase							:    int = 1_0       ):
							a__					=					defaultdict(__lowerCAmelCase       )

							for outer_width in range(3 ,   (t_limit // 4) + 2       ):
														if outer_width * outer_width > t_limit:
																					a__					=					max(
																					    ceil(sqrt(outer_width * outer_width - t_limit       )       ) ,   1       )
														else:
																					a__					=					1

														hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2

														for hole_width in range(__lowerCAmelCase ,   outer_width - 1 ,   2       ):
																					count[outer_width * outer_width - hole_width * hole_width] += 1

							return sum(1 for n in count.values() if 1 <= n <= 1_0       )


if __name__ == "__main__":
						print(f"""{solution() = }""")




