





'''simple docstring'''



import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
	import jax

	from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel






class 	__lowercase    ( unittest.TestCase  ):







	'''simple docstring'''

	def __init__(self       ,_lowerCamelCase       ,_lowerCamelCase=13       ,_lowerCamelCase=30       ,_lowerCamelCase=2       ,_lowerCamelCase=3       ,_lowerCamelCase=True       ,_lowerCamelCase=True       ,_lowerCamelCase=32       ,_lowerCamelCase=5       ,_lowerCamelCase=4       ,_lowerCamelCase=37       ,_lowerCamelCase="gelu"       ,_lowerCamelCase=0.1       ,_lowerCamelCase=0.1       ,_lowerCamelCase=10       ,_lowerCamelCase=0.0_2       ,)							->							Optional[int]:







				'''simple docstring'''





				__lowercase			  =							parent
				__lowercase			  =							batch_size
				__lowercase			  =							image_size
				__lowercase			  =							patch_size
				__lowercase			  =							num_channels
				__lowercase			  =							is_training
				__lowercase			  =							use_labels
				__lowercase			  =							hidden_size
				__lowercase			  =							num_hidden_layers
				__lowercase			  =							num_attention_heads
				__lowercase			  =							intermediate_size
				__lowercase			  =							hidden_act
				__lowercase			  =							hidden_dropout_prob
				__lowercase			  =							attention_probs_dropout_prob
				__lowercase			  =							type_sequence_label_size
				__lowercase			  =							initializer_range

				# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
				__lowercase			  =							(image_size // patch_size) ** 2
				__lowercase			  =							num_patches + 1


	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				__lowercase			  =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

				__lowercase			  =							ViTConfig(
				    image_size=self.image_size       ,patch_size=self.patch_size       ,num_channels=self.num_channels       ,hidden_size=self.hidden_size       ,num_hidden_layers=self.num_hidden_layers       ,num_attention_heads=self.num_attention_heads       ,intermediate_size=self.intermediate_size       ,hidden_act=self.hidden_act       ,hidden_dropout_prob=self.hidden_dropout_prob       ,attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,is_decoder=_A       ,initializer_range=self.initializer_range       ,)

				return config, pixel_values


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase	)							->							Any:







				'''simple docstring'''





				__lowercase			  =							FlaxViTModel(config=_A	)
				__lowercase			  =							model(_A	)
				# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
				__lowercase			  =							(self.image_size, self.image_size)
				__lowercase			  =							(self.patch_size, self.patch_size)
				__lowercase			  =							(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
				self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, num_patches + 1, self.hidden_size)	)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase	)							->							str:







				'''simple docstring'''





				__lowercase			  =							self.type_sequence_label_size
				__lowercase			  =							FlaxViTForImageClassification(config=_A	)
				__lowercase			  =							model(_A	)
				self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.type_sequence_label_size)	)

				# test greyscale images
				__lowercase			  =							1
				__lowercase			  =							FlaxViTForImageClassification(_A	)

				__lowercase			  =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
				__lowercase			  =							model(_A	)


	def   _UpperCAmelCase       (self	)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase			  =							self.prepare_config_and_inputs()
				(
				    __lowercase
				)			  =							config_and_inputs
				__lowercase			  =							{'pixel_values': pixel_values}
				return config, inputs_dict






@require_flax
class 	__lowercase    ( snake_case_	, unittest.TestCase  ):







	'''simple docstring'''

	a      :			int     =    (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()


	def   _UpperCAmelCase       (self	)							->							None:







				'''simple docstring'''





				__lowercase			  =							FlaxViTModelTester(self	)
				__lowercase			  =							ConfigTester(self       ,config_class=_A       ,has_text_modality=_A       ,hidden_size=37	)


	def   _UpperCAmelCase       (self	)							->							str:







				'''simple docstring'''





				self.config_tester.run_common_tests()


	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_A	)


	def   _UpperCAmelCase       (self	)							->							str:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_A	)


	def   _UpperCAmelCase       (self	)							->							List[str]:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs_for_common()

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

							__lowercase			  =							['pixel_values']
							self.assertListEqual(arg_names[:1]       ,_A	)


	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							with self.subTest(model_class.__name__	):
										__lowercase			  =							self._prepare_for_class(_A       ,_A	)
										__lowercase			  =							model_class(_A	)

										@jax.jit
										def model_jitted(_lowerCamelCase       ,**_lowerCamelCase	):
													return model(pixel_values=_A       ,**_A	)

										with self.subTest('''JIT Enabled'''	):
													__lowercase			  =							model_jitted(**_A	).to_tuple()

										with self.subTest('''JIT Disabled'''	):
													with jax.disable_jit():
																__lowercase			  =							model_jitted(**_A	).to_tuple()

										self.assertEqual(len(_A	)       ,len(_A	)	)
										for jitted_output, output in zip(_A       ,_A	):
													self.assertEqual(jitted_output.shape       ,output.shape	)


	@slow
	def   _UpperCAmelCase       (self	)							->							Optional[Any]:







				'''simple docstring'''





				for model_class_name in self.all_model_classes:
							__lowercase			  =							model_class_name.from_pretrained('''google/vit-base-patch16-224'''	)
							__lowercase			  =							model(np.ones((1, 3, 224, 224)	)	)
							self.assertIsNotNone(_A	)












'''simple docstring'''



import os
import random
import sys

from . import cryptomath_module as cryptoMath  # noqa: N812
from . import rabin_miller as rabinMiller  # noqa: N812




def 				_lowerCAmelCase  (			):
			print('''Making key files...'''			)
			make_key_files('''rsa'''						,    1_0_2_4			)
			print('''Key files generation successful.'''			)




def 				_lowerCAmelCase  (			lowerCamelCase_     :					int			):
			print('''Generating prime p...'''			)
			__lowercase			  =							rabinMiller.generate_large_prime(lowerCamelCase_			)
			print('''Generating prime q...'''			)
			__lowercase			  =							rabinMiller.generate_large_prime(lowerCamelCase_			)
			__lowercase			  =							p * q

			print('''Generating e that is relatively prime to (p - 1) * (q - 1)...'''			)
			while True:
						__lowercase			  =							random.randrange(2 ** (key_size - 1)						,    2 ** (key_size)			)
						if cryptoMath.gcd(lowerCamelCase_						,    (p - 1) * (q - 1)			) == 1:
									break

			print('''Calculating d that is mod inverse of e...'''			)
			__lowercase			  =							cryptoMath.find_mod_inverse(lowerCamelCase_						,    (p - 1) * (q - 1)			)

			__lowercase			  =							(n, e)
			__lowercase			  =							(n, d)
			return (public_key, private_key)




def 				_lowerCAmelCase  (			lowerCamelCase_     :					str						,    lowerCamelCase_     :					int			):
			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()

			__lowercase  ,						__lowercase			  =							generate_key(lowerCamelCase_			)
			print(f"\nWriting public key to file {name}_pubkey.txt..."			)
			with open(f"{name}_pubkey.txt"						,    '''w'''			) as out_file:
						out_file.write(f"{key_size},{public_key[0]},{public_key[1]}"			)

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


if __name__ == "__main__":
	main()





