

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


_a:      Any								=     logging.get_logger(__name__)

_a:      int								=     {
    """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   __UpperCamelCase      (   lowercase  ):
	SCREAMING_SNAKE_CASE__      			=       'blenderbot'
	SCREAMING_SNAKE_CASE__      			=       ['past_key_values']
	SCREAMING_SNAKE_CASE__      			=       {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}




	def __init__(						self		:  Any						,		lowerCAmelCase		:  Union[str, Any]=8_008						,		lowerCAmelCase		:  Any=128						,		lowerCAmelCase		:  Optional[int]=2						,		lowerCAmelCase		:  List[Any]=10_240						,		lowerCAmelCase		:  Union[str, Any]=32						,		lowerCAmelCase		:  Any=24						,		lowerCAmelCase		:  List[str]=10_240						,		lowerCAmelCase		:  List[Any]=32						,		lowerCAmelCase		:  List[str]=0.0						,		lowerCAmelCase		:  Tuple=0.0						,		lowerCAmelCase		:  Dict=True						,		lowerCAmelCase		:  Dict=True						,		lowerCAmelCase		:  Optional[Any]="gelu"						,		lowerCAmelCase		:  List[str]=2_560						,		lowerCAmelCase		:  Dict=0.1						,		lowerCAmelCase		:  Tuple=0.0						,		lowerCAmelCase		:  List[Any]=0.0						,		lowerCAmelCase		:  Dict=0.02						,		lowerCAmelCase		:  str=1						,		lowerCAmelCase		:  Any=False						,		lowerCAmelCase		:  Optional[Any]=0						,		lowerCAmelCase		:  int=1						,		lowerCAmelCase		:  str=2						,		lowerCAmelCase		:  List[str]=3						,		lowerCAmelCase		:  Tuple=2						,		**lowerCAmelCase		:  Optional[Any]						,		):


		'''simple docstring'''







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

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


class   __UpperCamelCase      (   lowercase  ):




	@property
	def  __A   (						self		:  Any							):


		'''simple docstring'''







		if self.task in ["default", "seq2seq-lm"]:
			UpperCAmelCase_    			=     OrderedDict(
			    [
			        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
			        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
			    ]							)
			if self.use_past:
				UpperCAmelCase_    			=     {0: "batch"}
				UpperCAmelCase_    			=     {0: "batch", 1: "past_decoder_sequence + sequence"}
			else:
				UpperCAmelCase_    			=     {0: "batch", 1: "decoder_sequence"}
				UpperCAmelCase_    			=     {0: "batch", 1: "decoder_sequence"}
			if self.use_past:
				self.fill_with_past_key_values_(lowerCAmelCase						,		direction="inputs"							)
		elif self.task == "causal-lm":
			UpperCAmelCase_    			=     OrderedDict(
			    [
			        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
			        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
			    ]							)
			if self.use_past:
				UpperCAmelCase_			,				UpperCAmelCase_    			=     self.num_layers
				for i in range(lowerCAmelCase							):
					UpperCAmelCase_    			=     {0: "batch", 2: "past_sequence + sequence"}
					UpperCAmelCase_    			=     {0: "batch", 2: "past_sequence + sequence"}
		else:
			UpperCAmelCase_    			=     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  __A   (						self		:  Union[str, Any]							):


		'''simple docstring'''







		if self.task in ["default", "seq2seq-lm"]:
			UpperCAmelCase_    			=     super().outputs
		else:
			UpperCAmelCase_    			=     super(lowerCAmelCase						,		self							).outputs
			if self.use_past:
				UpperCAmelCase_			,				UpperCAmelCase_    			=     self.num_layers
				for i in range(lowerCAmelCase							):
					UpperCAmelCase_    			=     {0: "batch", 2: "past_sequence + sequence"}
					UpperCAmelCase_    			=     {0: "batch", 2: "past_sequence + sequence"}
		return common_outputs




	def  __A   (						self		:  List[str]						,		lowerCAmelCase		:  PreTrainedTokenizer						,		lowerCAmelCase		:  int = -1						,		lowerCAmelCase		:  int = -1						,		lowerCAmelCase		:  bool = False						,		lowerCAmelCase		:  Optional[TensorType] = None						,		):


		'''simple docstring'''







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

		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_    			=     common_inputs["input_ids"].shape
			UpperCAmelCase_    			=     common_inputs["decoder_input_ids"].shape[1]
			UpperCAmelCase_			,				UpperCAmelCase_    			=     self.num_attention_heads
			UpperCAmelCase_    			=     (
			    batch,
			    num_encoder_attention_heads,
			    encoder_seq_length,
			    self._config.hidden_size // num_encoder_attention_heads,
			)
			UpperCAmelCase_    			=     decoder_seq_length
			UpperCAmelCase_    			=     (
			    batch,
			    num_decoder_attention_heads,
			    decoder_past_length,
			    self._config.hidden_size // num_decoder_attention_heads,
			)
			UpperCAmelCase_    			=     torch.cat(
			    [common_inputs["decoder_attention_mask"], torch.ones(lowerCAmelCase						,		lowerCAmelCase							)]						,		dim=1							)
			UpperCAmelCase_    			=     []
			UpperCAmelCase_			,				UpperCAmelCase_    			=     self.num_layers

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




	def  __A   (						self		:  List[str]						,		lowerCAmelCase		:  PreTrainedTokenizer						,		lowerCAmelCase		:  int = -1						,		lowerCAmelCase		:  int = -1						,		lowerCAmelCase		:  bool = False						,		lowerCAmelCase		:  Optional[TensorType] = None						,		):


		'''simple docstring'''







		UpperCAmelCase_    			=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
		    lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase							)

		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_    			=     common_inputs["input_ids"].shape
			UpperCAmelCase_    			=     seqlen
			UpperCAmelCase_			,				UpperCAmelCase_    			=     self.num_layers
			UpperCAmelCase_			,				UpperCAmelCase_    			=     self.num_attention_heads
			UpperCAmelCase_    			=     (
			    batch,
			    num_encoder_attention_heads,
			    past_key_values_length,
			    self._config.hidden_size // num_encoder_attention_heads,
			)
			UpperCAmelCase_    			=     common_inputs["attention_mask"].dtype
			UpperCAmelCase_    			=     torch.cat(
			    [common_inputs["attention_mask"], torch.ones(lowerCAmelCase						,		lowerCAmelCase						,		dtype=lowerCAmelCase							)]						,		dim=1							)
			UpperCAmelCase_    			=     [
			    (torch.zeros(lowerCAmelCase							), torch.zeros(lowerCAmelCase							)) for _ in range(lowerCAmelCase							)
			]
		return common_inputs




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


		'''simple docstring'''







		UpperCAmelCase_    			=     compute_effective_axis_dimension(
		    lowerCAmelCase						,		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_    			=     tokenizer.num_special_tokens_to_add(lowerCAmelCase							)
		UpperCAmelCase_    			=     compute_effective_axis_dimension(
		    lowerCAmelCase						,		fixed_dimension=OnnxConfig.default_fixed_sequence						,		num_token_to_add=lowerCAmelCase							)

		# Generate dummy inputs according to compute batch and sequence
		UpperCAmelCase_    			=     [" ".join([tokenizer.unk_token]							) * seq_length] * batch_size
		UpperCAmelCase_    			=     dict(tokenizer(lowerCAmelCase						,		return_tensors=lowerCAmelCase							)							)
		return common_inputs




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


		'''simple docstring'''







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

		elif self.task == "causal-lm":
			UpperCAmelCase_    			=     self._generate_dummy_inputs_for_causal_lm(
			    lowerCAmelCase						,		batch_size=lowerCAmelCase						,		seq_length=lowerCAmelCase						,		is_pair=lowerCAmelCase						,		framework=lowerCAmelCase							)
		else:
			UpperCAmelCase_    			=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    lowerCAmelCase						,		batch_size=lowerCAmelCase						,		seq_length=lowerCAmelCase						,		is_pair=lowerCAmelCase						,		framework=lowerCAmelCase							)

		return common_inputs




	def  __A   (						self		:  List[str]						,		lowerCAmelCase		:  Tuple						,		lowerCAmelCase		:  int						,		lowerCAmelCase		:  Tuple						,		lowerCAmelCase		:  Any							):


		'''simple docstring'''







		if self.task in ["default", "seq2seq-lm"]:
			UpperCAmelCase_    			=     super()._flatten_past_key_values_(lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase							)
		else:
			UpperCAmelCase_    			=     super(lowerCAmelCase						,		self							)._flatten_past_key_values_(
			    lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase							)




	def  __A   (						self		:  str						,		lowerCAmelCase		:  Mapping[str, Mapping[int, str]]						,		lowerCAmelCase		:  str							):


		'''simple docstring'''







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

		UpperCAmelCase_    			=     "past_key_values" if direction == "inputs" else "present"
		UpperCAmelCase_			,				UpperCAmelCase_    			=     self.num_layers

		UpperCAmelCase_    			=     "past_encoder_sequence"
		UpperCAmelCase_    			=     "past_decoder_sequence" if direction == "inputs" else "past_decoder_sequence + sequence"

		for i in range(lowerCAmelCase							):
			UpperCAmelCase_    			=     {0: "batch", 2: decoder_sequence}
			UpperCAmelCase_    			=     {0: "batch", 2: decoder_sequence}
			UpperCAmelCase_    			=     {0: "batch", 2: encoder_sequence}
			UpperCAmelCase_    			=     {0: "batch", 2: encoder_sequence}


from typing import TYPE_CHECKING

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


_a:      Tuple								=     {
    """configuration_segformer""": ["""SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SegformerConfig""", """SegformerOnnxConfig"""]
}

try:
				if not is_vision_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_a:      List[str]								=     ["""SegformerFeatureExtractor"""]
				_a:      Union[str, Any]								=     ["""SegformerImageProcessor"""]

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

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_a:      Optional[Any]								=     [
				    """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[int]								=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)