


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


_UpperCAmelCase      :	Any  =logging.get_logger(__name__)

_UpperCAmelCase      :	Dict  ={
    """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 					snake_case__(				UpperCAmelCase__				):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__   :	List[str]       =				"""layoutlmv2"""




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




						@classmethod
						def     lowercase_			(					self				)       ->     List[Any]:
										return {
										    "MODEL.MASK_ON": True,
										    "MODEL.PIXEL_STD": [57.3_75, 57.1_20, 58.3_95],
										    "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     lowercase_			(					self				)       ->     Optional[int]:
										lowerCAmelCase_			:   str     					=					detectrona.config.get_cfg()
										for k, v in self.detectrona_config_args.items():
														lowerCAmelCase_			:   Dict     					=					k.split('''.'''				)
														lowerCAmelCase_			:   Optional[int]     					=					detectrona_config
														for attribute in attributes[:-1]:
																		lowerCAmelCase_			:   Union[str, Any]     					=					getattr(__lowercase ,    __lowercase				)
														setattr(__lowercase ,    attributes[-1] ,    __lowercase				)

										return detectrona_config



import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
						import transformer_engine.pytorch as te



def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_=True			, lowerCAmelCase_=True			, lowerCAmelCase_=True			)->			Union[str, Any]:
				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(lowerCAmelCase_			, 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
												lowerCAmelCase_			:   Optional[int]     					=					module.bias is not None
												lowerCAmelCase_			:   Any     					=					te.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Dict     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Any     					=					module.bias.data.clone()

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

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, te.Linear			) and not to_transformer_engine and _convert_linear:
												lowerCAmelCase_			:   Dict     					=					module.bias is not None
												lowerCAmelCase_			:   Union[str, Any]     					=					nn.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Optional[Any]     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Union[str, Any]     					=					module.bias.data.clone()

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

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




def       lowerCAmelCase			(		lowerCAmelCase_			)->			str:
				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(lowerCAmelCase_			, (te.LayerNorm, te.Linear)			):
												return True
				return False