


"""simple docstring"""


import inspect
import unittest

import numpy as np

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

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


if is_torch_available():
	import torch

	from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel

	if is_vision_available():
		from transformers import MaskaFormerImageProcessor

if is_vision_available():
	from PIL import Image
class 						_UpperCamelCase						:
							'''simple docstring'''







							def __init__(  self					,     __a					,     __a=2					,     __a=True					,     __a=False					,     __a=10					,     __a=3					,     __a=32 * 8					,     __a=32 * 8					,     __a=4					,     __a=64					,     ):
										__lowerCAmelCase       					=    parent
										__lowerCAmelCase       					=    batch_size
										__lowerCAmelCase       					=    is_training
										__lowerCAmelCase       					=    use_auxiliary_loss
										__lowerCAmelCase       					=    num_queries
										__lowerCAmelCase       					=    num_channels
										__lowerCAmelCase       					=    min_size
										__lowerCAmelCase       					=    max_size
										__lowerCAmelCase       					=    num_labels
										__lowerCAmelCase       					=    hidden_dim
										__lowerCAmelCase       					=    hidden_dim

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]			).to(
										    __a			)

										__lowerCAmelCase       					=    torch.ones([self.batch_size, self.min_size, self.max_size]					,     device=__a			)

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

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

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    MaskaFormerConfig(
										    hidden_size=self.hidden_dim					,     )
										__lowerCAmelCase       					=    self.num_queries
										__lowerCAmelCase       					=    self.num_labels

										__lowerCAmelCase       					=    [1, 1, 1, 1]
										__lowerCAmelCase       					=    self.num_channels

										__lowerCAmelCase       					=    64
										__lowerCAmelCase       					=    1_28
										__lowerCAmelCase       					=    self.hidden_dim
										__lowerCAmelCase       					=    self.hidden_dim
										__lowerCAmelCase       					=    self.hidden_dim
										return config

							def     snake_case       (  self			):
										__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase       					=    self.prepare_config_and_inputs()
										__lowerCAmelCase       					=    {"pixel_values": pixel_values, "pixel_mask": pixel_mask}
										return config, inputs_dict

							def     snake_case       (  self					,     __a					,     __a			):
										__lowerCAmelCase       					=    output.encoder_hidden_states
										__lowerCAmelCase       					=    output.pixel_decoder_hidden_states
										__lowerCAmelCase       					=    output.transformer_decoder_hidden_states

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

							def     snake_case       (  self					,     __a					,     __a					,     __a					,     __a=False			):
										with torch.no_grad():
													__lowerCAmelCase       					=    MaskaFormerModel(config=__a			)
													model.to(__a			)
													model.eval()

													__lowerCAmelCase       					=    model(pixel_values=__a					,     pixel_mask=__a			)
													__lowerCAmelCase       					=    model(__a					,     output_hidden_states=__a			)

										self.parent.assertEqual(
										    output.transformer_decoder_last_hidden_state.shape					,     (self.batch_size, self.num_queries, self.hidden_dim)					,     )
										# let's ensure the other two hidden state exists
										self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None			)
										self.parent.assertTrue(output.encoder_last_hidden_state is not None			)

										if output_hidden_states:
													self.check_output_hidden_state(__a					,     __a			)



							def     snake_case       (  self					,     __a					,     __a					,     __a					,     __a					,     __a			):
										__lowerCAmelCase       					=    MaskaFormerForUniversalSegmentation(config=__a			)
										model.to(__a			)
										model.eval()

										def comm_check_on_output(__a			):
													# 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():
													__lowerCAmelCase       					=    model(pixel_values=__a					,     pixel_mask=__a			)
													__lowerCAmelCase       					=    model(__a			)

													comm_check_on_output(__a			)

													__lowerCAmelCase       					=    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 						_UpperCamelCase						(     lowercase__ ,lowercase__ ,unittest.TestCase	):
							'''simple docstring'''







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

							__UpperCAmelCase		:					Optional[Any]          =False
							__UpperCAmelCase		:					Dict          =False
							__UpperCAmelCase		:					int          =False
							__UpperCAmelCase		:					Optional[int]          =False

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    MaskaFormerModelTester(self			)
										__lowerCAmelCase       					=    ConfigTester(self					,     config_class=__a					,     has_text_modality=__a			)

							def     snake_case       (  self			):
										self.config_tester.run_common_tests()

							def     snake_case       (  self			):
										__lowerCAmelCase					,			__lowerCAmelCase       					=    self.model_tester.prepare_config_and_inputs_for_common()
										self.model_tester.create_and_check_maskaformer_model(__a					,     **__a					,     output_hidden_states=__a			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*__a			)

							@unittest.skip(reason="Mask2Former does not use inputs_embeds"			)
							def     snake_case       (  self			):
										pass

							@unittest.skip(reason="Mask2Former does not have a get_input_embeddings method"			)
							def     snake_case       (  self			):
										pass

							@unittest.skip(reason="Mask2Former is not a generative model"			)
							def     snake_case       (  self			):
										pass

							@unittest.skip(reason="Mask2Former does not use token embeddings"			)
							def     snake_case       (  self			):
										pass

							@require_torch_multi_gpu
							@unittest.skip(
							    reason="Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`"			)
							def     snake_case       (  self			):
										pass

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

							def     snake_case       (  self			):
										__lowerCAmelCase					,			__lowerCAmelCase       					=    self.model_tester.prepare_config_and_inputs_for_common()

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

													__lowerCAmelCase       					=    ["pixel_values"]
													self.assertListEqual(arg_names[:1]					,     __a			)

							@slow
							def     snake_case       (  self			):
										for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
													__lowerCAmelCase       					=    MaskaFormerModel.from_pretrained(__a			)
													self.assertIsNotNone(__a			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    (self.model_tester.min_size,) * 2
										__lowerCAmelCase       					=    {
										    "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(),
										}
										__lowerCAmelCase       					=    self.model_tester.get_config()

										__lowerCAmelCase       					=    MaskaFormerForUniversalSegmentation(__a			).to(__a			)
										__lowerCAmelCase       					=    model(**__a			)
										self.assertTrue(outputs.loss is not None			)

							def     snake_case       (  self			):
										__lowerCAmelCase					,			__lowerCAmelCase       					=    self.model_tester.prepare_config_and_inputs_for_common()
										self.model_tester.create_and_check_maskaformer_model(__a					,     **__a					,     output_hidden_states=__a			)

							def     snake_case       (  self			):
										__lowerCAmelCase					,			__lowerCAmelCase       					=    self.model_tester.prepare_config_and_inputs_for_common()

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

							def     snake_case       (  self			):
										if not self.model_tester.is_training:
													return

										__lowerCAmelCase       					=    self.all_model_classes[1]
										__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase       					=    self.model_tester.prepare_config_and_inputs()

										__lowerCAmelCase       					=    model_class(__a			)
										model.to(__a			)
										model.train()

										__lowerCAmelCase       					=    model(__a					,     mask_labels=__a					,     class_labels=__a			).loss
										loss.backward()



							def     snake_case       (  self			):
										__lowerCAmelCase       					=    self.all_model_classes[1]
										__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase       					=    self.model_tester.prepare_config_and_inputs()
										__lowerCAmelCase       					=    True
										__lowerCAmelCase       					=    True

										__lowerCAmelCase       					=    model_class(__a			).to(__a			)
										model.train()

										__lowerCAmelCase       					=    model(__a					,     mask_labels=__a					,     class_labels=__a			)

										__lowerCAmelCase       					=    outputs.encoder_hidden_states[0]
										encoder_hidden_states.retain_grad()

										__lowerCAmelCase       					=    outputs.pixel_decoder_hidden_states[0]
										pixel_decoder_hidden_states.retain_grad()

										__lowerCAmelCase       					=    outputs.transformer_decoder_hidden_states[0]
										transformer_decoder_hidden_states.retain_grad()

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


def _lowerCamelCase			(	):

			'''simple docstring'''




			__lowerCAmelCase       					=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
			return image


@require_vision
@slow
class 						_UpperCamelCase						(     unittest.TestCase	):
							'''simple docstring'''







							@cached_property
							def     snake_case       (  self			):
										return "facebook/mask2former-swin-small-coco-instance"

							@cached_property
							def     snake_case       (  self			):
										return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints			) if is_vision_available() else None

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    MaskaFormerModel.from_pretrained(self.model_checkpoints			).to(__a			)
										__lowerCAmelCase       					=    self.default_image_processor
										__lowerCAmelCase       					=    prepare_img()
										__lowerCAmelCase       					=    image_processor(__a					,     return_tensors="pt"			).to(__a			)
										__lowerCAmelCase       					=    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, 3_84, 3_84)			)

										with torch.no_grad():
													__lowerCAmelCase       					=    model(**__a			)

										__lowerCAmelCase       					=    torch.tensor(
										    [[-0.2_7_9_0, -1.0_7_1_7, -1.1_6_6_8], [-0.5_1_2_8, -0.3_1_2_8, -0.4_9_8_7], [-0.5_8_3_2, 0.1_9_7_1, -0.0_1_9_7]]			).to(__a			)
										self.assertTrue(
										    torch.allclose(
										        outputs.encoder_last_hidden_state[0, 0, :3, :3]					,     __a					,     atol=__a			)			)

										__lowerCAmelCase       					=    torch.tensor(
										    [[0.8_9_7_3, 1.1_8_4_7, 1.1_7_7_6], [1.1_9_3_4, 1.5_0_4_0, 1.5_1_2_8], [1.1_1_5_3, 1.4_4_8_6, 1.4_9_5_1]]			).to(__a			)
										self.assertTrue(
										    torch.allclose(
										        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]					,     __a					,     atol=__a			)			)

										__lowerCAmelCase       					=    torch.tensor(
										    [[2.1_1_5_2, 1.7_0_0_0, -0.8_6_0_3], [1.5_8_0_8, 1.8_0_0_4, -0.9_3_5_3], [1.6_0_4_3, 1.7_4_9_5, -0.5_9_9_9]]			).to(__a			)
										self.assertTrue(
										    torch.allclose(
										        outputs.transformer_decoder_last_hidden_state[0, :3, :3]					,     __a					,     atol=__a			)			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints			).to(__a			).eval()
										__lowerCAmelCase       					=    self.default_image_processor
										__lowerCAmelCase       					=    prepare_img()
										__lowerCAmelCase       					=    image_processor(__a					,     return_tensors="pt"			).to(__a			)
										__lowerCAmelCase       					=    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, 3_84, 3_84)			)

										with torch.no_grad():
													__lowerCAmelCase       					=    model(**__a			)
										# masks_queries_logits
										__lowerCAmelCase       					=    outputs.masks_queries_logits
										self.assertEqual(
										    masks_queries_logits.shape					,     (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)			)
										__lowerCAmelCase       					=    [
										    [-8.7_8_3_9, -9.0_0_5_6, -8.8_1_2_1],
										    [-7.4_1_0_4, -7.0_3_1_3, -6.5_4_0_1],
										    [-6.6_1_0_5, -6.3_4_2_7, -6.4_6_7_5],
										]
										__lowerCAmelCase       					=    torch.tensor(__a			).to(__a			)
										self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]					,     __a					,     atol=__a			)			)
										# class_queries_logits
										__lowerCAmelCase       					=    outputs.class_queries_logits
										self.assertEqual(class_queries_logits.shape					,     (1, model.config.num_queries, model.config.num_labels + 1)			)
										__lowerCAmelCase       					=    torch.tensor(
										    [
										        [1.8_3_2_4, -8.0_8_3_5, -4.1_9_2_2],
										        [0.8_4_5_0, -9.0_0_5_0, -3.6_0_5_3],
										        [0.3_0_4_5, -7.7_2_9_3, -3.0_2_7_5],
										    ]			).to(__a			)
										self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]					,     __a					,     atol=__a			)			)



							def     snake_case       (  self			):
										__lowerCAmelCase       					=    MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints			).to(__a			).eval()
										__lowerCAmelCase       					=    self.default_image_processor

										__lowerCAmelCase       					=    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"					,     )

										__lowerCAmelCase       					=    inputs["pixel_values"].to(__a			)
										__lowerCAmelCase       					=    [el.to(__a			) for el in inputs["mask_labels"]]
										__lowerCAmelCase       					=    [el.to(__a			) for el in inputs["class_labels"]]

										with torch.no_grad():
													__lowerCAmelCase       					=    model(**__a			)

										self.assertTrue(outputs.loss is not None			)








"""simple docstring"""


from itertools import product


def _lowerCamelCase			(	_UpperCamelCase					,				_UpperCamelCase   ):

			'''simple docstring'''




			__lowerCAmelCase       					=    sides_number
			__lowerCAmelCase       					=    max_face_number * dice_number
			__lowerCAmelCase       					=    [0] * (max_total + 1)

			__lowerCAmelCase       					=    1
			__lowerCAmelCase       					=    range(_UpperCamelCase					,				max_face_number + 1   )
			for dice_numbers in product(_UpperCamelCase					,				repeat=_UpperCamelCase   ):
						__lowerCAmelCase       					=    sum(_UpperCamelCase   )
						totals_frequencies[total] += 1

			return totals_frequencies


def _lowerCamelCase			(	):

			'''simple docstring'''




			__lowerCAmelCase       					=    total_frequency_distribution(
			    sides_number=4					,				dice_number=9   )
			__lowerCAmelCase       					=    total_frequency_distribution(
			    sides_number=6					,				dice_number=6   )

			__lowerCAmelCase       					=    0
			__lowerCAmelCase       					=    9
			__lowerCAmelCase       					=    4 * 9
			__lowerCAmelCase       					=    6
			for peter_total in range(_UpperCamelCase					,				max_peter_total + 1   ):
						peter_wins_count += peter_totals_frequencies[peter_total] * sum(
						    colin_totals_frequencies[min_colin_total:peter_total]   )

			__lowerCAmelCase       					=    (4**9) * (6**6)
			__lowerCAmelCase       					=    peter_wins_count / total_games_number

			__lowerCAmelCase       					=    round(_UpperCamelCase					,				ndigits=7   )

			return rounded_peter_win_probability


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




