"""simple docstring"""




import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
					    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
					    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
					    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
					    LayoutLMvaConfig,
					    LayoutLMvaForQuestionAnswering,
					    LayoutLMvaForSequenceClassification,
					    LayoutLMvaForTokenClassification,
					    LayoutLMvaModel,
					)
					from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
					from PIL import Image

					from transformers import LayoutLMvaImageProcessor
class     UpperCamelCase__:





						def __init__(       self		,__UpperCAmelCase		,__UpperCAmelCase=2		,__UpperCAmelCase=3		,__UpperCAmelCase=4		,__UpperCAmelCase=2		,__UpperCAmelCase=7		,__UpperCAmelCase=True		,__UpperCAmelCase=True		,__UpperCAmelCase=True		,__UpperCAmelCase=True		,__UpperCAmelCase=99		,__UpperCAmelCase=36		,__UpperCAmelCase=3		,__UpperCAmelCase=4		,__UpperCAmelCase=37		,__UpperCAmelCase="gelu"		,__UpperCAmelCase=0.1		,__UpperCAmelCase=0.1		,__UpperCAmelCase=5_12		,__UpperCAmelCase=16		,__UpperCAmelCase=2		,__UpperCAmelCase=0.0_2		,__UpperCAmelCase=6		,__UpperCAmelCase=6		,__UpperCAmelCase=3		,__UpperCAmelCase=4		,__UpperCAmelCase=None		,__UpperCAmelCase=10_00		,)					->				List[str]:
													A__	     =							parent
													A__	     =							batch_size
													A__	     =							num_channels
													A__	     =							image_size
													A__	     =							patch_size
													A__	     =							text_seq_length
													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 				snake_case__	(       self     )					->				Any:
													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     )
													# 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__	     =							t
																											if bbox[i, j, 2] < bbox[i, j, 0]:
																																		A__	     =							bbox[i, j, 2]
																																		A__	     =							bbox[i, j, 0]
																																		A__	     =							t

													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 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase     )					->				Dict:
													A__	     =							LayoutLMvaModel(config=__A     )
													model.to(__A     )
													model.eval()

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

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

													# text only
													A__	     =							model(__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=__A     )
													self.parent.assertEqual(
													    result.last_hidden_state.shape		,(self.batch_size, self.image_seq_length, self.hidden_size)     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase     )					->				List[Any]:
													A__	     =							self.num_labels
													A__	     =							LayoutLMvaForSequenceClassification(__A     )
													model.to(__A     )
													model.eval()
													A__	     =							model(
													    __A		,bbox=__A		,pixel_values=__A		,attention_mask=__A		,token_type_ids=__A		,labels=__A		,)
													self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_labels)     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase     )					->				str:
													A__	     =							self.num_labels
													A__	     =							LayoutLMvaForTokenClassification(config=__A     )
													model.to(__A     )
													model.eval()
													A__	     =							model(
													    __A		,bbox=__A		,pixel_values=__A		,attention_mask=__A		,token_type_ids=__A		,labels=__A		,)
													self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.text_seq_length, self.num_labels)     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase     )					->				List[str]:
													A__	     =							LayoutLMvaForQuestionAnswering(config=__A     )
													model.to(__A     )
													model.eval()
													A__	     =							model(
													    __A		,bbox=__A		,pixel_values=__A		,attention_mask=__A		,token_type_ids=__A		,start_positions=__A		,end_positions=__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 				snake_case__	(       self     )					->				Optional[Any]:
													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_torch
class     UpperCamelCase__(			UpperCamelCase__		,						UpperCamelCase__		,						unittest.TestCase ):
						lowerCAmelCase__  :						List[str]													=		False
						lowerCAmelCase__  :						Optional[Any]													=		False
						lowerCAmelCase__  :						Optional[Any]													=		False

						lowerCAmelCase__  :						Any													=		(
						    (
						        LayoutLMvaModel,
						        LayoutLMvaForSequenceClassification,
						        LayoutLMvaForTokenClassification,
						        LayoutLMvaForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						lowerCAmelCase__  :						Dict													=		(
						    {'document-question-answering': LayoutLMvaForQuestionAnswering, 'feature-extraction': LayoutLMvaModel}
						    if is_torch_available()
						    else {}
						)





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase     )					->				Union[str, Any]:
													return True





						def 				snake_case__	(       self     )					->				Any:
													A__	     =							LayoutLMvaModelTester(self     )
													A__	     =							ConfigTester(self		,config_class=__A		,hidden_size=37     )





						def 				snake_case__	(       self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase=False     )					->				Optional[int]:
													A__	     =							copy.deepcopy(__A     )
													if model_class in get_values(__A     ):
																				A__	     =							{
																				    k: v.unsqueeze(1     ).expand(-1		,self.model_tester.num_choices		,-1     ).contiguous()
																				    if isinstance(__A		,torch.Tensor     ) and v.ndim > 1
																				    else v
																				    for k, v in inputs_dict.items()
																				}
													if return_labels:
																				if model_class in get_values(__A     ):
																											A__	     =							torch.ones(self.model_tester.batch_size		,dtype=torch.long		,device=__A     )
																				elif model_class in get_values(__A     ):
																											A__	     =							torch.zeros(
																											    self.model_tester.batch_size		,dtype=torch.long		,device=__A     )
																											A__	     =							torch.zeros(
																											    self.model_tester.batch_size		,dtype=torch.long		,device=__A     )
																				elif model_class in [
																				    *get_values(__A     ),
																				]:
																											A__	     =							torch.zeros(
																											    self.model_tester.batch_size		,dtype=torch.long		,device=__A     )
																				elif model_class in [
																				    *get_values(__A     ),
																				]:
																											A__	     =							torch.zeros(
																											    (self.model_tester.batch_size, self.model_tester.text_seq_length)		,dtype=torch.long		,device=__A		,)

													return inputs_dict





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





						def 				snake_case__	(       self     )					->				int:
													A__	     =							self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__A     )





						def 				snake_case__	(       self     )					->				Optional[int]:
													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     )





						def 				snake_case__	(       self     )					->				int:
													A__	     =							self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*__A     )





						def 				snake_case__	(       self     )					->				List[str]:
													A__	     =							self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*__A     )





						def 				snake_case__	(       self     )					->				Optional[Any]:
													A__	     =							self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*__A     )





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



													for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				A__	     =							LayoutLMvaModel.from_pretrained(__A     )
																				self.assertIsNotNone(__A     )






def   UpperCAmelCase					(	):






							"""simple docstring"""





							A__	     =							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
							return image


@require_torch
class     UpperCamelCase__(			unittest.TestCase ):





						@cached_property
						def 				snake_case__	(       self     )					->				int:
													return LayoutLMvaImageProcessor(apply_ocr=__A     ) if is_vision_available() else None





						@slow
						def 				snake_case__	(       self     )					->				str:
													A__	     =							LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'     ).to(__A     )

													A__	     =							self.default_image_processor
													A__	     =							prepare_img()
													A__	     =							image_processor(images=__A		,return_tensors='pt'     ).pixel_values.to(__A     )

													A__	     =							torch.tensor([[1, 2]]     )
													A__	     =							torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]     ).unsqueeze(0     )

													# forward pass
													A__	     =							model(
													    input_ids=input_ids.to(__A     )		,bbox=bbox.to(__A     )		,pixel_values=pixel_values.to(__A     )		,)

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

													A__	     =							torch.tensor(
													    [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]     ).to(__A     )

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






"""simple docstring"""




import unittest

from transformers.testing_utils import require_bsa
from transformers.utils import is_bsa_available

from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin


if is_bsa_available():
				from transformers import MarkupLMFeatureExtractor
class     UpperCamelCase__(			unittest.TestCase ):





						def __init__(       self		,__UpperCAmelCase     )					->				str:
													A__	     =							parent





						def 				snake_case__	(       self     )					->				int:
													return {}






def   UpperCAmelCase					(	):






							"""simple docstring"""





							A__	     =							'<HTML>\n\n    <HEAD>\n    <TITLE>sample document</TITLE>\n    </HEAD>\n\n    <BODY BGCOLOR="FFFFFF">\n    <HR>\n    <a href="http://google.com">Goog</a>\n    <H1>This is one header</H1>\n    <H2>This is a another Header</H2>\n    <P>Travel from\n        <P>\n        <B>SFO to JFK</B>\n        <BR>\n        <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B>\n        <HR>\n        <div style="color:#0000FF">\n            <h3>Traveler <b> name </b> is\n            <p> John Doe </p>\n        </div>'

							A__	     =							'\n    <!DOCTYPE html>\n    <html>\n    <body>\n\n    <h1>My First Heading</h1>\n    <p>My first paragraph.</p>\n\n    </body>\n    </html>\n    '

							return [html_string_a, html_string_a]


@require_bsa
class     UpperCamelCase__(			__A		,						unittest.TestCase ):
						lowerCAmelCase__  :						Dict													=		MarkupLMFeatureExtractor if is_bsa_available() else None





						def 				snake_case__	(       self     )					->				Dict:
													A__	     =							MarkupLMFeatureExtractionTester(self     )





						@property
						def 				snake_case__	(       self     )					->				Optional[Any]:
													return self.feature_extract_tester.prepare_feat_extract_dict()





						def 				snake_case__	(       self     )					->				Any:
													# Initialize feature_extractor
													A__	     =							self.feature_extraction_class()

													# Test not batched input
													A__	     =							get_html_strings()[0]
													A__	     =							feature_extractor(__UpperCAmelCase     )

													# fmt: off
													A__	     =							[['sample document', 'Goog', 'This is one header', 'This is a another Header', 'Travel from', 'SFO to JFK', 'on May 2, 2015 at 2:00 pm. For details go to confirm.com', 'Traveler', 'name', 'is', 'John Doe']]
													A__	     =							[['/html/head/title', '/html/body/a', '/html/body/h1', '/html/body/h2', '/html/body/p', '/html/body/p/p/b[1]', '/html/body/p/p/b[2]/i', '/html/body/p/p/div/h3', '/html/body/p/p/div/h3/b', '/html/body/p/p/div/h3', '/html/body/p/p/div/h3/p']]
													# fmt: on

													self.assertEqual(encoding.nodes		,__UpperCAmelCase     )
													self.assertEqual(encoding.xpaths		,__UpperCAmelCase     )

													# Test batched
													A__	     =							get_html_strings()
													A__	     =							feature_extractor(__UpperCAmelCase     )

													# fmt: off
													A__	     =							expected_nodes + [['My First Heading', 'My first paragraph.']]
													A__	     =							expected_xpaths + [['/html/body/h1', '/html/body/p']]

													self.assertEqual(len(encoding.nodes     )		,2     )
													self.assertEqual(len(encoding.xpaths     )		,2     )

													self.assertEqual(encoding.nodes		,__UpperCAmelCase     )
													self.assertEqual(encoding.xpaths		,__UpperCAmelCase     )





