
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      :




	'''simple docstring'''





	def __init__(       self					: List[Any]							,A					: Optional[Any]							,A					: Optional[Any]=2							,A					: Dict=3							,A					: Union[str, Any]=4							,A					: str=2							,A					: Any=7							,A					: List[str]=True							,A					: List[str]=True							,A					: Optional[Any]=True							,A					: Optional[Any]=True							,A					: Optional[Any]=99							,A					: str=36							,A					: Optional[Any]=2							,A					: List[str]=4							,A					: Optional[Any]=37							,A					: int="gelu"							,A					: int=0.1							,A					: str=0.1							,A					: str=5_12							,A					: str=16							,A					: Tuple=2							,A					: Dict=0.02							,A					: str=6							,A					: Optional[Any]=6							,A					: Tuple=3							,A					: Optional[int]=4							,A					: int=None							,A					: Dict=10_00							,):
					__A   			= parent
					__A   			= batch_size
					__A   			= num_channels
					__A   			= image_size
					__A   			= patch_size
					__A   			= is_training
					__A   			= use_input_mask
					__A   			= use_token_type_ids
					__A   			= use_labels
					__A   			= vocab_size
					__A   			= hidden_size
					__A   			= num_hidden_layers
					__A   			= num_attention_heads
					__A   			= intermediate_size
					__A   			= hidden_act
					__A   			= hidden_dropout_prob
					__A   			= attention_probs_dropout_prob
					__A   			= max_position_embeddings
					__A   			= type_vocab_size
					__A   			= type_sequence_label_size
					__A   			= initializer_range
					__A   			= coordinate_size
					__A   			= shape_size
					__A   			= num_labels
					__A   			= num_choices
					__A   			= scope
					__A   			= range_bbox

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






	def 				UpperCamelCase_  (       self					: Dict ):
					__A   			= ids_tensor([self.batch_size, self.text_seq_length]							,self.vocab_size )

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

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

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

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

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

					__A   			= 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 				UpperCamelCase_  (       self					: Any							,A					: Optional[Any]							,A					: List[str]							,A					: Optional[Any]							,A					: Any							,A					: Optional[int]							,A					: Union[str, Any] ):
					__A   			= TFLayoutLMvaModel(config=A )

					# text + image
					__A   			= model(A							,pixel_values=A							,training=A )
					__A   			= model(
					    A							,bbox=A							,pixel_values=A							,attention_mask=A							,token_type_ids=A							,training=A							,)
					__A   			= model(A							,bbox=A							,pixel_values=A							,training=A )

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

					# text only
					__A   			= model(A							,training=A )
					self.parent.assertEqual(
					    result.last_hidden_state.shape							,(self.batch_size, self.text_seq_length, self.hidden_size) )

					# image only
					__A   			= model({"pixel_values": pixel_values}							,training=A )
					self.parent.assertEqual(
					    result.last_hidden_state.shape							,(self.batch_size, self.image_seq_length, self.hidden_size) )






	def 				UpperCamelCase_  (       self					: Union[str, Any]							,A					: Tuple							,A					: List[Any]							,A					: Tuple							,A					: List[str]							,A					: Tuple							,A					: Union[str, Any]							,A					: Tuple ):
					__A   			= self.num_labels
					__A   			= TFLayoutLMvaForSequenceClassification(config=A )
					__A   			= model(
					    A							,bbox=A							,pixel_values=A							,attention_mask=A							,token_type_ids=A							,labels=A							,training=A							,)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_labels) )






	def 				UpperCamelCase_  (       self					: int							,A					: Dict							,A					: str							,A					: Tuple							,A					: Optional[Any]							,A					: int							,A					: Any							,A					: Any ):
					__A   			= self.num_labels
					__A   			= TFLayoutLMvaForTokenClassification(config=A )
					__A   			= model(
					    A							,bbox=A							,pixel_values=A							,attention_mask=A							,token_type_ids=A							,labels=A							,training=A							,)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.text_seq_length, self.num_labels) )






	def 				UpperCamelCase_  (       self					: Union[str, Any]							,A					: int							,A					: Tuple							,A					: Optional[Any]							,A					: List[Any]							,A					: List[Any]							,A					: str							,A					: Tuple ):
					__A   			= 2
					__A   			= TFLayoutLMvaForQuestionAnswering(config=A )
					__A   			= model(
					    A							,bbox=A							,pixel_values=A							,attention_mask=A							,token_type_ids=A							,start_positions=A							,end_positions=A							,training=A							,)
					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 				UpperCamelCase_  (       self					: int ):
					__A   			= self.prepare_config_and_inputs()
					((__A)			,							(__A)			,							(__A)			,							(__A)			,							(__A)			,							(__A)			,							(__A)			,							(__A))   			= config_and_inputs
					__A   			= {
					    "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      (  __SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			unittest.TestCase  ):




	'''simple docstring'''





	snake_case_					    =     (
	    (
	        TFLayoutLMvaModel,
	        TFLayoutLMvaForQuestionAnswering,
	        TFLayoutLMvaForSequenceClassification,
	        TFLayoutLMvaForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	snake_case_					    =     (
	    {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel}
	    if is_tf_available()
	    else {}
	)

	snake_case_					    =     False
	snake_case_					    =     False
	snake_case_					    =     False






	def 				UpperCamelCase_  (       self					: int							,A					: Optional[Any]							,A					: List[str]							,A					: Optional[int]							,A					: str							,A					: Tuple ):
					return True






	def 				UpperCamelCase_  (       self					: Tuple							,A					: int							,A					: List[Any]							,A					: str=False ):
					__A   			= copy.deepcopy(A )

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

					if return_labels:
									if model_class in get_values(A ):
													__A   			= tf.ones(self.model_tester.batch_size							,dtype=tf.intaa )
									elif model_class in get_values(A ):
													__A   			= tf.zeros(self.model_tester.batch_size							,dtype=tf.intaa )
													__A   			= tf.zeros(self.model_tester.batch_size							,dtype=tf.intaa )
									elif model_class in get_values(A ):
													__A   			= tf.zeros(self.model_tester.batch_size							,dtype=tf.intaa )
									elif model_class in get_values(A ):
													__A   			= tf.zeros(
													    (self.model_tester.batch_size, self.model_tester.text_seq_length)							,dtype=tf.intaa )

					return inputs_dict






	def 				UpperCamelCase_  (       self					: str ):
					__A   			= TFLayoutLMvaModelTester(self )
					__A   			= ConfigTester(self							,config_class=A							,hidden_size=37 )






	def 				UpperCamelCase_  (       self					: Union[str, Any] ):
					self.config_tester.run_common_tests()






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

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

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

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

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

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

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

													__A   			= tuple(A )

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

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






	def 				UpperCamelCase_  (       self					: Tuple ):
					(
					    (
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							
					)   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(A							,A							,A							,A							,A							,A )






	def 				UpperCamelCase_  (       self					: List[Any] ):
					(
					    (
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							
					)   			= self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									__A   			= type
									self.model_tester.create_and_check_model(A							,A							,A							,A							,A							,A )






	def 				UpperCamelCase_  (       self					: int ):
					(
					    (
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							
					)   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(
					    A							,A							,A							,A							,A							,A							,A )






	def 				UpperCamelCase_  (       self					: Dict ):
					(
					    (
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							
					)   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(
					    A							,A							,A							,A							,A							,A							,A )






	def 				UpperCamelCase_  (       self					: List[Any] ):
					(
					    (
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							(
					    __A
					)			,							
					)   			= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(
					    A							,A							,A							,A							,A							,A							,A )







	@slow
	def 				UpperCamelCase_  (       self					: str ):
					for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A   			= TFLayoutLMvaModel.from_pretrained(A )
									self.assertIsNotNone(A )





def 						UpperCAmelCase			(	)		->	Any:







				"""simple docstring"""



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





@require_tf
class 							UpperCAmelCase      (  unittest.TestCase  ):




	'''simple docstring'''





	@cached_property
	def 				UpperCamelCase_  (       self					: Dict ):
					return LayoutLMvaImageProcessor(apply_ocr=A ) if is_vision_available() else None







	@slow
	def 				UpperCamelCase_  (       self					: str ):
					__A   			= TFLayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base" )

					__A   			= self.default_image_processor
					__A   			= prepare_img()
					__A   			= image_processor(images=A							,return_tensors="tf" ).pixel_values

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

					# forward pass
					__A   			= model(input_ids=A							,bbox=A							,pixel_values=A							,training=A )

					# verify the logits
					__A   			= (1, 1_99, 7_68)
					self.assertEqual(outputs.last_hidden_state.shape							,A )

					__A   			= tf.constant(
					    [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]] )

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


from collections import OrderedDict
from typing import Mapping

from packaging import version

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


SCREAMING_SNAKE_CASE							:Optional[Any]							 =				logging.get_logger(__name__)

SCREAMING_SNAKE_CASE							:List[Any]							 =				{
    'hustvl/yolos-small': 'https://huggingface.co/hustvl/yolos-small/resolve/main/config.json',
    # See all YOLOS models at https://huggingface.co/models?filter=yolos
}





class 							UpperCAmelCase      (  __SCREAMING_SNAKE_CASE  ):




	'''simple docstring'''





	snake_case_					    =     "yolos"






	def __init__(       self					: Any							,A					: Optional[Any]=7_68							,A					: Dict=12							,A					: Any=12							,A					: str=30_72							,A					: Any="gelu"							,A					: str=0.0							,A					: List[str]=0.0							,A					: Dict=0.02							,A					: int=1E-12							,A					: Tuple=[5_12, 8_64]							,A					: List[Any]=16							,A					: str=3							,A					: str=True							,A					: Any=1_00							,A					: Dict=True							,A					: Dict=False							,A					: Tuple=1							,A					: Union[str, Any]=5							,A					: Optional[Any]=2							,A					: Union[str, Any]=5							,A					: int=2							,A					: int=0.1							,**A					: List[str]							,):
					super().__init__(**A )

					__A   			= hidden_size
					__A   			= num_hidden_layers
					__A   			= num_attention_heads
					__A   			= intermediate_size
					__A   			= hidden_act
					__A   			= hidden_dropout_prob
					__A   			= attention_probs_dropout_prob
					__A   			= initializer_range
					__A   			= layer_norm_eps
					__A   			= image_size
					__A   			= patch_size
					__A   			= num_channels
					__A   			= qkv_bias
					__A   			= num_detection_tokens
					__A   			= use_mid_position_embeddings
					__A   			= auxiliary_loss
					# Hungarian matcher
					__A   			= class_cost
					__A   			= bbox_cost
					__A   			= giou_cost
					# Loss coefficients
					__A   			= bbox_loss_coefficient
					__A   			= giou_loss_coefficient
					__A   			= eos_coefficient





class 							UpperCAmelCase      (  __SCREAMING_SNAKE_CASE  ):




	'''simple docstring'''





	snake_case_					    =     version.parse("1.11"  )






	@property
	def 				UpperCamelCase_  (       self					: str ):
					return OrderedDict(
					    [
					        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
					    ] )






	@property
	def 				UpperCamelCase_  (       self					: List[Any] ):
					return 1E-4







	@property
	def 				UpperCamelCase_  (       self					: Optional[Any] ):
					return 12
