




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






						def __init__(   self		,			_A		,			_A=1_0_0		,			_A=1_3		,			_A=3_0		,			_A=2		,			_A=3		,			_A=True		,			_A=True		,			_A=3_2		,			_A=5		,			_A=4		,			_A=3_7		,			_A="gelu"		,			_A=0.1		,			_A=0.1		,			_A=1_0		,			_A=0.02		,			_A=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 				__SCREAMING_SNAKE_CASE(   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=UpperCamelCase_		,			initializer_range=self.initializer_range		,			)

											return config, pixel_values, labels






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



											__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 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



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






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



											__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 				__SCREAMING_SNAKE_CASE(   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 			a__    (					UpperCamelCase__ ,				unittest.TestCase      ):
						_a							:    Any					=    (
						    (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
						)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       FlaxBeitModelTester(self			)
											__lowerCAmelCase				   =       ConfigTester(self		,			config_class=UpperCamelCase_		,			has_text_modality=UpperCamelCase_		,			hidden_size=3_7			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											self.config_tester.run_common_tests()






						def 				__SCREAMING_SNAKE_CASE(   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(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 				__SCREAMING_SNAKE_CASE(   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(UpperCamelCase_		,			UpperCamelCase_			)
																					__lowerCAmelCase				   =       model_class(UpperCamelCase_			)

																					@jax.jit
																					def model_jitted(_A		,			**_A			):
																										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 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCamelCase_			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase_			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_			)






						@slow
						def 				__SCREAMING_SNAKE_CASE(   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, 2_2_4, 2_2_4)			)			)
																self.assertIsNotNone(UpperCamelCase_			)







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


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






						@cached_property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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






						@slow
						def 				__SCREAMING_SNAKE_CASE(   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=UpperCamelCase_		,			return_tensors="np"			).pixel_values

											# prepare bool_masked_pos
											__lowerCAmelCase				   =       np.ones((1, 1_9_6)		,			dtype=UpperCamelCase_			)

											# forward pass
											__lowerCAmelCase				   =       model(pixel_values=UpperCamelCase_		,			bool_masked_pos=UpperCamelCase_			)
											__lowerCAmelCase				   =       outputs.logits

											# verify the logits
											__lowerCAmelCase				   =       (1, 1_9_6, 8_1_9_2)
											self.assertEqual(logits.shape		,			UpperCamelCase_			)

											__lowerCAmelCase				   =       np.array(
											    [[-3.24_37, 0.50_72, -13.91_74], [-3.24_56, 0.49_48, -13.94_01], [-3.20_33, 0.51_21, -13.85_50]]			)

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






						@slow
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__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_0_0_0)
											self.assertEqual(logits.shape		,			UpperCamelCase_			)

											__lowerCAmelCase				   =       np.array([-1.23_85, -1.09_87, -1.01_08]			)

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

											__lowerCAmelCase				   =       2_8_1
											self.assertEqual(logits.argmax(-1			).item()		,			UpperCamelCase_			)






						@slow
						def 				__SCREAMING_SNAKE_CASE(   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=UpperCamelCase_		,			return_tensors="np"			)

											# forward pass
											__lowerCAmelCase				   =       model(**UpperCamelCase_			)
											__lowerCAmelCase				   =       outputs.logits

											# verify the logits
											__lowerCAmelCase				   =       (1, 2_1_8_4_1)
											self.assertEqual(logits.shape		,			UpperCamelCase_			)

											__lowerCAmelCase				   =       np.array([1.68_81, -0.27_87, 0.59_01]			)

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

											__lowerCAmelCase				   =       2_3_9_6
											self.assertEqual(logits.argmax(-1			).item()		,			UpperCamelCase_			)








import inspect
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
lowerCAmelCase       =							'src/transformers'


# This is to make sure the transformers module imported is the one in the repo.
lowerCAmelCase       =							direct_transformers_import(PATH_TO_TRANSFORMERS)

lowerCAmelCase       =							transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
lowerCAmelCase       =							re.compile(R'\[(.+?)\]\((https://huggingface\.co/.+?)\)')


lowerCAmelCase       =							{
    'DecisionTransformerConfig',
    'EncoderDecoderConfig',
    'MusicgenConfig',
    'RagConfig',
    'SpeechEncoderDecoderConfig',
    'TimmBackboneConfig',
    'VisionEncoderDecoderConfig',
    'VisionTextDualEncoderConfig',
    'LlamaConfig',
}
def 			_a (      SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		lowercase__            =      None

		# source code of `config_class`
		lowercase__            =      inspect.getsource(SCREAMING_SNAKE_CASE     )
		lowercase__            =      _re_checkpoint.findall(SCREAMING_SNAKE_CASE     )

		# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
		# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
		for ckpt_name, ckpt_link in checkpoints:
				# allow the link to end with `/`
				if ckpt_link.endswith('''/'''     ):
						lowercase__            =      ckpt_link[:-1]

				# verify the checkpoint name corresponds to the checkpoint link
				lowercase__            =      f'https://huggingface.co/{ckpt_name}'
				if ckpt_link == ckpt_link_from_name:
						lowercase__            =      ckpt_name
						break

		return checkpoint
def 			_a (      ):


		"""simple docstring"""




		lowercase__            =      []

		for config_class in list(CONFIG_MAPPING.values()     ):
				# Skip deprecated models
				if "models.deprecated" in config_class.__module__:
						continue
				lowercase__            =      get_checkpoint_from_config_class(SCREAMING_SNAKE_CASE     )

				lowercase__            =      config_class.__name__
				if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
						configs_without_checkpoint.append(SCREAMING_SNAKE_CASE     )

		if len(SCREAMING_SNAKE_CASE     ) > 0:
				lowercase__            =      '''\n'''.join(sorted(SCREAMING_SNAKE_CASE     )     )
				raise ValueError(f'The following configurations don\'t contain any valid checkpoint:\n{message}'     )


if __name__ == "__main__":
			check_config_docstrings_have_checkpoints()


