


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






				'''simple docstring'''



				def __init__(       self			:Tuple			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:List[Any]=2			,			lowerCAmelCase__			:List[Any]=3			,			lowerCAmelCase__			:Any=4			,			lowerCAmelCase__			:Optional[Any]=2			,			lowerCAmelCase__			:Dict=7			,			lowerCAmelCase__			:Optional[Any]=True			,			lowerCAmelCase__			:Tuple=True			,			lowerCAmelCase__			:List[Any]=True			,			lowerCAmelCase__			:List[str]=True			,			lowerCAmelCase__			:str=99			,			lowerCAmelCase__			:Optional[Any]=36			,			lowerCAmelCase__			:Optional[int]=2			,			lowerCAmelCase__			:int=4			,			lowerCAmelCase__			:List[str]=37			,			lowerCAmelCase__			:Dict="gelu"			,			lowerCAmelCase__			:List[Any]=0.1			,			lowerCAmelCase__			:Optional[Any]=0.1			,			lowerCAmelCase__			:Optional[Any]=512			,			lowerCAmelCase__			:List[str]=16			,			lowerCAmelCase__			:Union[str, Any]=2			,			lowerCAmelCase__			:str=0.02			,			lowerCAmelCase__			:Optional[Any]=6			,			lowerCAmelCase__			:int=6			,			lowerCAmelCase__			:Union[str, Any]=3			,			lowerCAmelCase__			:Union[str, Any]=4			,			lowerCAmelCase__			:Union[str, Any]=None			,			lowerCAmelCase__			:List[Any]=1_000			,			)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		parent
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		batch_size
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		num_channels
										__SCREAMING_SNAKE_CASE						:     str					=		image_size
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		patch_size
										__SCREAMING_SNAKE_CASE						:     Any					=		is_training
										__SCREAMING_SNAKE_CASE						:     Dict					=		use_input_mask
										__SCREAMING_SNAKE_CASE						:     Dict					=		use_token_type_ids
										__SCREAMING_SNAKE_CASE						:     int					=		use_labels
										__SCREAMING_SNAKE_CASE						:     Dict					=		vocab_size
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		hidden_size
										__SCREAMING_SNAKE_CASE						:     List[str]					=		num_hidden_layers
										__SCREAMING_SNAKE_CASE						:     List[str]					=		num_attention_heads
										__SCREAMING_SNAKE_CASE						:     List[str]					=		intermediate_size
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		hidden_act
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		hidden_dropout_prob
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		attention_probs_dropout_prob
										__SCREAMING_SNAKE_CASE						:     Any					=		max_position_embeddings
										__SCREAMING_SNAKE_CASE						:     str					=		type_vocab_size
										__SCREAMING_SNAKE_CASE						:     int					=		type_sequence_label_size
										__SCREAMING_SNAKE_CASE						:     Any					=		initializer_range
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		coordinate_size
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		shape_size
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		num_labels
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		num_choices
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		scope
										__SCREAMING_SNAKE_CASE						:     str					=		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						:     Optional[Any]					=		text_seq_length
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		(image_size // patch_size) ** 2 + 1
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		self.text_seq_length + self.image_seq_length


				def 		__magic_name__(       self			:List[Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     str					=		ids_tensor([self.batch_size, self.text_seq_length]			,			self.vocab_size	)

										__SCREAMING_SNAKE_CASE						:     Dict					=		ids_tensor([self.batch_size, self.text_seq_length, 4]			,			self.range_bbox	)
										__SCREAMING_SNAKE_CASE						:     Tuple					=		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						:     List[str]					=		bbox[i, j, 3]
																												__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		bbox[i, j, 1]
																												__SCREAMING_SNAKE_CASE						:     List[Any]					=		tmp_coordinate
																						if bbox[i, j, 2] < bbox[i, j, 0]:
																												__SCREAMING_SNAKE_CASE						:     Tuple					=		bbox[i, j, 2]
																												__SCREAMING_SNAKE_CASE						:     Tuple					=		bbox[i, j, 0]
																												__SCREAMING_SNAKE_CASE						:     str					=		tmp_coordinate
										__SCREAMING_SNAKE_CASE						:     str					=		tf.constant(lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

										__SCREAMING_SNAKE_CASE						:     str					=		None
										if self.use_input_mask:
																__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		random_attention_mask([self.batch_size, self.text_seq_length]	)

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		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						:     int					=		None
										__SCREAMING_SNAKE_CASE						:     Tuple					=		None
										if self.use_labels:
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		ids_tensor([self.batch_size]			,			self.type_sequence_label_size	)
																__SCREAMING_SNAKE_CASE						:     int					=		ids_tensor([self.batch_size, self.text_seq_length]			,			self.num_labels	)

										__SCREAMING_SNAKE_CASE						:     str					=		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 		__magic_name__(       self			:str			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Tuple			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Any	)		->			List[str]:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		TFLayoutLMvaModel(config=lowerCAmelCase__	)

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

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

										# text only
										__SCREAMING_SNAKE_CASE						:     int					=		model(lowerCAmelCase__			,			training=lowerCAmelCase__	)
										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=lowerCAmelCase__	)
										self.parent.assertEqual(
										    result.last_hidden_state.shape			,			(self.batch_size, self.image_seq_length, self.hidden_size)	)


				def 		__magic_name__(       self			:str			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:Optional[Any]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:Optional[Any]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Any					=		self.num_labels
										__SCREAMING_SNAKE_CASE						:     List[str]					=		TFLayoutLMvaForSequenceClassification(config=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		model(
										    lowerCAmelCase__			,			bbox=lowerCAmelCase__			,			pixel_values=lowerCAmelCase__			,			attention_mask=lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			labels=lowerCAmelCase__			,			training=lowerCAmelCase__			,			)
										self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_labels)	)


				def 		__magic_name__(       self			:List[str]			,			lowerCAmelCase__			:Optional[int]			,			lowerCAmelCase__			:Any			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Optional[Any]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Union[str, Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		self.num_labels
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		TFLayoutLMvaForTokenClassification(config=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		model(
										    lowerCAmelCase__			,			bbox=lowerCAmelCase__			,			pixel_values=lowerCAmelCase__			,			attention_mask=lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			labels=lowerCAmelCase__			,			training=lowerCAmelCase__			,			)
										self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.text_seq_length, self.num_labels)	)


				def 		__magic_name__(       self			:Dict			,			lowerCAmelCase__			:Optional[int]			,			lowerCAmelCase__			:Optional[int]			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Any			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:List[str]	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     int					=		2
										__SCREAMING_SNAKE_CASE						:     str					=		TFLayoutLMvaForQuestionAnswering(config=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Tuple					=		model(
										    lowerCAmelCase__			,			bbox=lowerCAmelCase__			,			pixel_values=lowerCAmelCase__			,			attention_mask=lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			start_positions=lowerCAmelCase__			,			end_positions=lowerCAmelCase__			,			training=lowerCAmelCase__			,			)
										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 		__magic_name__(       self			:Optional[int]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     str					=		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))						:     Dict					=		config_and_inputs
										__SCREAMING_SNAKE_CASE						:     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        _lowercase      (			A__						,			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			List[Any]						=		(
				    (
				        TFLayoutLMvaModel,
				        TFLayoutLMvaForQuestionAnswering,
				        TFLayoutLMvaForSequenceClassification,
				        TFLayoutLMvaForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE__				:			int						=		(
				    {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
				    if is_tf_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE__				:			Optional[int]						=		False
				SCREAMING_SNAKE_CASE__				:			int						=		False
				SCREAMING_SNAKE_CASE__				:			Optional[int]						=		False


				def 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:Optional[int]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:str	)		->			Any:
										return True


				def 		__magic_name__(       self			:str			,			lowerCAmelCase__			:Tuple			,			lowerCAmelCase__			:Tuple			,			lowerCAmelCase__			:Optional[Any]=False	)		->			dict:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		copy.deepcopy(lowerCAmelCase__	)

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

										if return_labels:
																if model_class in get_values(lowerCAmelCase__	):
																						__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		tf.ones(self.model_tester.batch_size			,			dtype=tf.intaa	)
																elif model_class in get_values(lowerCAmelCase__	):
																						__SCREAMING_SNAKE_CASE						:     Dict					=		tf.zeros(self.model_tester.batch_size			,			dtype=tf.intaa	)
																						__SCREAMING_SNAKE_CASE						:     int					=		tf.zeros(self.model_tester.batch_size			,			dtype=tf.intaa	)
																elif model_class in get_values(lowerCAmelCase__	):
																						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		tf.zeros(self.model_tester.batch_size			,			dtype=tf.intaa	)
																elif model_class in get_values(lowerCAmelCase__	):
																						__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		tf.zeros(
																						    (self.model_tester.batch_size, self.model_tester.text_seq_length)			,			dtype=tf.intaa	)

										return inputs_dict


				def 		__magic_name__(       self			:Tuple	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		TFLayoutLMvaModelTester(self	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		ConfigTester(self			,			config_class=lowerCAmelCase__			,			hidden_size=37	)


				def 		__magic_name__(       self			:str	)		->			Optional[int]:
										self.config_tester.run_common_tests()


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

																						# Test that model correctly compute the loss with kwargs
																						__SCREAMING_SNAKE_CASE						:     Tuple					=		self._prepare_for_class(inputs_dict.copy()			,			lowerCAmelCase__			,			return_labels=lowerCAmelCase__	)
																						__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		prepared_for_class.pop('''input_ids'''	)

																						__SCREAMING_SNAKE_CASE						:     List[Any]					=		model(lowerCAmelCase__			,			**lowerCAmelCase__	)[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()			,			lowerCAmelCase__			,			return_labels=lowerCAmelCase__	)
																						__SCREAMING_SNAKE_CASE						:     int					=		prepared_for_class.pop('''input_ids'''	)
																						if "labels" in prepared_for_class:
																												__SCREAMING_SNAKE_CASE						:     Tuple					=		prepared_for_class['''labels'''].numpy()
																												if len(labels.shape	) > 1 and labels.shape[1] != 1:
																																		__SCREAMING_SNAKE_CASE						:     Optional[int]					=		-100
																																		__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		tf.convert_to_tensor(lowerCAmelCase__	)
																																		__SCREAMING_SNAKE_CASE						:     Optional[int]					=		model(lowerCAmelCase__			,			**lowerCAmelCase__	)[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						:     int					=		self._prepare_for_class(inputs_dict.copy()			,			lowerCAmelCase__			,			return_labels=lowerCAmelCase__	)
																						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		model(lowerCAmelCase__	)[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						:     Any					=		self._prepare_for_class(inputs_dict.copy()			,			lowerCAmelCase__			,			return_labels=lowerCAmelCase__	)

																						# Get keys that were added with the _prepare_for_class function
																						__SCREAMING_SNAKE_CASE						:     List[str]					=		prepared_for_class.keys() - inputs_dict.keys()
																						__SCREAMING_SNAKE_CASE						:     str					=		inspect.signature(model.call	).parameters
																						__SCREAMING_SNAKE_CASE						:     Optional[int]					=		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						:     Optional[int]					=		signature_names.index(lowerCAmelCase__	)
																												__SCREAMING_SNAKE_CASE						:     Dict					=		label_key
																						__SCREAMING_SNAKE_CASE						:     List[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						:     Union[str, Any]					=		prepared_for_class[value]

																						__SCREAMING_SNAKE_CASE						:     Dict					=		tuple(lowerCAmelCase__	)

																						# Send to model
																						__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		model(tuple_input[:-1]	)[0]

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


				def 		__magic_name__(       self			:Optional[Any]	)		->			Tuple:
										(
										    (
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			
										)						:     int					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__	)


				def 		__magic_name__(       self			:Optional[int]	)		->			Tuple:
										(
										    (
										    __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()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																__SCREAMING_SNAKE_CASE						:     Any					=		type
																self.model_tester.create_and_check_model(lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__	)


				def 		__magic_name__(       self			:str	)		->			Union[str, Any]:
										(
										    (
										    __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_for_sequence_classification(
										    lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__	)


				def 		__magic_name__(       self			:str	)		->			Dict:
										(
										    (
										    __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[int]					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(
										    lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__	)


				def 		__magic_name__(       self			:Optional[Any]	)		->			Any:
										(
										    (
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			
										)						:     int					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(
										    lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__	)



				@slow
				def 		__magic_name__(       self			:int	)		->			Optional[Any]:
										for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__SCREAMING_SNAKE_CASE						:     List[Any]					=		TFLayoutLMvaModel.from_pretrained(lowerCAmelCase__	)
																self.assertIsNotNone(lowerCAmelCase__	)




def        _UpperCamelCase							(      ):
						__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
						return image
@require_tf
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				@cached_property
				def 		__magic_name__(       self			:Tuple	)		->			Optional[int]:
										return LayoutLMvaImageProcessor(apply_ocr=lowerCAmelCase__	) if is_vision_available() else None



				@slow
				def 		__magic_name__(       self			:Tuple	)		->			int:
										__SCREAMING_SNAKE_CASE						:     Any					=		TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''	)

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		self.default_image_processor
										__SCREAMING_SNAKE_CASE						:     Dict					=		prepare_img()
										__SCREAMING_SNAKE_CASE						:     int					=		image_processor(images=lowerCAmelCase__			,			return_tensors='''tf'''	).pixel_values

										__SCREAMING_SNAKE_CASE						:     str					=		tf.constant([[1, 2]]	)
										__SCREAMING_SNAKE_CASE						:     int					=		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=lowerCAmelCase__			,			bbox=lowerCAmelCase__			,			pixel_values=lowerCAmelCase__			,			training=lowerCAmelCase__	)

										# verify the logits
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		(1, 199, 768)
										self.assertEqual(outputs.last_hidden_state.shape			,			lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Union[str, 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]			,			lowerCAmelCase__			,			atol=1E-4	)	)




from ..utils import DummyObject, requires_backends
class        _lowercase      (			metaclass=A__       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Optional[int]						=		['''keras_nlp''']


				def __init__(       self			:Tuple			,			*lowerCAmelCase__			:Optional[Any]			,			**lowerCAmelCase__			:Dict	)		->			Dict:
										requires_backends(self			,			['''keras_nlp''']	)
