






"""simple docstring"""





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

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


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

UpperCamelCase_						:					Optional[Any]      				=			{
    """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/config.json""",
    # See all Blenderbot models at https://huggingface.co/models?filter=blenderbot
}


class   _lowercase			(  lowerCAmelCase    ):
      _a :  Dict										= '''blenderbot'''
      _a :  Optional[Any]										= ['''past_key_values''']
      _a :  Any										= {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}


      def __init__(     self					:						Union[str, Any]				, a					:						List[Any]=8_0_0_8				, a					:						List[Any]=1_2_8				, a					:						Union[str, Any]=2				, a					:						Optional[int]=1_0_2_4_0				, a					:						Tuple=3_2				, a					:						Optional[int]=2_4				, a					:						str=1_0_2_4_0				, a					:						Optional[Any]=3_2				, a					:						List[str]=0.0				, a					:						Union[str, Any]=0.0				, a					:						Dict=True				, a					:						List[Any]=True				, a					:						List[str]="gelu"				, a					:						int=2_5_6_0				, a					:						List[str]=0.1				, a					:						Any=0.0				, a					:						Optional[int]=0.0				, a					:						int=0.0_2				, a					:						List[str]=1				, a					:						Dict=False				, a					:						Optional[Any]=0				, a					:						Optional[int]=1				, a					:						Any=2				, a					:						List[str]=3				, a					:						Dict=2				, **a					:						int				, ):





            """simple docstring"""







            __snake_case   :     List[str]	      =vocab_size
            __snake_case   :     List[Any]	      =max_position_embeddings
            __snake_case   :     int	      =d_model
            __snake_case   :     Optional[Any]	      =encoder_ffn_dim
            __snake_case   :     Optional[Any]	      =encoder_layers
            __snake_case   :     Dict	      =encoder_attention_heads
            __snake_case   :     Any	      =decoder_ffn_dim
            __snake_case   :     List[Any]	      =decoder_layers
            __snake_case   :     Optional[int]	      =decoder_attention_heads
            __snake_case   :     Optional[int]	      =dropout
            __snake_case   :     Optional[int]	      =attention_dropout
            __snake_case   :     Dict	      =activation_dropout
            __snake_case   :     Dict	      =activation_function
            __snake_case   :     Any	      =init_std
            __snake_case   :     Tuple	      =encoder_layerdrop
            __snake_case   :     Any	      =decoder_layerdrop
            __snake_case   :     Tuple	      =use_cache
            __snake_case   :     Optional[int]	      =encoder_layers
            __snake_case   :     Tuple	      =scale_embedding  # scale factor will be sqrt(d_model) if True

            super().__init__(
                pad_token_id=a				, bos_token_id=a				, eos_token_id=a				, is_encoder_decoder=a				, decoder_start_token_id=a				, encoder_no_repeat_ngram_size=a				, forced_eos_token_id=a				, **a				, )



class   _lowercase			(  lowerCAmelCase    ):


      @property
      def _UpperCamelCase    (     self					:						List[str]    ):





            """simple docstring"""







            if self.task in ["default", "seq2seq-lm"]:
                  __snake_case   :     str	      =OrderedDict(
                      [
                          ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                          ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                      ]    )
                  if self.use_past:
                        __snake_case   :     Dict	      ={0: '''batch'''}
                        __snake_case   :     Union[str, Any]	      ={0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
                  else:
                        __snake_case   :     Any	      ={0: '''batch''', 1: '''decoder_sequence'''}
                        __snake_case   :     Any	      ={0: '''batch''', 1: '''decoder_sequence'''}
                  if self.use_past:
                        self.fill_with_past_key_values_(a				, direction='''inputs'''    )
            elif self.task == "causal-lm":
                  __snake_case   :     Optional[int]	      =OrderedDict(
                      [
                          ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                          ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                      ]    )
                  if self.use_past:
                        __snake_case      ,					__snake_case   :     str	      =self.num_layers
                        for i in range(a    ):
                              __snake_case   :     Optional[Any]	      ={0: '''batch''', 2: '''past_sequence + sequence'''}
                              __snake_case   :     Dict	      ={0: '''batch''', 2: '''past_sequence + sequence'''}
            else:
                  __snake_case   :     Dict	      =OrderedDict(
                      [
                          ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                          ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                          ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}),
                          ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}),
                      ]    )

            return common_inputs


      @property
      # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
      def _UpperCamelCase    (     self					:						Tuple    ):





            """simple docstring"""







            if self.task in ["default", "seq2seq-lm"]:
                  __snake_case   :     str	      =super().outputs
            else:
                  __snake_case   :     int	      =super(a				, self    ).outputs
                  if self.use_past:
                        __snake_case      ,					__snake_case   :     Union[str, Any]	      =self.num_layers
                        for i in range(a    ):
                              __snake_case   :     Tuple	      ={0: '''batch''', 2: '''past_sequence + sequence'''}
                              __snake_case   :     Optional[Any]	      ={0: '''batch''', 2: '''past_sequence + sequence'''}
            return common_outputs


      def _UpperCamelCase    (     self					:						str				, a					:						PreTrainedTokenizer				, a					:						int = -1				, a					:						int = -1				, a					:						bool = False				, a					:						Optional[TensorType] = None				, ):





            """simple docstring"""







            __snake_case   :     Dict	      =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                a				, a				, a				, a				, a    )
            # Generate decoder inputs
            __snake_case   :     str	      =seq_length if not self.use_past else 1
            __snake_case   :     Any	      =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                a				, a				, a				, a				, a    )
            __snake_case   :     Optional[Any]	      ={f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
            __snake_case   :     int	      =dict(**a				, **a    )

            if self.use_past:
                  if not is_torch_available():
                        raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''    )
                  else:
                        import torch
                  __snake_case      ,					__snake_case   :     List[str]	      =common_inputs['''input_ids'''].shape
                  __snake_case   :     Union[str, Any]	      =common_inputs['''decoder_input_ids'''].shape[1]
                  __snake_case      ,					__snake_case   :     str	      =self.num_attention_heads
                  __snake_case   :     Dict	      =(
                      batch,
                      num_encoder_attention_heads,
                      encoder_seq_length,
                      self._config.hidden_size // num_encoder_attention_heads,
                  )
                  __snake_case   :     Union[str, Any]	      =decoder_seq_length
                  __snake_case   :     int	      =(
                      batch,
                      num_decoder_attention_heads,
                      decoder_past_length,
                      self._config.hidden_size // num_decoder_attention_heads,
                  )
                  __snake_case   :     Tuple	      =torch.cat(
                      [common_inputs['''decoder_attention_mask'''], torch.ones(a				, a    )]				, dim=1    )
                  __snake_case   :     Any	      =[]
                  __snake_case      ,					__snake_case   :     List[Any]	      =self.num_layers

                  for _ in range(a    ):
                        common_inputs["past_key_values"].append(
                            (
                                torch.zeros(a    ),
                                torch.zeros(a    ),
                                torch.zeros(a    ),
                                torch.zeros(a    ),
                            )    )
            return common_inputs


      def _UpperCamelCase    (     self					:						int				, a					:						PreTrainedTokenizer				, a					:						int = -1				, a					:						int = -1				, a					:						bool = False				, a					:						Optional[TensorType] = None				, ):





            """simple docstring"""







            __snake_case   :     Optional[int]	      =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                a				, a				, a				, a				, a    )

            if self.use_past:
                  if not is_torch_available():
                        raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''    )
                  else:
                        import torch
                  __snake_case      ,					__snake_case   :     int	      =common_inputs['''input_ids'''].shape
                  __snake_case   :     Any	      =seqlen
                  __snake_case      ,					__snake_case   :     Optional[Any]	      =self.num_layers
                  __snake_case      ,					__snake_case   :     List[str]	      =self.num_attention_heads
                  __snake_case   :     List[Any]	      =(
                      batch,
                      num_encoder_attention_heads,
                      past_key_values_length,
                      self._config.hidden_size // num_encoder_attention_heads,
                  )
                  __snake_case   :     Dict	      =common_inputs['''attention_mask'''].dtype
                  __snake_case   :     Tuple	      =torch.cat(
                      [common_inputs['''attention_mask'''], torch.ones(a				, a				, dtype=a    )]				, dim=1    )
                  __snake_case   :     Optional[Any]	      =[
                      (torch.zeros(a    ), torch.zeros(a    )) for _ in range(a    )
                  ]
            return common_inputs


      def _UpperCamelCase    (     self					:						Optional[Any]				, a					:						PreTrainedTokenizer				, a					:						int = -1				, a					:						int = -1				, a					:						bool = False				, a					:						Optional[TensorType] = None				, ):





            """simple docstring"""







            __snake_case   :     List[str]	      =compute_effective_axis_dimension(
                a				, 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
            __snake_case   :     Any	      =tokenizer.num_special_tokens_to_add(a    )
            __snake_case   :     int	      =compute_effective_axis_dimension(
                a				, fixed_dimension=OnnxConfig.default_fixed_sequence				, num_token_to_add=a    )

            # Generate dummy inputs according to compute batch and sequence
            __snake_case   :     Union[str, Any]	      =[''' '''.join([tokenizer.unk_token]    ) * seq_length] * batch_size
            __snake_case   :     List[Any]	      =dict(tokenizer(a				, return_tensors=a    )    )
            return common_inputs


      def _UpperCamelCase    (     self					:						Optional[int]				, a					:						PreTrainedTokenizer				, a					:						int = -1				, a					:						int = -1				, a					:						bool = False				, a					:						Optional[TensorType] = None				, ):





            """simple docstring"""







            if self.task in ["default", "seq2seq-lm"]:
                  __snake_case   :     Tuple	      =self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                      a				, batch_size=a				, seq_length=a				, is_pair=a				, framework=a    )

            elif self.task == "causal-lm":
                  __snake_case   :     str	      =self._generate_dummy_inputs_for_causal_lm(
                      a				, batch_size=a				, seq_length=a				, is_pair=a				, framework=a    )
            else:
                  __snake_case   :     int	      =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                      a				, batch_size=a				, seq_length=a				, is_pair=a				, framework=a    )

            return common_inputs


      def _UpperCamelCase    (     self					:						Union[str, Any]				, a					:						Optional[int]				, a					:						Optional[Any]				, a					:						Optional[int]				, a					:						int    ):





            """simple docstring"""







            if self.task in ["default", "seq2seq-lm"]:
                  __snake_case   :     Union[str, Any]	      =super()._flatten_past_key_values_(a				, a				, a				, a    )
            else:
                  __snake_case   :     Any	      =super(a				, self    )._flatten_past_key_values_(
                      a				, a				, a				, a    )







      def _UpperCamelCase    (     self					:						Any				, a					:						Mapping[str, Mapping[int, str]]				, a					:						str    ):





            """simple docstring"""







            if direction not in ["inputs", "outputs"]:
                  raise ValueError(f'''direction must either be "inputs" or "outputs", but {direction} was given'''    )

            __snake_case   :     str	      ='''past_key_values''' if direction == '''inputs''' else '''present'''
            __snake_case      ,					__snake_case   :     List[str]	      =self.num_layers

            __snake_case   :     Dict	      ='''past_encoder_sequence'''
            __snake_case   :     Union[str, Any]	      ='''past_decoder_sequence''' if direction == '''inputs''' else '''past_decoder_sequence + sequence'''

            for i in range(a    ):
                  __snake_case   :     Optional[int]	      ={0: '''batch''', 2: decoder_sequence}
                  __snake_case   :     List[Any]	      ={0: '''batch''', 2: decoder_sequence}
                  __snake_case   :     Tuple	      ={0: '''batch''', 2: encoder_sequence}
                  __snake_case   :     Union[str, Any]	      ={0: '''batch''', 2: encoder_sequence}










"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCamelCase_						:					Dict      				=			{
    """configuration_layoutlm""": ["""LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """LayoutLMConfig""", """LayoutLMOnnxConfig"""],
    """tokenization_layoutlm""": ["""LayoutLMTokenizer"""],
}

try:
    if not is_tokenizers_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_						:					List[Any]      				=			["""LayoutLMTokenizerFast"""]

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_						:					Optional[Any]      				=			[
        """LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """LayoutLMForMaskedLM""",
        """LayoutLMForSequenceClassification""",
        """LayoutLMForTokenClassification""",
        """LayoutLMForQuestionAnswering""",
        """LayoutLMModel""",
        """LayoutLMPreTrainedModel""",
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_						:					Optional[int]      				=			[
        """TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """TFLayoutLMForMaskedLM""",
        """TFLayoutLMForSequenceClassification""",
        """TFLayoutLMForTokenClassification""",
        """TFLayoutLMForQuestionAnswering""",
        """TFLayoutLMMainLayer""",
        """TFLayoutLMModel""",
        """TFLayoutLMPreTrainedModel""",
    ]


if TYPE_CHECKING:
    from .configuration_layoutlm import LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMConfig, LayoutLMOnnxConfig
    from .tokenization_layoutlm import LayoutLMTokenizer

    try:
        if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_layoutlm_fast import LayoutLMTokenizerFast

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_layoutlm import (
            LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
            LayoutLMForMaskedLM,
            LayoutLMForQuestionAnswering,
            LayoutLMForSequenceClassification,
            LayoutLMForTokenClassification,
            LayoutLMModel,
            LayoutLMPreTrainedModel,
        )
    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_layoutlm import (
            TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFLayoutLMForMaskedLM,
            TFLayoutLMForQuestionAnswering,
            TFLayoutLMForSequenceClassification,
            TFLayoutLMForTokenClassification,
            TFLayoutLMMainLayer,
            TFLayoutLMModel,
            TFLayoutLMPreTrainedModel,
        )

else:
    import sys

    UpperCamelCase_						:					Optional[Any]      				=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


