"""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   lowerCAmelCase_    (			unittest.TestCase					):



				"""simple docstring"""

				def __init__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										snake_case								=			parent

				def 		snake_case (				self    ):






										"""simple docstring"""

										return {}





def    lowerCAmelCase__    (			)					->				Tuple:





						"""simple docstring"""






						snake_case								=			'<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>'

						snake_case								=			'\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   lowerCAmelCase_    (			lowerCAmelCase     ,    unittest.TestCase					):



				"""simple docstring"""
				_lowerCAmelCase     :    Any			  =						MarkupLMFeatureExtractor if is_bsa_available() else None

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			MarkupLMFeatureExtractionTester(self    )

				@property
				def 		snake_case (				self    ):






										"""simple docstring"""

										return self.feature_extract_tester.prepare_feat_extract_dict()

				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			self.feature_extraction_class()

										# Test not batched input
										snake_case								=			get_html_strings()[0]
										snake_case								=			feature_extractor(lowerCAmelCase    )

										# fmt: off
										snake_case								=			[['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']]
										snake_case								=			[['/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		,    lowerCAmelCase    )
										self.assertEqual(encoding.xpaths		,    lowerCAmelCase    )

										# Test batched
										snake_case								=			get_html_strings()
										snake_case								=			feature_extractor(lowerCAmelCase    )

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

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

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



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


SCREAMING_SNAKE_CASE__      						=  logging.get_logger(__name__)

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



class   lowerCAmelCase_    (			lowerCAmelCase					):



				"""simple docstring"""
				_lowerCAmelCase     :    Dict			  =						"""layoutlmv3"""

				def __init__(				self		,    lowerCAmelCase=5_02_65		,    lowerCAmelCase=7_68		,    lowerCAmelCase=12		,    lowerCAmelCase=12		,    lowerCAmelCase=30_72		,    lowerCAmelCase="gelu"		,    lowerCAmelCase=0.1		,    lowerCAmelCase=0.1		,    lowerCAmelCase=5_12		,    lowerCAmelCase=2		,    lowerCAmelCase=0.02		,    lowerCAmelCase=1E-5		,    lowerCAmelCase=1		,    lowerCAmelCase=0		,    lowerCAmelCase=2		,    lowerCAmelCase=10_24		,    lowerCAmelCase=1_28		,    lowerCAmelCase=1_28		,    lowerCAmelCase=True		,    lowerCAmelCase=32		,    lowerCAmelCase=1_28		,    lowerCAmelCase=64		,    lowerCAmelCase=2_56		,    lowerCAmelCase=True		,    lowerCAmelCase=True		,    lowerCAmelCase=True		,    lowerCAmelCase=2_24		,    lowerCAmelCase=3		,    lowerCAmelCase=16		,    lowerCAmelCase=None		,    **lowerCAmelCase		,    ):






										"""simple docstring"""

										super().__init__(
										    vocab_size=lowerCAmelCase		,    hidden_size=lowerCAmelCase		,    num_hidden_layers=lowerCAmelCase		,    num_attention_heads=lowerCAmelCase		,    intermediate_size=lowerCAmelCase		,    hidden_act=lowerCAmelCase		,    hidden_dropout_prob=lowerCAmelCase		,    attention_probs_dropout_prob=lowerCAmelCase		,    max_position_embeddings=lowerCAmelCase		,    type_vocab_size=lowerCAmelCase		,    initializer_range=lowerCAmelCase		,    layer_norm_eps=lowerCAmelCase		,    pad_token_id=lowerCAmelCase		,    bos_token_id=lowerCAmelCase		,    eos_token_id=lowerCAmelCase		,    **lowerCAmelCase		,    )
										snake_case								=			max_ad_position_embeddings
										snake_case								=			coordinate_size
										snake_case								=			shape_size
										snake_case								=			has_relative_attention_bias
										snake_case								=			rel_pos_bins
										snake_case								=			max_rel_pos
										snake_case								=			has_spatial_attention_bias
										snake_case								=			rel_ad_pos_bins
										snake_case								=			max_rel_ad_pos
										snake_case								=			text_embed
										snake_case								=			visual_embed
										snake_case								=			input_size
										snake_case								=			num_channels
										snake_case								=			patch_size
										snake_case								=			classifier_dropout





class   lowerCAmelCase_    (			lowerCAmelCase					):



				"""simple docstring"""
				_lowerCAmelCase     :    List[str]			  =						version.parse("""1.12"""					)

				@property
				def 		snake_case (				self    ):






										"""simple docstring"""

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






										"""simple docstring"""

										return 1E-5

				@property
				def 		snake_case (				self    ):






										"""simple docstring"""

										return 12

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase = -1		,    lowerCAmelCase = -1		,    lowerCAmelCase = False		,    lowerCAmelCase = None		,    lowerCAmelCase = 3		,    lowerCAmelCase = 40		,    lowerCAmelCase = 40		,    ):






										"""simple docstring"""

										setattr(processor.image_processor		,    'apply_ocr'		,    lowerCAmelCase    )

										# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
										snake_case								=			compute_effective_axis_dimension(
										    lowerCAmelCase		,    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
										snake_case								=			processor.tokenizer.num_special_tokens_to_add(lowerCAmelCase    )
										snake_case								=			compute_effective_axis_dimension(
										    lowerCAmelCase		,    fixed_dimension=OnnxConfig.default_fixed_sequence		,    num_token_to_add=lowerCAmelCase    )
										# Generate dummy inputs according to compute batch and sequence
										snake_case								=			[[' '.join([processor.tokenizer.unk_token]    ) * seq_length]] * batch_size

										# Generate dummy bounding boxes
										snake_case								=			[[[48, 84, 73, 1_28]]] * 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)
										snake_case								=			self._generate_dummy_images(lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase    )

										snake_case								=			dict(
										    processor(
										        lowerCAmelCase		,    text=lowerCAmelCase		,    boxes=lowerCAmelCase		,    return_tensors=lowerCAmelCase		,    )    )

										return inputs


