

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        a__       :







		"""simple docstring"""




		def __init__(			self    : int				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : List[Any]=2				,				UpperCAmelCase__    : Optional[int]=True				,				UpperCAmelCase__    : Optional[int]=False				,				UpperCAmelCase__    : List[Any]=1_0				,				UpperCAmelCase__    : Tuple=3				,				UpperCAmelCase__    : Optional[int]=3_2 * 8				,				UpperCAmelCase__    : str=3_2 * 8				,				UpperCAmelCase__    : Tuple=4				,				UpperCAmelCase__    : Any=6_4				,				)					->List[str]:
								"""simple docstring"""







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


		def  _lowercase      (			self    : Any		)					->Optional[int]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]		).to(
								    UpperCAmelCase__		)

								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   torch.ones([self.batch_size, self.min_size, self.max_size]				,				device=UpperCAmelCase__		)

								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   (
								    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]				,				device=UpperCAmelCase__		) > 0.5
								).float()
								SCREAMING_SNAKE_CASE :      Dict       =   (torch.rand((self.batch_size, self.num_labels)				,				device=UpperCAmelCase__		) > 0.5).long()

								SCREAMING_SNAKE_CASE :      int       =   self.get_config()
								return config, pixel_values, pixel_mask, mask_labels, class_labels


		def  _lowercase      (			self    : Dict		)					->Any:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Tuple       =   MaskaFormerConfig(
								    hidden_size=self.hidden_dim				,				)
								SCREAMING_SNAKE_CASE :      Optional[int]       =   self.num_queries
								SCREAMING_SNAKE_CASE :      int       =   self.num_labels

								SCREAMING_SNAKE_CASE :      Optional[Any]       =   [1, 1, 1, 1]
								SCREAMING_SNAKE_CASE :      Optional[int]       =   self.num_channels

								SCREAMING_SNAKE_CASE :      List[str]       =   6_4
								SCREAMING_SNAKE_CASE :      str       =   1_2_8
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   self.hidden_dim
								SCREAMING_SNAKE_CASE :      List[str]       =   self.hidden_dim
								SCREAMING_SNAKE_CASE :      int       =   self.hidden_dim
								return config


		def  _lowercase      (			self    : Optional[Any]		)					->Optional[int]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      int       =   self.prepare_config_and_inputs()
								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
								return config, inputs_dict


		def  _lowercase      (			self    : Any				,				UpperCAmelCase__    : List[Any]				,				UpperCAmelCase__    : Any		)					->Optional[Any]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   output.encoder_hidden_states
								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   output.pixel_decoder_hidden_states
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   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_layers		)


		def  _lowercase      (			self    : Optional[int]				,				UpperCAmelCase__    : List[str]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : List[Any]=False		)					->Union[str, Any]:
								"""simple docstring"""







								with torch.no_grad():
														SCREAMING_SNAKE_CASE :      Optional[int]       =   MaskaFormerModel(config=UpperCAmelCase__		)
														model.to(UpperCAmelCase__		)
														model.eval()

														SCREAMING_SNAKE_CASE :      Optional[Any]       =   model(pixel_values=UpperCAmelCase__				,				pixel_mask=UpperCAmelCase__		)
														SCREAMING_SNAKE_CASE :      int       =   model(UpperCAmelCase__				,				output_hidden_states=UpperCAmelCase__		)

								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(UpperCAmelCase__				,				UpperCAmelCase__		)




		def  _lowercase      (			self    : int				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Union[str, Any]		)					->Dict:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   MaskaFormerForUniversalSegmentation(config=UpperCAmelCase__		)
								model.to(UpperCAmelCase__		)
								model.eval()

								def comm_check_on_output(UpperCAmelCase__    : Tuple		):
														# 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():
														SCREAMING_SNAKE_CASE :      Optional[Any]       =   model(pixel_values=UpperCAmelCase__				,				pixel_mask=UpperCAmelCase__		)
														SCREAMING_SNAKE_CASE :      Union[str, Any]       =   model(UpperCAmelCase__		)

														comm_check_on_output(UpperCAmelCase__		)

														SCREAMING_SNAKE_CASE :      Any       =   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        a__       (   UpperCAmelCase	,			UpperCAmelCase	,			unittest.TestCase       ):







		"""simple docstring"""




		UpperCAmelCase__      : str			       =(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
		UpperCAmelCase__      : Dict			       ={"""feature-extraction""": MaskaFormerModel} if is_torch_available() else {}

		UpperCAmelCase__      : Optional[int]			       =False
		UpperCAmelCase__      : int			       =False
		UpperCAmelCase__      : List[Any]			       =False
		UpperCAmelCase__      : List[Any]			       =False


		def  _lowercase      (			self    : int		)					->List[str]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Any       =   MaskaFormerModelTester(self		)
								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   ConfigTester(self				,				config_class=UpperCAmelCase__				,				has_text_modality=UpperCAmelCase__		)


		def  _lowercase      (			self    : Any		)					->List[Any]:
								"""simple docstring"""







								self.config_tester.run_common_tests()


		def  _lowercase      (			self    : List[Any]		)					->List[str]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      List[Any]       =   self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.create_and_check_maskaformer_model(UpperCAmelCase__				,				**UpperCAmelCase__				,				output_hidden_states=UpperCAmelCase__		)


		def  _lowercase      (			self    : Tuple		)					->Optional[int]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      int       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*UpperCAmelCase__		)


		@unittest.skip(reason="""Mask2Former does not use inputs_embeds"""		)
		def  _lowercase      (			self    : Union[str, Any]		)					->List[str]:
								"""simple docstring"""







								pass


		@unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method"""		)
		def  _lowercase      (			self    : Union[str, Any]		)					->Union[str, Any]:
								"""simple docstring"""







								pass


		@unittest.skip(reason="""Mask2Former is not a generative model"""		)
		def  _lowercase      (			self    : List[Any]		)					->Any:
								"""simple docstring"""







								pass


		@unittest.skip(reason="""Mask2Former does not use token embeddings"""		)
		def  _lowercase      (			self    : int		)					->str:
								"""simple docstring"""







								pass


		@require_torch_multi_gpu
		@unittest.skip(
		    reason="""Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""		)
		def  _lowercase      (			self    : Tuple		)					->Union[str, Any]:
								"""simple docstring"""







								pass


		@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""		)
		def  _lowercase      (			self    : str		)					->Optional[int]:
								"""simple docstring"""







								pass


		def  _lowercase      (			self    : int		)					->Optional[int]:
								"""simple docstring"""







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

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

														SCREAMING_SNAKE_CASE :      int       =   ["""pixel_values"""]
														self.assertListEqual(arg_names[:1]				,				UpperCAmelCase__		)


		@slow
		def  _lowercase      (			self    : Union[str, Any]		)					->Dict:
								"""simple docstring"""







								for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
														SCREAMING_SNAKE_CASE :      Optional[Any]       =   MaskaFormerModel.from_pretrained(UpperCAmelCase__		)
														self.assertIsNotNone(UpperCAmelCase__		)


		def  _lowercase      (			self    : List[Any]		)					->Dict:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   (self.model_tester.min_size,) * 2
								SCREAMING_SNAKE_CASE :      List[Any]       =   {
								    """pixel_values""": torch.randn((2, 3, *size)				,				device=UpperCAmelCase__		),
								    """mask_labels""": torch.randn((2, 1_0, *size)				,				device=UpperCAmelCase__		),
								    """class_labels""": torch.zeros(2				,				1_0				,				device=UpperCAmelCase__		).long(),
								}
								SCREAMING_SNAKE_CASE :      List[Any]       =   self.model_tester.get_config()

								SCREAMING_SNAKE_CASE :      Any       =   MaskaFormerForUniversalSegmentation(UpperCAmelCase__		).to(UpperCAmelCase__		)
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   model(**UpperCAmelCase__		)
								self.assertTrue(outputs.loss is not None		)


		def  _lowercase      (			self    : Any		)					->Tuple:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Optional[int]       =   self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.create_and_check_maskaformer_model(UpperCAmelCase__				,				**UpperCAmelCase__				,				output_hidden_states=UpperCAmelCase__		)


		def  _lowercase      (			self    : Optional[int]		)					->List[str]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Any       =   self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
														SCREAMING_SNAKE_CASE :      str       =   model_class(UpperCAmelCase__		).to(UpperCAmelCase__		)
														SCREAMING_SNAKE_CASE :      Optional[int]       =   model(**UpperCAmelCase__				,				output_attentions=UpperCAmelCase__		)
														self.assertTrue(outputs.attentions is not None		)


		def  _lowercase      (			self    : Optional[Any]		)					->Dict:
								"""simple docstring"""







								if not self.model_tester.is_training:
														return

								SCREAMING_SNAKE_CASE :      List[str]       =   self.all_model_classes[1]
								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   self.model_tester.prepare_config_and_inputs()

								SCREAMING_SNAKE_CASE :      int       =   model_class(UpperCAmelCase__		)
								model.to(UpperCAmelCase__		)
								model.train()

								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   model(UpperCAmelCase__				,				mask_labels=UpperCAmelCase__				,				class_labels=UpperCAmelCase__		).loss
								loss.backward()




		def  _lowercase      (			self    : Dict		)					->List[Any]:
								"""simple docstring"""







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

								SCREAMING_SNAKE_CASE :      List[str]       =   model_class(UpperCAmelCase__		).to(UpperCAmelCase__		)
								model.train()

								SCREAMING_SNAKE_CASE :      List[str]       =   model(UpperCAmelCase__				,				mask_labels=UpperCAmelCase__				,				class_labels=UpperCAmelCase__		)

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

								SCREAMING_SNAKE_CASE :      str       =   outputs.pixel_decoder_hidden_states[0]
								pixel_decoder_hidden_states.retain_grad()

								SCREAMING_SNAKE_CASE :      List[Any]       =   outputs.transformer_decoder_hidden_states[0]
								transformer_decoder_hidden_states.retain_grad()

								SCREAMING_SNAKE_CASE :      Optional[Any]       =   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		)


UpperCAmelCase__ :     Optional[Any]				    =    1E-4






def 					__lowercase      (     )     ->						List[Any]:
						SCREAMING_SNAKE_CASE :      int       =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
						return image







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







		"""simple docstring"""




		@cached_property
		def  _lowercase      (			self    : List[str]		)					->str:
								"""simple docstring"""







								return "facebook/mask2former-swin-small-coco-instance"


		@cached_property
		def  _lowercase      (			self    : str		)					->str:
								"""simple docstring"""







								return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints		) if is_vision_available() else None


		def  _lowercase      (			self    : Dict		)					->Any:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      int       =   MaskaFormerModel.from_pretrained(self.model_checkpoints		).to(UpperCAmelCase__		)
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   self.default_image_processor
								SCREAMING_SNAKE_CASE :      Tuple       =   prepare_img()
								SCREAMING_SNAKE_CASE :      Any       =   image_processor(UpperCAmelCase__				,				return_tensors="""pt"""		).to(UpperCAmelCase__		)
								SCREAMING_SNAKE_CASE :      Optional[int]       =   inputs["""pixel_values"""].shape
								# check size is divisible by 32
								self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0		)
								# check size
								self.assertEqual(UpperCAmelCase__				,				(1, 3, 3_8_4, 3_8_4)		)

								with torch.no_grad():
														SCREAMING_SNAKE_CASE :      str       =   model(**UpperCAmelCase__		)

								SCREAMING_SNAKE_CASE :      Any       =   torch.tensor(
								    [[-0.27_90, -1.07_17, -1.16_68], [-0.51_28, -0.31_28, -0.49_87], [-0.58_32, 0.19_71, -0.01_97]]		).to(UpperCAmelCase__		)
								self.assertTrue(
								    torch.allclose(
								        outputs.encoder_last_hidden_state[0, 0, :3, :3]				,				UpperCAmelCase__				,				atol=UpperCAmelCase__		)		)

								SCREAMING_SNAKE_CASE :      List[str]       =   torch.tensor(
								    [[0.89_73, 1.18_47, 1.17_76], [1.19_34, 1.50_40, 1.51_28], [1.11_53, 1.44_86, 1.49_51]]		).to(UpperCAmelCase__		)
								self.assertTrue(
								    torch.allclose(
								        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]				,				UpperCAmelCase__				,				atol=UpperCAmelCase__		)		)

								SCREAMING_SNAKE_CASE :      str       =   torch.tensor(
								    [[2.11_52, 1.70_00, -0.86_03], [1.58_08, 1.80_04, -0.93_53], [1.60_43, 1.74_95, -0.59_99]]		).to(UpperCAmelCase__		)
								self.assertTrue(
								    torch.allclose(
								        outputs.transformer_decoder_last_hidden_state[0, :3, :3]				,				UpperCAmelCase__				,				atol=UpperCAmelCase__		)		)


		def  _lowercase      (			self    : Any		)					->int:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Optional[int]       =   MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints		).to(UpperCAmelCase__		).eval()
								SCREAMING_SNAKE_CASE :      Dict       =   self.default_image_processor
								SCREAMING_SNAKE_CASE :      Any       =   prepare_img()
								SCREAMING_SNAKE_CASE :      List[str]       =   image_processor(UpperCAmelCase__				,				return_tensors="""pt"""		).to(UpperCAmelCase__		)
								SCREAMING_SNAKE_CASE :      Dict       =   inputs["""pixel_values"""].shape
								# check size is divisible by 32
								self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0		)
								# check size
								self.assertEqual(UpperCAmelCase__				,				(1, 3, 3_8_4, 3_8_4)		)

								with torch.no_grad():
														SCREAMING_SNAKE_CASE :      Optional[Any]       =   model(**UpperCAmelCase__		)
								# masks_queries_logits
								SCREAMING_SNAKE_CASE :      List[Any]       =   outputs.masks_queries_logits
								self.assertEqual(
								    masks_queries_logits.shape				,				(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)		)
								SCREAMING_SNAKE_CASE :      int       =   [
								    [-8.78_39, -9.00_56, -8.81_21],
								    [-7.41_04, -7.03_13, -6.54_01],
								    [-6.61_05, -6.34_27, -6.46_75],
								]
								SCREAMING_SNAKE_CASE :      Dict       =   torch.tensor(UpperCAmelCase__		).to(UpperCAmelCase__		)
								self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]				,				UpperCAmelCase__				,				atol=UpperCAmelCase__		)		)
								# class_queries_logits
								SCREAMING_SNAKE_CASE :      Any       =   outputs.class_queries_logits
								self.assertEqual(class_queries_logits.shape				,				(1, model.config.num_queries, model.config.num_labels + 1)		)
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   torch.tensor(
								    [
								        [1.83_24, -8.08_35, -4.19_22],
								        [0.84_50, -9.00_50, -3.60_53],
								        [0.30_45, -7.72_93, -3.02_75],
								    ]		).to(UpperCAmelCase__		)
								self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]				,				UpperCAmelCase__				,				atol=UpperCAmelCase__		)		)




		def  _lowercase      (			self    : Tuple		)					->int:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Any       =   MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints		).to(UpperCAmelCase__		).eval()
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   self.default_image_processor

								SCREAMING_SNAKE_CASE :      int       =   image_processor(
								    [np.zeros((3, 8_0_0, 1_3_3_3)		), np.zeros((3, 8_0_0, 1_3_3_3)		)]				,				segmentation_maps=[np.zeros((3_8_4, 3_8_4)		).astype(np.floataa		), np.zeros((3_8_4, 3_8_4)		).astype(np.floataa		)]				,				return_tensors="""pt"""				,				)

								SCREAMING_SNAKE_CASE :      int       =   inputs["""pixel_values"""].to(UpperCAmelCase__		)
								SCREAMING_SNAKE_CASE :      Optional[int]       =   [el.to(UpperCAmelCase__		) for el in inputs["""mask_labels"""]]
								SCREAMING_SNAKE_CASE :      Tuple       =   [el.to(UpperCAmelCase__		) for el in inputs["""class_labels"""]]

								with torch.no_grad():
														SCREAMING_SNAKE_CASE :      Dict       =   model(**UpperCAmelCase__		)

								self.assertTrue(outputs.loss is not None		)







import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

UpperCAmelCase__ :     Optional[Any]				    =    3






def 					__lowercase      (     _A			)     ->						int:
      print("""Generating primitive root of p"""			)
      while True:
            SCREAMING_SNAKE_CASE :      int       =   random.randrange(3						,    _A			)
            if pow(_A						,    2						,    _A			) == 1:
                  continue
            if pow(_A						,    _A						,    _A			) == 1:
                  continue
            return g






def 					__lowercase      (     _A			)     ->						tuple[tuple[int, int, int, int], tuple[int, int]]:
      print("""Generating prime p..."""			)
      SCREAMING_SNAKE_CASE :      Optional[Any]       =   rabin_miller.generate_large_prime(_A			)  # select large prime number.
      SCREAMING_SNAKE_CASE :      Optional[Any]       =   primitive_root(_A			)  # one primitive root on modulo p.
      SCREAMING_SNAKE_CASE :      Optional[Any]       =   random.randrange(3						,    _A			)  # private_key -> have to be greater than 2 for safety.
      SCREAMING_SNAKE_CASE :      List[str]       =   cryptomath.find_mod_inverse(pow(_A						,    _A						,    _A			)						,    _A			)

      SCREAMING_SNAKE_CASE :      str       =   (key_size, e_a, e_a, p)
      SCREAMING_SNAKE_CASE :      Union[str, Any]       =   (key_size, d)

      return public_key, private_key






def 					__lowercase      (     _A						,    _A			)     ->						None:
      if os.path.exists(F"{name}_pubkey.txt"			) or os.path.exists(F"{name}_privkey.txt"			):
            print("""\nWARNING:"""			)
            print(
                F"\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n"
                """Use a different name or delete these files and re-run this program."""			)
            sys.exit()

      SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      List[str]       =   generate_key(_A			)
      print(F"\nWriting public key to file {name}_pubkey.txt..."			)
      with open(F"{name}_pubkey.txt"						,    """w"""			) as fo:
            fo.write(F"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}"			)

      print(F"Writing private key to file {name}_privkey.txt..."			)
      with open(F"{name}_privkey.txt"						,    """w"""			) as fo:
            fo.write(F"{private_key[0]},{private_key[1]}"			)






def 					__lowercase      (     )     ->						None:
      print("""Making key files..."""			)
      make_key_files("""elgamal"""						,    2048			)
      print("""Key files generation successful"""			)


if __name__ == "__main__":
  main()




