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

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


__lowerCAmelCase      :					Any   	=       logging.get_logger(__name__)

__lowerCAmelCase      :					Union[str, Any]   	=       {
    'facebook/m2m100_418M': 'https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}




class 	UpperCAmelCase_      (			_A					):


		'''simple docstring'''







		a__   	=		"""m2m_100"""
		a__   	=		["""past_key_values"""]
		a__   	=		{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}
		def __init__(			self      :			Tuple      ,							UpperCamelCase__      :			int=12_8112      ,							UpperCamelCase__      :			str=1024      ,							UpperCamelCase__      :			Optional[Any]=12      ,							UpperCamelCase__      :			Tuple=4096      ,							UpperCamelCase__      :			Optional[int]=16      ,							UpperCamelCase__      :			List[Any]=12      ,							UpperCamelCase__      :			List[Any]=4096      ,							UpperCamelCase__      :			Optional[int]=16      ,							UpperCamelCase__      :			Optional[int]=0.05      ,							UpperCamelCase__      :			str=0.05      ,							UpperCamelCase__      :			Optional[Any]=True      ,							UpperCamelCase__      :			Dict=True      ,							UpperCamelCase__      :			List[str]="relu"      ,							UpperCamelCase__      :			Any=1024      ,							UpperCamelCase__      :			str=0.1      ,							UpperCamelCase__      :			Optional[int]=0.1      ,							UpperCamelCase__      :			Any=0.0      ,							UpperCamelCase__      :			Any=0.02      ,							UpperCamelCase__      :			Optional[Any]=2      ,							UpperCamelCase__      :			List[Any]=True      ,							UpperCamelCase__      :			List[str]=1      ,							UpperCamelCase__      :			str=0      ,							UpperCamelCase__      :			Any=2      ,							**UpperCamelCase__      :			Tuple      ,							)      ->     Union[str, Any]:







				"""simple docstring"""






				__magic_name__    					=	vocab_size
				__magic_name__    					=	max_position_embeddings
				__magic_name__    					=	d_model
				__magic_name__    					=	encoder_ffn_dim
				__magic_name__    					=	encoder_layers
				__magic_name__    					=	encoder_attention_heads
				__magic_name__    					=	decoder_ffn_dim
				__magic_name__    					=	decoder_layers
				__magic_name__    					=	decoder_attention_heads
				__magic_name__    					=	dropout
				__magic_name__    					=	attention_dropout
				__magic_name__    					=	activation_dropout
				__magic_name__    					=	activation_function
				__magic_name__    					=	init_std
				__magic_name__    					=	encoder_layerdrop
				__magic_name__    					=	decoder_layerdrop
				__magic_name__    					=	use_cache
				__magic_name__    					=	encoder_layers
				__magic_name__    					=	scale_embedding  # scale factor will be sqrt(d_model) if True

				super().__init__(
				    pad_token_id=UpperCamelCase__      ,							bos_token_id=UpperCamelCase__      ,							eos_token_id=UpperCamelCase__      ,							is_encoder_decoder=UpperCamelCase__      ,							decoder_start_token_id=UpperCamelCase__      ,							**UpperCamelCase__      ,							)







class 	UpperCAmelCase_      (			_A					):


		'''simple docstring'''







		@property
		def 							_lowercase							(			self      :			Dict   )      ->     Mapping[str, Mapping[int, str]]:







				"""simple docstring"""






				__magic_name__    					=	OrderedDict(
				    [
				        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
				        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
				    ]   )

				if self.use_past:
						__magic_name__    					=	{0: """batch"""}
						__magic_name__    					=	{0: """batch""", 1: """past_decoder_sequence + sequence"""}
				else:
						__magic_name__    					=	{0: """batch""", 1: """decoder_sequence"""}
						__magic_name__    					=	{0: """batch""", 1: """decoder_sequence"""}

				if self.use_past:
						self.fill_with_past_key_values_(UpperCamelCase__      ,							direction="""inputs"""   )
				return common_inputs
		def 							_lowercase							(			self      :			str      ,							UpperCamelCase__      :			PreTrainedTokenizer      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			Optional[TensorType] = None      ,							)      ->     Mapping[str, Any]:







				"""simple docstring"""






				__magic_name__    					=	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
				__magic_name__    					=	tokenizer.num_special_tokens_to_add(UpperCamelCase__   )
				__magic_name__    					=	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
				__magic_name__    					=	[""" """.join([tokenizer.unk_token]   ) * seq_length] * batch_size
				__magic_name__    					=	dict(tokenizer(UpperCamelCase__      ,							return_tensors=UpperCamelCase__   )   )
				return common_inputs
		def 							_lowercase							(			self      :			List[Any]      ,							UpperCamelCase__      :			PreTrainedTokenizer      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			int = -1      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			Optional[TensorType] = None      ,							)      ->     Mapping[str, Any]:







				"""simple docstring"""






				__magic_name__    					=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
				    UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__   )

				# Generate decoder inputs
				__magic_name__    					=	seq_length if not self.use_past else 1
				__magic_name__    					=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
				    UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__   )
				__magic_name__    					=	{F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
				__magic_name__    					=	dict(**UpperCamelCase__      ,							**UpperCamelCase__   )

				if self.use_past:
						if not is_torch_available():
								raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""   )
						else:
								import torch
						__magic_name__					,   __magic_name__    					=	common_inputs["""input_ids"""].shape
						__magic_name__    					=	common_inputs["""decoder_input_ids"""].shape[1]
						__magic_name__					,   __magic_name__    					=	self.num_attention_heads
						__magic_name__    					=	(
						    batch,
						    num_encoder_attention_heads,
						    encoder_seq_length,
						    self._config.hidden_size // num_encoder_attention_heads,
						)
						__magic_name__    					=	decoder_seq_length + 3
						__magic_name__    					=	(
						    batch,
						    num_decoder_attention_heads,
						    decoder_past_length,
						    self._config.hidden_size // num_decoder_attention_heads,
						)

						__magic_name__    					=	torch.cat(
						    [common_inputs["""decoder_attention_mask"""], torch.ones(UpperCamelCase__      ,							UpperCamelCase__   )]      ,							dim=1   )

						__magic_name__    					=	[]
						# If the number of encoder and decoder layers are present in the model configuration, both are considered
						__magic_name__					,   __magic_name__    					=	self.num_layers
						__magic_name__    					=	min(UpperCamelCase__      ,							UpperCamelCase__   )
						__magic_name__    					=	max(UpperCamelCase__      ,							UpperCamelCase__   ) - min_num_layers
						__magic_name__    					=	"""encoder""" if num_encoder_layers > num_decoder_layers else """decoder"""

						for _ in range(UpperCamelCase__   ):
								common_inputs["past_key_values"].append(
								    (
								        torch.zeros(UpperCamelCase__   ),
								        torch.zeros(UpperCamelCase__   ),
								        torch.zeros(UpperCamelCase__   ),
								        torch.zeros(UpperCamelCase__   ),
								    )   )
						# TODO: test this.
						__magic_name__    					=	encoder_shape if remaining_side_name == """encoder""" else decoder_shape
						for _ in range(UpperCamelCase__      ,							UpperCamelCase__   ):
								common_inputs["past_key_values"].append((torch.zeros(UpperCamelCase__   ), torch.zeros(UpperCamelCase__   ))   )
				return common_inputs





		a__   	=		_generate_dummy_inputs_for_default_and_seqaseq_lm



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 	UpperCAmelCase_      (			_A					):


  '''simple docstring'''







  a__   	=		["""image_processor""", """tokenizer"""]
  a__   	=		"""LayoutLMv2ImageProcessor"""
  a__   	=		("""LayoutLMv2Tokenizer""", """LayoutLMv2TokenizerFast""")
  def __init__(			self      :			int      ,							UpperCamelCase__      :			Dict=None      ,							UpperCamelCase__      :			int=None      ,							**UpperCamelCase__      :			str   )      ->     Union[str, Any]:







    """simple docstring"""






    __magic_name__    					=	None
    if "feature_extractor" in kwargs:
      warnings.warn(
          """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
          """ instead."""      ,							UpperCamelCase__      ,							)
      __magic_name__    					=	kwargs.pop("""feature_extractor"""   )

    __magic_name__    					=	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      :			List[str]      ,							UpperCamelCase__      :			Union[str, Any]      ,							UpperCamelCase__      :			Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None      ,							UpperCamelCase__      :			Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None      ,							UpperCamelCase__      :			Union[List[List[int]], List[List[List[int]]]] = None      ,							UpperCamelCase__      :			Optional[Union[List[int], List[List[int]]]] = None      ,							UpperCamelCase__      :			bool = True      ,							UpperCamelCase__      :			Union[bool, str, PaddingStrategy] = False      ,							UpperCamelCase__      :			Union[bool, str, TruncationStrategy] = False      ,							UpperCamelCase__      :			Optional[int] = None      ,							UpperCamelCase__      :			int = 0      ,							UpperCamelCase__      :			Optional[int] = None      ,							UpperCamelCase__      :			Optional[bool] = None      ,							UpperCamelCase__      :			Optional[bool] = None      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			bool = False      ,							UpperCamelCase__      :			bool = True      ,							UpperCamelCase__      :			Optional[Union[str, TensorType]] = None      ,							**UpperCamelCase__      :			Union[str, Any]      ,							)      ->     BatchEncoding:







    """simple docstring"""






    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
    __magic_name__    					=	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__   ):
        __magic_name__    					=	[text]  # add batch dimension (as the image processor always adds a batch dimension)
      __magic_name__    					=	features["""words"""]

    __magic_name__    					=	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
    __magic_name__    					=	features.pop("""pixel_values"""   )
    if return_overflowing_tokens is True:
      __magic_name__    					=	self.get_overflowing_images(UpperCamelCase__      ,							encoded_inputs["""overflow_to_sample_mapping"""]   )
    __magic_name__    					=	images

    return encoded_inputs
  def 							_lowercase							(			self      :			Optional[int]      ,							UpperCamelCase__      :			Tuple      ,							UpperCamelCase__      :			Optional[int]   )      ->     Any:







    """simple docstring"""






    __magic_name__    					=	[]
    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 							_lowercase							(			self      :			Optional[int]      ,							*UpperCamelCase__      :			str      ,							**UpperCamelCase__      :			int   )      ->     Union[str, Any]:







    """simple docstring"""






    return self.tokenizer.batch_decode(*UpperCamelCase__      ,							**UpperCamelCase__   )
  def 							_lowercase							(			self      :			Dict      ,							*UpperCamelCase__      :			Optional[int]      ,							**UpperCamelCase__      :			str   )      ->     str:







    """simple docstring"""






    return self.tokenizer.decode(*UpperCamelCase__      ,							**UpperCamelCase__   )
  @property
  def 							_lowercase							(			self      :			Optional[Any]   )      ->     Optional[int]:







    """simple docstring"""






    return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]
  @property
  def 							_lowercase							(			self      :			Union[str, Any]   )      ->     Optional[int]:







    """simple docstring"""






    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 							_lowercase							(			self      :			Union[str, Any]   )      ->     Dict:







    """simple docstring"""






    warnings.warn(
        """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."""      ,							UpperCamelCase__      ,							)
    return self.image_processor

