




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


_UpperCAmelCase						:    List[str]      =  logging.get_logger(__name__)

_UpperCAmelCase						:    str      =  {
    "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							(				lowerCAmelCase):
       _a			       =    '''layoutlmv2'''






       def __init__( self:       Optional[Any]							,       _lowerCAmelCase:       Optional[int]=3_05_22							,       _lowerCAmelCase:       List[Any]=7_68							,       _lowerCAmelCase:       Union[str, Any]=12							,       _lowerCAmelCase:       Optional[int]=12							,       _lowerCAmelCase:       Tuple=30_72							,       _lowerCAmelCase:       Optional[Any]="gelu"							,       _lowerCAmelCase:       List[str]=0.1							,       _lowerCAmelCase:       Optional[Any]=0.1							,       _lowerCAmelCase:       Any=5_12							,       _lowerCAmelCase:       Optional[Any]=2							,       _lowerCAmelCase:       Dict=0.02							,       _lowerCAmelCase:       int=1e-1_2							,       _lowerCAmelCase:       Tuple=0							,       _lowerCAmelCase:       Union[str, Any]=10_24							,       _lowerCAmelCase:       Optional[int]=1_28							,       _lowerCAmelCase:       Tuple=32							,       _lowerCAmelCase:       Tuple=True							,       _lowerCAmelCase:       Tuple=2_56							,       _lowerCAmelCase:       Tuple=64							,       _lowerCAmelCase:       Dict=True							,       _lowerCAmelCase:       Dict=[7, 7, 2_56]							,       _lowerCAmelCase:       str=1_28							,       _lowerCAmelCase:       Tuple=1_28							,       _lowerCAmelCase:       List[Any]=True							,       _lowerCAmelCase:       Optional[int]=True							,       _lowerCAmelCase:       List[str]=False							,       _lowerCAmelCase:       Optional[int]=None							,       **_lowerCAmelCase:       str							,       ):
         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							,       )
         lowercase     :str   						=    max_ad_position_embeddings
         lowercase     :int   						=    max_rel_pos
         lowercase     :Tuple   						=    rel_pos_bins
         lowercase     :int   						=    fast_qkv
         lowercase     :Tuple   						=    max_rel_ad_pos
         lowercase     :int   						=    rel_ad_pos_bins
         lowercase     :List[Any]   						=    convert_sync_batchnorm
         lowercase     :List[str]   						=    image_feature_pool_shape
         lowercase     :Union[str, Any]   						=    coordinate_size
         lowercase     :Union[str, Any]   						=    shape_size
         lowercase     :Optional[int]   						=    has_relative_attention_bias
         lowercase     :Optional[int]   						=    has_spatial_attention_bias
         lowercase     :Union[str, Any]   						=    has_visual_segment_embedding
         lowercase     :List[Any]   						=    (
             detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
         )






       @classmethod
       def        SCREAMING_SNAKE_CASE     ( self:       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": [[32], [64], [1_28], [2_56], [5_12]],
             "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
             "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 20_00,
             "MODEL.RPN.PRE_NMS_TOPK_TEST": 10_00,
             "MODEL.RPN.POST_NMS_TOPK_TRAIN": 10_00,
             "MODEL.POST_NMS_TOPK_TEST": 10_00,
             "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": 1_01,
             "MODEL.RESNETS.SIZES": [[32], [64], [1_28], [2_56], [5_12]],
             "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        SCREAMING_SNAKE_CASE     ( self:       str					):
         lowercase     :Tuple   						=    detectrona.config.get_cfg()
         for k, v in self.detectrona_config_args.items():
           lowercase     :Tuple   						=    k.split("."					)
           lowercase     :Optional[Any]   						=    detectrona_config
           for attribute in attributes[:-1]:
             lowercase     :Tuple   						=    getattr(_lowerCAmelCase							,       _lowerCAmelCase					)
           setattr(_lowerCAmelCase							,       attributes[-1]							,       _lowerCAmelCase					)

         return detectrona_config









import logging
from pathlib import Path

import numpy as np
import pytorch_lightning as pl
import torch
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.utilities import rank_zero_only

from utils import save_json



def    UpperCAmelCase__	(  lowerCamelCase ):
  lowercase     :Optional[Any]   						=    filter(lambda lowerCamelCase : p.requires_grad,      model.parameters() )
  lowercase     :Optional[int]   						=    sum([np.prod(p.size() ) for p in model_parameters] )
  return params


_UpperCAmelCase						:    Optional[Any]      =  logging.getLogger(__name__)




class  __lowerCAmelCase							(				pl.Callback):






       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]							,       _lowerCAmelCase:       List[Any]							,       _lowerCAmelCase:       Union[str, Any]					):
         lowercase     :Dict   						=    {F"lr_group_{i}": param["lr"] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups					)}
         pl_module.logger.log_metrics(_lowerCAmelCase					)






       @rank_zero_only
       def        SCREAMING_SNAKE_CASE     ( self:       Optional[Any]							,       _lowerCAmelCase:       pl.Trainer							,       _lowerCAmelCase:       pl.LightningModule							,       _lowerCAmelCase:       str							,       _lowerCAmelCase:       List[str]=True					):
         logger.info(F"***** {type_path} results at step {trainer.global_step:05d} *****"					)
         lowercase     :Tuple   						=    trainer.callback_metrics
         trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ["log", "progress_bar", "preds"]}					)
         # Log results
         lowercase     :Optional[int]   						=    Path(pl_module.hparams.output_dir					)
         if type_path == "test":
           lowercase     :List[Any]   						=    od / "test_results.txt"
           lowercase     :Optional[int]   						=    od / "test_generations.txt"
         else:
           # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json
           # If people want this it will be easy enough to add back.
           lowercase     :int   						=    od / F"{type_path}_results/{trainer.global_step:05d}.txt"
           lowercase     :Tuple   						=    od / F"{type_path}_generations/{trainer.global_step:05d}.txt"
           results_file.parent.mkdir(exist_ok=_lowerCAmelCase					)
           generations_file.parent.mkdir(exist_ok=_lowerCAmelCase					)
         with open(_lowerCAmelCase							,       "a+"					) as writer:
           for key in sorted(_lowerCAmelCase					):
             if key in ["log", "progress_bar", "preds"]:
               continue
             lowercase     :int   						=    metrics[key]
             if isinstance(_lowerCAmelCase							,       torch.Tensor					):
               lowercase     :int   						=    val.item()
             lowercase     :Optional[Any]   						=    F"{key}: {val:.6f}\n"
             writer.write(_lowerCAmelCase					)

         if not save_generations:
           return

         if "preds" in metrics:
           lowercase     :str   						=    "\n".join(metrics["preds"]					)
           generations_file.open("w+"					).write(_lowerCAmelCase					)






       @rank_zero_only
       def        SCREAMING_SNAKE_CASE     ( self:       Optional[int]							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       Tuple					):
         try:
           lowercase     :Optional[int]   						=    pl_module.model.model.num_parameters()
         except AttributeError:
           lowercase     :Dict   						=    pl_module.model.num_parameters()

         lowercase     :Any   						=    count_trainable_parameters(_lowerCAmelCase					)
         # mp stands for million parameters
         trainer.logger.log_metrics({"n_params": npars, "mp": npars / 1e6, "grad_mp": n_trainable_pars / 1e6}					)






       @rank_zero_only
       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]							,       _lowerCAmelCase:       pl.Trainer							,       _lowerCAmelCase:       pl.LightningModule					):
         save_json(pl_module.metrics							,       pl_module.metrics_save_path					)
         return self._write_logs(_lowerCAmelCase							,       _lowerCAmelCase							,       "test"					)






       @rank_zero_only
       def        SCREAMING_SNAKE_CASE     ( self:       List[str]							,       _lowerCAmelCase:       pl.Trainer							,       _lowerCAmelCase:       Dict					):
         save_json(pl_module.metrics							,       pl_module.metrics_save_path					)
         # Uncommenting this will save val generations
         # return self._write_logs(trainer, pl_module, "valid")







def    UpperCAmelCase__	(  lowerCamelCase,      lowerCamelCase,      lowerCamelCase=1,      lowerCamelCase=False ):
  if metric == "rouge2":
    lowercase     :str   						=    "{val_avg_rouge2:.4f}-{step_count}"
  elif metric == "bleu":
    lowercase     :int   						=    "{val_avg_bleu:.4f}-{step_count}"
  elif metric == "loss":
    lowercase     :List[str]   						=    "{val_avg_loss:.4f}-{step_count}"
  else:
    raise NotImplementedError(
        F"seq2seq callbacks only support rouge2, bleu and loss, got {metric}, You can make your own by adding to"
        " this function." )

  lowercase     :int   						=    ModelCheckpoint(
      dirpath=lowerCamelCase,      filename=lowerCamelCase,      monitor=F"val_{metric}",      mode="min" if "loss" in metric else "max",      save_top_k=lowerCamelCase,      )
  return checkpoint_callback







def    UpperCAmelCase__	(  lowerCamelCase,      lowerCamelCase ):
  return EarlyStopping(
      monitor=F"val_{metric}",      mode="min" if "loss" in metric else "max",      patience=lowerCamelCase,      verbose=lowerCamelCase,      )



