


from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


__UpperCamelCase							:							str    							=					logging.get_logger(__name__)

class    __magic_name__						(  __snake_case):
     A:		List[str]								=   ["input_ids", "attention_mask"]


     def __init__(   self				:  Optional[Any]				,						lowerCamelCase__				:  Any="[PAD]"				,						lowerCamelCase__				:  str="[BOS]"				,						lowerCamelCase__				:  str="[EOS]"				,						lowerCamelCase__				:  str="[MASK]"				,						lowerCamelCase__				:  Dict="[CLS]"				,						lowerCamelCase__				:  Optional[Any]="[SEP]"				,						lowerCamelCase__				:  Optional[int]=2048				,						**lowerCamelCase__				:  int				,						)	->   None:





      '''simple docstring'''






      UpperCamelCase__       :     Dict 							= AddedToken(lowerCamelCase_				,						lstrip=lowerCamelCase_				,						rstrip=lowerCamelCase_      ) if isinstance(lowerCamelCase_				,						lowerCamelCase_      ) else pad_token
      UpperCamelCase__       :     str 							= AddedToken(lowerCamelCase_				,						lstrip=lowerCamelCase_				,						rstrip=lowerCamelCase_      ) if isinstance(lowerCamelCase_				,						lowerCamelCase_      ) else bos_token
      UpperCamelCase__       :     Tuple 							= AddedToken(lowerCamelCase_				,						lstrip=lowerCamelCase_				,						rstrip=lowerCamelCase_      ) if isinstance(lowerCamelCase_				,						lowerCamelCase_      ) else eos_token
      UpperCamelCase__       :     List[Any] 							= AddedToken(lowerCamelCase_				,						lstrip=lowerCamelCase_				,						rstrip=lowerCamelCase_      ) if isinstance(lowerCamelCase_				,						lowerCamelCase_      ) else mask_token
      UpperCamelCase__       :     Optional[Any] 							= AddedToken(lowerCamelCase_				,						lstrip=lowerCamelCase_				,						rstrip=lowerCamelCase_      ) if isinstance(lowerCamelCase_				,						lowerCamelCase_      ) else cls_token
      UpperCamelCase__       :     Union[str, Any] 							= AddedToken(lowerCamelCase_				,						lstrip=lowerCamelCase_				,						rstrip=lowerCamelCase_      ) if isinstance(lowerCamelCase_				,						lowerCamelCase_      ) else sep_token

      super().__init__(
          pad_token=lowerCamelCase_				,						bos_token=lowerCamelCase_				,						eos_token=lowerCamelCase_				,						mask_token=lowerCamelCase_				,						cls_token=lowerCamelCase_				,						sep_token=lowerCamelCase_				,						model_max_length=lowerCamelCase_				,						**lowerCamelCase_				,						)

      UpperCamelCase__       :     List[str] 							= 2**8  # utf is 8 bits

      # define special tokens dict
      UpperCamelCase__       :     Dict[str, int] 							= {
          self.pad_token: 0,
          self.bos_token: 1,
          self.eos_token: 2,
          self.mask_token: 3,
          self.cls_token: 4,
          self.sep_token: 5,
      }
      UpperCamelCase__       :     Any 							= len(self.special_tokens_encoder      )
      UpperCamelCase__       :     Dict[int, str] 							= {v: k for k, v in self.special_tokens_encoder.items()}


     def   UpperCAmelCase__						(   self				:  Dict      )	->   Dict[str, int]:





      '''simple docstring'''






      UpperCamelCase__       :     List[str] 							= self.special_tokens_encoder.copy()
      vocab.update(self.added_tokens_encoder      )
      for i in range(self._utf_vocab_size      ):
       UpperCamelCase__       :     Union[str, Any] 							= chr(lowerCamelCase_      )
       UpperCamelCase__       :     int 							= i + len(self.special_tokens_encoder      )
      return vocab


     @property
     def   UpperCAmelCase__						(   self				:  List[str]      )	->   Optional[Any]:





      '''simple docstring'''






      return self._utf_vocab_size + self._num_special_tokens


     def   UpperCAmelCase__						(   self				:  Tuple				,						lowerCamelCase__				:  List[int]				,						lowerCamelCase__				:  Optional[List[int]] = None				,						lowerCamelCase__				:  bool = False      )	->   List[int]:





      '''simple docstring'''






      if already_has_special_tokens:
       return super().get_special_tokens_mask(
           token_ids_a=lowerCamelCase_				,						token_ids_a=lowerCamelCase_				,						already_has_special_tokens=lowerCamelCase_      )

      # normal case: some special tokens
      if token_ids_a is None:
       return [1] + [0] * len(lowerCamelCase_      ) + [1]
      return [1] + ([0] * len(lowerCamelCase_      )) + [1] + ([0] * len(lowerCamelCase_      )) + [1]


     def   UpperCAmelCase__						(   self				:  int				,						lowerCamelCase__				:  List[int]				,						lowerCamelCase__				:  Optional[List[int]] = None      )	->   List[int]:





      '''simple docstring'''






      if token_ids_a is None:
       return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
      else:
       return [self.cls_token_id] + token_ids_a + [self.sep_token_id] + token_ids_a + [self.sep_token_id]


     def   UpperCAmelCase__						(   self				:  List[str]				,						lowerCamelCase__				:  str      )	->   List[str]:





      '''simple docstring'''






      UpperCamelCase__       :     Any 							= [chr(lowerCamelCase_      ) for i in text.encode('''utf-8'''      )]
      return tokens


     def   UpperCAmelCase__						(   self				:  str				,						lowerCamelCase__				:  int      )	->   Optional[Any]:





      '''simple docstring'''






      if token in self.special_tokens_encoder:
       UpperCamelCase__       :     Optional[Any] 							= self.special_tokens_encoder[token]
      elif token in self.added_tokens_encoder:
       UpperCamelCase__       :     int 							= self.added_tokens_encoder[token]
      elif len(lowerCamelCase_      ) != 1:
       UpperCamelCase__       :     Any 							= self.unk_token_id
      else:
       UpperCamelCase__       :     str 							= ord(lowerCamelCase_      ) + self._num_special_tokens
      return token_id


     def   UpperCAmelCase__						(   self				:  Tuple				,						lowerCamelCase__				:  List[str]      )	->   Dict:





      '''simple docstring'''






      if index in self.special_tokens_decoder:
       UpperCamelCase__       :     int 							= self.special_tokens_decoder[index]
      elif index in self.added_tokens_decoder:
       UpperCamelCase__       :     Any 							= self.added_tokens_decoder[index]
      else:
       UpperCamelCase__       :     Any 							= chr(index - self._num_special_tokens      )
      return token


     def   UpperCAmelCase__						(   self				:  Any				,						lowerCamelCase__				:  Optional[Any]      )	->   Optional[Any]:





      '''simple docstring'''






      UpperCamelCase__       :     Tuple 							= b""""""
      for token in tokens:
       if token in self.special_tokens_decoder:
        UpperCamelCase__       :     int 							= self.special_tokens_decoder[token].encode('''utf-8'''      )
       elif token in self.added_tokens_decoder:
        UpperCamelCase__       :     Dict 							= self.special_tokens_decoder[token].encode('''utf-8'''      )
       elif token in self.special_tokens_encoder:
        UpperCamelCase__       :     Dict 							= token.encode('''utf-8'''      )
       elif token in self.added_tokens_encoder:
        UpperCamelCase__       :     Optional[Any] 							= token.encode('''utf-8'''      )
       else:
        UpperCamelCase__       :     List[str] 							= bytes([ord(lowerCamelCase_      )]      )
       bstring += tok_string
      UpperCamelCase__       :     Dict 							= bstring.decode('''utf-8'''				,						errors='''replace'''      )
      return string


     def   UpperCAmelCase__						(   self				:  Tuple				,						lowerCamelCase__				:  str				,						lowerCamelCase__				:  Optional[str] = None      )	->   Tuple[str]:





      '''simple docstring'''






      return ()







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


UpperCamelCase_									=   logging.get_logger(__name__)

UpperCamelCase_									=   {
    '''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				(     __snake_case ):






   '''simple docstring'''






   A__      : List[Any]       =						"layoutlmv2"


   def __init__(    self:     Tuple  ,lowerCamelCase_:     Dict=30522  ,lowerCamelCase_:     str=768  ,lowerCamelCase_:     Any=12  ,lowerCamelCase_:     Tuple=12  ,lowerCamelCase_:     int=3072  ,lowerCamelCase_:     int="gelu"  ,lowerCamelCase_:     Dict=0.1  ,lowerCamelCase_:     List[Any]=0.1  ,lowerCamelCase_:     List[Any]=512  ,lowerCamelCase_:     Tuple=2  ,lowerCamelCase_:     Dict=0.0_2  ,lowerCamelCase_:     Dict=1e-12  ,lowerCamelCase_:     Dict=0  ,lowerCamelCase_:     List[str]=1024  ,lowerCamelCase_:     Optional[Any]=128  ,lowerCamelCase_:     List[Any]=32  ,lowerCamelCase_:     List[Any]=True  ,lowerCamelCase_:     Dict=256  ,lowerCamelCase_:     int=64  ,lowerCamelCase_:     str=True  ,lowerCamelCase_:     Tuple=[7, 7, 256]  ,lowerCamelCase_:     Union[str, Any]=128  ,lowerCamelCase_:     Any=128  ,lowerCamelCase_:     int=True  ,lowerCamelCase_:     Union[str, Any]=True  ,lowerCamelCase_:     Optional[int]=False  ,lowerCamelCase_:     List[str]=None  ,**lowerCamelCase_:     str  ,)	->      Any:
       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_  ,**lowerCamelCase_  ,)
       UpperCAmelCase_     :       Optional[int]  		=					max_ad_position_embeddings
       UpperCAmelCase_     :       Optional[int]  		=					max_rel_pos
       UpperCAmelCase_     :       Optional[int]  		=					rel_pos_bins
       UpperCAmelCase_     :       List[str]  		=					fast_qkv
       UpperCAmelCase_     :       int  		=					max_rel_ad_pos
       UpperCAmelCase_     :       int  		=					rel_ad_pos_bins
       UpperCAmelCase_     :       Optional[Any]  		=					convert_sync_batchnorm
       UpperCAmelCase_     :       str  		=					image_feature_pool_shape
       UpperCAmelCase_     :       List[Any]  		=					coordinate_size
       UpperCAmelCase_     :       int  		=					shape_size
       UpperCAmelCase_     :       Optional[Any]  		=					has_relative_attention_bias
       UpperCAmelCase_     :       str  		=					has_spatial_attention_bias
       UpperCAmelCase_     :       Tuple  		=					has_visual_segment_embedding
       UpperCAmelCase_     :       Optional[Any]  		=					(
           detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
       )


   @classmethod
   def       A__    (    self:     Any							)	->      Dict:
       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": [[32], [64], [128], [256], [512]],
           "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
           "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 2000,
           "MODEL.RPN.PRE_NMS_TOPK_TEST": 1000,
           "MODEL.RPN.POST_NMS_TOPK_TRAIN": 1000,
           "MODEL.POST_NMS_TOPK_TEST": 1000,
           "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": 14,
           "MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
           "MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
           "MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
           "MODEL.RESNETS.DEPTH": 101,
           "MODEL.RESNETS.SIZES": [[32], [64], [128], [256], [512]],
           "MODEL.RESNETS.ASPECT_RATIOS": [[0.5, 1.0, 2.0]],
           "MODEL.RESNETS.OUT_FEATURES": ["res2", "res3", "res4", "res5"],
           "MODEL.RESNETS.NUM_GROUPS": 32,
           "MODEL.RESNETS.WIDTH_PER_GROUP": 8,
           "MODEL.RESNETS.STRIDE_IN_1X1": False,
       }






   def       A__    (    self:     Tuple							)	->      int:
       UpperCAmelCase_     :       Any  		=					detectrona.config.get_cfg()
       for k, v in self.detectrona_config_args.items():
           UpperCAmelCase_     :       Optional[int]  		=					k.split("""."""							)
           UpperCAmelCase_     :       List[str]  		=					detectrona_config
           for attribute in attributes[:-1]:
               UpperCAmelCase_     :       Optional[Any]  		=					getattr(lowerCamelCase_  ,lowerCamelCase_							)
           setattr(lowerCamelCase_  ,attributes[-1]  ,lowerCamelCase_							)

       return detectrona_config
