





"""simple docstring"""
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 					_UpperCAmelCase   :


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

												A		=  torch.ones([self.batch_size, self.min_size, self.max_size]     ,       device=__UpperCamelCase		)

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

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


					def 	lowerCamelCase       (					self			:Optional[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			: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			:Any     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Tuple		):
												A		=  output.encoder_hidden_states
												A		=  output.pixel_decoder_hidden_states
												A		=  output.transformer_decoder_hidden_states

												self.parent.assertTrue(len(__UpperCamelCase		)     ,       len(config.backbone_config.depths		)		)
												self.parent.assertTrue(len(__UpperCamelCase		)     ,       len(config.backbone_config.depths		)		)
												self.parent.assertTrue(len(__UpperCamelCase		)     ,       config.decoder_config.decoder_layers		)


					def 	lowerCamelCase       (					self			:List[str]     ,       __UpperCamelCase			:Optional[Any]     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:int=False		):
												with torch.no_grad():
																			A		=  MaskFormerModel(config=__UpperCamelCase		)
																			model.to(__UpperCamelCase		)
																			model.eval()

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


					def 	lowerCamelCase       (					self			:str     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:str		):
												A		=  MaskFormerForInstanceSegmentation(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()

												def comm_check_on_output(__UpperCamelCase			: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=__UpperCamelCase     ,       pixel_mask=__UpperCamelCase		)
																			A		=  model(__UpperCamelCase		)

																			comm_check_on_output(__UpperCamelCase		)

																			A		=  model(
																			    pixel_values=__UpperCamelCase     ,       pixel_mask=__UpperCamelCase     ,       mask_labels=__UpperCamelCase     ,       class_labels=__UpperCamelCase		)

												comm_check_on_output(__UpperCamelCase		)

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







@require_torch
class 					_UpperCAmelCase   (	lowercase_     ,							lowercase_     ,							unittest.TestCase ):
					UpperCamelCase      =							(MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
					UpperCamelCase      =							(
					    {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation}
					    if is_torch_available()
					    else {}
					)

					UpperCamelCase      =							False
					UpperCamelCase      =							False
					UpperCamelCase      =							False
					UpperCamelCase      =							False


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												A		=  MaskFormerModelTester(self		)
												A		=  ConfigTester(self     ,       config_class=__UpperCamelCase     ,       has_text_modality=__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:List[Any]		):
												self.config_tester.run_common_tests()


					def 	lowerCamelCase       (					self			:Tuple		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()
												self.model_tester.create_and_check_maskformer_model(__UpperCamelCase     ,       **__UpperCamelCase     ,       output_hidden_states=__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*__UpperCamelCase		)


					@unittest.skip(reason="MaskFormer does not use inputs_embeds"		)
					def 	lowerCamelCase       (					self			:Tuple		):
												pass


					@unittest.skip(reason="MaskFormer does not have a get_input_embeddings method"		)
					def 	lowerCamelCase       (					self			:Tuple		):
												pass


					@unittest.skip(reason="MaskFormer is not a generative model"		)
					def 	lowerCamelCase       (					self			:str		):
												pass


					@unittest.skip(reason="MaskFormer does not use token embeddings"		)
					def 	lowerCamelCase       (					self			: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			:Union[str, Any]		):
												pass


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


					def 	lowerCamelCase       (					self			:List[Any]		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			A		=  model_class(__UpperCamelCase		)
																			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]     ,       __UpperCamelCase		)


					@slow
					def 	lowerCamelCase       (					self			:List[Any]		):
												for model_name in ["facebook/maskformer-swin-small-coco"]:
																			A		=  MaskFormerModel.from_pretrained(__UpperCamelCase		)
																			self.assertIsNotNone(__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:List[str]		):
												A		=  (self.model_tester.min_size,) * 2
												A		=  {
												    "pixel_values": torch.randn((2, 3, *size)     ,       device=__UpperCamelCase		),
												    "mask_labels": torch.randn((2, 10, *size)     ,       device=__UpperCamelCase		),
												    "class_labels": torch.zeros(2     ,       10     ,       device=__UpperCamelCase		).long(),
												}

												A		=  MaskFormerForInstanceSegmentation(MaskFormerConfig()		).to(__UpperCamelCase		)
												A		=  model(**__UpperCamelCase		)
												self.assertTrue(outputs.loss is not None		)


					def 	lowerCamelCase       (					self			:Optional[int]		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()
												self.model_tester.create_and_check_maskformer_model(__UpperCamelCase     ,       **__UpperCamelCase     ,       output_hidden_states=__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Dict		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()

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


					def 	lowerCamelCase       (					self			:List[str]		):
												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(__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.train()

												A		=  model(__UpperCamelCase     ,       mask_labels=__UpperCamelCase     ,       class_labels=__UpperCamelCase		).loss
												loss.backward()


					def 	lowerCamelCase       (					self			:Tuple		):
												# 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(__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.train()

												A		=  model(__UpperCamelCase     ,       mask_labels=__UpperCamelCase     ,       class_labels=__UpperCamelCase		)

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

												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     :		Tuple       						= 1e-4






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






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


					@cached_property
					def 	lowerCamelCase       (					self			:Any		):
												return (
												    MaskFormerImageProcessor.from_pretrained("facebook/maskformer-swin-small-coco"		)
												    if is_vision_available()
												    else None
												)


					def 	lowerCamelCase       (					self			:List[str]		):
												A		=  MaskFormerModel.from_pretrained("facebook/maskformer-swin-small-coco"		).to(__UpperCamelCase		)
												A		=  self.default_image_processor
												A		=  prepare_img()
												A		=  image_processor(__UpperCamelCase     ,       return_tensors="pt"		).to(__UpperCamelCase		)
												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(__UpperCamelCase     ,       (1, 3, 8_00, 10_88)		)

												with torch.no_grad():
																			A		=  model(**__UpperCamelCase		)

												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(__UpperCamelCase		)
												self.assertTrue(
												    torch.allclose(
												        outputs.encoder_last_hidden_state[0, 0, :3, :3]     ,       __UpperCamelCase     ,       atol=__UpperCamelCase		)		)

												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(__UpperCamelCase		)
												self.assertTrue(
												    torch.allclose(
												        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]     ,       __UpperCamelCase     ,       atol=__UpperCamelCase		)		)

												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(__UpperCamelCase		)
												self.assertTrue(
												    torch.allclose(
												        outputs.transformer_decoder_last_hidden_state[0, :3, :3]     ,       __UpperCamelCase     ,       atol=__UpperCamelCase		)		)


					def 	lowerCamelCase       (					self			:str		):
												A		=  (
												    MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"		)
												    .to(__UpperCamelCase		)
												    .eval()
												)
												A		=  self.default_image_processor
												A		=  prepare_img()
												A		=  image_processor(__UpperCamelCase     ,       return_tensors="pt"		).to(__UpperCamelCase		)
												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(__UpperCamelCase     ,       (1, 3, 8_00, 10_88)		)

												with torch.no_grad():
																			A		=  model(**__UpperCamelCase		)
												# 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(__UpperCamelCase		).to(__UpperCamelCase		)
												self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]     ,       __UpperCamelCase     ,       atol=__UpperCamelCase		)		)
												# 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.6_512e00, -5.2_572e00, -3.3_519e00],
												        [3.6_169e-02, -5.9_025e00, -2.9_313e00],
												        [1.0_766e-04, -7.7_630e00, -5.1_263e00],
												    ]		).to(__UpperCamelCase		)
												self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]     ,       __UpperCamelCase     ,       atol=__UpperCamelCase		)		)


					def 	lowerCamelCase       (					self			:int		):
												A		=  (
												    MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-resnet101-coco-stuff"		)
												    .to(__UpperCamelCase		)
												    .eval()
												)
												A		=  self.default_image_processor
												A		=  prepare_img()
												A		=  image_processor(__UpperCamelCase     ,       return_tensors="pt"		).to(__UpperCamelCase		)
												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(__UpperCamelCase     ,       (1, 3, 8_00, 10_88)		)

												with torch.no_grad():
																			A		=  model(**__UpperCamelCase		)
												# 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(__UpperCamelCase		).to(__UpperCamelCase		)
												self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]     ,       __UpperCamelCase     ,       atol=__UpperCamelCase		)		)
												# 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(__UpperCamelCase		)
												self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]     ,       __UpperCamelCase     ,       atol=__UpperCamelCase		)		)


					def 	lowerCamelCase       (					self			:List[Any]		):
												A		=  (
												    MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"		)
												    .to(__UpperCamelCase		)
												    .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(__UpperCamelCase		)
												A		=  [el.to(__UpperCamelCase		) for el in inputs["mask_labels"]]
												A		=  [el.to(__UpperCamelCase		) for el in inputs["class_labels"]]

												with torch.no_grad():
																			A		=  model(**__UpperCamelCase		)

												self.assertTrue(outputs.loss is not None		)









"""simple docstring"""
from math import isqrt, loga






def 					A__				(			UpperCamelCase       ):
							A		=  [True] * max_number
							for i in range(2					,  isqrt(max_number - 1       ) + 1       ):
														if is_prime[i]:
																					for j in range(i**2					,  UpperCamelCase					,  UpperCamelCase       ):
																												A		=  False

							return [i for i in range(2					,  UpperCamelCase       ) if is_prime[i]]






def 					A__				(			UpperCamelCase = 800_800					,  UpperCamelCase = 800_800       ):
							A		=  degree * loga(UpperCamelCase       )
							A		=  int(UpperCamelCase       )
							A		=  calculate_prime_numbers(UpperCamelCase       )

							A		=  0
							A		=  0
							A		=  len(UpperCamelCase       ) - 1
							while left < right:
														while (
														    prime_numbers[right] * loga(prime_numbers[left]       )
														    + prime_numbers[left] * loga(prime_numbers[right]       )
														    > upper_bound
														):
																					right -= 1
														hybrid_integers_count += right - left
														left += 1

							return hybrid_integers_count


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


