
'''simple docstring'''



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   UpperCAmelCase_					(		unittest.TestCase			):






						'''simple docstring'''






						def __init__( self   ,       _lowercase   ,       _lowercase=100   ,       _lowercase=13   ,       _lowercase=30   ,       _lowercase=2   ,       _lowercase=3   ,       _lowercase=True   ,       _lowercase=True   ,       _lowercase=32   ,       _lowercase=5   ,       _lowercase=4   ,       _lowercase=37   ,       _lowercase="gelu"   ,       _lowercase=0.1   ,       _lowercase=0.1   ,       _lowercase=10   ,       _lowercase=0.02   ,       _lowercase=3   ,       ):

												"""simple docstring"""



												_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  _lowercase     ( self				):

												"""simple docstring"""



												_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=_lowercase   ,       initializer_range=self.initializer_range   ,       )

												return config, pixel_values, labels




						def  _lowercase     ( self   ,       _lowercase   ,       _lowercase   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			FlaxBeitModel(config=_lowercase				)
												_lowerCAmelCase  						=			model(_lowercase				)
												self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)				)




						def  _lowercase     ( self   ,       _lowercase   ,       _lowercase   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			FlaxBeitForMaskedImageModeling(config=_lowercase				)
												_lowerCAmelCase  						=			model(_lowercase				)
												self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length - 1, self.vocab_size)				)




						def  _lowercase     ( self   ,       _lowercase   ,       _lowercase   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			self.type_sequence_label_size
												_lowerCAmelCase  						=			FlaxBeitForImageClassification(config=_lowercase				)
												_lowerCAmelCase  						=			model(_lowercase				)
												self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.type_sequence_label_size)				)

												# test greyscale images
												_lowerCAmelCase  						=			1
												_lowerCAmelCase  						=			FlaxBeitForImageClassification(_lowercase				)

												_lowerCAmelCase  						=			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
												_lowerCAmelCase  						=			model(_lowercase				)




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			self.prepare_config_and_inputs()
												(
												    (
												    _lowerCAmelCase
												)						,   (
												    _lowerCAmelCase
												)						,   (
												    _lowerCAmelCase
												)						,   
												)  						=			config_and_inputs
												_lowerCAmelCase  						=			{"""pixel_values""": pixel_values}
												return config, inputs_dict






@require_flax
class   UpperCAmelCase_					(		_SCREAMING_SNAKE_CASE	,							unittest.TestCase			):






						'''simple docstring'''






						_lowercase		:				Tuple									=       (
						    (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
						)




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			FlaxBeitModelTester(self				)
												_lowerCAmelCase  						=			ConfigTester(self   ,       config_class=_lowercase   ,       has_text_modality=_lowercase   ,       hidden_size=37				)




						def  _lowercase     ( self				):

												"""simple docstring"""



												self.config_tester.run_common_tests()




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase						,   _lowerCAmelCase  						=			self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		_lowerCAmelCase  						=			model_class(_lowercase				)
																		_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]   ,       _lowercase				)




						def  _lowercase     ( self				):

												"""simple docstring"""



												_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(_lowercase   ,       _lowercase				)
																								_lowerCAmelCase  						=			model_class(_lowercase				)

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

																								with self.subTest("""JIT Enabled"""				):
																														_lowerCAmelCase  						=			model_jitted(**_lowercase				).to_tuple()

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

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




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_lowercase				)




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*_lowercase				)




						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*_lowercase				)




						@slow
						def  _lowercase     ( self				):

												"""simple docstring"""



												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(_lowercase				)






def A				():
						_lowerCAmelCase  						=			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
						return image





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






						'''simple docstring'''






						@cached_property
						def  _lowercase     ( self				):

												"""simple docstring"""



												return BeitImageProcessor.from_pretrained("""microsoft/beit-base-patch16-224"""				) if is_vision_available() else None




						@slow
						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			FlaxBeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k"""				)

												_lowerCAmelCase  						=			self.default_image_processor
												_lowerCAmelCase  						=			prepare_img()
												_lowerCAmelCase  						=			image_processor(images=_lowercase   ,       return_tensors="""np"""				).pixel_values

												# prepare bool_masked_pos
												_lowerCAmelCase  						=			np.ones((1, 196)   ,       dtype=_lowercase				)

												# forward pass
												_lowerCAmelCase  						=			model(pixel_values=_lowercase   ,       bool_masked_pos=_lowercase				)
												_lowerCAmelCase  						=			outputs.logits

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

												_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]   ,       _lowercase   ,       atol=1e-2				)				)




						@slow
						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			FlaxBeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224"""				)

												_lowerCAmelCase  						=			self.default_image_processor
												_lowerCAmelCase  						=			prepare_img()
												_lowerCAmelCase  						=			image_processor(images=_lowercase   ,       return_tensors="""np"""				)

												# forward pass
												_lowerCAmelCase  						=			model(**_lowercase				)
												_lowerCAmelCase  						=			outputs.logits

												# verify the logits
												_lowerCAmelCase  						=			(1, 1_000)
												self.assertEqual(logits.shape   ,       _lowercase				)

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

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

												_lowerCAmelCase  						=			281
												self.assertEqual(logits.argmax(-1				).item()   ,       _lowercase				)




						@slow
						def  _lowercase     ( self				):

												"""simple docstring"""



												_lowerCAmelCase  						=			FlaxBeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k"""				)

												_lowerCAmelCase  						=			self.default_image_processor
												_lowerCAmelCase  						=			prepare_img()
												_lowerCAmelCase  						=			image_processor(images=_lowercase   ,       return_tensors="""np"""				)

												# forward pass
												_lowerCAmelCase  						=			model(**_lowercase				)
												_lowerCAmelCase  						=			outputs.logits

												# verify the logits
												_lowerCAmelCase  						=			(1, 21_841)
												self.assertEqual(logits.shape   ,       _lowercase				)

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

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

												_lowerCAmelCase  						=			2_396
												self.assertEqual(logits.argmax(-1				).item()   ,       _lowercase				)


'''simple docstring'''



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowercase    =		logging.get_logger(__name__)

_lowercase    =		{
    """YituTech/conv-bert-base""": """https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json""",
    """YituTech/conv-bert-medium-small""": (
        """https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json"""
    ),
    """YituTech/conv-bert-small""": """https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json""",
    # See all ConvBERT models at https://huggingface.co/models?filter=convbert
}




class   UpperCAmelCase_					(		_SCREAMING_SNAKE_CASE			):






						'''simple docstring'''






						_lowercase		:				List[Any]									=       '''convbert'''




						def __init__( self   ,       _lowercase=30_522   ,       _lowercase=768   ,       _lowercase=12   ,       _lowercase=12   ,       _lowercase=3_072   ,       _lowercase="gelu"   ,       _lowercase=0.1   ,       _lowercase=0.1   ,       _lowercase=512   ,       _lowercase=2   ,       _lowercase=0.02   ,       _lowercase=1e-12   ,       _lowercase=1   ,       _lowercase=0   ,       _lowercase=2   ,       _lowercase=768   ,       _lowercase=2   ,       _lowercase=9   ,       _lowercase=1   ,       _lowercase=None   ,       **_lowercase   ,       ):

												"""simple docstring"""



												super().__init__(
												    pad_token_id=_lowercase   ,       bos_token_id=_lowercase   ,       eos_token_id=_lowercase   ,       **_lowercase   ,       )

												_lowerCAmelCase  						=			vocab_size
												_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  						=			max_position_embeddings
												_lowerCAmelCase  						=			type_vocab_size
												_lowerCAmelCase  						=			initializer_range
												_lowerCAmelCase  						=			layer_norm_eps
												_lowerCAmelCase  						=			embedding_size
												_lowerCAmelCase  						=			head_ratio
												_lowerCAmelCase  						=			conv_kernel_size
												_lowerCAmelCase  						=			num_groups
												_lowerCAmelCase  						=			classifier_dropout






class   UpperCAmelCase_					(		_SCREAMING_SNAKE_CASE			):






						'''simple docstring'''






						@property
						def  _lowercase     ( self				):

												"""simple docstring"""



												if self.task == "multiple-choice":
																		_lowerCAmelCase  						=			{0: """batch""", 1: """choice""", 2: """sequence"""}
												else:
																		_lowerCAmelCase  						=			{0: """batch""", 1: """sequence"""}
												return OrderedDict(
												    [
												        ("""input_ids""", dynamic_axis),
												        ("""attention_mask""", dynamic_axis),
												        ("""token_type_ids""", dynamic_axis),
												    ]				)
