




"""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    , lowercase    , lowercase=13    , lowercase=30    , lowercase=2    , lowercase=3    , lowercase=True    , lowercase=True    , lowercase=32    , lowercase=2    , lowercase=4    , lowercase=37    , lowercase="gelu"    , lowercase=0.1    , lowercase=0.1    , lowercase=10    , lowercase=0.02    , lowercase=3    , lowercase=None    , ):
							_lowerCamelCase	:     Any       =       parent
							_lowerCamelCase	:     Dict       =       batch_size
							_lowerCamelCase	:     int       =       image_size
							_lowerCamelCase	:     List[Any]       =       patch_size
							_lowerCamelCase	:     List[Any]       =       num_channels
							_lowerCamelCase	:     Any       =       is_training
							_lowerCamelCase	:     str       =       use_labels
							_lowerCamelCase	:     Union[str, Any]       =       hidden_size
							_lowerCamelCase	:     List[Any]       =       num_hidden_layers
							_lowerCamelCase	:     int       =       num_attention_heads
							_lowerCamelCase	:     int       =       intermediate_size
							_lowerCamelCase	:     List[str]       =       hidden_act
							_lowerCamelCase	:     List[Any]       =       hidden_dropout_prob
							_lowerCamelCase	:     List[Any]       =       attention_probs_dropout_prob
							_lowerCamelCase	:     Any       =       type_sequence_label_size
							_lowerCamelCase	:     List[Any]       =       initializer_range
							_lowerCamelCase	:     Optional[int]       =       scope

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

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

							_lowerCamelCase	:     str       =       self.get_config()

							return config, pixel_values, labels
			def    A_       (    self    ):
							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=lowercase    , initializer_range=self.initializer_range    , )
			def    A_       (    self    , lowercase    , lowercase    , lowercase    ):
							_lowerCamelCase	:     str       =       TFViTModel(config=lowercase    )
							_lowerCamelCase	:     str       =       model(lowercase    , training=lowercase    )
							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.
							_lowerCamelCase	:     Optional[int]       =       self.image_size // 2
							_lowerCamelCase	:     Optional[int]       =       pixel_values[:, :, :image_size, :image_size]
							_lowerCamelCase	:     Optional[Any]       =       model(lowercase    , interpolate_pos_encoding=lowercase    , training=lowercase    )
							_lowerCamelCase	:     Tuple       =       (image_size // self.patch_size) ** 2 + 1
							self.parent.assertEqual(result.last_hidden_state.shape    , (self.batch_size, seq_length, self.hidden_size)    )
			def    A_       (    self    , lowercase    , lowercase    , lowercase    ):
							_lowerCamelCase	:     Optional[Any]       =       self.type_sequence_label_size
							_lowerCamelCase	:     List[str]       =       TFViTForImageClassification(lowercase    )
							_lowerCamelCase	:     Dict       =       model(lowercase    , labels=lowercase    , training=lowercase    )
							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.
							_lowerCamelCase	:     Dict       =       self.image_size // 2
							_lowerCamelCase	:     List[Any]       =       pixel_values[:, :, :image_size, :image_size]
							_lowerCamelCase	:     Optional[Any]       =       model(lowercase    , interpolate_pos_encoding=lowercase    , training=lowercase    )
							self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.type_sequence_label_size)    )

							# test greyscale images
							_lowerCamelCase	:     str       =       1
							_lowerCamelCase	:     Optional[Any]       =       TFViTForImageClassification(lowercase    )
							_lowerCamelCase	:     Any       =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
							_lowerCamelCase	:     str       =       model(lowercase    )
							self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.type_sequence_label_size)    )






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






@require_tf
class lowerCAmelCase__      (	lowercase,      lowercase,      unittest.TestCase		):






			'''simple docstring'''



			lowerCamelCase__ =     (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
			lowerCamelCase__ =     (
			    {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification}
			    if is_tf_available()
			    else {}
			)

			lowerCamelCase__ =     False
			lowerCamelCase__ =     False
			lowerCamelCase__ =     False
			def    A_       (    self    ):
							_lowerCamelCase	:     Tuple       =       TFViTModelTester(self    )
							_lowerCamelCase	:     Dict       =       ConfigTester(self    , config_class=lowercase    , has_text_modality=lowercase    , hidden_size=37    )
			def    A_       (    self    ):
							self.config_tester.run_common_tests()
			@unittest.skip(reason='ViT does not use inputs_embeds'    )
			def    A_       (    self    ):
							pass
			@unittest.skip(reason='ViT does not use inputs_embeds'    )
			def    A_       (    self    ):
							pass
			def    A_       (    self    ):
							_lowerCamelCase,					_lowerCamelCase	:     int       =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
											_lowerCamelCase	:     Dict       =       model_class(lowercase    )
											self.assertIsInstance(model.get_input_embeddings()    , (tf.keras.layers.Layer)    )
											_lowerCamelCase	:     str       =       model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(lowercase    , tf.keras.layers.Layer    )    )
			def    A_       (    self    ):
							_lowerCamelCase,					_lowerCamelCase	:     Dict       =       self.model_tester.prepare_config_and_inputs_for_common()

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

											_lowerCamelCase	:     int       =       ['pixel_values']
											self.assertListEqual(arg_names[:1]    , lowercase    )
			def    A_       (    self    ):
							_lowerCamelCase	:     int       =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase    )
			def    A_       (    self    ):
							_lowerCamelCase	:     Tuple       =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowercase    )






			@slow
			def    A_       (    self    ):
							_lowerCamelCase	:     Optional[Any]       =       TFViTModel.from_pretrained('google/vit-base-patch16-224'    )
							self.assertIsNotNone(lowercase    )







def 	_snake_case     (							):
				_lowerCamelCase	:     Tuple       =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
				return image






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






			'''simple docstring'''



			@cached_property
			def    A_       (    self    ):
							return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'    ) if is_vision_available() else None






			@slow
			def    A_       (    self    ):
							_lowerCamelCase	:     Dict       =       TFViTForImageClassification.from_pretrained('google/vit-base-patch16-224'    )

							_lowerCamelCase	:     Optional[Any]       =       self.default_image_processor
							_lowerCamelCase	:     Dict       =       prepare_img()
							_lowerCamelCase	:     Optional[int]       =       image_processor(images=lowercase    , return_tensors='tf'    )

							# forward pass
							_lowerCamelCase	:     Optional[Any]       =       model(**lowercase    )

							# verify the logits
							_lowerCamelCase	:     List[str]       =       tf.TensorShape((1, 1000)    )
							self.assertEqual(outputs.logits.shape    , lowercase    )

							_lowerCamelCase	:     Optional[Any]       =       tf.constant([-0.27_44, 0.82_15, -0.08_36]    )

							tf.debugging.assert_near(outputs.logits[0, :3]    , lowercase    , 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


lowercase__			=		logging.get_logger(__name__)

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






class lowerCAmelCase__      (	lowercase		):






			'''simple docstring'''



			lowerCamelCase__ =     """layoutlmv3"""
			def __init__(    self    , lowercase=50265    , lowercase=768    , lowercase=12    , lowercase=12    , lowercase=3072    , lowercase="gelu"    , lowercase=0.1    , lowercase=0.1    , lowercase=512    , lowercase=2    , lowercase=0.02    , lowercase=1E-5    , lowercase=1    , lowercase=0    , lowercase=2    , lowercase=1024    , lowercase=128    , lowercase=128    , lowercase=True    , lowercase=32    , lowercase=128    , lowercase=64    , lowercase=256    , lowercase=True    , lowercase=True    , lowercase=True    , lowercase=224    , lowercase=3    , lowercase=16    , lowercase=None    , **lowercase    , ):
							super().__init__(
							    vocab_size=lowercase    , hidden_size=lowercase    , num_hidden_layers=lowercase    , num_attention_heads=lowercase    , intermediate_size=lowercase    , hidden_act=lowercase    , hidden_dropout_prob=lowercase    , attention_probs_dropout_prob=lowercase    , max_position_embeddings=lowercase    , type_vocab_size=lowercase    , initializer_range=lowercase    , layer_norm_eps=lowercase    , pad_token_id=lowercase    , bos_token_id=lowercase    , eos_token_id=lowercase    , **lowercase    , )
							_lowerCamelCase	:     List[str]       =       max_ad_position_embeddings
							_lowerCamelCase	:     Optional[Any]       =       coordinate_size
							_lowerCamelCase	:     int       =       shape_size
							_lowerCamelCase	:     Optional[Any]       =       has_relative_attention_bias
							_lowerCamelCase	:     Dict       =       rel_pos_bins
							_lowerCamelCase	:     Tuple       =       max_rel_pos
							_lowerCamelCase	:     int       =       has_spatial_attention_bias
							_lowerCamelCase	:     Optional[int]       =       rel_ad_pos_bins
							_lowerCamelCase	:     List[Any]       =       max_rel_ad_pos
							_lowerCamelCase	:     Any       =       text_embed
							_lowerCamelCase	:     List[Any]       =       visual_embed
							_lowerCamelCase	:     str       =       input_size
							_lowerCamelCase	:     Optional[Any]       =       num_channels
							_lowerCamelCase	:     Tuple       =       patch_size
							_lowerCamelCase	:     Optional[int]       =       classifier_dropout






class lowerCAmelCase__      (	lowercase		):






			'''simple docstring'''



			lowerCamelCase__ =     version.parse("""1.12"""		)
			@property
			def    A_       (    self    ):
							# The order of inputs is different for question answering and sequence classification
							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    A_       (    self    ):
							return 1E-5
			@property
			def    A_       (    self    ):
							return 12






			def    A_       (    self    , lowercase    , lowercase = -1    , lowercase = -1    , lowercase = False    , lowercase = None    , lowercase = 3    , lowercase = 40    , lowercase = 40    , ):
							setattr(processor.image_processor    , 'apply_ocr'    , lowercase    )

							# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
							_lowerCamelCase	:     Any       =       compute_effective_axis_dimension(
							    lowercase    , 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
							_lowerCamelCase	:     Tuple       =       processor.tokenizer.num_special_tokens_to_add(lowercase    )
							_lowerCamelCase	:     Any       =       compute_effective_axis_dimension(
							    lowercase    , fixed_dimension=OnnxConfig.default_fixed_sequence    , num_token_to_add=lowercase    )
							# Generate dummy inputs according to compute batch and sequence
							_lowerCamelCase	:     List[Any]       =       [[' '.join([processor.tokenizer.unk_token]    ) * seq_length]] * batch_size

							# Generate dummy bounding boxes
							_lowerCamelCase	:     List[str]       =       [[[48, 84, 73, 128]]] * 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)
							_lowerCamelCase	:     Tuple       =       self._generate_dummy_images(lowercase    , lowercase    , lowercase    , lowercase    )

							_lowerCamelCase	:     str       =       dict(
							    processor(
							        lowercase    , text=lowercase    , boxes=lowercase    , return_tensors=lowercase    , )    )

							return inputs