




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

			"""simple docstring"""







			def __init__( self     ,		lowerCamelCase__     ,		lowerCamelCase__=100     ,		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.02     ,		lowerCamelCase__=3     ,		)       -> int:



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



							'''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=lowerCamelCase__     ,		initializer_range=self.initializer_range     ,		)

							return config, pixel_values, labels


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> List[Any]:



							'''simple docstring'''
							__lowerCamelCase									=       FlaxBeitModel(config=lowerCamelCase__				)
							__lowerCamelCase									=       model(lowerCamelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape     ,		(self.batch_size, self.seq_length, self.hidden_size)				)


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       FlaxBeitForMaskedImageModeling(config=lowerCamelCase__				)
							__lowerCamelCase									=       model(lowerCamelCase__				)
							self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length - 1, self.vocab_size)				)


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> int:



							'''simple docstring'''
							__lowerCamelCase									=       self.type_sequence_label_size
							__lowerCamelCase									=       FlaxBeitForImageClassification(config=lowerCamelCase__				)
							__lowerCamelCase									=       model(lowerCamelCase__				)
							self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.type_sequence_label_size)				)

							# test greyscale images
							__lowerCamelCase									=       1
							__lowerCamelCase									=       FlaxBeitForImageClassification(lowerCamelCase__				)

							__lowerCamelCase									=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
							__lowerCamelCase									=       model(lowerCamelCase__				)






			def  lowercase_       ( self				)       -> Optional[int]:



							'''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        __lowerCAmelCase					(			__magic_name__     ,							unittest.TestCase					):

			"""simple docstring"""







			snake_case_  						=			(
			    (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
			)


			def  lowercase_       ( self				)       -> None:



							'''simple docstring'''
							__lowerCamelCase									=       FlaxBeitModelTester(self				)
							__lowerCamelCase									=       ConfigTester(self     ,		config_class=lowerCamelCase__     ,		has_text_modality=lowerCamelCase__     ,		hidden_size=37				)


			def  lowercase_       ( self				)       -> str:



							'''simple docstring'''
							self.config_tester.run_common_tests()


			def  lowercase_       ( self				)       -> str:



							'''simple docstring'''
							__lowerCamelCase			,		__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
											__lowerCamelCase									=       model_class(lowerCamelCase__				)
											__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]     ,		lowerCamelCase__				)


			def  lowercase_       ( self				)       -> str:



							'''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(lowerCamelCase__     ,		lowerCamelCase__				)
															__lowerCamelCase									=       model_class(lowerCamelCase__				)

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

															with self.subTest('JIT Enabled'				):
																			__lowerCamelCase									=       model_jitted(**lowerCamelCase__				).to_tuple()

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

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


			def  lowercase_       ( self				)       -> Tuple:



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__				)


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



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__				)


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



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__				)






			@slow
			def  lowercase_       ( self				)       -> List[str]:



							'''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(lowerCamelCase__				)







def      lowerCamelCase_						(			)	->			int:





				"""simple docstring"""


				__lowerCamelCase									=       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
				return image







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

			"""simple docstring"""







			@cached_property
			def  lowercase_       ( self				)       -> Optional[int]:



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


			@slow
			def  lowercase_       ( self				)       -> Optional[int]:



							'''simple docstring'''
							__lowerCamelCase									=       FlaxBeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k'				)

							__lowerCamelCase									=       self.default_image_processor
							__lowerCamelCase									=       prepare_img()
							__lowerCamelCase									=       image_processor(images=lowerCamelCase__     ,		return_tensors='np'				).pixel_values

							# prepare bool_masked_pos
							__lowerCamelCase									=       np.ones((1, 196)     ,		dtype=lowerCamelCase__				)

							# forward pass
							__lowerCamelCase									=       model(pixel_values=lowerCamelCase__     ,		bool_masked_pos=lowerCamelCase__				)
							__lowerCamelCase									=       outputs.logits

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

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


			@slow
			def  lowercase_       ( self				)       -> Union[str, Any]:



							'''simple docstring'''
							__lowerCamelCase									=       FlaxBeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'				)

							__lowerCamelCase									=       self.default_image_processor
							__lowerCamelCase									=       prepare_img()
							__lowerCamelCase									=       image_processor(images=lowerCamelCase__     ,		return_tensors='np'				)

							# forward pass
							__lowerCamelCase									=       model(**lowerCamelCase__				)
							__lowerCamelCase									=       outputs.logits

							# verify the logits
							__lowerCamelCase									=       (1, 1_000)
							self.assertEqual(logits.shape     ,		lowerCamelCase__				)

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

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

							__lowerCamelCase									=       281
							self.assertEqual(logits.argmax(-1				).item()     ,		lowerCamelCase__				)






			@slow
			def  lowercase_       ( self				)       -> int:



							'''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=lowerCamelCase__     ,		return_tensors='np'				)

							# forward pass
							__lowerCamelCase									=       model(**lowerCamelCase__				)
							__lowerCamelCase									=       outputs.logits

							# verify the logits
							__lowerCamelCase									=       (1, 21_841)
							self.assertEqual(logits.shape     ,		lowerCamelCase__				)

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

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

							__lowerCamelCase									=       2_396
							self.assertEqual(logits.argmax(-1				).item()     ,		lowerCamelCase__				)








import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			['''image_processor''', '''tokenizer''']
			snake_case_  						=			'''OwlViTImageProcessor'''
			snake_case_  						=			('''CLIPTokenizer''', '''CLIPTokenizerFast''')


			def __init__( self     ,		lowerCamelCase__=None     ,		lowerCamelCase__=None     ,		**lowerCamelCase__				)       -> Tuple:



							'''simple docstring'''
							__lowerCamelCase									=       None
							if "feature_extractor" in kwargs:
											warnings.warn(
											    'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
											    ' instead.'     ,		lowerCamelCase__     ,		)
											__lowerCamelCase									=       kwargs.pop('feature_extractor'				)

							__lowerCamelCase									=       image_processor if image_processor is not None else feature_extractor
							if image_processor is None:
											raise ValueError('You need to specify an `image_processor`.'				)
							if tokenizer is None:
											raise ValueError('You need to specify a `tokenizer`.'				)

							super().__init__(lowerCamelCase__     ,		lowerCamelCase__				)


			def __call__( self     ,		lowerCamelCase__=None     ,		lowerCamelCase__=None     ,		lowerCamelCase__=None     ,		lowerCamelCase__="max_length"     ,		lowerCamelCase__="np"     ,		**lowerCamelCase__				)       -> Optional[int]:



							'''simple docstring'''

							if text is None and query_images is None and images is None:
											raise ValueError(
											    'You have to specify at least one text or query image or image. All three cannot be none.'				)

							if text is not None:
											if isinstance(lowerCamelCase__     ,		lowerCamelCase__				) or (isinstance(lowerCamelCase__     ,		lowerCamelCase__				) and not isinstance(text[0]     ,		lowerCamelCase__				)):
															__lowerCamelCase									=       [self.tokenizer(lowerCamelCase__     ,		padding=lowerCamelCase__     ,		return_tensors=lowerCamelCase__     ,		**lowerCamelCase__				)]

											elif isinstance(lowerCamelCase__     ,		lowerCamelCase__				) and isinstance(text[0]     ,		lowerCamelCase__				):
															__lowerCamelCase									=       []

															# Maximum number of queries across batch
															__lowerCamelCase									=       max([len(lowerCamelCase__				) for t in text]				)

															# Pad all batch samples to max number of text queries
															for t in text:
																			if len(lowerCamelCase__				) != max_num_queries:
																							__lowerCamelCase									=       t + [' '] * (max_num_queries - len(lowerCamelCase__				))

																			__lowerCamelCase									=       self.tokenizer(lowerCamelCase__     ,		padding=lowerCamelCase__     ,		return_tensors=lowerCamelCase__     ,		**lowerCamelCase__				)
																			encodings.append(lowerCamelCase__				)
											else:
															raise TypeError('Input text should be a string, a list of strings or a nested list of strings'				)

											if return_tensors == "np":
															__lowerCamelCase									=       np.concatenate([encoding['input_ids'] for encoding in encodings]     ,		axis=0				)
															__lowerCamelCase									=       np.concatenate([encoding['attention_mask'] for encoding in encodings]     ,		axis=0				)

											elif return_tensors == "jax" and is_flax_available():
															import jax.numpy as jnp

															__lowerCamelCase									=       jnp.concatenate([encoding['input_ids'] for encoding in encodings]     ,		axis=0				)
															__lowerCamelCase									=       jnp.concatenate([encoding['attention_mask'] for encoding in encodings]     ,		axis=0				)

											elif return_tensors == "pt" and is_torch_available():
															import torch

															__lowerCamelCase									=       torch.cat([encoding['input_ids'] for encoding in encodings]     ,		dim=0				)
															__lowerCamelCase									=       torch.cat([encoding['attention_mask'] for encoding in encodings]     ,		dim=0				)

											elif return_tensors == "tf" and is_tf_available():
															import tensorflow as tf

															__lowerCamelCase									=       tf.stack([encoding['input_ids'] for encoding in encodings]     ,		axis=0				)
															__lowerCamelCase									=       tf.stack([encoding['attention_mask'] for encoding in encodings]     ,		axis=0				)

											else:
															raise ValueError('Target return tensor type could not be returned'				)

											__lowerCamelCase									=       BatchEncoding()
											__lowerCamelCase									=       input_ids
											__lowerCamelCase									=       attention_mask

							if query_images is not None:
											__lowerCamelCase									=       BatchEncoding()
											__lowerCamelCase									=       self.image_processor(
											    lowerCamelCase__     ,		return_tensors=lowerCamelCase__     ,		**lowerCamelCase__				).pixel_values
											__lowerCamelCase									=       query_pixel_values

							if images is not None:
											__lowerCamelCase									=       self.image_processor(lowerCamelCase__     ,		return_tensors=lowerCamelCase__     ,		**lowerCamelCase__				)

							if text is not None and images is not None:
											__lowerCamelCase									=       image_features.pixel_values
											return encoding
							elif query_images is not None and images is not None:
											__lowerCamelCase									=       image_features.pixel_values
											return encoding
							elif text is not None or query_images is not None:
											return encoding
							else:
											return BatchEncoding(data=dict(**lowerCamelCase__				)     ,		tensor_type=lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Tuple:



							'''simple docstring'''
							return self.image_processor.post_process(*lowerCamelCase__     ,		**lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Optional[Any]:



							'''simple docstring'''
							return self.image_processor.post_process_object_detection(*lowerCamelCase__     ,		**lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Optional[int]:



							'''simple docstring'''
							return self.image_processor.post_process_image_guided_detection(*lowerCamelCase__     ,		**lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Optional[int]:



							'''simple docstring'''
							return self.tokenizer.batch_decode(*lowerCamelCase__     ,		**lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Optional[int]:



							'''simple docstring'''
							return self.tokenizer.decode(*lowerCamelCase__     ,		**lowerCamelCase__				)


			@property
			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							warnings.warn(
							    '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'     ,		lowerCamelCase__     ,		)
							return self.image_processor_class






			@property
			def  lowercase_       ( self				)       -> Union[str, Any]:



							'''simple docstring'''
							warnings.warn(
							    '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'     ,		lowerCamelCase__     ,		)
							return self.image_processor


