






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

from ... import PretrainedConfig, PreTrainedTokenizer
from ...onnx import OnnxConfig, PatchingSpec
from ...utils import TensorType, is_torch_available, logging


__UpperCAmelCase										=		logging.get_logger(__name__)

__UpperCAmelCase										=		{
    'microsoft/layoutlm-base-uncased': (
        'https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/config.json'
    ),
    'microsoft/layoutlm-large-uncased': (
        'https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/config.json'
    ),
}



class 						lowerCamelCase   (_snake_case    ):







		'''simple docstring'''



		_snake_case					:					Optional[Any] 		=						'''layoutlm'''



		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.02       ,	_UpperCamelCase=1E-12       ,	_UpperCamelCase=0       ,	_UpperCamelCase="absolute"       ,	_UpperCamelCase=True       ,	_UpperCamelCase=1_0_2_4       ,	**_UpperCamelCase       ,	)			->							Dict:
							super().__init__(pad_token_id=_UpperCamelCase       ,	**_UpperCamelCase     )
							UpperCAmelCase_					:   List[str]   =		vocab_size
							UpperCAmelCase_					:   Tuple   =		hidden_size
							UpperCAmelCase_					:   Union[str, Any]   =		num_hidden_layers
							UpperCAmelCase_					:   Optional[int]   =		num_attention_heads
							UpperCAmelCase_					:   Any   =		hidden_act
							UpperCAmelCase_					:   Union[str, Any]   =		intermediate_size
							UpperCAmelCase_					:   str   =		hidden_dropout_prob
							UpperCAmelCase_					:   str   =		attention_probs_dropout_prob
							UpperCAmelCase_					:   Tuple   =		max_position_embeddings
							UpperCAmelCase_					:   int   =		type_vocab_size
							UpperCAmelCase_					:   str   =		initializer_range
							UpperCAmelCase_					:   str   =		layer_norm_eps
							UpperCAmelCase_					:   Union[str, Any]   =		position_embedding_type
							UpperCAmelCase_					:   Dict   =		use_cache
							UpperCAmelCase_					:   Optional[Any]   =		max_ad_position_embeddings





class 						lowerCamelCase   (_snake_case    ):







		'''simple docstring'''



		def __init__(							self       ,	_UpperCamelCase       ,	_UpperCamelCase = "default"       ,	_UpperCamelCase = None       ,	)			->							Dict:
							super().__init__(_UpperCamelCase       ,	task=_UpperCamelCase       ,	patching_specs=_UpperCamelCase     )
							UpperCAmelCase_					:   Optional[Any]   =		config.max_ad_position_embeddings - 1



		@property
		def       __UpperCAmelCase    (							self     )			->							Mapping[str, Mapping[int, str]]:
							return OrderedDict(
							    [
							        ('input_ids', {0: 'batch', 1: 'sequence'}),
							        ('bbox', {0: 'batch', 1: 'sequence'}),
							        ('attention_mask', {0: 'batch', 1: 'sequence'}),
							        ('token_type_ids', {0: 'batch', 1: 'sequence'}),
							    ]     )



		def       __UpperCAmelCase    (							self       ,	_UpperCamelCase       ,	_UpperCamelCase = -1       ,	_UpperCamelCase = -1       ,	_UpperCamelCase = False       ,	_UpperCamelCase = None       ,	)			->							Mapping[str, Any]:
							UpperCAmelCase_					:   Tuple   =		super().generate_dummy_inputs(
							    _UpperCamelCase       ,	batch_size=_UpperCamelCase       ,	seq_length=_UpperCamelCase       ,	is_pair=_UpperCamelCase       ,	framework=_UpperCamelCase     )

							# Generate a dummy bbox
							UpperCAmelCase_					:   Optional[Any]   =		[4_8, 8_4, 7_3, 1_2_8]

							if not framework == TensorType.PYTORCH:
												raise NotImplementedError('Exporting LayoutLM to ONNX is currently only supported for PyTorch.'     )

							if not is_torch_available():
												raise ValueError('Cannot generate dummy inputs without PyTorch installed.'     )
							import torch

							UpperCAmelCase_ ,						UpperCAmelCase_					:   Optional[Any]   =		input_dict['input_ids'].shape
							UpperCAmelCase_					:   Optional[int]   =		torch.tensor([*[box] * seq_length]     ).tile(_UpperCamelCase       ,	1       ,	1     )
							return input_dict














import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
		import transformer_engine.pytorch as te




def   lowercase__   (  __snake_case					:							List[str]  , __snake_case					:							List[Any]=True  , __snake_case					:							str=True  , __snake_case					:							Tuple=True						):


					'''simple docstring'''


					if not is_fpa_available():
										raise ImportError('Using `convert_model` requires transformer_engine to be installed.'						)
					for name, module in model.named_children():
										if isinstance(__snake_case  , nn.Linear						) and to_transformer_engine and _convert_linear:
															# Return early if the linear layer weights are not multiples of 16
															if any(p % 16 != 0 for p in module.weight.shape						):
																				return
															UpperCAmelCase_					:   Optional[int]   =		module.bias is not None
															UpperCAmelCase_					:   Optional[int]   =		te.Linear(
															    module.in_features  , module.out_features  , bias=__snake_case  , params_dtype=module.weight.dtype						)
															UpperCAmelCase_					:   List[str]   =		module.weight.data.clone()
															if has_bias:
																				UpperCAmelCase_					:   List[Any]   =		module.bias.data.clone()

															setattr(__snake_case  , __snake_case  , __snake_case						)
										elif isinstance(__snake_case  , nn.LayerNorm						) and to_transformer_engine and _convert_ln:
															UpperCAmelCase_					:   int   =		te.LayerNorm(module.normalized_shape[0]  , eps=module.eps  , params_dtype=module.weight.dtype						)
															UpperCAmelCase_					:   Any   =		module.weight.data.clone()
															UpperCAmelCase_					:   Any   =		module.bias.data.clone()

															setattr(__snake_case  , __snake_case  , __snake_case						)
										elif isinstance(__snake_case  , te.Linear						) and not to_transformer_engine and _convert_linear:
															UpperCAmelCase_					:   int   =		module.bias is not None
															UpperCAmelCase_					:   Tuple   =		nn.Linear(
															    module.in_features  , module.out_features  , bias=__snake_case  , params_dtype=module.weight.dtype						)
															UpperCAmelCase_					:   str   =		module.weight.data.clone()
															if has_bias:
																				UpperCAmelCase_					:   Dict   =		module.bias.data.clone()

															setattr(__snake_case  , __snake_case  , __snake_case						)
										elif isinstance(__snake_case  , te.LayerNorm						) and not to_transformer_engine and _convert_ln:
															UpperCAmelCase_					:   List[Any]   =		nn.LayerNorm(module.normalized_shape[0]  , eps=module.eps  , params_dtype=module.weight.dtype						)
															UpperCAmelCase_					:   List[str]   =		module.weight.data.clone()
															UpperCAmelCase_					:   Tuple   =		module.bias.data.clone()

															setattr(__snake_case  , __snake_case  , __snake_case						)
										else:
															convert_model(
															    __snake_case  , to_transformer_engine=__snake_case  , _convert_linear=_convert_linear  , _convert_ln=_convert_ln  , )






def   lowercase__   (  __snake_case					:							List[Any]						):


					'''simple docstring'''


					if not is_fpa_available():
										raise ImportError('Using `has_transformer_engine_layers` requires transformer_engine to be installed.'						)
					for m in model.modules():
										if isinstance(__snake_case  , (te.LayerNorm, te.Linear)						):
															return True
					return False






