



"""simple docstring"""
from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
					from PIL import Image

					from transformers import ViTImageProcessor



class   lowerCAmelCase_    :



				"""simple docstring"""

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






										"""simple docstring"""

										snake_case								=			parent
										snake_case								=			batch_size
										snake_case								=			image_size
										snake_case								=			patch_size
										snake_case								=			num_channels
										snake_case								=			is_training
										snake_case								=			use_labels
										snake_case								=			hidden_size
										snake_case								=			num_hidden_layers
										snake_case								=			num_attention_heads
										snake_case								=			intermediate_size
										snake_case								=			hidden_act
										snake_case								=			hidden_dropout_prob
										snake_case								=			attention_probs_dropout_prob
										snake_case								=			type_sequence_label_size
										snake_case								=			initializer_range
										snake_case								=			scope

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

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

										snake_case								=			self.get_config()

										return config, pixel_values, labels

				def 		snake_case (				self    ):






										"""simple docstring"""

										return ViTConfig(
										    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		,    )

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase    ):






										"""simple docstring"""

										snake_case								=			TFViTModel(config=lowerCamelCase__    )
										snake_case								=			model(lowerCamelCase__		,    training=lowerCamelCase__    )
										self.parent.assertEqual(result.last_hidden_state.shape		,    (self.batch_size, self.seq_length, self.hidden_size)    )

										# Test with an image with different size than the one specified in config.
										snake_case								=			self.image_size // 2
										snake_case								=			pixel_values[:, :, :image_size, :image_size]
										snake_case								=			model(lowerCamelCase__		,    interpolate_pos_encoding=lowerCamelCase__		,    training=lowerCamelCase__    )
										snake_case								=			(image_size // self.patch_size) ** 2 + 1
										self.parent.assertEqual(result.last_hidden_state.shape		,    (self.batch_size, seq_length, self.hidden_size)    )

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase    ):






										"""simple docstring"""

										snake_case								=			self.type_sequence_label_size
										snake_case								=			TFViTForImageClassification(lowerCamelCase__    )
										snake_case								=			model(lowerCamelCase__		,    labels=lowerCamelCase__		,    training=lowerCamelCase__    )
										self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.type_sequence_label_size)    )

										# Test with an image with different size than the one specified in config.
										snake_case								=			self.image_size // 2
										snake_case								=			pixel_values[:, :, :image_size, :image_size]
										snake_case								=			model(lowerCamelCase__		,    interpolate_pos_encoding=lowerCamelCase__		,    training=lowerCamelCase__    )
										self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.type_sequence_label_size)    )

										# test greyscale images
										snake_case								=			1
										snake_case								=			TFViTForImageClassification(lowerCamelCase__    )
										snake_case								=			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
										snake_case								=			model(lowerCamelCase__    )
										self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.type_sequence_label_size)    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			self.prepare_config_and_inputs()
										snake_case								=			config_and_inputs
										snake_case								=			{'''pixel_values''': pixel_values}
										return config, inputs_dict





@require_tf
class   lowerCAmelCase_    (			__lowerCAmelCase     ,    __lowerCAmelCase     ,    unittest.TestCase					):



				"""simple docstring"""
				_lowerCAmelCase     :    List[str]			  =						(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
				_lowerCAmelCase     :    int			  =						(
				    {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
				    if is_tf_available()
				    else {}
				)

				_lowerCAmelCase     :    Dict			  =						False
				_lowerCAmelCase     :    str			  =						False
				_lowerCAmelCase     :    int			  =						False

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			TFViTModelTester(self    )
										snake_case								=			ConfigTester(self		,    config_class=lowerCamelCase__		,    has_text_modality=lowerCamelCase__		,    hidden_size=37    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										self.config_tester.run_common_tests()

				@unittest.skip(reason='ViT does not use inputs_embeds'    )
				def 		snake_case (				self    ):






										"""simple docstring"""

										pass

				@unittest.skip(reason='ViT does not use inputs_embeds'    )
				def 		snake_case (				self    ):






										"""simple docstring"""

										pass

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																snake_case								=			model_class(lowerCamelCase__    )
																self.assertIsInstance(model.get_input_embeddings()		,    (tf.keras.layers.Layer)    )
																snake_case								=			model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(lowerCamelCase__		,    tf.keras.layers.Layer    )    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			self.model_tester.prepare_config_and_inputs_for_common()

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

																snake_case								=			['''pixel_values''']
																self.assertListEqual(arg_names[:1]		,    lowerCamelCase__    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCamelCase__    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__    )

				@slow
				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			TFViTModel.from_pretrained('google/vit-base-patch16-224'    )
										self.assertIsNotNone(lowerCamelCase__    )





def    lowerCAmelCase__    (			)					->				int:





						"""simple docstring"""






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



@require_tf
@require_vision
class   lowerCAmelCase_    (			unittest.TestCase					):



				"""simple docstring"""

				@cached_property
				def 		snake_case (				self    ):






										"""simple docstring"""

										return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'    ) if is_vision_available() else None

				@slow
				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			TFViTForImageClassification.from_pretrained('google/vit-base-patch16-224'    )

										snake_case								=			self.default_image_processor
										snake_case								=			prepare_img()
										snake_case								=			image_processor(images=lowerCamelCase__		,    return_tensors='tf'    )

										# forward pass
										snake_case								=			model(**lowerCamelCase__    )

										# verify the logits
										snake_case								=			tf.TensorShape((1, 10_00)    )
										self.assertEqual(outputs.logits.shape		,    lowerCamelCase__    )

										snake_case								=			tf.constant([-0.27_44, 0.82_15, -0.08_36]    )

										tf.debugging.assert_near(outputs.logits[0, :3]		,    lowerCamelCase__		,    atol=1E-4    )



"""simple docstring"""
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

from packaging import version

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


if TYPE_CHECKING:
					from ...processing_utils import ProcessorMixin
					from ...utils import TensorType


SCREAMING_SNAKE_CASE__      						=  logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__      						=  {
    "microsoft/layoutlmv3-base": "https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json",
}



class   lowerCAmelCase_    (			lowerCAmelCase					):



				"""simple docstring"""
				_lowerCAmelCase     :    Dict			  =						"""layoutlmv3"""

				def __init__(				self		,    lowerCAmelCase=5_02_65		,    lowerCAmelCase=7_68		,    lowerCAmelCase=12		,    lowerCAmelCase=12		,    lowerCAmelCase=30_72		,    lowerCAmelCase="gelu"		,    lowerCAmelCase=0.1		,    lowerCAmelCase=0.1		,    lowerCAmelCase=5_12		,    lowerCAmelCase=2		,    lowerCAmelCase=0.02		,    lowerCAmelCase=1E-5		,    lowerCAmelCase=1		,    lowerCAmelCase=0		,    lowerCAmelCase=2		,    lowerCAmelCase=10_24		,    lowerCAmelCase=1_28		,    lowerCAmelCase=1_28		,    lowerCAmelCase=True		,    lowerCAmelCase=32		,    lowerCAmelCase=1_28		,    lowerCAmelCase=64		,    lowerCAmelCase=2_56		,    lowerCAmelCase=True		,    lowerCAmelCase=True		,    lowerCAmelCase=True		,    lowerCAmelCase=2_24		,    lowerCAmelCase=3		,    lowerCAmelCase=16		,    lowerCAmelCase=None		,    **lowerCAmelCase		,    ):






										"""simple docstring"""

										super().__init__(
										    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		,    pad_token_id=lowerCAmelCase		,    bos_token_id=lowerCAmelCase		,    eos_token_id=lowerCAmelCase		,    **lowerCAmelCase		,    )
										snake_case								=			max_ad_position_embeddings
										snake_case								=			coordinate_size
										snake_case								=			shape_size
										snake_case								=			has_relative_attention_bias
										snake_case								=			rel_pos_bins
										snake_case								=			max_rel_pos
										snake_case								=			has_spatial_attention_bias
										snake_case								=			rel_ad_pos_bins
										snake_case								=			max_rel_ad_pos
										snake_case								=			text_embed
										snake_case								=			visual_embed
										snake_case								=			input_size
										snake_case								=			num_channels
										snake_case								=			patch_size
										snake_case								=			classifier_dropout





class   lowerCAmelCase_    (			lowerCAmelCase					):



				"""simple docstring"""
				_lowerCAmelCase     :    List[str]			  =						version.parse("""1.12"""					)

				@property
				def 		snake_case (				self    ):






										"""simple docstring"""

										if self.task in ["question-answering", "sequence-classification"]:
																return OrderedDict(
																    [
																        ('input_ids', {0: 'batch', 1: 'sequence'}),
																        ('attention_mask', {0: 'batch', 1: 'sequence'}),
																        ('bbox', {0: 'batch', 1: 'sequence'}),
																        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
																    ]    )
										else:
																return OrderedDict(
																    [
																        ('input_ids', {0: 'batch', 1: 'sequence'}),
																        ('bbox', {0: 'batch', 1: 'sequence'}),
																        ('attention_mask', {0: 'batch', 1: 'sequence'}),
																        ('pixel_values', {0: 'batch', 1: 'num_channels'}),
																    ]    )

				@property
				def 		snake_case (				self    ):






										"""simple docstring"""

										return 1E-5

				@property
				def 		snake_case (				self    ):






										"""simple docstring"""

										return 12

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase = -1		,    lowerCAmelCase = -1		,    lowerCAmelCase = False		,    lowerCAmelCase = None		,    lowerCAmelCase = 3		,    lowerCAmelCase = 40		,    lowerCAmelCase = 40		,    ):






										"""simple docstring"""

										setattr(processor.image_processor		,    'apply_ocr'		,    lowerCAmelCase    )

										# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
										snake_case								=			compute_effective_axis_dimension(
										    lowerCAmelCase		,    fixed_dimension=OnnxConfig.default_fixed_batch		,    num_token_to_add=0    )
										# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
										snake_case								=			processor.tokenizer.num_special_tokens_to_add(lowerCAmelCase    )
										snake_case								=			compute_effective_axis_dimension(
										    lowerCAmelCase		,    fixed_dimension=OnnxConfig.default_fixed_sequence		,    num_token_to_add=lowerCAmelCase    )
										# Generate dummy inputs according to compute batch and sequence
										snake_case								=			[[' '.join([processor.tokenizer.unk_token]    ) * seq_length]] * batch_size

										# Generate dummy bounding boxes
										snake_case								=			[[[48, 84, 73, 1_28]]] * batch_size

										# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
										# batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch)
										snake_case								=			self._generate_dummy_images(lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase    )

										snake_case								=			dict(
										    processor(
										        lowerCAmelCase		,    text=lowerCAmelCase		,    boxes=lowerCAmelCase		,    return_tensors=lowerCAmelCase		,    )    )

										return inputs


