





'''simple docstring'''

from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..utils import maybe_allow_in_graph
from .activations import get_activation
from .attention_processor import Attention
from .embeddings import CombinedTimestepLabelEmbeddings


@maybe_allow_in_graph
class    a_	(							nn.Module			):

    '''simple docstring'''


    def __init__(						self					,						A					,						A					,						A					,						A=0.0					,						A = None					,						A = "geglu"					,						A = None					,						A = False					,						A = False					,						A = False					,						A = False					,						A = True					,						A = "layer_norm"					,						A = False					,						)						->  Union[str, Any]:
        super().__init__()
        _SCREAMING_SNAKE_CASE   							=     only_cross_attention

        _SCREAMING_SNAKE_CASE   							=     (num_embeds_ada_norm is not None) and norm_type == 'ada_norm_zero'
        _SCREAMING_SNAKE_CASE   							=     (num_embeds_ada_norm is not None) and norm_type == 'ada_norm'

        if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None:
            raise ValueError(
                f'`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to'
                f' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.'			)

        # Define 3 blocks. Each block has its own normalization layer.
        # 1. Self-Attn
        if self.use_ada_layer_norm:
            _SCREAMING_SNAKE_CASE   							=     AdaLayerNorm(UpperCamelCase_					,						UpperCamelCase_			)
        elif self.use_ada_layer_norm_zero:
            _SCREAMING_SNAKE_CASE   							=     AdaLayerNormZero(UpperCamelCase_					,						UpperCamelCase_			)
        else:
            _SCREAMING_SNAKE_CASE   							=     nn.LayerNorm(UpperCamelCase_					,						elementwise_affine=UpperCamelCase_			)
        _SCREAMING_SNAKE_CASE   							=     Attention(
            query_dim=UpperCamelCase_					,						heads=UpperCamelCase_					,						dim_head=UpperCamelCase_					,						dropout=UpperCamelCase_					,						bias=UpperCamelCase_					,						cross_attention_dim=cross_attention_dim if only_cross_attention else None					,						upcast_attention=UpperCamelCase_					,						)

        # 2. Cross-Attn
        if cross_attention_dim is not None or double_self_attention:
            # We currently only use AdaLayerNormZero for self attention where there will only be one attention block.
            # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during
            # the second cross attention block.
            _SCREAMING_SNAKE_CASE   							=     (
                AdaLayerNorm(UpperCamelCase_					,						UpperCamelCase_			)
                if self.use_ada_layer_norm
                else nn.LayerNorm(UpperCamelCase_					,						elementwise_affine=UpperCamelCase_			)
            )
            _SCREAMING_SNAKE_CASE   							=     Attention(
                query_dim=UpperCamelCase_					,						cross_attention_dim=cross_attention_dim if not double_self_attention else None					,						heads=UpperCamelCase_					,						dim_head=UpperCamelCase_					,						dropout=UpperCamelCase_					,						bias=UpperCamelCase_					,						upcast_attention=UpperCamelCase_					,						)  # is self-attn if encoder_hidden_states is none
        else:
            _SCREAMING_SNAKE_CASE   							=     None
            _SCREAMING_SNAKE_CASE   							=     None

        # 3. Feed-forward
        _SCREAMING_SNAKE_CASE   							=     nn.LayerNorm(UpperCamelCase_					,						elementwise_affine=UpperCamelCase_			)
        _SCREAMING_SNAKE_CASE   							=     FeedForward(UpperCamelCase_					,						dropout=UpperCamelCase_					,						activation_fn=UpperCamelCase_					,						final_dropout=UpperCamelCase_			)

        # let chunk size default to None
        _SCREAMING_SNAKE_CASE   							=     None
        _SCREAMING_SNAKE_CASE   							=     0


    def      snake_case_(						self					,						A					,						A			)						->  List[Any]:
        # Sets chunk feed-forward
        _SCREAMING_SNAKE_CASE   							=     chunk_size
        _SCREAMING_SNAKE_CASE   							=     dim





    def      snake_case_(						self					,						A					,						A = None					,						A = None					,						A = None					,						A = None					,						A = None					,						A = None					,						)						->  Tuple:
        # Notice that normalization is always applied before the real computation in the following blocks.
        # 1. Self-Attention
        if self.use_ada_layer_norm:
            _SCREAMING_SNAKE_CASE   							=     self.norma(UpperCamelCase_					,						UpperCamelCase_			)
        elif self.use_ada_layer_norm_zero:
            _SCREAMING_SNAKE_CASE   							=     self.norma(
                UpperCamelCase_					,						UpperCamelCase_					,						UpperCamelCase_					,						hidden_dtype=hidden_states.dtype			)
        else:
            _SCREAMING_SNAKE_CASE   							=     self.norma(UpperCamelCase_			)

        _SCREAMING_SNAKE_CASE   							=     cross_attention_kwargs if cross_attention_kwargs is not None else {}

        _SCREAMING_SNAKE_CASE   							=     self.attna(
            UpperCamelCase_					,						encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None					,						attention_mask=UpperCamelCase_					,						**UpperCamelCase_					,						)
        if self.use_ada_layer_norm_zero:
            _SCREAMING_SNAKE_CASE   							=     gate_msa.unsqueeze(1			) * attn_output
        _SCREAMING_SNAKE_CASE   							=     attn_output + hidden_states

        # 2. Cross-Attention
        if self.attna is not None:
            _SCREAMING_SNAKE_CASE   							=     (
                self.norma(UpperCamelCase_					,						UpperCamelCase_			) if self.use_ada_layer_norm else self.norma(UpperCamelCase_			)
            )

            _SCREAMING_SNAKE_CASE   							=     self.attna(
                UpperCamelCase_					,						encoder_hidden_states=UpperCamelCase_					,						attention_mask=UpperCamelCase_					,						**UpperCamelCase_					,						)
            _SCREAMING_SNAKE_CASE   							=     attn_output + hidden_states

        # 3. Feed-forward
        _SCREAMING_SNAKE_CASE   							=     self.norma(UpperCamelCase_			)

        if self.use_ada_layer_norm_zero:
            _SCREAMING_SNAKE_CASE   							=     norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]

        if self._chunk_size is not None:
            # "feed_forward_chunk_size" can be used to save memory
            if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0:
                raise ValueError(
                    f'`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.'			)

            _SCREAMING_SNAKE_CASE   							=     norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
            _SCREAMING_SNAKE_CASE   							=     torch.cat(
                [self.ff(UpperCamelCase_			) for hid_slice in norm_hidden_states.chunk(UpperCamelCase_					,						dim=self._chunk_dim			)]					,						dim=self._chunk_dim					,						)
        else:
            _SCREAMING_SNAKE_CASE   							=     self.ff(UpperCamelCase_			)

        if self.use_ada_layer_norm_zero:
            _SCREAMING_SNAKE_CASE   							=     gate_mlp.unsqueeze(1			) * ff_output

        _SCREAMING_SNAKE_CASE   							=     ff_output + hidden_states

        return hidden_states




class    a_	(							nn.Module			):

    '''simple docstring'''


    def __init__(						self					,						A					,						A = None					,						A = 4					,						A = 0.0					,						A = "geglu"					,						A = False					,						)						->  Any:
        super().__init__()
        _SCREAMING_SNAKE_CASE   							=     int(dim * mult			)
        _SCREAMING_SNAKE_CASE   							=     dim_out if dim_out is not None else dim

        if activation_fn == "gelu":
            _SCREAMING_SNAKE_CASE   							=     GELU(UpperCamelCase_					,						UpperCamelCase_			)
        if activation_fn == "gelu-approximate":
            _SCREAMING_SNAKE_CASE   							=     GELU(UpperCamelCase_					,						UpperCamelCase_					,						approximate="""tanh"""			)
        elif activation_fn == "geglu":
            _SCREAMING_SNAKE_CASE   							=     GEGLU(UpperCamelCase_					,						UpperCamelCase_			)
        elif activation_fn == "geglu-approximate":
            _SCREAMING_SNAKE_CASE   							=     ApproximateGELU(UpperCamelCase_					,						UpperCamelCase_			)

        _SCREAMING_SNAKE_CASE   							=     nn.ModuleList([]			)
        # project in
        self.net.append(UpperCamelCase_			)
        # project dropout
        self.net.append(nn.Dropout(UpperCamelCase_			)			)
        # project out
        self.net.append(nn.Linear(UpperCamelCase_					,						UpperCamelCase_			)			)
        # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout
        if final_dropout:
            self.net.append(nn.Dropout(UpperCamelCase_			)			)





    def      snake_case_(						self					,						A			)						->  Any:
        for module in self.net:
            _SCREAMING_SNAKE_CASE   							=     module(UpperCamelCase_			)
        return hidden_states




class    a_	(							nn.Module			):

    '''simple docstring'''


    def __init__(						self					,						A					,						A					,						A = "none"			)						->  Optional[int]:
        super().__init__()
        _SCREAMING_SNAKE_CASE   							=     nn.Linear(UpperCamelCase_					,						UpperCamelCase_			)
        _SCREAMING_SNAKE_CASE   							=     approximate


    def      snake_case_(						self					,						A			)						->  Any:
        if gate.device.type != "mps":
            return F.gelu(UpperCamelCase_					,						approximate=self.approximate			)
        # mps: gelu is not implemented for float16
        return F.gelu(gate.to(dtype=torch.floataa			)					,						approximate=self.approximate			).to(dtype=gate.dtype			)





    def      snake_case_(						self					,						A			)						->  Optional[int]:
        _SCREAMING_SNAKE_CASE   							=     self.proj(UpperCamelCase_			)
        _SCREAMING_SNAKE_CASE   							=     self.gelu(UpperCamelCase_			)
        return hidden_states




class    a_	(							nn.Module			):

    '''simple docstring'''


    def __init__(						self					,						A					,						A			)						->  Optional[int]:
        super().__init__()
        _SCREAMING_SNAKE_CASE   							=     nn.Linear(UpperCamelCase_					,						dim_out * 2			)


    def      snake_case_(						self					,						A			)						->  List[str]:
        if gate.device.type != "mps":
            return F.gelu(UpperCamelCase_			)
        # mps: gelu is not implemented for float16
        return F.gelu(gate.to(dtype=torch.floataa			)			).to(dtype=gate.dtype			)





    def      snake_case_(						self					,						A			)						->  Union[str, Any]:
        _SCREAMING_SNAKE_CASE   							=     self.proj(UpperCamelCase_			).chunk(2					,						dim=-1			)
        return hidden_states * self.gelu(UpperCamelCase_			)




class    a_	(							nn.Module			):

    '''simple docstring'''


    def __init__(						self					,						A					,						A			)						->  Optional[Any]:
        super().__init__()
        _SCREAMING_SNAKE_CASE   							=     nn.Linear(UpperCamelCase_					,						UpperCamelCase_			)





    def      snake_case_(						self					,						A			)						->  Dict:
        _SCREAMING_SNAKE_CASE   							=     self.proj(UpperCamelCase_			)
        return x * torch.sigmoid(1.702 * x			)




class    a_	(							nn.Module			):

    '''simple docstring'''


    def __init__(						self					,						A					,						A			)						->  List[str]:
        super().__init__()
        _SCREAMING_SNAKE_CASE   							=     nn.Embedding(UpperCamelCase_					,						UpperCamelCase_			)
        _SCREAMING_SNAKE_CASE   							=     nn.SiLU()
        _SCREAMING_SNAKE_CASE   							=     nn.Linear(UpperCamelCase_					,						embedding_dim * 2			)
        _SCREAMING_SNAKE_CASE   							=     nn.LayerNorm(UpperCamelCase_					,						elementwise_affine=UpperCamelCase_			)





    def      snake_case_(						self					,						A					,						A			)						->  Any:
        _SCREAMING_SNAKE_CASE   							=     self.linear(self.silu(self.emb(UpperCamelCase_			)			)			)
        _SCREAMING_SNAKE_CASE   							=     torch.chunk(UpperCamelCase_					,						2			)
        _SCREAMING_SNAKE_CASE   							=     self.norm(UpperCamelCase_			) * (1 + scale) + shift
        return x




class    a_	(							nn.Module			):

    '''simple docstring'''


    def __init__(						self					,						A					,						A			)						->  Union[str, Any]:
        super().__init__()

        _SCREAMING_SNAKE_CASE   							=     CombinedTimestepLabelEmbeddings(UpperCamelCase_					,						UpperCamelCase_			)

        _SCREAMING_SNAKE_CASE   							=     nn.SiLU()
        _SCREAMING_SNAKE_CASE   							=     nn.Linear(UpperCamelCase_					,						6 * embedding_dim					,						bias=UpperCamelCase_			)
        _SCREAMING_SNAKE_CASE   							=     nn.LayerNorm(UpperCamelCase_					,						elementwise_affine=UpperCamelCase_					,						eps=1e-6			)





    def      snake_case_(						self					,						A					,						A					,						A					,						A=None			)						->  Any:
        _SCREAMING_SNAKE_CASE   							=     self.linear(self.silu(self.emb(UpperCamelCase_					,						UpperCamelCase_					,						hidden_dtype=UpperCamelCase_			)			)			)
        _SCREAMING_SNAKE_CASE   							=     emb.chunk(6					,						dim=1			)
        _SCREAMING_SNAKE_CASE   							=     self.norm(UpperCamelCase_			) * (1 + scale_msa[:, None]) + shift_msa[:, None]
        return x, gate_msa, shift_mlp, scale_mlp, gate_mlp




class    a_	(							nn.Module			):

    '''simple docstring'''


    def __init__(						self					,						A					,						A					,						A					,						A = None					,						A = 1e-5			)						->  int:
        super().__init__()
        _SCREAMING_SNAKE_CASE   							=     num_groups
        _SCREAMING_SNAKE_CASE   							=     eps

        if act_fn is None:
            _SCREAMING_SNAKE_CASE   							=     None
        else:
            _SCREAMING_SNAKE_CASE   							=     get_activation(UpperCamelCase_			)

        _SCREAMING_SNAKE_CASE   							=     nn.Linear(UpperCamelCase_					,						out_dim * 2			)





    def      snake_case_(						self					,						A					,						A			)						->  int:
        if self.act:
            _SCREAMING_SNAKE_CASE   							=     self.act(UpperCamelCase_			)
        _SCREAMING_SNAKE_CASE   							=     self.linear(UpperCamelCase_			)
        _SCREAMING_SNAKE_CASE   							=     emb[:, :, None, None]
        _SCREAMING_SNAKE_CASE   							=     emb.chunk(2					,						dim=1			)

        _SCREAMING_SNAKE_CASE   							=     F.group_norm(UpperCamelCase_					,						self.num_groups					,						eps=self.eps			)
        _SCREAMING_SNAKE_CASE   							=     x * (1 + scale) + shift
        return x





"""simple docstring"""



import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType





class 				a			(					lowercase      ):
      UpperCamelCase							:   Tuple           =     ["""image_processor""", """tokenizer"""]
      UpperCamelCase							:   List[str]           =     """LayoutLMv2ImageProcessor"""
      UpperCamelCase							:   Optional[int]           =     ("""LayoutXLMTokenizer""", """LayoutXLMTokenizerFast""")



      def __init__(   self     ,	UpperCamelCase_=None     ,	UpperCamelCase_=None     ,	**UpperCamelCase_      ):
          if "feature_extractor" in kwargs:
              warnings.warn(
                  'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                  ' instead.'     ,	UpperCamelCase_     ,	)
              UpperCAmelCase__	:      Tuple     							=					kwargs.pop('feature_extractor'      )

          UpperCAmelCase__	:      Optional[Any]     							=					image_processor if image_processor is not None else feature_extractor
          if image_processor is None:
              raise ValueError('You need to specify an `image_processor`.'      )
          if tokenizer is None:
              raise ValueError('You need to specify a `tokenizer`.'      )

          super().__init__(UpperCamelCase_     ,	UpperCamelCase_      )



      def __call__(   self     ,	UpperCamelCase_     ,	UpperCamelCase_ = None     ,	UpperCamelCase_ = None     ,	UpperCamelCase_ = None     ,	UpperCamelCase_ = None     ,	UpperCamelCase_ = True     ,	UpperCamelCase_ = False     ,	UpperCamelCase_ = None     ,	UpperCamelCase_ = None     ,	UpperCamelCase_ = 0     ,	UpperCamelCase_ = None     ,	UpperCamelCase_ = None     ,	UpperCamelCase_ = None     ,	UpperCamelCase_ = False     ,	UpperCamelCase_ = False     ,	UpperCamelCase_ = False     ,	UpperCamelCase_ = False     ,	UpperCamelCase_ = True     ,	UpperCamelCase_ = None     ,	**UpperCamelCase_     ,	):
          # verify input
          if self.image_processor.apply_ocr and (boxes is not None):
              raise ValueError(
                  'You cannot provide bounding boxes '
                  'if you initialized the image processor with apply_ocr set to True.'      )

          if self.image_processor.apply_ocr and (word_labels is not None):
              raise ValueError(
                  'You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'      )

          if return_overflowing_tokens is True and return_offsets_mapping is False:
              raise ValueError('You cannot return overflowing tokens without returning the offsets mapping.'      )

          # first, apply the image processor
          UpperCAmelCase__	:      List[Any]     							=					self.image_processor(images=UpperCamelCase_     ,	return_tensors=UpperCamelCase_      )

          # second, apply the tokenizer
          if text is not None and self.image_processor.apply_ocr and text_pair is None:
              if isinstance(UpperCamelCase_     ,	UpperCamelCase_      ):
                  UpperCAmelCase__	:      Any     							=					[text]  # add batch dimension (as the image processor always adds a batch dimension)
              UpperCAmelCase__	:      Union[str, Any]     							=					features['words']

          UpperCAmelCase__	:      List[Any]     							=					self.tokenizer(
              text=text if text is not None else features['words']     ,	text_pair=text_pair if text_pair is not None else None     ,	boxes=boxes if boxes is not None else features['boxes']     ,	word_labels=UpperCamelCase_     ,	add_special_tokens=UpperCamelCase_     ,	padding=UpperCamelCase_     ,	truncation=UpperCamelCase_     ,	max_length=UpperCamelCase_     ,	stride=UpperCamelCase_     ,	pad_to_multiple_of=UpperCamelCase_     ,	return_token_type_ids=UpperCamelCase_     ,	return_attention_mask=UpperCamelCase_     ,	return_overflowing_tokens=UpperCamelCase_     ,	return_special_tokens_mask=UpperCamelCase_     ,	return_offsets_mapping=UpperCamelCase_     ,	return_length=UpperCamelCase_     ,	verbose=UpperCamelCase_     ,	return_tensors=UpperCamelCase_     ,	**UpperCamelCase_     ,	)

          # add pixel values
          UpperCAmelCase__	:      List[Any]     							=					features.pop('pixel_values'      )
          if return_overflowing_tokens is True:
              UpperCAmelCase__	:      int     							=					self.get_overflowing_images(UpperCamelCase_     ,	encoded_inputs['overflow_to_sample_mapping']      )
          UpperCAmelCase__	:      Dict     							=					images

          return encoded_inputs



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_      ):
          # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
          UpperCAmelCase__	:      Dict     							=					[]
          for sample_idx in overflow_to_sample_mapping:
              images_with_overflow.append(images[sample_idx]      )

          if len(UpperCamelCase_      ) != len(UpperCamelCase_      ):
              raise ValueError(
                  'Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'
                  F''' {len(UpperCamelCase_      )} and {len(UpperCamelCase_      )}'''      )

          return images_with_overflow



      def  __snake_case							(   self     ,	*UpperCamelCase_     ,	**UpperCamelCase_      ):
          return self.tokenizer.batch_decode(*UpperCamelCase_     ,	**UpperCamelCase_      )



      def  __snake_case							(   self     ,	*UpperCamelCase_     ,	**UpperCamelCase_      ):
          return self.tokenizer.decode(*UpperCamelCase_     ,	**UpperCamelCase_      )



      @property
      def  __snake_case							(   self      ):
          return ["input_ids", "bbox", "attention_mask", "image"]



      @property
      def  __snake_case							(   self      ):
          warnings.warn(
              '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'     ,	UpperCamelCase_     ,	)
          return self.image_processor_class



      @property
      def  __snake_case							(   self      ):
          warnings.warn(
              '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'     ,	UpperCamelCase_     ,	)
          return self.image_processor

