


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




			def __init__(						self							:		int       ,       __magic_name__							:		List[Any]       ,       __magic_name__							:		str=2       ,       __magic_name__							:		Union[str, Any]=3       ,       __magic_name__							:		Union[str, Any]=4       ,       __magic_name__							:		List[str]=2       ,       __magic_name__							:		Dict=7       ,       __magic_name__							:		Tuple=True       ,       __magic_name__							:		Optional[Any]=True       ,       __magic_name__							:		int=True       ,       __magic_name__							:		List[Any]=True       ,       __magic_name__							:		Tuple=9_9       ,       __magic_name__							:		Optional[int]=3_6       ,       __magic_name__							:		Tuple=2       ,       __magic_name__							:		Tuple=4       ,       __magic_name__							:		Dict=3_7       ,       __magic_name__							:		Optional[Any]="gelu"       ,       __magic_name__							:		Tuple=0.1       ,       __magic_name__							:		List[Any]=0.1       ,       __magic_name__							:		Optional[int]=5_1_2       ,       __magic_name__							:		Optional[int]=1_6       ,       __magic_name__							:		int=2       ,       __magic_name__							:		str=0.02       ,       __magic_name__							:		Tuple=6       ,       __magic_name__							:		Tuple=6       ,       __magic_name__							:		List[Any]=3       ,       __magic_name__							:		Union[str, Any]=4       ,       __magic_name__							:		Tuple=None       ,       __magic_name__							:		List[Any]=1_0_0_0       ,       ):







						"""simple docstring"""







						UpperCamelCase         =					parent
						UpperCamelCase         =					batch_size
						UpperCamelCase         =					num_channels
						UpperCamelCase         =					image_size
						UpperCamelCase         =					patch_size
						UpperCamelCase         =					is_training
						UpperCamelCase         =					use_input_mask
						UpperCamelCase         =					use_token_type_ids
						UpperCamelCase         =					use_labels
						UpperCamelCase         =					vocab_size
						UpperCamelCase         =					hidden_size
						UpperCamelCase         =					num_hidden_layers
						UpperCamelCase         =					num_attention_heads
						UpperCamelCase         =					intermediate_size
						UpperCamelCase         =					hidden_act
						UpperCamelCase         =					hidden_dropout_prob
						UpperCamelCase         =					attention_probs_dropout_prob
						UpperCamelCase         =					max_position_embeddings
						UpperCamelCase         =					type_vocab_size
						UpperCamelCase         =					type_sequence_label_size
						UpperCamelCase         =					initializer_range
						UpperCamelCase         =					coordinate_size
						UpperCamelCase         =					shape_size
						UpperCamelCase         =					num_labels
						UpperCamelCase         =					num_choices
						UpperCamelCase         =					scope
						UpperCamelCase         =					range_bbox

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




			def   lowerCamelCase_					(						self							:		str						):







						"""simple docstring"""







						UpperCamelCase         =					ids_tensor([self.batch_size, self.text_seq_length]       ,       self.vocab_size						)

						UpperCamelCase         =					ids_tensor([self.batch_size, self.text_seq_length, 4]       ,       self.range_bbox						)
						UpperCamelCase         =					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]:
															UpperCamelCase         =					bbox[i, j, 3]
															UpperCamelCase         =					bbox[i, j, 1]
															UpperCamelCase         =					tmp_coordinate
												if bbox[i, j, 2] < bbox[i, j, 0]:
															UpperCamelCase         =					bbox[i, j, 2]
															UpperCamelCase         =					bbox[i, j, 0]
															UpperCamelCase         =					tmp_coordinate
						UpperCamelCase         =					tf.constant(UpperCAmelCase_						)

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

						UpperCamelCase         =					None
						if self.use_input_mask:
									UpperCamelCase         =					random_attention_mask([self.batch_size, self.text_seq_length]						)

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

						UpperCamelCase         =					None
						UpperCamelCase         =					None
						if self.use_labels:
									UpperCamelCase         =					ids_tensor([self.batch_size]       ,       self.type_sequence_label_size						)
									UpperCamelCase         =					ids_tensor([self.batch_size, self.text_seq_length]       ,       self.num_labels						)

						UpperCamelCase         =					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   lowerCamelCase_					(						self							:		int       ,       __magic_name__							:		int       ,       __magic_name__							:		Union[str, Any]       ,       __magic_name__							:		Optional[Any]       ,       __magic_name__							:		Tuple       ,       __magic_name__							:		Tuple       ,       __magic_name__							:		Optional[Any]						):







						"""simple docstring"""







						UpperCamelCase         =					TFLayoutLMvaModel(config=UpperCAmelCase_						)

						# text + image
						UpperCamelCase         =					model(UpperCAmelCase_       ,       pixel_values=UpperCAmelCase_       ,       training=UpperCAmelCase_						)
						UpperCamelCase         =					model(
						    UpperCAmelCase_       ,       bbox=UpperCAmelCase_       ,       pixel_values=UpperCAmelCase_       ,       attention_mask=UpperCAmelCase_       ,       token_type_ids=UpperCAmelCase_       ,       training=UpperCAmelCase_       ,       )
						UpperCamelCase         =					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
						UpperCamelCase         =					model(UpperCAmelCase_       ,       training=UpperCAmelCase_						)
						self.parent.assertEqual(
						    result.last_hidden_state.shape       ,       (self.batch_size, self.text_seq_length, self.hidden_size)						)

						# image only
						UpperCamelCase         =					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   lowerCamelCase_					(						self							:		Union[str, Any]       ,       __magic_name__							:		List[str]       ,       __magic_name__							:		Optional[int]       ,       __magic_name__							:		Union[str, Any]       ,       __magic_name__							:		List[Any]       ,       __magic_name__							:		List[str]       ,       __magic_name__							:		str       ,       __magic_name__							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.num_labels
						UpperCamelCase         =					TFLayoutLMvaForSequenceClassification(config=UpperCAmelCase_						)
						UpperCamelCase         =					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   lowerCamelCase_					(						self							:		str       ,       __magic_name__							:		Tuple       ,       __magic_name__							:		str       ,       __magic_name__							:		Tuple       ,       __magic_name__							:		Optional[Any]       ,       __magic_name__							:		str       ,       __magic_name__							:		List[str]       ,       __magic_name__							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.num_labels
						UpperCamelCase         =					TFLayoutLMvaForTokenClassification(config=UpperCAmelCase_						)
						UpperCamelCase         =					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   lowerCamelCase_					(						self							:		int       ,       __magic_name__							:		Any       ,       __magic_name__							:		Dict       ,       __magic_name__							:		Tuple       ,       __magic_name__							:		Tuple       ,       __magic_name__							:		int       ,       __magic_name__							:		int       ,       __magic_name__							:		int						):







						"""simple docstring"""







						UpperCamelCase         =					2
						UpperCamelCase         =					TFLayoutLMvaForQuestionAnswering(config=UpperCAmelCase_						)
						UpperCamelCase         =					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   lowerCamelCase_					(						self							:		Union[str, Any]						):







						"""simple docstring"""







						UpperCamelCase         =					self.prepare_config_and_inputs()
						((UpperCamelCase)  ,  (UpperCamelCase)  ,  (UpperCamelCase)  ,  (UpperCamelCase)  ,  (UpperCamelCase)  ,  (UpperCamelCase)  ,  (UpperCamelCase)  ,  (UpperCamelCase))         =					config_and_inputs
						UpperCamelCase         =					{
						    """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    UpperCAmelCase					(       snake_case__      , snake_case__      , unittest.TestCase    ):
			lowercase						= (
			    (
			        TFLayoutLMvaModel,
			        TFLayoutLMvaForQuestionAnswering,
			        TFLayoutLMvaForSequenceClassification,
			        TFLayoutLMvaForTokenClassification,
			    )
			    if is_tf_available()
			    else ()
			)
			lowercase						= (
			    {"""document-question-answering""": TFLayoutLMvaForQuestionAnswering, """feature-extraction""": TFLayoutLMvaModel}
			    if is_tf_available()
			    else {}
			)

			lowercase						= False
			lowercase						= False
			lowercase						= False




			def   lowerCamelCase_					(						self							:		int       ,       __magic_name__							:		int       ,       __magic_name__							:		Optional[Any]       ,       __magic_name__							:		Dict       ,       __magic_name__							:		Optional[int]       ,       __magic_name__							:		Dict						):







						"""simple docstring"""







						return True




			def   lowerCamelCase_					(						self							:		Optional[Any]       ,       __magic_name__							:		Optional[Any]       ,       __magic_name__							:		int       ,       __magic_name__							:		List[str]=False						):







						"""simple docstring"""







						UpperCamelCase         =					copy.deepcopy(UpperCAmelCase_						)

						if model_class in get_values(UpperCAmelCase_						):
									UpperCamelCase         =					{
									    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_						):
												UpperCamelCase         =					tf.ones(self.model_tester.batch_size       ,       dtype=tf.intaa						)
									elif model_class in get_values(UpperCAmelCase_						):
												UpperCamelCase         =					tf.zeros(self.model_tester.batch_size       ,       dtype=tf.intaa						)
												UpperCamelCase         =					tf.zeros(self.model_tester.batch_size       ,       dtype=tf.intaa						)
									elif model_class in get_values(UpperCAmelCase_						):
												UpperCamelCase         =					tf.zeros(self.model_tester.batch_size       ,       dtype=tf.intaa						)
									elif model_class in get_values(UpperCAmelCase_						):
												UpperCamelCase         =					tf.zeros(
												    (self.model_tester.batch_size, self.model_tester.text_seq_length)       ,       dtype=tf.intaa						)

						return inputs_dict




			def   lowerCamelCase_					(						self							:		List[str]						):







						"""simple docstring"""







						UpperCamelCase         =					TFLayoutLMvaModelTester(self						)
						UpperCamelCase         =					ConfigTester(self       ,       config_class=UpperCAmelCase_       ,       hidden_size=3_7						)




			def   lowerCamelCase_					(						self							:		str						):







						"""simple docstring"""







						self.config_tester.run_common_tests()




			def   lowerCamelCase_					(						self							:		Tuple						):







						"""simple docstring"""







						UpperCamelCase  ,  UpperCamelCase         =					self.model_tester.prepare_config_and_inputs_for_common()
						for model_class in self.all_model_classes:
									UpperCamelCase         =					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
												UpperCamelCase         =					self._prepare_for_class(inputs_dict.copy()       ,       UpperCAmelCase_       ,       return_labels=UpperCAmelCase_						)
												UpperCamelCase         =					prepared_for_class[
												    sorted(prepared_for_class.keys() - inputs_dict.keys()       ,       reverse=UpperCAmelCase_						)[0]
												]
												UpperCamelCase         =					added_label.shape.as_list()[:1]

												# Test that model correctly compute the loss with kwargs
												UpperCamelCase         =					self._prepare_for_class(inputs_dict.copy()       ,       UpperCAmelCase_       ,       return_labels=UpperCAmelCase_						)
												UpperCamelCase         =					prepared_for_class.pop("""input_ids"""						)

												UpperCamelCase         =					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
												UpperCamelCase         =					self._prepare_for_class(inputs_dict.copy()       ,       UpperCAmelCase_       ,       return_labels=UpperCAmelCase_						)
												UpperCamelCase         =					prepared_for_class.pop("""input_ids"""						)
												if "labels" in prepared_for_class:
															UpperCamelCase         =					prepared_for_class["""labels"""].numpy()
															if len(labels.shape						) > 1 and labels.shape[1] != 1:
																		UpperCamelCase         =					-1_0_0
																		UpperCamelCase         =					tf.convert_to_tensor(UpperCAmelCase_						)
																		UpperCamelCase         =					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
												UpperCamelCase         =					self._prepare_for_class(inputs_dict.copy()       ,       UpperCAmelCase_       ,       return_labels=UpperCAmelCase_						)
												UpperCamelCase         =					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
												UpperCamelCase         =					self._prepare_for_class(inputs_dict.copy()       ,       UpperCAmelCase_       ,       return_labels=UpperCAmelCase_						)

												# Get keys that were added with the _prepare_for_class function
												UpperCamelCase         =					prepared_for_class.keys() - inputs_dict.keys()
												UpperCamelCase         =					inspect.signature(model.call						).parameters
												UpperCamelCase         =					list(signature.keys()						)

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

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

												for index, value in sorted_tuple_index_mapping:
															UpperCamelCase         =					prepared_for_class[value]

												UpperCamelCase         =					tuple(UpperCAmelCase_						)

												# Send to model
												UpperCamelCase         =					model(tuple_input[:-1]						)[0]

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




			def   lowerCamelCase_					(						self							:		Any						):







						"""simple docstring"""







						(
						    (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  						
						)         =					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_						)




			def   lowerCamelCase_					(						self							:		Tuple						):







						"""simple docstring"""







						(
						    (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  						
						)         =					self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
									UpperCamelCase         =					type
									self.model_tester.create_and_check_model(UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_						)




			def   lowerCamelCase_					(						self							:		Optional[int]						):







						"""simple docstring"""







						(
						    (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  						
						)         =					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(
						    UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_						)




			def   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







						(
						    (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  						
						)         =					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(
						    UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_       ,       UpperCAmelCase_						)




			def   lowerCamelCase_					(						self							:		Union[str, Any]						):







						"""simple docstring"""







						(
						    (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  (
						    UpperCamelCase						
						)  ,  						
						)         =					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   lowerCamelCase_					(						self							:		Optional[Any]						):







						"""simple docstring"""







						for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									UpperCamelCase         =					TFLayoutLMvaModel.from_pretrained(UpperCAmelCase_						)
									self.assertIsNotNone(UpperCAmelCase_						)






def 							_lowercase    (		):





			"""simple docstring"""




			UpperCamelCase         =					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
			return image




@require_tf
class    UpperCAmelCase					(       unittest.TestCase    ):




			@cached_property
			def   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







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





			@slow
			def   lowerCamelCase_					(						self							:		List[Any]						):







						"""simple docstring"""







						UpperCamelCase         =					TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""						)

						UpperCamelCase         =					self.default_image_processor
						UpperCamelCase         =					prepare_img()
						UpperCamelCase         =					image_processor(images=UpperCAmelCase_       ,       return_tensors="""tf"""						).pixel_values

						UpperCamelCase         =					tf.constant([[1, 2]]						)
						UpperCamelCase         =					tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]						)       ,       axis=0						)

						# forward pass
						UpperCamelCase         =					model(input_ids=UpperCAmelCase_       ,       bbox=UpperCAmelCase_       ,       pixel_values=UpperCAmelCase_       ,       training=UpperCAmelCase_						)

						# verify the logits
						UpperCamelCase         =					(1, 1_9_9, 7_6_8)
						self.assertEqual(outputs.last_hidden_state.shape       ,       UpperCAmelCase_						)

						UpperCamelCase         =					tf.constant(
						    [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]						)

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








import torch

from transformers import AutoModel




class    UpperCAmelCase					(       torch.nn.Module    ):




			def __init__(						self							:		int       ,       __magic_name__							:		List[Any]="sayef/fsner-bert-base-uncased"						):







						"""simple docstring"""







						super(__magic_name__       ,       self						).__init__()

						UpperCamelCase         =					AutoModel.from_pretrained(__magic_name__       ,       return_dict=__magic_name__						)
						UpperCamelCase         =					torch.nn.CosineSimilarity(3       ,       1e-08						)
						UpperCamelCase         =					torch.nn.Softmax(dim=1						)




			def   lowerCamelCase_					(						self							:		Optional[int]       ,       **__magic_name__							:		List[Any]						):







						"""simple docstring"""







						return self.bert(**__magic_name__						).last_hidden_state




			def   lowerCamelCase_					(						self							:		Tuple       ,       __magic_name__							:		int						):







						"""simple docstring"""







						return token_embeddings.sum(2       ,       keepdim=__magic_name__						)




			def   lowerCamelCase_					(						self							:		Optional[Any]       ,       __magic_name__							:		List[str]       ,       __magic_name__							:		Union[str, Any]       ,       __magic_name__							:		Optional[Any]=1						):







						"""simple docstring"""







						return self.softmax(T * self.cos(__magic_name__       ,       __magic_name__						)						)





			def   lowerCamelCase_					(						self							:		Union[str, Any]       ,       __magic_name__							:		List[Any]       ,       __magic_name__							:		List[str]						):







						"""simple docstring"""







						UpperCamelCase         =					W_supports["""sizes"""].tolist()
						UpperCamelCase         =					W_supports["""start_token_id"""].item()
						UpperCamelCase         =					W_supports["""end_token_id"""].item()

						del W_supports["sizes"]
						del W_supports["start_token_id"]
						del W_supports["end_token_id"]

						UpperCamelCase         =					self.BERT(**__magic_name__						)
						UpperCamelCase         =					self.BERT(**__magic_name__						)

						UpperCamelCase         =					None
						UpperCamelCase         =					None

						UpperCamelCase         =					W_supports["""input_ids"""] == start_token_id
						UpperCamelCase         =					W_supports["""input_ids"""] == end_token_id

						for i, size in enumerate(__magic_name__						):
									if i == 0:
												UpperCamelCase         =					0
									else:
												UpperCamelCase         =					support_sizes[i - 1]

									UpperCamelCase         =					S[s : s + size][start_token_masks[s : s + size]]
									UpperCamelCase         =					S[s : s + size][end_token_masks[s : s + size]]

									UpperCamelCase         =					torch.matmul(q[i]       ,       s_start.T						).sum(1						).softmax(0						)
									UpperCamelCase         =					torch.matmul(q[i]       ,       s_end.T						).sum(1						).softmax(0						)

									if p_starts is not None:
												UpperCamelCase         =					torch.vstack((p_starts, p_start)						)
												UpperCamelCase         =					torch.vstack((p_ends, p_end)						)
									else:
												UpperCamelCase         =					p_start
												UpperCamelCase         =					p_end

						return p_starts, p_ends




