


"""simple docstring"""


import inspect
import warnings

import torch

from .state import AcceleratorState, GradientState
from .utils import DistributedType, honor_type, is_tpu_available


if is_tpu_available(check_device=False):
		import torch_xla.core.xla_model as xm






def    a__       ( __lowercase   ,	__lowercase      )		->		Optional[int]:
				if isinstance(__lowercase   ,	(list, tuple)      ):
								return honor_type(__lowercase   ,	(move_to_device(__lowercase   ,	__lowercase      ) for t in state)      )
				elif isinstance(__lowercase   ,	__lowercase      ):
								return type(__lowercase      )({k: move_to_device(__lowercase   ,	__lowercase      ) for k, v in state.items()}      )
				elif isinstance(__lowercase   ,	torch.Tensor      ):
								return state.to(__lowercase      )
				return state




class snake_case					(     torch.optim.Optimizer):







		def __init__(    self					:				str			,      a__					:				List[Any]			,      a__					:				Optional[int]=True			,      a__					:				Dict=None						)				->  Union[str, Any]:





						'''simple docstring'''







						_A         =	optimizer
						_A         =	scaler
						_A         =	AcceleratorState()
						_A         =	GradientState()
						_A         =	device_placement
						_A         =	False
						_A         =	None

						# Handle device placement
						if device_placement:
										_A         =	self.optimizer.state_dict()
										if self.accelerator_state.distributed_type == DistributedType.TPU:
														xm.send_cpu_data_to_device(a__			,      self.accelerator_state.device						)
										else:
														_A         =	move_to_device(a__			,      self.accelerator_state.device						)
										self.optimizer.load_state_dict(a__						)







		@property
		def        a_       (    self					:				Optional[Any]						)				->  Tuple:





						'''simple docstring'''







						return self.optimizer.state







		@state.setter
		def        a_       (    self					:				Dict			,      a__					:				Any						)				->  Dict:





						'''simple docstring'''







						_A         =	state







		@property
		def        a_       (    self					:				List[Any]						)				->  Any:





						'''simple docstring'''







						return self.optimizer.param_groups







		@param_groups.setter
		def        a_       (    self					:				Any			,      a__					:				Tuple						)				->  str:





						'''simple docstring'''







						_A         =	param_groups







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





						'''simple docstring'''







						return self.optimizer.defaults







		@defaults.setter
		def        a_       (    self					:				int			,      a__					:				List[str]						)				->  Optional[int]:





						'''simple docstring'''







						_A         =	defaults







		def        a_       (    self					:				Tuple			,      a__					:				List[Any]						)				->  str:





						'''simple docstring'''







						self.optimizer.add_param_group(a__						)







		def        a_       (    self					:				Any			,      a__					:				Any						)				->  List[str]:





						'''simple docstring'''







						if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
										xm.send_cpu_data_to_device(a__			,      self.accelerator_state.device						)
						self.optimizer.load_state_dict(a__						)







		def        a_       (    self					:				Optional[Any]						)				->  Dict:





						'''simple docstring'''







						return self.optimizer.state_dict()







		def        a_       (    self					:				Any			,      a__					:				List[str]=None						)				->  str:





						'''simple docstring'''







						if self.gradient_state.sync_gradients:
										_A         =	"set_to_none" in inspect.signature(self.optimizer.zero_grad						).parameters
										if accept_arg:
														if set_to_none is None:
																		_A         =	False
														self.optimizer.zero_grad(set_to_none=a__						)
										else:
														if set_to_none is not None:
																		raise ValueError("`set_to_none` for Optimizer.zero_grad` is not supported by this optimizer."						)
														self.optimizer.zero_grad()







		def        a_       (    self					:				Union[str, Any]			,      a__					:				int=None						)				->  List[Any]:





						'''simple docstring'''







						if self.gradient_state.sync_gradients:
										if self.accelerator_state.distributed_type == DistributedType.TPU:
														_A         =	{"closure": closure} if closure is not None else {}
														xm.optimizer_step(self.optimizer			,      optimizer_args=a__						)
										elif self.scaler is not None:
														_A         =	False
														if self._last_scale is None:
																		# `get_scale` is an async operation requiring full synchronization
																		# on CPU and GPUs before finishing. As a result, we store away
																		# the prior one to reduce the call overhead
																		_A         =	self.scaler.get_scale()
																		_A         =	True
														self.scaler.step(self.optimizer			,      a__						)
														self.scaler.update()
														_A         =	self.scaler.get_scale()
														if not new_scale:
																		# If we reduced the loss scale, it means the optimizer step was skipped because of gradient overflow.
																		_A         =	scale_after < self._last_scale
														_A         =	scale_after

										else:
														self.optimizer.step(a__						)







		def        a_       (    self					:				Optional[int]			,      a__					:				Dict						)				->  Tuple:





						'''simple docstring'''







						for param_group in self.optimizer.param_groups:
										_A         =	[parameters_map.get(a__			,      a__						) for p in param_group["params"]]







		@property
		def        a_       (    self					:				Optional[int]						)				->  Union[str, Any]:





						'''simple docstring'''







						warnings.warn(
						    "The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use "
						    "`optimizer.step_was_skipped` instead."			,      a__			,      )
						return self._is_overflow







		@property
		def        a_       (    self					:				Any						)				->  Any:





						'''simple docstring'''







						return self._is_overflow







		def __getstate__(    self					:				str						)				->  int:





						'''simple docstring'''







						return self.__dict__.copy()







		def __setstate__(    self					:				Union[str, Any]			,      a__					:				str						)				->  Any:





						'''simple docstring'''







						self.__dict__.update(a__						)






"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import is_detectrona_available, logging


__UpperCamelCase			:		List[Any]		    =   logging.get_logger(__name__)

__UpperCamelCase			:		Union[str, 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 	a				(					a__   ):
 snake_case__    	=    '''layoutlmv2'''



 def __init__(			self							,    _snake_case=3_05_22							,    _snake_case=7_68							,    _snake_case=12							,    _snake_case=12							,    _snake_case=30_72							,    _snake_case="gelu"							,    _snake_case=0.1							,    _snake_case=0.1							,    _snake_case=5_12							,    _snake_case=2							,    _snake_case=0.02							,    _snake_case=1E-12							,    _snake_case=0							,    _snake_case=10_24							,    _snake_case=1_28							,    _snake_case=32							,    _snake_case=True							,    _snake_case=2_56							,    _snake_case=64							,    _snake_case=True							,    _snake_case=[7, 7, 2_56]							,    _snake_case=1_28							,    _snake_case=1_28							,    _snake_case=True							,    _snake_case=True							,    _snake_case=False							,    _snake_case=None							,    **_snake_case							,    ):







  """simple docstring"""






  super().__init__(
      vocab_size=_snake_case							,    hidden_size=_snake_case							,    num_hidden_layers=_snake_case							,    num_attention_heads=_snake_case							,    intermediate_size=_snake_case							,    hidden_act=_snake_case							,    hidden_dropout_prob=_snake_case							,    attention_probs_dropout_prob=_snake_case							,    max_position_embeddings=_snake_case							,    type_vocab_size=_snake_case							,    initializer_range=_snake_case							,    layer_norm_eps=_snake_case							,    pad_token_id=_snake_case							,    **_snake_case							,    )
  lowerCAmelCase  							=      max_ad_position_embeddings
  lowerCAmelCase  							=      max_rel_pos
  lowerCAmelCase  							=      rel_pos_bins
  lowerCAmelCase  							=      fast_qkv
  lowerCAmelCase  							=      max_rel_ad_pos
  lowerCAmelCase  							=      rel_ad_pos_bins
  lowerCAmelCase  							=      convert_sync_batchnorm
  lowerCAmelCase  							=      image_feature_pool_shape
  lowerCAmelCase  							=      coordinate_size
  lowerCAmelCase  							=      shape_size
  lowerCAmelCase  							=      has_relative_attention_bias
  lowerCAmelCase  							=      has_spatial_attention_bias
  lowerCAmelCase  							=      has_visual_segment_embedding
  lowerCAmelCase  							=      (
      detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
  )



 @classmethod
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  return {
      "MODEL.MASK_ON": True,
      "MODEL.PIXEL_STD": [57.375, 57.120, 58.395],
      "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 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      detectrona.config.get_cfg()
  for k, v in self.detectrona_config_args.items():
   lowerCAmelCase  							=      k.split('.'			)
   lowerCAmelCase  							=      detectrona_config
   for attribute in attributes[:-1]:
    lowerCAmelCase  							=      getattr(_snake_case							,    _snake_case			)
   setattr(_snake_case							,    attributes[-1]							,    _snake_case			)

  return detectrona_config





