




"""simple docstring"""







import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
  from PIL import Image
else:

  class 				snake_case_			:







     """simple docstring"""




     @staticmethod
     def   UpperCAmelCase__			(		*lowerCamelCase_       ,					**lowerCamelCase_)				->   Optional[int]:
         pass

@is_pipeline_test
@require_torch
@require_vision
class 				snake_case_			(					unittest.TestCase			):







   """simple docstring"""




   A_				      =       MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING







   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_       ,					lowerCamelCase_       ,					lowerCamelCase_)				->   Dict:
       UpperCamelCase              =	pipeline('''visual-question-answering'''       ,					model='''hf-internal-testing/tiny-vilt-random-vqa''')
       UpperCamelCase              =	[
           {
               '''image''': Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''),
               '''question''': '''How many cats are there?''',
           },
           {
               '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
               '''question''': '''How many cats are there?''',
           },
       ]
       return vqa_pipeline, examples







   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_       ,					lowerCamelCase_)				->   Dict:
       UpperCamelCase              =	vqa_pipeline(lowerCamelCase_       ,					top_k=1)
       self.assertEqual(
           lowerCamelCase_       ,					[
               [{'''score''': ANY(lowerCamelCase_), '''answer''': ANY(lowerCamelCase_)}],
               [{'''score''': ANY(lowerCamelCase_), '''answer''': ANY(lowerCamelCase_)}],
           ]       ,					)







   @require_torch
   def   UpperCAmelCase__			(		self)				->   Union[str, Any]:
       UpperCamelCase              =	pipeline('''visual-question-answering'''       ,					model='''hf-internal-testing/tiny-vilt-random-vqa''')
       UpperCamelCase              =	'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
       UpperCamelCase              =	'''How many cats are there?'''

       UpperCamelCase              =	vqa_pipeline(image=lowerCamelCase_       ,					question='''How many cats are there?'''       ,					top_k=2)
       self.assertEqual(
           lowerCamelCase_       ,					[{'''score''': ANY(lowerCamelCase_), '''answer''': ANY(lowerCamelCase_)}, {'''score''': ANY(lowerCamelCase_), '''answer''': ANY(lowerCamelCase_)}])

       UpperCamelCase              =	vqa_pipeline({'''image''': image, '''question''': question}       ,					top_k=2)
       self.assertEqual(
           lowerCamelCase_       ,					[{'''score''': ANY(lowerCamelCase_), '''answer''': ANY(lowerCamelCase_)}, {'''score''': ANY(lowerCamelCase_), '''answer''': ANY(lowerCamelCase_)}])







   @slow
   @require_torch
   def   UpperCAmelCase__			(		self)				->   Optional[int]:
       UpperCamelCase              =	pipeline('''visual-question-answering'''       ,					model='''dandelin/vilt-b32-finetuned-vqa''')
       UpperCamelCase              =	'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
       UpperCamelCase              =	'''How many cats are there?'''

       UpperCamelCase              =	vqa_pipeline(image=lowerCamelCase_       ,					question=lowerCamelCase_       ,					top_k=2)
       self.assertEqual(
           nested_simplify(lowerCamelCase_       ,					decimals=4)       ,					[{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}])

       UpperCamelCase              =	vqa_pipeline({'''image''': image, '''question''': question}       ,					top_k=2)
       self.assertEqual(
           nested_simplify(lowerCamelCase_       ,					decimals=4)       ,					[{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}])

       UpperCamelCase              =	vqa_pipeline(
           [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}]       ,					top_k=2)
       self.assertEqual(
           nested_simplify(lowerCamelCase_       ,					decimals=4)       ,					[[{'''score''': 0.8799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]] * 2       ,					)







   @require_tf
   @unittest.skip('''Visual question answering not implemented in TF''')
   def   UpperCAmelCase__			(		self)				->   Optional[int]:
       pass




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







			"""simple docstring"""



			def __init__(	self				,   A				,   A=100				,   A=13				,   A=30				,   A=2				,   A=3				,   A=True				,   A=True				,   A=32				,   A=5				,   A=4				,   A=37				,   A="gelu"				,   A=0.1				,   A=0.1				,   A=10				,   A=0.0_2				,   A=3				,   ):
						_lowerCamelCase					:					str	       =    parent
						_lowerCamelCase					:					Optional[Any]	       =    vocab_size
						_lowerCamelCase					:					int	       =    batch_size
						_lowerCamelCase					:					Tuple	       =    image_size
						_lowerCamelCase					:					Optional[Any]	       =    patch_size
						_lowerCamelCase					:					Union[str, Any]	       =    num_channels
						_lowerCamelCase					:					Tuple	       =    is_training
						_lowerCamelCase					:					str	       =    use_labels
						_lowerCamelCase					:					Tuple	       =    hidden_size
						_lowerCamelCase					:					Tuple	       =    num_hidden_layers
						_lowerCamelCase					:					Any	       =    num_attention_heads
						_lowerCamelCase					:					int	       =    intermediate_size
						_lowerCamelCase					:					List[Any]	       =    hidden_act
						_lowerCamelCase					:					int	       =    hidden_dropout_prob
						_lowerCamelCase					:					Dict	       =    attention_probs_dropout_prob
						_lowerCamelCase					:					List[str]	       =    type_sequence_label_size
						_lowerCamelCase					:					int	       =    initializer_range

						# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
						_lowerCamelCase					:					Optional[Any]	       =    (image_size // patch_size) ** 2
						_lowerCamelCase					:					Optional[int]	       =    num_patches + 1
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Optional[Any]	       =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

						_lowerCamelCase					:					List[Any]	       =    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=A				,   initializer_range=self.initializer_range				,   )

						return config, pixel_values, labels
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A							):
						_lowerCamelCase					:					Tuple	       =    FlaxBeitModel(config=A							)
						_lowerCamelCase					:					List[str]	       =    model(A							)
						self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)							)
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A							):
						_lowerCamelCase					:					List[str]	       =    FlaxBeitForMaskedImageModeling(config=A							)
						_lowerCamelCase					:					Any	       =    model(A							)
						self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length - 1, self.vocab_size)							)
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A							):
						_lowerCamelCase					:					Optional[Any]	       =    self.type_sequence_label_size
						_lowerCamelCase					:					Tuple	       =    FlaxBeitForImageClassification(config=A							)
						_lowerCamelCase					:					Tuple	       =    model(A							)
						self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.type_sequence_label_size)							)

						# test greyscale images
						_lowerCamelCase					:					List[Any]	       =    1
						_lowerCamelCase					:					str	       =    FlaxBeitForImageClassification(A							)

						_lowerCamelCase					:					Optional[int]	       =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
						_lowerCamelCase					:					List[Any]	       =    model(A							)

			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					str	       =    self.prepare_config_and_inputs()
						(
						    (
						    _lowerCamelCase
						)       ,							(
						    _lowerCamelCase
						)       ,							(
						    _lowerCamelCase
						)       ,							
						)					:					Optional[Any]	       =    config_and_inputs
						_lowerCamelCase					:					int	       =    {'pixel_values': pixel_values}
						return config, inputs_dict







@require_flax
class 					A_(SCREAMING_SNAKE_CASE_   ,   unittest.TestCase      ):







			"""simple docstring"""



			a_       :			List[Any]												=			(
			    (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
			)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Tuple	       =    FlaxBeitModelTester(self							)
						_lowerCamelCase					:					List[str]	       =    ConfigTester(self				,   config_class=A				,   has_text_modality=A				,   hidden_size=37							)
			def    _lowerCAmelCase		(	self							):
						self.config_tester.run_common_tests()
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase       ,							_lowerCamelCase					:					Tuple	       =    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									_lowerCamelCase					:					List[str]	       =    model_class(A							)
									_lowerCamelCase					:					int	       =    inspect.signature(model.__call__							)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									_lowerCamelCase					:					Optional[Any]	       =    [*signature.parameters.keys()]

									_lowerCamelCase					:					List[Any]	       =    ['pixel_values']
									self.assertListEqual(arg_names[:1]				,   A							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase       ,							_lowerCamelCase					:					List[str]	       =    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									with self.subTest(model_class.__name__							):
												_lowerCamelCase					:					Dict	       =    self._prepare_for_class(A				,   A							)
												_lowerCamelCase					:					str	       =    model_class(A							)

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

												with self.subTest('JIT Enabled'							):
															_lowerCamelCase					:					List[Any]	       =    model_jitted(**A							).to_tuple()

												with self.subTest('JIT Disabled'							):
															with jax.disable_jit():
																		_lowerCamelCase					:					Optional[int]	       =    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							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Tuple	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					int	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*A							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Optional[int]	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*A							)

			@slow
			def    _lowerCAmelCase		(	self							):
						for model_class_name in self.all_model_classes:
									_lowerCamelCase					:					str	       =    model_class_name.from_pretrained('microsoft/beit-base-patch16-224'							)
									_lowerCamelCase					:					Dict	       =    model(np.ones((1, 3, 224, 224)							)							)
									self.assertIsNotNone(A							)






def     UpperCAmelCase_					(				):




			'''simple docstring'''



			_lowerCamelCase					:					Union[str, Any]	       =    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
			return image







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







			"""simple docstring"""



			@cached_property
			def    _lowerCAmelCase		(	self							):
						return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224'							) if is_vision_available() else None
			@slow
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					List[str]	       =    FlaxBeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k'							)

						_lowerCamelCase					:					int	       =    self.default_image_processor
						_lowerCamelCase					:					List[str]	       =    prepare_img()
						_lowerCamelCase					:					Any	       =    image_processor(images=A				,   return_tensors='np'							).pixel_values

						# prepare bool_masked_pos
						_lowerCamelCase					:					Optional[Any]	       =    np.ones((1, 196)				,   dtype=A							)

						# forward pass
						_lowerCamelCase					:					Optional[int]	       =    model(pixel_values=A				,   bool_masked_pos=A							)
						_lowerCamelCase					:					Optional[int]	       =    outputs.logits

						# verify the logits
						_lowerCamelCase					:					str	       =    (1, 196, 8192)
						self.assertEqual(logits.shape				,   A							)

						_lowerCamelCase					:					Dict	       =    np.array(
						    [[-3.2_4_3_7, 0.5_0_7_2, -1_3.9_1_7_4], [-3.2_4_5_6, 0.4_9_4_8, -1_3.9_4_0_1], [-3.2_0_3_3, 0.5_1_2_1, -1_3.8_5_5_0]]							)

						self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3]				,   A				,   atol=1E-2							)							)
			@slow
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Any	       =    FlaxBeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'							)

						_lowerCamelCase					:					Tuple	       =    self.default_image_processor
						_lowerCamelCase					:					Dict	       =    prepare_img()
						_lowerCamelCase					:					Optional[Any]	       =    image_processor(images=A				,   return_tensors='np'							)

						# forward pass
						_lowerCamelCase					:					Tuple	       =    model(**A							)
						_lowerCamelCase					:					Optional[int]	       =    outputs.logits

						# verify the logits
						_lowerCamelCase					:					int	       =    (1, 1000)
						self.assertEqual(logits.shape				,   A							)

						_lowerCamelCase					:					Tuple	       =    np.array([-1.2_3_8_5, -1.0_9_8_7, -1.0_1_0_8]							)

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

						_lowerCamelCase					:					str	       =    281
						self.assertEqual(logits.argmax(-1							).item()				,   A							)

			@slow
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Optional[Any]	       =    FlaxBeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k'							)

						_lowerCamelCase					:					Any	       =    self.default_image_processor
						_lowerCamelCase					:					Union[str, Any]	       =    prepare_img()
						_lowerCamelCase					:					Tuple	       =    image_processor(images=A				,   return_tensors='np'							)

						# forward pass
						_lowerCamelCase					:					Union[str, Any]	       =    model(**A							)
						_lowerCamelCase					:					Optional[int]	       =    outputs.logits

						# verify the logits
						_lowerCamelCase					:					List[Any]	       =    (1, 2_1841)
						self.assertEqual(logits.shape				,   A							)

						_lowerCamelCase					:					Tuple	       =    np.array([1.6_8_8_1, -0.2_7_8_7, 0.5_9_0_1]							)

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

						_lowerCamelCase					:					List[Any]	       =    2396
						self.assertEqual(logits.argmax(-1							).item()				,   A							)





