
import inspect
import unittest

import numpy as np

from transformers import BeitConfig
from transformers.testing_utils import require_flax, require_vision, slow
from transformers.utils import cached_property, is_flax_available, is_vision_available

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


if is_flax_available():
			import jax

			from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel

if is_vision_available():
			from PIL import Image

			from transformers import BeitImageProcessor





class       _lowerCAmelCase   (      unittest.TestCase			):




				def __init__(   self							,							_UpperCamelCase							,							_UpperCamelCase=100							,							_UpperCamelCase=13							,							_UpperCamelCase=30							,							_UpperCamelCase=2							,							_UpperCamelCase=3							,							_UpperCamelCase=True							,							_UpperCamelCase=True							,							_UpperCamelCase=32							,							_UpperCamelCase=5							,							_UpperCamelCase=4							,							_UpperCamelCase=37							,							_UpperCamelCase="gelu"							,							_UpperCamelCase=0.1							,							_UpperCamelCase=0.1							,							_UpperCamelCase=10							,							_UpperCamelCase=0.02							,							_UpperCamelCase=3							,							)							-> List[str]:
							lowerCAmelCase_         =      parent
							lowerCAmelCase_         =      vocab_size
							lowerCAmelCase_         =      batch_size
							lowerCAmelCase_         =      image_size
							lowerCAmelCase_         =      patch_size
							lowerCAmelCase_         =      num_channels
							lowerCAmelCase_         =      is_training
							lowerCAmelCase_         =      use_labels
							lowerCAmelCase_         =      hidden_size
							lowerCAmelCase_         =      num_hidden_layers
							lowerCAmelCase_         =      num_attention_heads
							lowerCAmelCase_         =      intermediate_size
							lowerCAmelCase_         =      hidden_act
							lowerCAmelCase_         =      hidden_dropout_prob
							lowerCAmelCase_         =      attention_probs_dropout_prob
							lowerCAmelCase_         =      type_sequence_label_size
							lowerCAmelCase_         =      initializer_range

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




				def 	__a		(   self	)							-> int:
							lowerCAmelCase_         =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

							lowerCAmelCase_         =      None
							if self.use_labels:
										lowerCAmelCase_         =      ids_tensor([self.batch_size]							,							self.type_sequence_label_size	)

							lowerCAmelCase_         =      BeitConfig(
							    vocab_size=self.vocab_size							,							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=_UpperCamelCase							,							initializer_range=self.initializer_range							,							)

							return config, pixel_values, labels




				def 	__a		(   self							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase	)							-> Dict:
							lowerCAmelCase_         =      FlaxBeitModel(config=_UpperCamelCase	)
							lowerCAmelCase_         =      model(_UpperCamelCase	)
							self.parent.assertEqual(result.last_hidden_state.shape							,							(self.batch_size, self.seq_length, self.hidden_size)	)




				def 	__a		(   self							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase	)							-> int:
							lowerCAmelCase_         =      FlaxBeitForMaskedImageModeling(config=_UpperCamelCase	)
							lowerCAmelCase_         =      model(_UpperCamelCase	)
							self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length - 1, self.vocab_size)	)




				def 	__a		(   self							,							_UpperCamelCase							,							_UpperCamelCase							,							_UpperCamelCase	)							-> Optional[int]:
							lowerCAmelCase_         =      self.type_sequence_label_size
							lowerCAmelCase_         =      FlaxBeitForImageClassification(config=_UpperCamelCase	)
							lowerCAmelCase_         =      model(_UpperCamelCase	)
							self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.type_sequence_label_size)	)

							# test greyscale images
							lowerCAmelCase_         =      1
							lowerCAmelCase_         =      FlaxBeitForImageClassification(_UpperCamelCase	)

							lowerCAmelCase_         =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
							lowerCAmelCase_         =      model(_UpperCamelCase	)




				def 	__a		(   self	)							-> Any:
							lowerCAmelCase_         =      self.prepare_config_and_inputs()
							(
							    (
							    lowerCAmelCase_
							)      ,    (
							    lowerCAmelCase_
							)      ,    (
							    lowerCAmelCase_
							)      ,    
							)         =      config_and_inputs
							lowerCAmelCase_         =      {"pixel_values": pixel_values}
							return config, inputs_dict





@require_flax
class       _lowerCAmelCase   (      __a   , unittest.TestCase			):
				_lowercase     						=(
				    (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
				)




				def 	__a		(   self	)							-> None:
							lowerCAmelCase_         =      FlaxBeitModelTester(self	)
							lowerCAmelCase_         =      ConfigTester(self							,							config_class=_UpperCamelCase							,							has_text_modality=_UpperCamelCase							,							hidden_size=37	)




				def 	__a		(   self	)							-> Dict:
							self.config_tester.run_common_tests()




				def 	__a		(   self	)							-> str:
							lowerCAmelCase_      ,    lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs_for_common()

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

										lowerCAmelCase_         =      ["pixel_values"]
										self.assertListEqual(arg_names[:1]							,							_UpperCamelCase	)




				def 	__a		(   self	)							-> List[Any]:
							lowerCAmelCase_      ,    lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										with self.subTest(model_class.__name__	):
													lowerCAmelCase_         =      self._prepare_for_class(_UpperCamelCase							,							_UpperCamelCase	)
													lowerCAmelCase_         =      model_class(_UpperCamelCase	)

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

													with self.subTest("JIT Enabled"	):
																lowerCAmelCase_         =      model_jitted(**_UpperCamelCase	).to_tuple()

													with self.subTest("JIT Disabled"	):
																with jax.disable_jit():
																			lowerCAmelCase_         =      model_jitted(**_UpperCamelCase	).to_tuple()

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




				def 	__a		(   self	)							-> Optional[int]:
							lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCamelCase	)




				def 	__a		(   self	)							-> List[str]:
							lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*_UpperCamelCase	)




				def 	__a		(   self	)							-> Optional[Any]:
							lowerCAmelCase_         =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase	)




				@slow
				def 	__a		(   self	)							-> List[Any]:
							for model_class_name in self.all_model_classes:
										lowerCAmelCase_         =      model_class_name.from_pretrained("microsoft/beit-base-patch16-224"	)
										lowerCAmelCase_         =      model(np.ones((1, 3, 224, 224)	)	)
										self.assertIsNotNone(_UpperCamelCase	)






def   lowerCamelCase__							(							):




			"""simple docstring"""


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





@require_vision
@require_flax
class       _lowerCAmelCase   (      unittest.TestCase			):




				@cached_property
				def 	__a		(   self	)							-> Optional[Any]:
							return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224"	) if is_vision_available() else None




				@slow
				def 	__a		(   self	)							-> List[str]:
							lowerCAmelCase_         =      FlaxBeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k"	)

							lowerCAmelCase_         =      self.default_image_processor
							lowerCAmelCase_         =      prepare_img()
							lowerCAmelCase_         =      image_processor(images=_UpperCamelCase							,							return_tensors="np"	).pixel_values

							# prepare bool_masked_pos
							lowerCAmelCase_         =      np.ones((1, 196)							,							dtype=_UpperCamelCase	)

							# forward pass
							lowerCAmelCase_         =      model(pixel_values=_UpperCamelCase							,							bool_masked_pos=_UpperCamelCase	)
							lowerCAmelCase_         =      outputs.logits

							# verify the logits
							lowerCAmelCase_         =      (1, 196, 8_192)
							self.assertEqual(logits.shape							,							_UpperCamelCase	)

							lowerCAmelCase_         =      np.array(
							    [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]	)

							self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3]							,							_UpperCamelCase							,							atol=1e-2	)	)




				@slow
				def 	__a		(   self	)							-> Dict:
							lowerCAmelCase_         =      FlaxBeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224"	)

							lowerCAmelCase_         =      self.default_image_processor
							lowerCAmelCase_         =      prepare_img()
							lowerCAmelCase_         =      image_processor(images=_UpperCamelCase							,							return_tensors="np"	)

							# forward pass
							lowerCAmelCase_         =      model(**_UpperCamelCase	)
							lowerCAmelCase_         =      outputs.logits

							# verify the logits
							lowerCAmelCase_         =      (1, 1_000)
							self.assertEqual(logits.shape							,							_UpperCamelCase	)

							lowerCAmelCase_         =      np.array([-1.2385, -1.0987, -1.0108]	)

							self.assertTrue(np.allclose(logits[0, :3]							,							_UpperCamelCase							,							atol=1e-4	)	)

							lowerCAmelCase_         =      281
							self.assertEqual(logits.argmax(-1	).item()							,							_UpperCamelCase	)




				@slow
				def 	__a		(   self	)							-> Union[str, Any]:
							lowerCAmelCase_         =      FlaxBeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k"	)

							lowerCAmelCase_         =      self.default_image_processor
							lowerCAmelCase_         =      prepare_img()
							lowerCAmelCase_         =      image_processor(images=_UpperCamelCase							,							return_tensors="np"	)

							# forward pass
							lowerCAmelCase_         =      model(**_UpperCamelCase	)
							lowerCAmelCase_         =      outputs.logits

							# verify the logits
							lowerCAmelCase_         =      (1, 21_841)
							self.assertEqual(logits.shape							,							_UpperCamelCase	)

							lowerCAmelCase_         =      np.array([1.6881, -0.2787, 0.5901]	)

							self.assertTrue(np.allclose(logits[0, :3]							,							_UpperCamelCase							,							atol=1e-4	)	)

							lowerCAmelCase_         =      2_396
							self.assertEqual(logits.argmax(-1	).item()							,							_UpperCamelCase	)





from functools import reduce

_A  =	(
    "73167176531330624919225119674426574742355349194934"
    "96983520312774506326239578318016984801869478851843"
    "85861560789112949495459501737958331952853208805511"
    "12540698747158523863050715693290963295227443043557"
    "66896648950445244523161731856403098711121722383113"
    "62229893423380308135336276614282806444486645238749"
    "30358907296290491560440772390713810515859307960866"
    "70172427121883998797908792274921901699720888093776"
    "65727333001053367881220235421809751254540594752243"
    "52584907711670556013604839586446706324415722155397"
    "53697817977846174064955149290862569321978468622482"
    "83972241375657056057490261407972968652414535100474"
    "82166370484403199890008895243450658541227588666881"
    "16427171479924442928230863465674813919123162824586"
    "17866458359124566529476545682848912883142607690042"
    "24219022671055626321111109370544217506941658960408"
    "07198403850962455444362981230987879927244284909188"
    "84580156166097919133875499200524063689912560717606"
    "05886116467109405077541002256983155200055935729725"
    "71636269561882670428252483600823257530420752963450"
)






def   lowerCamelCase__							(							__lowerCAmelCase    :    str = N							):




			"""simple docstring"""


			return max(
			    # mypy cannot properly interpret reduce
			    int(reduce(lambda __lowerCAmelCase       ,				__lowerCAmelCase							: str(int(__lowerCAmelCase							) * int(__lowerCAmelCase							)							)       ,				n[i : i + 13]							)							)
			    for i in range(len(__lowerCAmelCase							) - 12							)							)


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



