



"""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      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Optional[int]:
				A__											=					parent







			def   snake_case__	(      self		)      ->	List[Any]:
				return {}



def       _lowerCamelCase				(							)	-> Tuple:






	"""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__			(   _lowerCAmelCase       ,		unittest.TestCase      ):

			"""simple docstring"""


			A__	:					Any							  =     MarkupLMFeatureExtractor if is_bsa_available() else None


			def   snake_case__	(      self		)      ->	Union[str, Any]:
				A__											=					MarkupLMFeatureExtractionTester(self		)


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







			def   snake_case__	(      self		)      ->	Union[str, Any]:
				# Initialize feature_extractor
				A__											=					self.feature_extraction_class()

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

				# 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						,	SCREAMING_SNAKE_CASE__		)
				self.assertEqual(encoding.xpaths						,	SCREAMING_SNAKE_CASE__		)

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

				# 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						,	SCREAMING_SNAKE_CASE__		)
				self.assertEqual(encoding.xpaths						,	SCREAMING_SNAKE_CASE__		)









"""simple docstring"""







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

from packaging import version

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


if TYPE_CHECKING:
			from ...processing_utils import ProcessorMixin
			from ...utils import TensorType


UpperCamelCase     =							logging.get_logger(__name__)

UpperCamelCase     =							{
    """microsoft/layoutlmv3-base""": """https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json""",
}






class       UpperCamelCase__			(   _lowerCAmelCase      ):

			"""simple docstring"""


			A__	:					int							  =     "layoutlmv3"


			def __init__(      self						,	SCREAMING_SNAKE_CASE__=50265						,	SCREAMING_SNAKE_CASE__=768						,	SCREAMING_SNAKE_CASE__=12						,	SCREAMING_SNAKE_CASE__=12						,	SCREAMING_SNAKE_CASE__=3072						,	SCREAMING_SNAKE_CASE__="gelu"						,	SCREAMING_SNAKE_CASE__=0.1						,	SCREAMING_SNAKE_CASE__=0.1						,	SCREAMING_SNAKE_CASE__=512						,	SCREAMING_SNAKE_CASE__=2						,	SCREAMING_SNAKE_CASE__=0.0_2						,	SCREAMING_SNAKE_CASE__=1e-5						,	SCREAMING_SNAKE_CASE__=1						,	SCREAMING_SNAKE_CASE__=0						,	SCREAMING_SNAKE_CASE__=2						,	SCREAMING_SNAKE_CASE__=1024						,	SCREAMING_SNAKE_CASE__=128						,	SCREAMING_SNAKE_CASE__=128						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=32						,	SCREAMING_SNAKE_CASE__=128						,	SCREAMING_SNAKE_CASE__=64						,	SCREAMING_SNAKE_CASE__=256						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=224						,	SCREAMING_SNAKE_CASE__=3						,	SCREAMING_SNAKE_CASE__=16						,	SCREAMING_SNAKE_CASE__=None						,	**SCREAMING_SNAKE_CASE__						,	)      ->	Any:
				super().__init__(
				    vocab_size=SCREAMING_SNAKE_CASE__						,	hidden_size=SCREAMING_SNAKE_CASE__						,	num_hidden_layers=SCREAMING_SNAKE_CASE__						,	num_attention_heads=SCREAMING_SNAKE_CASE__						,	intermediate_size=SCREAMING_SNAKE_CASE__						,	hidden_act=SCREAMING_SNAKE_CASE__						,	hidden_dropout_prob=SCREAMING_SNAKE_CASE__						,	attention_probs_dropout_prob=SCREAMING_SNAKE_CASE__						,	max_position_embeddings=SCREAMING_SNAKE_CASE__						,	type_vocab_size=SCREAMING_SNAKE_CASE__						,	initializer_range=SCREAMING_SNAKE_CASE__						,	layer_norm_eps=SCREAMING_SNAKE_CASE__						,	pad_token_id=SCREAMING_SNAKE_CASE__						,	bos_token_id=SCREAMING_SNAKE_CASE__						,	eos_token_id=SCREAMING_SNAKE_CASE__						,	**SCREAMING_SNAKE_CASE__						,	)
				A__											=					max_ad_position_embeddings
				A__											=					coordinate_size
				A__											=					shape_size
				A__											=					has_relative_attention_bias
				A__											=					rel_pos_bins
				A__											=					max_rel_pos
				A__											=					has_spatial_attention_bias
				A__											=					rel_ad_pos_bins
				A__											=					max_rel_ad_pos
				A__											=					text_embed
				A__											=					visual_embed
				A__											=					input_size
				A__											=					num_channels
				A__											=					patch_size
				A__											=					classifier_dropout






class       UpperCamelCase__			(   _lowerCAmelCase      ):

			"""simple docstring"""


			A__	:					Union[str, Any]							  =     version.parse("1.12"      )


			@property
			def   snake_case__	(      self		)      ->	Mapping[str, Mapping[int, str]]:
				# The order of inputs is different for question answering and sequence classification
				if self.task in ["question-answering", "sequence-classification"]:
					return OrderedDict(
					    [
					        ("input_ids", {0: "batch", 1: "sequence"}),
					        ("attention_mask", {0: "batch", 1: "sequence"}),
					        ("bbox", {0: "batch", 1: "sequence"}),
					        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
					    ]		)
				else:
					return OrderedDict(
					    [
					        ("input_ids", {0: "batch", 1: "sequence"}),
					        ("bbox", {0: "batch", 1: "sequence"}),
					        ("attention_mask", {0: "batch", 1: "sequence"}),
					        ("pixel_values", {0: "batch", 1: "num_channels"}),
					    ]		)


			@property
			def   snake_case__	(      self		)      ->	float:
				return 1e-5


			@property
			def   snake_case__	(      self		)      ->	int:
				return 12







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = -1						,	SCREAMING_SNAKE_CASE__ = -1						,	SCREAMING_SNAKE_CASE__ = False						,	SCREAMING_SNAKE_CASE__ = None						,	SCREAMING_SNAKE_CASE__ = 3						,	SCREAMING_SNAKE_CASE__ = 40						,	SCREAMING_SNAKE_CASE__ = 40						,	)      ->	Mapping[str, Any]:
				setattr(processor.image_processor						,	"apply_ocr"						,	SCREAMING_SNAKE_CASE__		)

				# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
				A__											=					compute_effective_axis_dimension(
				    SCREAMING_SNAKE_CASE__						,	fixed_dimension=OnnxConfig.default_fixed_batch						,	num_token_to_add=0		)
				# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
				A__											=					processor.tokenizer.num_special_tokens_to_add(SCREAMING_SNAKE_CASE__		)
				A__											=					compute_effective_axis_dimension(
				    SCREAMING_SNAKE_CASE__						,	fixed_dimension=OnnxConfig.default_fixed_sequence						,	num_token_to_add=SCREAMING_SNAKE_CASE__		)
				# Generate dummy inputs according to compute batch and sequence
				A__											=					[[" ".join([processor.tokenizer.unk_token]		) * seq_length]] * batch_size

				# Generate dummy bounding boxes
				A__											=					[[[48, 84, 73, 128]]] * batch_size

				# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
				# batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch)
				A__											=					self._generate_dummy_images(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)

				A__											=					dict(
				    processor(
				        SCREAMING_SNAKE_CASE__						,	text=SCREAMING_SNAKE_CASE__						,	boxes=SCREAMING_SNAKE_CASE__						,	return_tensors=SCREAMING_SNAKE_CASE__						,	)		)

				return inputs




