





'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import is_detectrona_available, logging


__UpperCamelCase      :   Union[str, Any]								=     logging.get_logger(__name__)

__UpperCamelCase      :   Any								=     {
    '''layoutlmv2-base-uncased''': '''https://huggingface.co/microsoft/layoutlmv2-base-uncased/resolve/main/config.json''',
    '''layoutlmv2-large-uncased''': '''https://huggingface.co/microsoft/layoutlmv2-large-uncased/resolve/main/config.json''',
    # See all LayoutLMv2 models at https://huggingface.co/models?filter=layoutlmv2
}

# soft dependency
if is_detectrona_available():
	import detectrona



class  lowerCamelCase__				(						snake_case_						):


	"""simple docstring"""





	__magic_name__							   =		"""layoutlmv2"""




	def __init__(					self   ,     UpperCAmelCase__=3_0_5_2_2   ,     UpperCAmelCase__=7_6_8   ,     UpperCAmelCase__=1_2   ,     UpperCAmelCase__=1_2   ,     UpperCAmelCase__=3_0_7_2   ,     UpperCAmelCase__="gelu"   ,     UpperCAmelCase__=0.1   ,     UpperCAmelCase__=0.1   ,     UpperCAmelCase__=5_1_2   ,     UpperCAmelCase__=2   ,     UpperCAmelCase__=0.0_2   ,     UpperCAmelCase__=1e-12   ,     UpperCAmelCase__=0   ,     UpperCAmelCase__=1_0_2_4   ,     UpperCAmelCase__=1_2_8   ,     UpperCAmelCase__=3_2   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=2_5_6   ,     UpperCAmelCase__=6_4   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=[7, 7, 2_5_6]   ,     UpperCAmelCase__=1_2_8   ,     UpperCAmelCase__=1_2_8   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=False   ,     UpperCAmelCase__=None   ,     **UpperCAmelCase__   ,     )       ->				Union[str, Any]:
					super().__init__(
					    vocab_size=UpperCAmelCase__   ,     hidden_size=UpperCAmelCase__   ,     num_hidden_layers=UpperCAmelCase__   ,     num_attention_heads=UpperCAmelCase__   ,     intermediate_size=UpperCAmelCase__   ,     hidden_act=UpperCAmelCase__   ,     hidden_dropout_prob=UpperCAmelCase__   ,     attention_probs_dropout_prob=UpperCAmelCase__   ,     max_position_embeddings=UpperCAmelCase__   ,     type_vocab_size=UpperCAmelCase__   ,     initializer_range=UpperCAmelCase__   ,     layer_norm_eps=UpperCAmelCase__   ,     pad_token_id=UpperCAmelCase__   ,     **UpperCAmelCase__   ,     )
					_A :				Dict				=  max_ad_position_embeddings
					_A :				Dict				=  max_rel_pos
					_A :				Union[str, Any]				=  rel_pos_bins
					_A :				List[str]				=  fast_qkv
					_A :				Union[str, Any]				=  max_rel_ad_pos
					_A :				Any				=  rel_ad_pos_bins
					_A :				Any				=  convert_sync_batchnorm
					_A :				Any				=  image_feature_pool_shape
					_A :				str				=  coordinate_size
					_A :				Any				=  shape_size
					_A :				List[Any]				=  has_relative_attention_bias
					_A :				Tuple				=  has_spatial_attention_bias
					_A :				Tuple				=  has_visual_segment_embedding
					_A :				str				=  (
					    detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
					)




	@classmethod
	def 				_lowerCamelCase				(					self			)       ->				Any:
					return {
					    "MODEL.MASK_ON": True,
					    "MODEL.PIXEL_STD": [5_7.3_7_5, 5_7.1_2_0, 5_8.3_9_5],
					    "MODEL.BACKBONE.NAME": "build_resnet_fpn_backbone",
					    "MODEL.FPN.IN_FEATURES": ["res2", "res3", "res4", "res5"],
					    "MODEL.ANCHOR_GENERATOR.SIZES": [[3_2], [6_4], [1_2_8], [2_5_6], [5_1_2]],
					    "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
					    "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 2_0_0_0,
					    "MODEL.RPN.PRE_NMS_TOPK_TEST": 1_0_0_0,
					    "MODEL.RPN.POST_NMS_TOPK_TRAIN": 1_0_0_0,
					    "MODEL.POST_NMS_TOPK_TEST": 1_0_0_0,
					    "MODEL.ROI_HEADS.NAME": "StandardROIHeads",
					    "MODEL.ROI_HEADS.NUM_CLASSES": 5,
					    "MODEL.ROI_HEADS.IN_FEATURES": ["p2", "p3", "p4", "p5"],
					    "MODEL.ROI_BOX_HEAD.NAME": "FastRCNNConvFCHead",
					    "MODEL.ROI_BOX_HEAD.NUM_FC": 2,
					    "MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION": 1_4,
					    "MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
					    "MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
					    "MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
					    "MODEL.RESNETS.DEPTH": 1_0_1,
					    "MODEL.RESNETS.SIZES": [[3_2], [6_4], [1_2_8], [2_5_6], [5_1_2]],
					    "MODEL.RESNETS.ASPECT_RATIOS": [[0.5, 1.0, 2.0]],
					    "MODEL.RESNETS.OUT_FEATURES": ["res2", "res3", "res4", "res5"],
					    "MODEL.RESNETS.NUM_GROUPS": 3_2,
					    "MODEL.RESNETS.WIDTH_PER_GROUP": 8,
					    "MODEL.RESNETS.STRIDE_IN_1X1": False,
					}




	def 				_lowerCamelCase				(					self			)       ->				Optional[int]:
					_A :				Tuple				=  detectrona.config.get_cfg()
					for k, v in self.detectrona_config_args.items():
									_A :				Tuple				=  k.split('''.'''			)
									_A :				Optional[Any]				=  detectrona_config
									for attribute in attributes[:-1]:
													_A :				Dict				=  getattr(UpperCAmelCase__   ,     UpperCAmelCase__			)
									setattr(UpperCAmelCase__   ,     attributes[-1]   ,     UpperCAmelCase__			)

					return detectrona_config







'''simple docstring'''






import textwrap

import pyarrow as pa
import pytest

from datasets import Features, Image
from datasets.packaged_modules.text.text import Text

from ..utils import require_pil





@pytest.fixture
def      lowercase  (  lowerCAmelCase :						Union[str, Any]):



				"""simple docstring"""


				_A :				int				=  tmp_path / '''text.txt'''
				_A :				int				=  textwrap.dedent(
				    '''\
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

        Second paragraph:
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        ''')
				with open(lowerCAmelCase   , '''w'''   , encoding='''utf-8''') as f:
								f.write(lowerCAmelCase)
				return str(lowerCAmelCase)





@pytest.fixture
def      lowercase  (  lowerCAmelCase :						Optional[Any]   , lowerCAmelCase :						Tuple):



				"""simple docstring"""


				_A :				str				=  tmp_path / '''text_with_image.txt'''
				with open(lowerCAmelCase   , '''w'''   , encoding='''utf-8''') as f:
								f.write(lowerCAmelCase)
				return str(lowerCAmelCase)





@pytest.mark.parametrize('''keep_linebreaks'''   , [True, False])
def      lowercase  (  lowerCAmelCase :						Optional[Any]   , lowerCAmelCase :						Union[str, Any]):



				"""simple docstring"""


				with open(lowerCAmelCase   , encoding='''utf-8''') as f:
								_A :				Any				=  f.read().splitlines(keepends=lowerCAmelCase)
				_A :				Any				=  Text(keep_linebreaks=lowerCAmelCase   , encoding='''utf-8''')
				_A :				Optional[Any]				=  text._generate_tables([[text_file]])
				_A :				Dict				=  pa.concat_tables([table for _, table in generator]).to_pydict()['''text''']
				assert generated_content == expected_content





@require_pil
def      lowercase  (  lowerCAmelCase :						Dict):



				"""simple docstring"""


				with open(lowerCAmelCase   , encoding='''utf-8''') as f:
								_A :				Tuple				=  f.read().splitlines()[0]
				_A :				Optional[int]				=  Text(encoding='''utf-8'''   , features=Features({'''image''': Image()}))
				_A :				Any				=  text._generate_tables([[text_file_with_image]])
				_A :				str				=  pa.concat_tables([table for _, table in generator])
				assert pa_table.schema.field('''image''').type == Image()()
				_A :				List[str]				=  pa_table.to_pydict()['''image''']
				assert generated_content == [{"path": image_file, "bytes": None}]





@pytest.mark.parametrize('''sample_by'''   , ['''line''', '''paragraph''', '''document'''])
def      lowercase  (  lowerCAmelCase :						int   , lowerCAmelCase :						Optional[int]):



				"""simple docstring"""


				with open(lowerCAmelCase   , encoding='''utf-8''') as f:
								_A :				Any				=  f.read()
				if sample_by == "line":
								_A :				str				=  expected_content.splitlines()
				elif sample_by == "paragraph":
								_A :				str				=  expected_content.split('''\n\n''')
				elif sample_by == "document":
								_A :				Tuple				=  [expected_content]
				_A :				str				=  Text(sample_by=lowerCAmelCase   , encoding='''utf-8'''   , chunksize=100)
				_A :				Optional[Any]				=  text._generate_tables([[text_file]])
				_A :				int				=  pa.concat_tables([table for _, table in generator]).to_pydict()['''text''']
				assert generated_content == expected_content
