





"""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












"""simple docstring"""
import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest







class 	a				(					a__   ):
 snake_case__    	=    (HeunDiscreteScheduler,)
 snake_case__    	=    1_0



 def 					UpperCamelCase__   (			self							,    **_snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      {
      'num_train_timesteps': 11_00,
      'beta_start': 0.0_001,
      'beta_end': 0.02,
      'beta_schedule': 'linear',
  }

  config.update(**_snake_case			)
  return config



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  for timesteps in [10, 50, 1_00, 10_00]:
   self.check_over_configs(num_train_timesteps=_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001]							,    [0.0_002, 0.002, 0.02]			):
   self.check_over_configs(beta_start=_snake_case							,    beta_end=_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  for schedule in ["linear", "scaled_linear", "exp"]:
   self.check_over_configs(beta_schedule=_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  for clip_sample_range in [1.0, 2.0, 3.0]:
   self.check_over_configs(clip_sample_range=_snake_case							,    clip_sample=_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  for prediction_type in ["epsilon", "v_prediction", "sample"]:
   self.check_over_configs(prediction_type=_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.scheduler_classes[0]
  lowerCAmelCase  							=      self.get_scheduler_config()
  lowerCAmelCase  							=      scheduler_class(**_snake_case			)

  scheduler.set_timesteps(self.num_inference_steps			)

  lowerCAmelCase  							=      self.dummy_model()
  lowerCAmelCase  							=      self.dummy_sample_deter * scheduler.init_noise_sigma
  lowerCAmelCase  							=      sample.to(_snake_case			)

  for i, t in enumerate(scheduler.timesteps			):
   lowerCAmelCase  							=      scheduler.scale_model_input(_snake_case							,    _snake_case			)

   lowerCAmelCase  							=      model(_snake_case							,    _snake_case			)

   lowerCAmelCase  							=      scheduler.step(_snake_case							,    _snake_case							,    _snake_case			)
   lowerCAmelCase  							=      output.prev_sample

  lowerCAmelCase  							=      torch.sum(torch.abs(_snake_case			)			)
  lowerCAmelCase  							=      torch.mean(torch.abs(_snake_case			)			)

  if torch_device in ["cpu", "mps"]:
   assert abs(result_sum.item() - 0.1_233			) < 1E-2
   assert abs(result_mean.item() - 0.0_002			) < 1E-3
  else:
   # CUDA
   assert abs(result_sum.item() - 0.1_233			) < 1E-2
   assert abs(result_mean.item() - 0.0_002			) < 1E-3



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.scheduler_classes[0]
  lowerCAmelCase  							=      self.get_scheduler_config(prediction_type='v_prediction'			)
  lowerCAmelCase  							=      scheduler_class(**_snake_case			)

  scheduler.set_timesteps(self.num_inference_steps			)

  lowerCAmelCase  							=      self.dummy_model()
  lowerCAmelCase  							=      self.dummy_sample_deter * scheduler.init_noise_sigma
  lowerCAmelCase  							=      sample.to(_snake_case			)

  for i, t in enumerate(scheduler.timesteps			):
   lowerCAmelCase  							=      scheduler.scale_model_input(_snake_case							,    _snake_case			)

   lowerCAmelCase  							=      model(_snake_case							,    _snake_case			)

   lowerCAmelCase  							=      scheduler.step(_snake_case							,    _snake_case							,    _snake_case			)
   lowerCAmelCase  							=      output.prev_sample

  lowerCAmelCase  							=      torch.sum(torch.abs(_snake_case			)			)
  lowerCAmelCase  							=      torch.mean(torch.abs(_snake_case			)			)

  if torch_device in ["cpu", "mps"]:
   assert abs(result_sum.item() - 4.69_34E-07			) < 1E-2
   assert abs(result_mean.item() - 6.11_12E-10			) < 1E-3
  else:
   # CUDA
   assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72E-07			) < 1E-2
   assert abs(result_mean.item() - 0.0_002			) < 1E-3



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.scheduler_classes[0]
  lowerCAmelCase  							=      self.get_scheduler_config()
  lowerCAmelCase  							=      scheduler_class(**_snake_case			)

  scheduler.set_timesteps(self.num_inference_steps							,    device=_snake_case			)

  lowerCAmelCase  							=      self.dummy_model()
  lowerCAmelCase  							=      self.dummy_sample_deter.to(_snake_case			) * scheduler.init_noise_sigma

  for t in scheduler.timesteps:
   lowerCAmelCase  							=      scheduler.scale_model_input(_snake_case							,    _snake_case			)

   lowerCAmelCase  							=      model(_snake_case							,    _snake_case			)

   lowerCAmelCase  							=      scheduler.step(_snake_case							,    _snake_case							,    _snake_case			)
   lowerCAmelCase  							=      output.prev_sample

  lowerCAmelCase  							=      torch.sum(torch.abs(_snake_case			)			)
  lowerCAmelCase  							=      torch.mean(torch.abs(_snake_case			)			)

  if str(_snake_case			).startswith('cpu'			):
   # The following sum varies between 148 and 156 on mps. Why?
   assert abs(result_sum.item() - 0.1_233			) < 1E-2
   assert abs(result_mean.item() - 0.0_002			) < 1E-3
  elif str(_snake_case			).startswith('mps'			):
   # Larger tolerance on mps
   assert abs(result_mean.item() - 0.0_002			) < 1E-2
  else:
   # CUDA
   assert abs(result_sum.item() - 0.1_233			) < 1E-2
   assert abs(result_mean.item() - 0.0_002			) < 1E-3



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.scheduler_classes[0]
  lowerCAmelCase  							=      self.get_scheduler_config()
  lowerCAmelCase  							=      scheduler_class(**_snake_case							,    use_karras_sigmas=_snake_case			)

  scheduler.set_timesteps(self.num_inference_steps							,    device=_snake_case			)

  lowerCAmelCase  							=      self.dummy_model()
  lowerCAmelCase  							=      self.dummy_sample_deter.to(_snake_case			) * scheduler.init_noise_sigma
  lowerCAmelCase  							=      sample.to(_snake_case			)

  for t in scheduler.timesteps:
   lowerCAmelCase  							=      scheduler.scale_model_input(_snake_case							,    _snake_case			)

   lowerCAmelCase  							=      model(_snake_case							,    _snake_case			)

   lowerCAmelCase  							=      scheduler.step(_snake_case							,    _snake_case							,    _snake_case			)
   lowerCAmelCase  							=      output.prev_sample

  lowerCAmelCase  							=      torch.sum(torch.abs(_snake_case			)			)
  lowerCAmelCase  							=      torch.mean(torch.abs(_snake_case			)			)

  assert abs(result_sum.item() - 0.00_015			) < 1E-2
  assert abs(result_mean.item() - 1.98_69_55_45_35_03_46_95E-07			) < 1E-2





