






from dataclasses import dataclass
from typing import Dict, Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .attention_processor import AttentionProcessor, AttnProcessor
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder


@dataclass
class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   42




class 						_a       (  UpperCAmelCase__						,       UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   True




  @register_to_config
  def __init__(     self		,		_UpperCAmelCase = 3		,		_UpperCAmelCase = 3		,		_UpperCAmelCase = ("DownEncoderBlock2D",)		,		_UpperCAmelCase = ("UpDecoderBlock2D",)		,		_UpperCAmelCase = (64,)		,		_UpperCAmelCase = 1		,		_UpperCAmelCase = "silu"		,		_UpperCAmelCase = 4		,		_UpperCAmelCase = 32		,		_UpperCAmelCase = 32		,		_UpperCAmelCase = 0.1_8_2_1_5		,		)			-> Optional[Any]:
     super().__init__()

     # pass init params to Encoder
     UpperCamelCase_									=     Encoder(
         in_channels=_UpperCAmelCase		,		out_channels=_UpperCAmelCase		,		down_block_types=_UpperCAmelCase		,		block_out_channels=_UpperCAmelCase		,		layers_per_block=_UpperCAmelCase		,		act_fn=_UpperCAmelCase		,		norm_num_groups=_UpperCAmelCase		,		double_z=_UpperCAmelCase		,		)

     # pass init params to Decoder
     UpperCamelCase_									=     Decoder(
         in_channels=_UpperCAmelCase		,		out_channels=_UpperCAmelCase		,		up_block_types=_UpperCAmelCase		,		block_out_channels=_UpperCAmelCase		,		layers_per_block=_UpperCAmelCase		,		norm_num_groups=_UpperCAmelCase		,		act_fn=_UpperCAmelCase		,		)

     UpperCamelCase_									=     nn.Convad(2 * latent_channels		,		2 * latent_channels		,		1 )
     UpperCamelCase_									=     nn.Convad(_UpperCAmelCase		,		_UpperCAmelCase		,		1 )

     UpperCamelCase_									=     False
     UpperCamelCase_									=     False

     # only relevant if vae tiling is enabled
     UpperCamelCase_									=     self.config.sample_size
     UpperCamelCase_									=     (
         self.config.sample_size[0]
         if isinstance(self.config.sample_size		,		(list, tuple) )
         else self.config.sample_size
     )
     UpperCamelCase_									=     int(sample_size / (2 ** (len(self.config.block_out_channels ) - 1)) )
     UpperCamelCase_									=     0.2_5




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase=False )			-> Union[str, Any]:
     if isinstance(_UpperCAmelCase		,		(Encoder, Decoder) ):
        UpperCamelCase_									=     value




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase = True )			-> str:
     UpperCamelCase_									=     use_tiling




  def    _UpperCAmelCase     (     self )			-> Union[str, Any]:
     self.enable_tiling(_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> Tuple:
     UpperCamelCase_									=     True




  def    _UpperCAmelCase     (     self )			-> int:
     UpperCamelCase_									=     False




  @property
  # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
  def    _UpperCAmelCase     (     self )			-> Dict[str, AttentionProcessor]:
     UpperCamelCase_									=     {}

     def fn_recursive_add_processors(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase ):
        if hasattr(_UpperCAmelCase		,		'set_processor' ):
           UpperCamelCase_									=     module.processor

        for sub_name, child in module.named_children():
           fn_recursive_add_processors(f"""{name}.{sub_name}"""		,		_UpperCAmelCase		,		_UpperCAmelCase )

        return processors

     for name, module in self.named_children():
        fn_recursive_add_processors(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )

     return processors




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase )			-> Optional[Any]:
     UpperCamelCase_									=     len(self.attn_processors.keys() )

     if isinstance(_UpperCAmelCase		,		_UpperCAmelCase ) and len(_UpperCAmelCase ) != count:
        raise ValueError(
            f"""A dict of processors was passed, but the number of processors {len(_UpperCAmelCase )} does not match the"""
            f""" number of attention layers: {count}. Please make sure to pass {count} processor classes.""" )

     def fn_recursive_attn_processor(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase ):
        if hasattr(_UpperCAmelCase		,		'set_processor' ):
           if not isinstance(_UpperCAmelCase		,		_UpperCAmelCase ):
              module.set_processor(_UpperCAmelCase )
           else:
              module.set_processor(processor.pop(f"""{name}.processor""" ) )

        for sub_name, child in module.named_children():
           fn_recursive_attn_processor(f"""{name}.{sub_name}"""		,		_UpperCAmelCase		,		_UpperCAmelCase )

     for name, module in self.named_children():
        fn_recursive_attn_processor(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> int:
     self.set_attn_processor(AttnProcessor() )




  @apply_forward_hook
  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase = True )			-> AutoencoderKLOutput:
     if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size):
        return self.tiled_encode(_UpperCAmelCase		,		return_dict=_UpperCAmelCase )

     if self.use_slicing and x.shape[0] > 1:
        UpperCamelCase_									=     [self.encoder(_UpperCAmelCase ) for x_slice in x.split(1 )]
        UpperCamelCase_									=     torch.cat(_UpperCAmelCase )
     else:
        UpperCamelCase_									=     self.encoder(_UpperCAmelCase )

     UpperCamelCase_									=     self.quant_conv(_UpperCAmelCase )
     UpperCamelCase_									=     DiagonalGaussianDistribution(_UpperCAmelCase )

     if not return_dict:
        return (posterior,)

     return AutoencoderKLOutput(latent_dist=_UpperCAmelCase )




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase = True )			-> Union[DecoderOutput, torch.FloatTensor]:
     if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size):
        return self.tiled_decode(_UpperCAmelCase		,		return_dict=_UpperCAmelCase )

     UpperCamelCase_									=     self.post_quant_conv(_UpperCAmelCase )
     UpperCamelCase_									=     self.decoder(_UpperCAmelCase )

     if not return_dict:
        return (dec,)

     return DecoderOutput(sample=_UpperCAmelCase )




  @apply_forward_hook
  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase = True )			-> Union[DecoderOutput, torch.FloatTensor]:
     if self.use_slicing and z.shape[0] > 1:
        UpperCamelCase_									=     [self._decode(_UpperCAmelCase ).sample for z_slice in z.split(1 )]
        UpperCamelCase_									=     torch.cat(_UpperCAmelCase )
     else:
        UpperCamelCase_									=     self._decode(_UpperCAmelCase ).sample

     if not return_dict:
        return (decoded,)

     return DecoderOutput(sample=_UpperCAmelCase )




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> Any:
     UpperCamelCase_									=     min(a.shape[2]		,		b.shape[2]		,		_UpperCAmelCase )
     for y in range(_UpperCAmelCase ):
        UpperCamelCase_									=     a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
     return b




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> Dict:
     UpperCamelCase_									=     min(a.shape[3]		,		b.shape[3]		,		_UpperCAmelCase )
     for x in range(_UpperCAmelCase ):
        UpperCamelCase_									=     a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
     return b




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase = True )			-> AutoencoderKLOutput:
     UpperCamelCase_									=     int(self.tile_sample_min_size * (1 - self.tile_overlap_factor) )
     UpperCamelCase_									=     int(self.tile_latent_min_size * self.tile_overlap_factor )
     UpperCamelCase_									=     self.tile_latent_min_size - blend_extent

     # Split the image into 512x512 tiles and encode them separately.
     UpperCamelCase_									=     []
     for i in range(0		,		x.shape[2]		,		_UpperCAmelCase ):
        UpperCamelCase_									=     []
        for j in range(0		,		x.shape[3]		,		_UpperCAmelCase ):
           UpperCamelCase_									=     x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
           UpperCamelCase_									=     self.encoder(_UpperCAmelCase )
           UpperCamelCase_									=     self.quant_conv(_UpperCAmelCase )
           row.append(_UpperCAmelCase )
        rows.append(_UpperCAmelCase )
     UpperCamelCase_									=     []
     for i, row in enumerate(_UpperCAmelCase ):
        UpperCamelCase_									=     []
        for j, tile in enumerate(_UpperCAmelCase ):
           # blend the above tile and the left tile
           # to the current tile and add the current tile to the result row
           if i > 0:
              UpperCamelCase_									=     self.blend_v(rows[i - 1][j]		,		_UpperCAmelCase		,		_UpperCAmelCase )
           if j > 0:
              UpperCamelCase_									=     self.blend_h(row[j - 1]		,		_UpperCAmelCase		,		_UpperCAmelCase )
           result_row.append(tile[:, :, :row_limit, :row_limit] )
        result_rows.append(torch.cat(_UpperCAmelCase		,		dim=3 ) )

     UpperCamelCase_									=     torch.cat(_UpperCAmelCase		,		dim=2 )
     UpperCamelCase_									=     DiagonalGaussianDistribution(_UpperCAmelCase )

     if not return_dict:
        return (posterior,)

     return AutoencoderKLOutput(latent_dist=_UpperCAmelCase )




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase = True )			-> Union[DecoderOutput, torch.FloatTensor]:
     UpperCamelCase_									=     int(self.tile_latent_min_size * (1 - self.tile_overlap_factor) )
     UpperCamelCase_									=     int(self.tile_sample_min_size * self.tile_overlap_factor )
     UpperCamelCase_									=     self.tile_sample_min_size - blend_extent

     # Split z into overlapping 64x64 tiles and decode them separately.
     # The tiles have an overlap to avoid seams between tiles.
     UpperCamelCase_									=     []
     for i in range(0		,		z.shape[2]		,		_UpperCAmelCase ):
        UpperCamelCase_									=     []
        for j in range(0		,		z.shape[3]		,		_UpperCAmelCase ):
           UpperCamelCase_									=     z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
           UpperCamelCase_									=     self.post_quant_conv(_UpperCAmelCase )
           UpperCamelCase_									=     self.decoder(_UpperCAmelCase )
           row.append(_UpperCAmelCase )
        rows.append(_UpperCAmelCase )
     UpperCamelCase_									=     []
     for i, row in enumerate(_UpperCAmelCase ):
        UpperCamelCase_									=     []
        for j, tile in enumerate(_UpperCAmelCase ):
           # blend the above tile and the left tile
           # to the current tile and add the current tile to the result row
           if i > 0:
              UpperCamelCase_									=     self.blend_v(rows[i - 1][j]		,		_UpperCAmelCase		,		_UpperCAmelCase )
           if j > 0:
              UpperCamelCase_									=     self.blend_h(row[j - 1]		,		_UpperCAmelCase		,		_UpperCAmelCase )
           result_row.append(tile[:, :, :row_limit, :row_limit] )
        result_rows.append(torch.cat(_UpperCAmelCase		,		dim=3 ) )

     UpperCamelCase_									=     torch.cat(_UpperCAmelCase		,		dim=2 )
     if not return_dict:
        return (dec,)

     return DecoderOutput(sample=_UpperCAmelCase )




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase = False		,		_UpperCAmelCase = True		,		_UpperCAmelCase = None		,		)			-> Union[DecoderOutput, torch.FloatTensor]:
     UpperCamelCase_									=     sample
     UpperCamelCase_									=     self.encode(_UpperCAmelCase ).latent_dist
     if sample_posterior:
        UpperCamelCase_									=     posterior.sample(generator=_UpperCAmelCase )
     else:
        UpperCamelCase_									=     posterior.mode()
     UpperCamelCase_									=     self.decode(_UpperCAmelCase ).sample

     if not return_dict:
        return (dec,)

     return DecoderOutput(sample=_UpperCAmelCase )













from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


if TYPE_CHECKING:
 from ...processing_utils import ProcessorMixin
 from ...utils import TensorType


snake_case__      :	Tuple       	=      logging.get_logger(__name__)

snake_case__      :	str       	=      {
    """microsoft/layoutlmv3-base""": """https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json""",
}


class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   """layoutlmv3"""




  def __init__(     self		,		_UpperCAmelCase=50265		,		_UpperCAmelCase=768		,		_UpperCAmelCase=12		,		_UpperCAmelCase=12		,		_UpperCAmelCase=3072		,		_UpperCAmelCase="gelu"		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=512		,		_UpperCAmelCase=2		,		_UpperCAmelCase=0.0_2		,		_UpperCAmelCase=1e-5		,		_UpperCAmelCase=1		,		_UpperCAmelCase=0		,		_UpperCAmelCase=2		,		_UpperCAmelCase=1024		,		_UpperCAmelCase=128		,		_UpperCAmelCase=128		,		_UpperCAmelCase=True		,		_UpperCAmelCase=32		,		_UpperCAmelCase=128		,		_UpperCAmelCase=64		,		_UpperCAmelCase=256		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=224		,		_UpperCAmelCase=3		,		_UpperCAmelCase=16		,		_UpperCAmelCase=None		,		**_UpperCAmelCase		,		)			-> Tuple:
     super().__init__(
         vocab_size=_UpperCAmelCase		,		hidden_size=_UpperCAmelCase		,		num_hidden_layers=_UpperCAmelCase		,		num_attention_heads=_UpperCAmelCase		,		intermediate_size=_UpperCAmelCase		,		hidden_act=_UpperCAmelCase		,		hidden_dropout_prob=_UpperCAmelCase		,		attention_probs_dropout_prob=_UpperCAmelCase		,		max_position_embeddings=_UpperCAmelCase		,		type_vocab_size=_UpperCAmelCase		,		initializer_range=_UpperCAmelCase		,		layer_norm_eps=_UpperCAmelCase		,		pad_token_id=_UpperCAmelCase		,		bos_token_id=_UpperCAmelCase		,		eos_token_id=_UpperCAmelCase		,		**_UpperCAmelCase		,		)
     UpperCamelCase_									=     max_ad_position_embeddings
     UpperCamelCase_									=     coordinate_size
     UpperCamelCase_									=     shape_size
     UpperCamelCase_									=     has_relative_attention_bias
     UpperCamelCase_									=     rel_pos_bins
     UpperCamelCase_									=     max_rel_pos
     UpperCamelCase_									=     has_spatial_attention_bias
     UpperCamelCase_									=     rel_ad_pos_bins
     UpperCamelCase_									=     max_rel_ad_pos
     UpperCamelCase_									=     text_embed
     UpperCamelCase_									=     visual_embed
     UpperCamelCase_									=     input_size
     UpperCamelCase_									=     num_channels
     UpperCamelCase_									=     patch_size
     UpperCamelCase_									=     classifier_dropout




class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   version.parse("""1.12"""    )




  @property
  def    _UpperCAmelCase     (     self )			-> Mapping[str, Mapping[int, str]]:
     # The order of inputs is different for question answering and sequence classification
     if self.task in ["question-answering", "sequence-classification"]:
        return OrderedDict(
            [
                ('input_ids', {0: 'batch', 1: 'sequence'}),
                ('attention_mask', {0: 'batch', 1: 'sequence'}),
                ('bbox', {0: 'batch', 1: 'sequence'}),
                ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
            ] )
     else:
        return OrderedDict(
            [
                ('input_ids', {0: 'batch', 1: 'sequence'}),
                ('bbox', {0: 'batch', 1: 'sequence'}),
                ('attention_mask', {0: 'batch', 1: 'sequence'}),
                ('pixel_values', {0: 'batch', 1: 'num_channels'}),
            ] )




  @property
  def    _UpperCAmelCase     (     self )			-> float:
     return 1e-5




  @property
  def    _UpperCAmelCase     (     self )			-> int:
     return 12




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase = -1		,		_UpperCAmelCase = -1		,		_UpperCAmelCase = False		,		_UpperCAmelCase = None		,		_UpperCAmelCase = 3		,		_UpperCAmelCase = 40		,		_UpperCAmelCase = 40		,		)			-> Mapping[str, Any]:
     setattr(processor.image_processor		,		'apply_ocr'		,		_UpperCAmelCase )

     # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
     UpperCamelCase_									=     compute_effective_axis_dimension(
         _UpperCAmelCase		,		fixed_dimension=OnnxConfig.default_fixed_batch		,		num_token_to_add=0 )
     # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
     UpperCamelCase_									=     processor.tokenizer.num_special_tokens_to_add(_UpperCAmelCase )
     UpperCamelCase_									=     compute_effective_axis_dimension(
         _UpperCAmelCase		,		fixed_dimension=OnnxConfig.default_fixed_sequence		,		num_token_to_add=_UpperCAmelCase )
     # Generate dummy inputs according to compute batch and sequence
     UpperCamelCase_									=     [[' '.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size

     # Generate dummy bounding boxes
     UpperCamelCase_									=     [[[48, 84, 73, 128]]] * batch_size

     # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
     # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch)
     UpperCamelCase_									=     self._generate_dummy_images(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )

     UpperCamelCase_									=     dict(
         processor(
             _UpperCAmelCase		,		text=_UpperCAmelCase		,		boxes=_UpperCAmelCase		,		return_tensors=_UpperCAmelCase		,		) )

     return inputs





