






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


							def __init__(			self      :						Union[str, Any]			,    snake_case      :						List[Any]			,    snake_case      :						str=2			,    snake_case      :						List[str]=True			,    snake_case      :						Any=False			,    snake_case      :						Dict=10			,    snake_case      :						Optional[Any]=3			,    snake_case      :						Optional[Any]=32 * 4			,    snake_case      :						int=32 * 6			,    snake_case      :						Optional[int]=4			,    snake_case      :						Tuple=32			,    ):
													'''simple docstring'''





													A__						:  Union[str, Any]		  =  parent
													A__						:  Any		  =  batch_size
													A__						:  Optional[int]		  =  is_training
													A__						:  List[str]		  =  use_auxiliary_loss
													A__						:  Tuple		  =  num_queries
													A__						:  int		  =  num_channels
													A__						:  Optional[int]		  =  min_size
													A__						:  List[str]		  =  max_size
													A__						:  Optional[int]		  =  num_labels
													A__						:  Dict		  =  mask_feature_size


							def 						_UpperCamelCase       (			self      :						str							):
													'''simple docstring'''





													A__						:  Tuple		  =  floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]							).to(
													    __a							)

													A__						:  str		  =  torch.ones([self.batch_size, self.min_size, self.max_size]			,    device=__a							)

													A__						:  List[Any]		  =  (
													    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]			,    device=__a							) > 0.5
													).float()
													A__						:  List[str]		  =  (torch.rand((self.batch_size, self.num_labels)			,    device=__a							) > 0.5).long()

													A__						:  str		  =  self.get_config()
													return config, pixel_values, pixel_mask, mask_labels, class_labels


							def 						_UpperCamelCase       (			self      :						int							):
													'''simple docstring'''





													return MaskFormerConfig.from_backbone_and_decoder_configs(
													    backbone_config=SwinConfig(
													        depths=[1, 1, 1, 1]			,    )			,    decoder_config=DetrConfig(
													        decoder_ffn_dim=128			,    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 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													A__  ,  A__  ,  A__  ,  A__  ,  A__						:  Any		  =  self.prepare_config_and_inputs()
													A__						:  int		  =  {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
													return config, inputs_dict


							def 						_UpperCamelCase       (			self      :						List[Any]			,    snake_case      :						List[Any]			,    snake_case      :						List[str]							):
													'''simple docstring'''





													A__						:  List[Any]		  =  output.encoder_hidden_states
													A__						:  List[Any]		  =  output.pixel_decoder_hidden_states
													A__						:  Optional[int]		  =  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 						_UpperCamelCase       (			self      :						Union[str, Any]			,    snake_case      :						Optional[int]			,    snake_case      :						Optional[int]			,    snake_case      :						Union[str, Any]			,    snake_case      :						List[Any]=False							):
													'''simple docstring'''





													with torch.no_grad():
																			A__						:  Optional[Any]		  =  MaskFormerModel(config=__a							)
																			model.to(__a							)
																			model.eval()

																			A__						:  List[Any]		  =  model(pixel_values=__a			,    pixel_mask=__a							)
																			A__						:  Any		  =  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 						_UpperCamelCase       (			self      :						Optional[int]			,    snake_case      :						List[Any]			,    snake_case      :						Union[str, Any]			,    snake_case      :						Any			,    snake_case      :						Any			,    snake_case      :						int							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  MaskFormerForInstanceSegmentation(config=__a							)
													model.to(__a							)
													model.eval()

													def comm_check_on_output(snake_case      :						Dict							):
																			# 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__						:  List[Any]		  =  model(pixel_values=__a			,    pixel_mask=__a							)
																			A__						:  List[str]		  =  model(__a							)

																			comm_check_on_output(__a							)

																			A__						:  List[Any]		  =  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						(				UpperCamelCase       ,		UpperCamelCase       ,		unittest.TestCase		):
							snake_case_     =							(MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
							snake_case_     =							(
							    {'feature-extraction': MaskFormerModel, 'image-segmentation': MaskFormerForInstanceSegmentation}
							    if is_torch_available()
							    else {}
							)

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


							def 						_UpperCamelCase       (			self      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  Dict		  =  MaskFormerModelTester(self							)
													A__						:  Dict		  =  ConfigTester(self			,    config_class=__a			,    has_text_modality=__a							)


							def 						_UpperCamelCase       (			self      :						Dict							):
													'''simple docstring'''





													self.config_tester.run_common_tests()


							def 						_UpperCamelCase       (			self      :						int							):
													'''simple docstring'''





													A__  ,  A__						:  str		  =  self.model_tester.prepare_config_and_inputs_for_common()
													self.model_tester.create_and_check_maskformer_model(__a			,    **__a			,    output_hidden_states=__a							)


							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													A__						:  str		  =  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 						_UpperCamelCase       (			self      :						str							):
													'''simple docstring'''





													pass


							@unittest.skip(reason="""MaskFormer does not have a get_input_embeddings method"""							)
							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													pass


							@unittest.skip(reason="""MaskFormer is not a generative model"""							)
							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													pass


							@unittest.skip(reason="""MaskFormer does not use token embeddings"""							)
							def 						_UpperCamelCase       (			self      :						List[Any]							):
													'''simple docstring'''





													pass


							@require_torch_multi_gpu
							@unittest.skip(
							    reason="""MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`"""							)
							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													pass


							@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""							)
							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													pass


							def 						_UpperCamelCase       (			self      :						Dict							):
													'''simple docstring'''





													A__  ,  A__						:  List[str]		  =  self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			A__						:  Tuple		  =  model_class(__a							)
																			A__						:  Optional[Any]		  =  inspect.signature(model.forward							)
																			# signature.parameters is an OrderedDict => so arg_names order is deterministic
																			A__						:  Optional[int]		  =  [*signature.parameters.keys()]

																			A__						:  List[Any]		  =  ["""pixel_values"""]
																			self.assertListEqual(arg_names[:1]			,    __a							)


							@slow
							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													for model_name in ["facebook/maskformer-swin-small-coco"]:
																			A__						:  Tuple		  =  MaskFormerModel.from_pretrained(__a							)
																			self.assertIsNotNone(__a							)


							def 						_UpperCamelCase       (			self      :						Any							):
													'''simple docstring'''





													A__						:  List[str]		  =  (self.model_tester.min_size,) * 2
													A__						:  Tuple		  =  {
													    """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__						:  Optional[int]		  =  MaskFormerForInstanceSegmentation(MaskFormerConfig()							).to(__a							)
													A__						:  str		  =  model(**__a							)
													self.assertTrue(outputs.loss is not None							)


							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													A__  ,  A__						:  Union[str, Any]		  =  self.model_tester.prepare_config_and_inputs_for_common()
													self.model_tester.create_and_check_maskformer_model(__a			,    **__a			,    output_hidden_states=__a							)


							def 						_UpperCamelCase       (			self      :						List[Any]							):
													'''simple docstring'''





													A__  ,  A__						:  List[Any]		  =  self.model_tester.prepare_config_and_inputs_for_common()

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


							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													if not self.model_tester.is_training:
																			return
													# only MaskFormerForInstanceSegmentation has the loss
													A__						:  int		  =  self.all_model_classes[1]
													A__  ,  A__  ,  A__  ,  A__  ,  A__						:  Union[str, Any]		  =  self.model_tester.prepare_config_and_inputs()

													A__						:  Optional[Any]		  =  model_class(__a							)
													model.to(__a							)
													model.train()

													A__						:  Tuple		  =  model(__a			,    mask_labels=__a			,    class_labels=__a							).loss
													loss.backward()


							def 						_UpperCamelCase       (			self      :						Dict							):
													'''simple docstring'''





													A__						:  int		  =  self.all_model_classes[1]
													A__  ,  A__  ,  A__  ,  A__  ,  A__						:  List[str]		  =  self.model_tester.prepare_config_and_inputs()
													A__						:  Optional[Any]		  =  True
													A__						:  Union[str, Any]		  =  True

													A__						:  List[Any]		  =  model_class(__a							)
													model.to(__a							)
													model.train()

													A__						:  List[Any]		  =  model(__a			,    mask_labels=__a			,    class_labels=__a							)

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

													A__						:  Union[str, Any]		  =  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__						:  List[str]		  =  outputs.transformer_decoder_hidden_states[0]
													transformer_decoder_hidden_states.retain_grad()

													A__						:  List[Any]		  =  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      _lowerCAmelCase		(							)		->Optional[int]:
						A__						:  Dict		  =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
						return image






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


							@cached_property
							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													return (
													    MaskFormerImageProcessor.from_pretrained("""facebook/maskformer-swin-small-coco"""							)
													    if is_vision_available()
													    else None
													)


							def 						_UpperCamelCase       (			self      :						Any							):
													'''simple docstring'''





													A__						:  List[str]		  =  MaskFormerModel.from_pretrained("""facebook/maskformer-swin-small-coco"""							).to(__a							)
													A__						:  Any		  =  self.default_image_processor
													A__						:  int		  =  prepare_img()
													A__						:  List[Any]		  =  image_processor(__a			,    return_tensors="""pt"""							).to(__a							)
													A__						:  Union[str, Any]		  =  inputs["""pixel_values"""].shape
													# check size is divisible by 32
													self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0							)
													# check size
													self.assertEqual(__a			,    (1, 3, 800, 1088)							)

													with torch.no_grad():
																			A__						:  Optional[Any]		  =  model(**__a							)

													A__						:  str		  =  torch.tensor(
													    [[-0.0482, 0.9228, 0.4951], [-0.2547, 0.8017, 0.8527], [-0.0069, 0.3385, -0.0089]]							).to(__a							)
													self.assertTrue(
													    torch.allclose(
													        outputs.encoder_last_hidden_state[0, 0, :3, :3]			,    __a			,    atol=__a							)							)

													A__						:  int		  =  torch.tensor(
													    [[-0.8422, -0.8434, -0.9718], [-1.0144, -0.5565, -0.4195], [-1.0038, -0.4484, -0.1961]]							).to(__a							)
													self.assertTrue(
													    torch.allclose(
													        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]			,    __a			,    atol=__a							)							)

													A__						:  List[Any]		  =  torch.tensor(
													    [[0.2852, -0.0159, 0.9735], [0.6254, 0.1858, 0.8529], [-0.0680, -0.4116, 1.8413]]							).to(__a							)
													self.assertTrue(
													    torch.allclose(
													        outputs.transformer_decoder_last_hidden_state[0, :3, :3]			,    __a			,    atol=__a							)							)


							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  (
													    MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""							)
													    .to(__a							)
													    .eval()
													)
													A__						:  List[Any]		  =  self.default_image_processor
													A__						:  Tuple		  =  prepare_img()
													A__						:  Tuple		  =  image_processor(__a			,    return_tensors="""pt"""							).to(__a							)
													A__						:  Tuple		  =  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, 800, 1088)							)

													with torch.no_grad():
																			A__						:  Optional[Any]		  =  model(**__a							)
													# masks_queries_logits
													A__						:  Tuple		  =  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__						:  List[Any]		  =  [
													    [-1.3737124, -1.7724937, -1.9364233],
													    [-1.5977281, -1.9867939, -2.1523695],
													    [-1.5795398, -1.9269832, -2.093942],
													]
													A__						:  Tuple		  =  torch.tensor(__a							).to(__a							)
													self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]			,    __a			,    atol=__a							)							)
													# class_queries_logits
													A__						:  List[str]		  =  outputs.class_queries_logits
													self.assertEqual(
													    class_queries_logits.shape			,    (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)							)
													A__						:  int		  =  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(__a							)
													self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]			,    __a			,    atol=__a							)							)


							def 						_UpperCamelCase       (			self      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  Union[str, Any]		  =  (
													    MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-resnet101-coco-stuff"""							)
													    .to(__a							)
													    .eval()
													)
													A__						:  Any		  =  self.default_image_processor
													A__						:  Any		  =  prepare_img()
													A__						:  str		  =  image_processor(__a			,    return_tensors="""pt"""							).to(__a							)
													A__						:  Dict		  =  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, 800, 1088)							)

													with torch.no_grad():
																			A__						:  List[Any]		  =  model(**__a							)
													# masks_queries_logits
													A__						:  Union[str, Any]		  =  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__						:  List[str]		  =  [[-0.9046, -2.6366, -4.6062], [-3.4179, -5.7890, -8.8057], [-4.9179, -7.6560, -10.7711]]
													A__						:  Tuple		  =  torch.tensor(__a							).to(__a							)
													self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]			,    __a			,    atol=__a							)							)
													# class_queries_logits
													A__						:  Any		  =  outputs.class_queries_logits
													self.assertEqual(
													    class_queries_logits.shape			,    (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)							)
													A__						:  Any		  =  torch.tensor(
													    [[4.7188, -3.2585, -2.8857], [6.6871, -2.9181, -1.2487], [7.2449, -2.2764, -2.1874]]							).to(__a							)
													self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]			,    __a			,    atol=__a							)							)


							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													A__						:  List[str]		  =  (
													    MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""							)
													    .to(__a							)
													    .eval()
													)
													A__						:  int		  =  self.default_image_processor

													A__						:  Optional[int]		  =  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__						:  Union[str, Any]		  =  inputs["""pixel_values"""].to(__a							)
													A__						:  Dict		  =  [el.to(__a							) for el in inputs["""mask_labels"""]]
													A__						:  str		  =  [el.to(__a							) for el in inputs["""class_labels"""]]

													with torch.no_grad():
																			A__						:  Optional[Any]		  =  model(**__a							)

													self.assertTrue(outputs.loss is not None							)








"""simple docstring"""



from timeit import timeit




def      _lowerCAmelCase		(							UpperCAmelCase__       :					int      )		->int:
						if number < 0:
												raise ValueError("""the value of input must not be negative"""      )
						A__						:  Optional[int]		  =  0
						while number:
												number &= number - 1
												result += 1
						return result




def      _lowerCAmelCase		(							UpperCAmelCase__       :					int      )		->int:
						if number < 0:
												raise ValueError("""the value of input must not be negative"""      )
						A__						:  Tuple		  =  0
						while number:
												if number % 2 == 1:
																		result += 1
												number >>= 1
						return result




def      _lowerCAmelCase		(							)		->None:

						def do_benchmark(UpperCAmelCase__       :					int      ) -> None:
												A__						:  Optional[int]		  =  """import __main__ as z"""
												print(f'Benchmark when {number = }:'      )
												print(f'{get_set_bits_count_using_modulo_operator(UpperCAmelCase__      ) = }'      )
												A__						:  Any		  =  timeit("""z.get_set_bits_count_using_modulo_operator(25)""",						setup=UpperCAmelCase__      )
												print(f'timeit() runs in {timing} seconds'      )
												print(f'{get_set_bits_count_using_brian_kernighans_algorithm(UpperCAmelCase__      ) = }'      )
												A__						:  List[str]		  =  timeit(
												    """z.get_set_bits_count_using_brian_kernighans_algorithm(25)""",						setup=UpperCAmelCase__,						)
												print(f'timeit() runs in {timing} seconds'      )

						for number in (2_5, 3_7, 5_8, 0):
												do_benchmark(UpperCAmelCase__      )
												print()


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						benchmark()






