



'''simple docstring'''

from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

from transformers import is_tf_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow
from transformers.utils import cached_property

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


if is_tf_available():
	import tensorflow as tf

	from transformers import (
	    TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
	    TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
	    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
	    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
	    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
	    LayoutLMvaConfig,
	    TFLayoutLMvaForQuestionAnswering,
	    TFLayoutLMvaForSequenceClassification,
	    TFLayoutLMvaForTokenClassification,
	    TFLayoutLMvaModel,
	)

if is_vision_available():
	from PIL import Image

	from transformers import LayoutLMvaImageProcessor


class 	a__     :


	def __init__(self : List[Any],					__UpperCAmelCase : List[str],					__UpperCAmelCase : Optional[int]=2,					__UpperCAmelCase : Optional[Any]=3,					__UpperCAmelCase : Optional[Any]=4,					__UpperCAmelCase : Dict=2,					__UpperCAmelCase : List[Any]=7,					__UpperCAmelCase : Dict=True,					__UpperCAmelCase : str=True,					__UpperCAmelCase : Optional[Any]=True,					__UpperCAmelCase : Tuple=True,					__UpperCAmelCase : List[Any]=99,					__UpperCAmelCase : Dict=36,					__UpperCAmelCase : Union[str, Any]=2,					__UpperCAmelCase : Optional[int]=4,					__UpperCAmelCase : Dict=37,					__UpperCAmelCase : Optional[Any]="gelu",					__UpperCAmelCase : Tuple=0.1,					__UpperCAmelCase : Any=0.1,					__UpperCAmelCase : Optional[int]=512,					__UpperCAmelCase : Any=16,					__UpperCAmelCase : str=2,					__UpperCAmelCase : Union[str, Any]=0.02,					__UpperCAmelCase : Dict=6,					__UpperCAmelCase : int=6,					__UpperCAmelCase : Union[str, Any]=3,					__UpperCAmelCase : Any=4,					__UpperCAmelCase : Tuple=None,					__UpperCAmelCase : Any=1000,					)     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= parent
					SCREAMING_SNAKE_CASE  :						List[str]      					= batch_size
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= num_channels
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= image_size
					SCREAMING_SNAKE_CASE  :						Tuple      					= patch_size
					SCREAMING_SNAKE_CASE  :						Dict      					= is_training
					SCREAMING_SNAKE_CASE  :						List[str]      					= use_input_mask
					SCREAMING_SNAKE_CASE  :						Dict      					= use_token_type_ids
					SCREAMING_SNAKE_CASE  :						Any      					= use_labels
					SCREAMING_SNAKE_CASE  :						str      					= vocab_size
					SCREAMING_SNAKE_CASE  :						Tuple      					= hidden_size
					SCREAMING_SNAKE_CASE  :						int      					= num_hidden_layers
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= num_attention_heads
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= intermediate_size
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= hidden_act
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= hidden_dropout_prob
					SCREAMING_SNAKE_CASE  :						Dict      					= attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE  :						List[str]      					= max_position_embeddings
					SCREAMING_SNAKE_CASE  :						int      					= type_vocab_size
					SCREAMING_SNAKE_CASE  :						Any      					= type_sequence_label_size
					SCREAMING_SNAKE_CASE  :						Dict      					= initializer_range
					SCREAMING_SNAKE_CASE  :						Dict      					= coordinate_size
					SCREAMING_SNAKE_CASE  :						int      					= shape_size
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= num_labels
					SCREAMING_SNAKE_CASE  :						int      					= num_choices
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= scope
					SCREAMING_SNAKE_CASE  :						Any      					= range_bbox

					# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
					SCREAMING_SNAKE_CASE  :						Any      					= text_seq_length
					SCREAMING_SNAKE_CASE  :						Dict      					= (image_size // patch_size) ** 2 + 1
					SCREAMING_SNAKE_CASE  :						Tuple      					= self.text_seq_length + self.image_seq_length


	def 				lowercase__  (self : int      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Dict      					= ids_tensor([self.batch_size, self.text_seq_length],					self.vocab_size      )

					SCREAMING_SNAKE_CASE  :						List[Any]      					= ids_tensor([self.batch_size, self.text_seq_length, 4],					self.range_bbox      )
					SCREAMING_SNAKE_CASE  :						str      					= bbox.numpy()
					# Ensure that bbox is legal
					for i in range(bbox.shape[0]      ):
									for j in range(bbox.shape[1]      ):
													if bbox[i, j, 3] < bbox[i, j, 1]:
																	SCREAMING_SNAKE_CASE  :						Dict      					= bbox[i, j, 3]
																	SCREAMING_SNAKE_CASE  :						str      					= bbox[i, j, 1]
																	SCREAMING_SNAKE_CASE  :						str      					= tmp_coordinate
													if bbox[i, j, 2] < bbox[i, j, 0]:
																	SCREAMING_SNAKE_CASE  :						str      					= bbox[i, j, 2]
																	SCREAMING_SNAKE_CASE  :						Tuple      					= bbox[i, j, 0]
																	SCREAMING_SNAKE_CASE  :						Optional[Any]      					= tmp_coordinate
					SCREAMING_SNAKE_CASE  :						int      					= tf.constant(__UpperCAmelCase      )

					SCREAMING_SNAKE_CASE  :						List[str]      					= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= None
					if self.use_input_mask:
									SCREAMING_SNAKE_CASE  :						Dict      					= random_attention_mask([self.batch_size, self.text_seq_length]      )

					SCREAMING_SNAKE_CASE  :						Any      					= None
					if self.use_token_type_ids:
									SCREAMING_SNAKE_CASE  :						Any      					= ids_tensor([self.batch_size, self.text_seq_length],					self.type_vocab_size      )

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= None
					SCREAMING_SNAKE_CASE  :						Dict      					= None
					if self.use_labels:
									SCREAMING_SNAKE_CASE  :						List[str]      					= ids_tensor([self.batch_size],					self.type_sequence_label_size      )
									SCREAMING_SNAKE_CASE  :						Optional[int]      					= ids_tensor([self.batch_size, self.text_seq_length],					self.num_labels      )

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= LayoutLMvaConfig(
					    vocab_size=self.vocab_size,					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,					max_position_embeddings=self.max_position_embeddings,					type_vocab_size=self.type_vocab_size,					initializer_range=self.initializer_range,					coordinate_size=self.coordinate_size,					shape_size=self.shape_size,					input_size=self.image_size,					patch_size=self.patch_size,					)

					return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels


	def 				lowercase__  (self : List[Any],					__UpperCAmelCase : Tuple,					__UpperCAmelCase : Tuple,					__UpperCAmelCase : int,					__UpperCAmelCase : Optional[Any],					__UpperCAmelCase : str,					__UpperCAmelCase : Optional[int]      )     ->			Optional[int]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Tuple      					= TFLayoutLMvaModel(config=__UpperCAmelCase      )

					# text + image
					SCREAMING_SNAKE_CASE  :						Tuple      					= model(__UpperCAmelCase,					pixel_values=__UpperCAmelCase,					training=__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						List[str]      					= model(
					    __UpperCAmelCase,					bbox=__UpperCAmelCase,					pixel_values=__UpperCAmelCase,					attention_mask=__UpperCAmelCase,					token_type_ids=__UpperCAmelCase,					training=__UpperCAmelCase,					)
					SCREAMING_SNAKE_CASE  :						Any      					= model(__UpperCAmelCase,					bbox=__UpperCAmelCase,					pixel_values=__UpperCAmelCase,					training=__UpperCAmelCase      )

					self.parent.assertEqual(result.last_hidden_state.shape,					(self.batch_size, self.seq_length, self.hidden_size)      )

					# text only
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= model(__UpperCAmelCase,					training=__UpperCAmelCase      )
					self.parent.assertEqual(
					    result.last_hidden_state.shape,					(self.batch_size, self.text_seq_length, self.hidden_size)      )

					# image only
					SCREAMING_SNAKE_CASE  :						Any      					= model({'''pixel_values''': pixel_values},					training=__UpperCAmelCase      )
					self.parent.assertEqual(
					    result.last_hidden_state.shape,					(self.batch_size, self.image_seq_length, self.hidden_size)      )


	def 				lowercase__  (self : Union[str, Any],					__UpperCAmelCase : Union[str, Any],					__UpperCAmelCase : Optional[int],					__UpperCAmelCase : List[str],					__UpperCAmelCase : Union[str, Any],					__UpperCAmelCase : Tuple,					__UpperCAmelCase : List[Any],					__UpperCAmelCase : Optional[Any]      )     ->			List[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.num_labels
					SCREAMING_SNAKE_CASE  :						List[Any]      					= TFLayoutLMvaForSequenceClassification(config=__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= model(
					    __UpperCAmelCase,					bbox=__UpperCAmelCase,					pixel_values=__UpperCAmelCase,					attention_mask=__UpperCAmelCase,					token_type_ids=__UpperCAmelCase,					labels=__UpperCAmelCase,					training=__UpperCAmelCase,					)
					self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.num_labels)      )


	def 				lowercase__  (self : Any,					__UpperCAmelCase : str,					__UpperCAmelCase : Tuple,					__UpperCAmelCase : List[Any],					__UpperCAmelCase : Optional[int],					__UpperCAmelCase : List[str],					__UpperCAmelCase : Dict,					__UpperCAmelCase : Optional[Any]      )     ->			Any:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Dict      					= self.num_labels
					SCREAMING_SNAKE_CASE  :						List[Any]      					= TFLayoutLMvaForTokenClassification(config=__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						List[Any]      					= model(
					    __UpperCAmelCase,					bbox=__UpperCAmelCase,					pixel_values=__UpperCAmelCase,					attention_mask=__UpperCAmelCase,					token_type_ids=__UpperCAmelCase,					labels=__UpperCAmelCase,					training=__UpperCAmelCase,					)
					self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.text_seq_length, self.num_labels)      )


	def 				lowercase__  (self : List[str],					__UpperCAmelCase : Optional[int],					__UpperCAmelCase : Dict,					__UpperCAmelCase : List[Any],					__UpperCAmelCase : Tuple,					__UpperCAmelCase : Any,					__UpperCAmelCase : str,					__UpperCAmelCase : Tuple      )     ->			List[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[str]      					= 2
					SCREAMING_SNAKE_CASE  :						Tuple      					= TFLayoutLMvaForQuestionAnswering(config=__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= model(
					    __UpperCAmelCase,					bbox=__UpperCAmelCase,					pixel_values=__UpperCAmelCase,					attention_mask=__UpperCAmelCase,					token_type_ids=__UpperCAmelCase,					start_positions=__UpperCAmelCase,					end_positions=__UpperCAmelCase,					training=__UpperCAmelCase,					)
					self.parent.assertEqual(result.start_logits.shape,					(self.batch_size, self.seq_length)      )
					self.parent.assertEqual(result.end_logits.shape,					(self.batch_size, self.seq_length)      )






	def 				lowercase__  (self : Optional[int]      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						int      					= self.prepare_config_and_inputs()
					((SCREAMING_SNAKE_CASE)					,					(SCREAMING_SNAKE_CASE)					,					(SCREAMING_SNAKE_CASE)					,					(SCREAMING_SNAKE_CASE)					,					(SCREAMING_SNAKE_CASE)					,					(SCREAMING_SNAKE_CASE)					,					(SCREAMING_SNAKE_CASE)					,					(SCREAMING_SNAKE_CASE))  :						List[Any]      					= config_and_inputs
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= {
					    '''input_ids''': input_ids,
					    '''bbox''': bbox,
					    '''pixel_values''': pixel_values,
					    '''token_type_ids''': token_type_ids,
					    '''attention_mask''': input_mask,
					}
					return config, inputs_dict



@require_tf
class 	a__     (				_lowercase,			_lowercase,			unittest.TestCase	):
	__magic_name__		:    List[str]	    =     (
	    (
	        TFLayoutLMvaModel,
	        TFLayoutLMvaForQuestionAnswering,
	        TFLayoutLMvaForSequenceClassification,
	        TFLayoutLMvaForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	__magic_name__		:    Optional[int]	    =     (
	    {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel}
	    if is_tf_available()
	    else {}
	)

	__magic_name__		:    Union[str, Any]	    =     False
	__magic_name__		:    Optional[Any]	    =     False
	__magic_name__		:    Optional[int]	    =     False


	def 				lowercase__  (self : Any,					__UpperCAmelCase : List[Any],					__UpperCAmelCase : Dict,					__UpperCAmelCase : Dict,					__UpperCAmelCase : Union[str, Any],					__UpperCAmelCase : Union[str, Any]      )     ->			Dict:

					"""simple docstring"""

					return True


	def 				lowercase__  (self : List[Any],					__UpperCAmelCase : Tuple,					__UpperCAmelCase : Union[str, Any],					__UpperCAmelCase : str=False      )     ->			dict:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= copy.deepcopy(__UpperCAmelCase      )

					if model_class in get_values(__UpperCAmelCase      ):
									SCREAMING_SNAKE_CASE  :						str      					= {
									    k: tf.tile(tf.expand_dims(__UpperCAmelCase,					1      ),					(1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)      )
									    if isinstance(__UpperCAmelCase,					tf.Tensor      ) and v.ndim > 0
									    else v
									    for k, v in inputs_dict.items()
									}

					if return_labels:
									if model_class in get_values(__UpperCAmelCase      ):
													SCREAMING_SNAKE_CASE  :						List[Any]      					= tf.ones(self.model_tester.batch_size,					dtype=tf.intaa      )
									elif model_class in get_values(__UpperCAmelCase      ):
													SCREAMING_SNAKE_CASE  :						int      					= tf.zeros(self.model_tester.batch_size,					dtype=tf.intaa      )
													SCREAMING_SNAKE_CASE  :						Optional[int]      					= tf.zeros(self.model_tester.batch_size,					dtype=tf.intaa      )
									elif model_class in get_values(__UpperCAmelCase      ):
													SCREAMING_SNAKE_CASE  :						Optional[Any]      					= tf.zeros(self.model_tester.batch_size,					dtype=tf.intaa      )
									elif model_class in get_values(__UpperCAmelCase      ):
													SCREAMING_SNAKE_CASE  :						str      					= tf.zeros(
													    (self.model_tester.batch_size, self.model_tester.text_seq_length),					dtype=tf.intaa      )

					return inputs_dict


	def 				lowercase__  (self : List[str]      )     ->			Any:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Tuple      					= TFLayoutLMvaModelTester(self      )
					SCREAMING_SNAKE_CASE  :						Tuple      					= ConfigTester(self,					config_class=__UpperCAmelCase,					hidden_size=37      )


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

					"""simple docstring"""

					self.config_tester.run_common_tests()


	def 				lowercase__  (self : Tuple      )     ->			Optional[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						Tuple      					= self.model_tester.prepare_config_and_inputs_for_common()
					for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE  :						List[Any]      					= model_class(__UpperCAmelCase      )
									if getattr(__UpperCAmelCase,					'''hf_compute_loss''',					__UpperCAmelCase      ):
													# The number of elements in the loss should be the same as the number of elements in the label
													SCREAMING_SNAKE_CASE  :						Dict      					= self._prepare_for_class(inputs_dict.copy(),					__UpperCAmelCase,					return_labels=__UpperCAmelCase      )
													SCREAMING_SNAKE_CASE  :						Dict      					= prepared_for_class[
													    sorted(prepared_for_class.keys() - inputs_dict.keys(),					reverse=__UpperCAmelCase      )[0]
													]
													SCREAMING_SNAKE_CASE  :						Any      					= added_label.shape.as_list()[:1]

													# Test that model correctly compute the loss with kwargs
													SCREAMING_SNAKE_CASE  :						List[str]      					= self._prepare_for_class(inputs_dict.copy(),					__UpperCAmelCase,					return_labels=__UpperCAmelCase      )
													SCREAMING_SNAKE_CASE  :						str      					= prepared_for_class.pop('''input_ids'''      )

													SCREAMING_SNAKE_CASE  :						List[str]      					= model(__UpperCAmelCase,					**__UpperCAmelCase      )[0]
													self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )

													# Test that model correctly compute the loss when we mask some positions
													SCREAMING_SNAKE_CASE  :						List[str]      					= self._prepare_for_class(inputs_dict.copy(),					__UpperCAmelCase,					return_labels=__UpperCAmelCase      )
													SCREAMING_SNAKE_CASE  :						Optional[Any]      					= prepared_for_class.pop('''input_ids'''      )
													if "labels" in prepared_for_class:
																	SCREAMING_SNAKE_CASE  :						Dict      					= prepared_for_class['''labels'''].numpy()
																	if len(labels.shape      ) > 1 and labels.shape[1] != 1:
																					SCREAMING_SNAKE_CASE  :						Dict      					= -100
																					SCREAMING_SNAKE_CASE  :						int      					= tf.convert_to_tensor(__UpperCAmelCase      )
																					SCREAMING_SNAKE_CASE  :						Optional[int]      					= model(__UpperCAmelCase,					**__UpperCAmelCase      )[0]
																					self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )
																					self.assertTrue(not np.any(np.isnan(loss.numpy()      )      )      )

                # Test that model correctly compute the loss with a dict
													SCREAMING_SNAKE_CASE  :						Dict      					= self._prepare_for_class(inputs_dict.copy(),					__UpperCAmelCase,					return_labels=__UpperCAmelCase      )
													SCREAMING_SNAKE_CASE  :						str      					= model(__UpperCAmelCase      )[0]
													self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )

													# Test that model correctly compute the loss with a tuple
													SCREAMING_SNAKE_CASE  :						List[Any]      					= self._prepare_for_class(inputs_dict.copy(),					__UpperCAmelCase,					return_labels=__UpperCAmelCase      )

													# Get keys that were added with the _prepare_for_class function
													SCREAMING_SNAKE_CASE  :						int      					= prepared_for_class.keys() - inputs_dict.keys()
													SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= inspect.signature(model.call      ).parameters
													SCREAMING_SNAKE_CASE  :						Dict      					= list(signature.keys()      )

													# Create a dictionary holding the location of the tensors in the tuple
													SCREAMING_SNAKE_CASE  :						Any      					= {0: '''input_ids'''}
													for label_key in label_keys:
																	SCREAMING_SNAKE_CASE  :						str      					= signature_names.index(__UpperCAmelCase      )
																	SCREAMING_SNAKE_CASE  :						Any      					= label_key
													SCREAMING_SNAKE_CASE  :						Any      					= sorted(tuple_index_mapping.items()      )
													# Initialize a list with their default values, update the values and convert to a tuple
													SCREAMING_SNAKE_CASE  :						int      					= []

													for name in signature_names:
																	if name != "kwargs":
																					list_input.append(signature[name].default      )

													for index, value in sorted_tuple_index_mapping:
																	SCREAMING_SNAKE_CASE  :						List[str]      					= prepared_for_class[value]

													SCREAMING_SNAKE_CASE  :						str      					= tuple(__UpperCAmelCase      )

													# Send to model
													SCREAMING_SNAKE_CASE  :						Optional[Any]      					= model(tuple_input[:-1]      )[0]

													self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )


	def 				lowercase__  (self : Optional[int]      )     ->			Dict:

					"""simple docstring"""

					(
					    (
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					
					)  :						List[str]      					= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase      )


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

					"""simple docstring"""

					(
					    (
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					
					)  :						Optional[Any]      					= self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									SCREAMING_SNAKE_CASE  :						List[str]      					= type
									self.model_tester.create_and_check_model(__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase      )


	def 				lowercase__  (self : Dict      )     ->			Optional[Any]:

					"""simple docstring"""

					(
					    (
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					
					)  :						List[Any]      					= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(
					    __UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase      )


	def 				lowercase__  (self : List[Any]      )     ->			str:

					"""simple docstring"""

					(
					    (
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					
					)  :						Any      					= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(
					    __UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase      )


	def 				lowercase__  (self : List[str]      )     ->			Any:

					"""simple docstring"""

					(
					    (
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					
					)  :						Union[str, Any]      					= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(
					    __UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase      )






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

					"""simple docstring"""

					for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= TFLayoutLMvaModel.from_pretrained(__UpperCAmelCase      )
									self.assertIsNotNone(__UpperCAmelCase      )






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




@require_tf
class 	a__     (				unittest.TestCase	):


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

					"""simple docstring"""

					return LayoutLMvaImageProcessor(apply_ocr=__UpperCAmelCase      ) if is_vision_available() else None






	@slow
	def 				lowercase__  (self : int      )     ->			Any:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''      )

					SCREAMING_SNAKE_CASE  :						List[str]      					= self.default_image_processor
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= prepare_img()
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= image_processor(images=__UpperCAmelCase,					return_tensors='''tf'''      ).pixel_values

					SCREAMING_SNAKE_CASE  :						Dict      					= tf.constant([[1, 2]]      )
					SCREAMING_SNAKE_CASE  :						List[str]      					= tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]      ),					axis=0      )

					# forward pass
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= model(input_ids=__UpperCAmelCase,					bbox=__UpperCAmelCase,					pixel_values=__UpperCAmelCase,					training=__UpperCAmelCase      )

					# verify the logits
					SCREAMING_SNAKE_CASE  :						str      					= (1, 199, 768)
					self.assertEqual(outputs.last_hidden_state.shape,					__UpperCAmelCase      )

					SCREAMING_SNAKE_CASE  :						List[Any]      					= tf.constant(
					    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]      )

					self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3],					__UpperCAmelCase,					atol=1e-4      )      )









'''simple docstring'''

from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case_			=     logging.get_logger(__name__)

snake_case_			=     {
    """microsoft/markuplm-base""": """https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json""",
    """microsoft/markuplm-large""": """https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json""",
}


class 	a__     (				_lowercase	):
	__magic_name__		:    List[Any]	    =     "markuplm"


	def __init__(self : List[Any],					__UpperCAmelCase : int=30522,					__UpperCAmelCase : Tuple=768,					__UpperCAmelCase : str=12,					__UpperCAmelCase : List[Any]=12,					__UpperCAmelCase : Tuple=3072,					__UpperCAmelCase : Union[str, Any]="gelu",					__UpperCAmelCase : Union[str, Any]=0.1,					__UpperCAmelCase : str=0.1,					__UpperCAmelCase : Union[str, Any]=512,					__UpperCAmelCase : Dict=2,					__UpperCAmelCase : int=0.02,					__UpperCAmelCase : Optional[Any]=1e-12,					__UpperCAmelCase : Union[str, Any]=0,					__UpperCAmelCase : Optional[Any]=0,					__UpperCAmelCase : Union[str, Any]=2,					__UpperCAmelCase : int=256,					__UpperCAmelCase : Tuple=1024,					__UpperCAmelCase : Optional[Any]=216,					__UpperCAmelCase : str=1001,					__UpperCAmelCase : Optional[Any]=32,					__UpperCAmelCase : Dict=50,					__UpperCAmelCase : Union[str, Any]="absolute",					__UpperCAmelCase : Tuple=True,					__UpperCAmelCase : List[str]=None,					**__UpperCAmelCase : Union[str, Any],					)     ->			Union[str, Any]:

					"""simple docstring"""

					super().__init__(
					    pad_token_id=__UpperCAmelCase,					bos_token_id=__UpperCAmelCase,					eos_token_id=__UpperCAmelCase,					**__UpperCAmelCase,					)
					SCREAMING_SNAKE_CASE  :						List[Any]      					= vocab_size
					SCREAMING_SNAKE_CASE  :						Dict      					= hidden_size
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= num_hidden_layers
					SCREAMING_SNAKE_CASE  :						Dict      					= num_attention_heads
					SCREAMING_SNAKE_CASE  :						Any      					= hidden_act
					SCREAMING_SNAKE_CASE  :						Tuple      					= intermediate_size
					SCREAMING_SNAKE_CASE  :						Tuple      					= hidden_dropout_prob
					SCREAMING_SNAKE_CASE  :						List[str]      					= attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE  :						List[str]      					= max_position_embeddings
					SCREAMING_SNAKE_CASE  :						Dict      					= type_vocab_size
					SCREAMING_SNAKE_CASE  :						Tuple      					= initializer_range
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= layer_norm_eps
					SCREAMING_SNAKE_CASE  :						int      					= position_embedding_type
					SCREAMING_SNAKE_CASE  :						List[Any]      					= use_cache
					SCREAMING_SNAKE_CASE  :						int      					= classifier_dropout
					# additional properties
					SCREAMING_SNAKE_CASE  :						List[Any]      					= max_depth
					SCREAMING_SNAKE_CASE  :						Any      					= max_xpath_tag_unit_embeddings
					SCREAMING_SNAKE_CASE  :						int      					= max_xpath_subs_unit_embeddings
					SCREAMING_SNAKE_CASE  :						Dict      					= tag_pad_id
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= subs_pad_id
					SCREAMING_SNAKE_CASE  :						int      					= xpath_unit_hidden_size




