



'''simple docstring'''






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

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


A_							:				int						=			logging.get_logger(__name__)

A_							:				List[Any]						=			{
    """facebook/mbart-large-cc25""": """https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json""",
    # See all MBART models at https://huggingface.co/models?filter=mbart
}



class 							lowercase (						_lowerCamelCase       ):


 """simple docstring"""


 UpperCAmelCase					=						"""mbart"""
 UpperCAmelCase					=						["""past_key_values"""]
 UpperCAmelCase					=						{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}







 def __init__(							self						,a_=50_265						,a_=1_024						,a_=12						,a_=4_096						,a_=16						,a_=12						,a_=4_096						,a_=16						,a_=0.0						,a_=0.0						,a_=True						,a_=True						,a_="gelu"						,a_=1_024						,a_=0.1						,a_=0.0						,a_=0.0						,a_=0.02						,a_=0.0						,a_=False						,a_=1						,a_=0						,a_=2						,a_=2						,**a_						,)	->     List[str]:
        _UpperCAmelCase						:		Tuple      =				vocab_size
        _UpperCAmelCase						:		Tuple      =				max_position_embeddings
        _UpperCAmelCase						:		Any      =				d_model
        _UpperCAmelCase						:		str      =				encoder_ffn_dim
        _UpperCAmelCase						:		List[Any]      =				encoder_layers
        _UpperCAmelCase						:		List[str]      =				encoder_attention_heads
        _UpperCAmelCase						:		List[str]      =				decoder_ffn_dim
        _UpperCAmelCase						:		int      =				decoder_layers
        _UpperCAmelCase						:		str      =				decoder_attention_heads
        _UpperCAmelCase						:		int      =				dropout
        _UpperCAmelCase						:		Optional[Any]      =				attention_dropout
        _UpperCAmelCase						:		List[str]      =				activation_dropout
        _UpperCAmelCase						:		str      =				activation_function
        _UpperCAmelCase						:		int      =				init_std
        _UpperCAmelCase						:		Any      =				encoder_layerdrop
        _UpperCAmelCase						:		Optional[int]      =				decoder_layerdrop
        _UpperCAmelCase						:		int      =				classifier_dropout
        _UpperCAmelCase						:		str      =				use_cache
        _UpperCAmelCase						:		List[str]      =				encoder_layers
        _UpperCAmelCase						:		List[Any]      =				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_						,forced_eos_token_id=a_						,**a_						,)







class 							lowercase (						_lowerCamelCase       ):


 """simple docstring"""


 @property
 def _snake_case				(							self							)	->     Mapping[str, Mapping[int, str]]:
        if self.task in ["default", "seq2seq-lm"]:
               _UpperCAmelCase						:		List[str]      =				OrderedDict(
                   [
                       ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                       ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                   ]							)

               if self.use_past:
                      _UpperCAmelCase						:		str      =				{0: """batch"""}
                      _UpperCAmelCase						:		Union[str, Any]      =				{0: """batch""", 1: """past_decoder_sequence + sequence"""}
               else:
                      _UpperCAmelCase						:		Any      =				{0: """batch""", 1: """decoder_sequence"""}
                      _UpperCAmelCase						:		Any      =				{0: """batch""", 1: """decoder_sequence"""}

               if self.use_past:
                      self.fill_with_past_key_values_(a_						,direction="""inputs"""							)
        elif self.task == "causal-lm":
               # TODO: figure this case out.
               _UpperCAmelCase						:		List[str]      =				OrderedDict(
                   [
                       ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                       ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                   ]							)
               if self.use_past:
                      _UpperCAmelCase  ,_UpperCAmelCase						:		Union[str, Any]      =				self.num_layers
                      for i in range(a_							):
                             _UpperCAmelCase						:		int      =				{0: """batch""", 2: """past_sequence + sequence"""}
                             _UpperCAmelCase						:		int      =				{0: """batch""", 2: """past_sequence + sequence"""}
        else:
               _UpperCAmelCase						:		Optional[int]      =				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
 def _snake_case				(							self							)	->     Mapping[str, Mapping[int, str]]:
        if self.task in ["default", "seq2seq-lm"]:
               _UpperCAmelCase						:		Any      =				super().outputs
        else:
               _UpperCAmelCase						:		Any      =				super(a_						,self							).outputs
               if self.use_past:
                      _UpperCAmelCase  ,_UpperCAmelCase						:		int      =				self.num_layers
                      for i in range(a_							):
                             _UpperCAmelCase						:		Union[str, Any]      =				{0: """batch""", 2: """past_sequence + sequence"""}
                             _UpperCAmelCase						:		Union[str, Any]      =				{0: """batch""", 2: """past_sequence + sequence"""}
        return common_outputs







 def _snake_case				(							self						,a_						,a_ = -1						,a_ = -1						,a_ = False						,a_ = None						,)	->     Mapping[str, Any]:
        _UpperCAmelCase						:		str      =				self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            a_						,a_						,a_						,a_						,a_							)

        # Generate decoder inputs
        _UpperCAmelCase						:		List[Any]      =				seq_length if not self.use_past else 1
        _UpperCAmelCase						:		int      =				self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            a_						,a_						,a_						,a_						,a_							)
        _UpperCAmelCase						:		List[Any]      =				{f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
        _UpperCAmelCase						:		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
               _UpperCAmelCase  ,_UpperCAmelCase						:		Dict      =				common_inputs["""input_ids"""].shape
               _UpperCAmelCase						:		Optional[Any]      =				common_inputs["""decoder_input_ids"""].shape[1]
               _UpperCAmelCase  ,_UpperCAmelCase						:		Dict      =				self.num_attention_heads
               _UpperCAmelCase						:		Dict      =				(
                   batch,
                   num_encoder_attention_heads,
                   encoder_seq_length,
                   self._config.hidden_size // num_encoder_attention_heads,
               )
               _UpperCAmelCase						:		List[str]      =				decoder_seq_length + 3
               _UpperCAmelCase						:		Optional[int]      =				(
                   batch,
                   num_decoder_attention_heads,
                   decoder_past_length,
                   self._config.hidden_size // num_decoder_attention_heads,
               )

               _UpperCAmelCase						:		List[Any]      =				torch.cat(
                   [common_inputs["""decoder_attention_mask"""], torch.ones(a_						,a_							)]						,dim=1							)

               _UpperCAmelCase						:		Dict      =				[]
               # If the number of encoder and decoder layers are present in the model configuration, both are considered
               _UpperCAmelCase  ,_UpperCAmelCase						:		Any      =				self.num_layers
               _UpperCAmelCase						:		Optional[Any]      =				min(a_						,a_							)
               _UpperCAmelCase						:		int      =				max(a_						,a_							) - min_num_layers
               _UpperCAmelCase						:		List[Any]      =				"""encoder""" if num_encoder_layers > num_decoder_layers else """decoder"""

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







 def _snake_case				(							self						,a_						,a_ = -1						,a_ = -1						,a_ = False						,a_ = None						,)	->     Mapping[str, Any]:
        _UpperCAmelCase						:		Union[str, Any]      =				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
               _UpperCAmelCase  ,_UpperCAmelCase						:		Tuple      =				common_inputs["""input_ids"""].shape
               # Not using the same length for past_key_values
               _UpperCAmelCase						:		List[Any]      =				seqlen + 2
               _UpperCAmelCase  ,_UpperCAmelCase						:		Optional[int]      =				self.num_layers
               _UpperCAmelCase  ,_UpperCAmelCase						:		List[str]      =				self.num_attention_heads
               _UpperCAmelCase						:		List[str]      =				(
                   batch,
                   num_encoder_attention_heads,
                   past_key_values_length,
                   self._config.hidden_size // num_encoder_attention_heads,
               )

               _UpperCAmelCase						:		Tuple      =				common_inputs["""attention_mask"""].dtype
               _UpperCAmelCase						:		Optional[int]      =				torch.cat(
                   [common_inputs["""attention_mask"""], torch.ones(a_						,a_						,dtype=a_							)]						,dim=1							)
               _UpperCAmelCase						:		Tuple      =				[
                   (torch.zeros(a_							), torch.zeros(a_							)) for _ in range(a_							)
               ]
        return common_inputs







 def _snake_case				(							self						,a_						,a_ = -1						,a_ = -1						,a_ = False						,a_ = None						,)	->     Mapping[str, Any]:
        # Copied from OnnxConfig.generate_dummy_inputs
        # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
        # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
        _UpperCAmelCase						:		Optional[Any]      =				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
        _UpperCAmelCase						:		Optional[int]      =				tokenizer.num_special_tokens_to_add(a_							)
        _UpperCAmelCase						:		Tuple      =				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
        _UpperCAmelCase						:		Tuple      =				[""" """.join([tokenizer.unk_token]							) * seq_length] * batch_size
        _UpperCAmelCase						:		Union[str, Any]      =				dict(tokenizer(a_						,return_tensors=a_							)							)
        return common_inputs







 def _snake_case				(							self						,a_						,a_ = -1						,a_ = -1						,a_ = False						,a_ = None						,)	->     Mapping[str, Any]:
        if self.task in ["default", "seq2seq-lm"]:
               _UpperCAmelCase						:		Union[str, Any]      =				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":
               _UpperCAmelCase						:		Union[str, Any]      =				self._generate_dummy_inputs_for_causal_lm(
                   a_						,batch_size=a_						,seq_length=a_						,is_pair=a_						,framework=a_							)
        else:
               _UpperCAmelCase						:		str      =				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 _snake_case				(							self						,a_						,a_						,a_						,a_							)	->     List[str]:
        if self.task in ["default", "seq2seq-lm"]:
               _UpperCAmelCase						:		Tuple      =				super()._flatten_past_key_values_(a_						,a_						,a_						,a_							)
        else:
               _UpperCAmelCase						:		List[Any]      =				super(a_						,self							)._flatten_past_key_values_(
                   a_						,a_						,a_						,a_							)








'''simple docstring'''






from typing import TYPE_CHECKING

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


A_							:				Union[str, Any]						=			{
    """configuration_segformer""": ["""SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SegformerConfig""", """SegformerOnnxConfig"""]
}

try:
   if not is_vision_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A_							:				Tuple						=			["""SegformerFeatureExtractor"""]
   A_							:				Tuple						=			["""SegformerImageProcessor"""]

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A_							:				Dict						=			[
       """SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """SegformerDecodeHead""",
       """SegformerForImageClassification""",
       """SegformerForSemanticSegmentation""",
       """SegformerLayer""",
       """SegformerModel""",
       """SegformerPreTrainedModel""",
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A_							:				List[str]						=			[
       """TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """TFSegformerDecodeHead""",
       """TFSegformerForImageClassification""",
       """TFSegformerForSemanticSegmentation""",
       """TFSegformerModel""",
       """TFSegformerPreTrainedModel""",
   ]


if TYPE_CHECKING:
   from .configuration_segformer import SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SegformerConfig, SegformerOnnxConfig

   try:
      if not is_vision_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .feature_extraction_segformer import SegformerFeatureExtractor
      from .image_processing_segformer import SegformerImageProcessor

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_segformer import (
          SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          SegformerDecodeHead,
          SegformerForImageClassification,
          SegformerForSemanticSegmentation,
          SegformerLayer,
          SegformerModel,
          SegformerPreTrainedModel,
      )
   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_segformer import (
          TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFSegformerDecodeHead,
          TFSegformerForImageClassification,
          TFSegformerForSemanticSegmentation,
          TFSegformerModel,
          TFSegformerPreTrainedModel,
      )

else:
   import sys

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



