





'''simple docstring'''




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

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


if TYPE_CHECKING:
							from ... import PreTrainedTokenizerBase, TensorType

__UpperCAmelCase            =       logging.get_logger(__name__)




class      UpperCamelCase__					(					lowercase_			):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__				=	'''vision-encoder-decoder'''
					SCREAMING_SNAKE_CASE__				=	True


					def __init__(						self				:			Optional[int]      ,						**lowerCamelCase_				:			str						):

							'''simple docstring'''



							super().__init__(**lowerCamelCase_						)
							if "encoder" not in kwargs or "decoder" not in kwargs:
									raise ValueError(
									    f'''A configuraton of type {self.model_type} cannot be instantiated because '''
									    f'''not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}'''						)

							SCREAMING_SNAKE_CASE :				Dict		=			kwargs.pop("""encoder"""						)
							SCREAMING_SNAKE_CASE :				List[str]		=			encoder_config.pop("""model_type"""						)
							SCREAMING_SNAKE_CASE :				List[Any]		=			kwargs.pop("""decoder"""						)
							SCREAMING_SNAKE_CASE :				str		=			decoder_config.pop("""model_type"""						)

							SCREAMING_SNAKE_CASE :				int		=			AutoConfig.for_model(lowerCamelCase_      ,						**lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				int		=			AutoConfig.for_model(lowerCamelCase_      ,						**lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				int		=			True


					@classmethod
					def lowerCamelCase_			(						cls				:			int      ,						lowerCamelCase_				:			PretrainedConfig      ,						lowerCamelCase_				:			PretrainedConfig      ,						**lowerCamelCase_				:			Any						):

							'''simple docstring'''



							logger.info("""Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config"""						)
							SCREAMING_SNAKE_CASE :				Optional[int]		=			True
							SCREAMING_SNAKE_CASE :				Tuple		=			True

							return cls(encoder=encoder_config.to_dict()      ,						decoder=decoder_config.to_dict()      ,						**lowerCamelCase_						)



					def lowerCamelCase_			(						self				:			Any						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			copy.deepcopy(self.__dict__						)
							SCREAMING_SNAKE_CASE :				Any		=			self.encoder.to_dict()
							SCREAMING_SNAKE_CASE :				str		=			self.decoder.to_dict()
							SCREAMING_SNAKE_CASE :				Dict		=			self.__class__.model_type
							return output







class      UpperCamelCase__					(					lowercase_			):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__				=	version.parse('''1.11'''			)


					@property
					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							return OrderedDict(
							    [
							        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
							    ]						)


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

							'''simple docstring'''



							return 1e-4



					@property
					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}}						)







class      UpperCamelCase__					(					lowercase_			):




					"""simple docstring"""


					@property
					def lowerCamelCase_			(						self				:			List[str]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			OrderedDict()
							SCREAMING_SNAKE_CASE :				Optional[Any]		=			{0: """batch""", 1: """past_decoder_sequence + sequence"""}
							SCREAMING_SNAKE_CASE :				Optional[int]		=			{0: """batch""", 1: """past_decoder_sequence + sequence"""}
							SCREAMING_SNAKE_CASE :				Any		=			{0: """batch""", 1: """encoder_sequence"""}

							return common_inputs



					def lowerCamelCase_			(						self				:			List[str]      ,						lowerCamelCase_				:			"PreTrainedTokenizerBase"      ,						lowerCamelCase_				:			int = -1      ,						lowerCamelCase_				:			int = -1      ,						lowerCamelCase_				:			bool = False      ,						lowerCamelCase_				:			Optional["TensorType"] = None      ,						):

							'''simple docstring'''



							import torch

							SCREAMING_SNAKE_CASE :				Tuple		=			OrderedDict()

							SCREAMING_SNAKE_CASE :				List[Any]		=			super().generate_dummy_inputs(
							    lowerCamelCase_      ,						batch_size=lowerCamelCase_      ,						seq_length=lowerCamelCase_      ,						is_pair=lowerCamelCase_      ,						framework=lowerCamelCase_						)

							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				Optional[Any]		=			dummy_input["""input_ids"""].shape
							SCREAMING_SNAKE_CASE :				List[str]		=			(batch, encoder_sequence, self._config.encoder_hidden_size)
							SCREAMING_SNAKE_CASE :				List[Any]		=			dummy_input.pop("""input_ids"""						)
							SCREAMING_SNAKE_CASE :				Optional[Any]		=			dummy_input.pop("""attention_mask"""						)
							SCREAMING_SNAKE_CASE :				int		=			torch.zeros(lowerCamelCase_						)

							return common_inputs







class      UpperCamelCase__					(					lowercase_			):




					"""simple docstring"""


					@property
					def lowerCamelCase_			(						self				:			Dict						):

							'''simple docstring'''



							pass


					def lowerCamelCase_			(						self				:			Tuple      ,						lowerCamelCase_				:			PretrainedConfig						):

							'''simple docstring'''



							return VisionEncoderDecoderEncoderOnnxConfig(lowerCamelCase_						)



					def lowerCamelCase_			(						self				:			Optional[Any]      ,						lowerCamelCase_				:			PretrainedConfig      ,						lowerCamelCase_				:			PretrainedConfig      ,						lowerCamelCase_				:			str = "default"						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Optional[Any]		=			encoder_config.hidden_size
							return VisionEncoderDecoderDecoderOnnxConfig(lowerCamelCase_      ,						lowerCamelCase_						)










'''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				:			str      ,						lowerCamelCase_				:			str						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				List[str]		=			parent



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

							'''simple docstring'''



							return {}







def 		__A		(  ):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE :				Any		=			"""<HTML>

    <HEAD>
    <TITLE>sample document</TITLE>
    </HEAD>

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

		SCREAMING_SNAKE_CASE :				int		=			"""
    <!DOCTYPE html>
    <html>
    <body>

    <h1>My First Heading</h1>
    <p>My first paragraph.</p>

    </body>
    </html>
    """

		return [html_string_a, html_string_a]




@require_bsa
class      UpperCamelCase__					(					lowercase_						,					unittest.TestCase			):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__				=	MarkupLMFeatureExtractor if is_bsa_available() else None


					def lowerCamelCase_			(						self				:			Optional[int]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				int		=			MarkupLMFeatureExtractionTester(self						)


					@property
					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							return self.feature_extract_tester.prepare_feat_extract_dict()



					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			self.feature_extraction_class()

							# Test not batched input
							SCREAMING_SNAKE_CASE :				Optional[int]		=			get_html_strings()[0]
							SCREAMING_SNAKE_CASE :				List[Any]		=			feature_extractor(lowerCamelCase_						)

							# fmt: off
							SCREAMING_SNAKE_CASE :				Optional[int]		=			[["""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"""]]
							SCREAMING_SNAKE_CASE :				List[str]		=			[["""/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
							SCREAMING_SNAKE_CASE :				str		=			get_html_strings()
							SCREAMING_SNAKE_CASE :				str		=			feature_extractor(lowerCamelCase_						)

							# fmt: off
							SCREAMING_SNAKE_CASE :				int		=			expected_nodes + [["""My First Heading""", """My first paragraph."""]]
							SCREAMING_SNAKE_CASE :				List[Any]		=			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_						)



