






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


lowerCamelCase : Tuple						   =       logging.get_logger(__name__)

lowerCamelCase : Any						   =       {
    '''Helsinki-NLP/opus-mt-en-de''': '''https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json''',
    # See all Marian models at https://huggingface.co/models?filter=marian
}

class 		lowerCAmelCase     ( __a			):

					'''simple docstring'''

					_A						:			Any									=      '''marian'''
					_A						:			Optional[Any]									=      ['''past_key_values''']
					_A						:			Dict									=      {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}



					def __init__(  self				:	List[Any]		,     __a				:	Dict=58101		,     __a				:	Optional[int]=None		,     __a				:	int=1024		,     __a				:	Dict=12		,     __a				:	Optional[Any]=4096		,     __a				:	Union[str, Any]=16		,     __a				:	str=12		,     __a				:	Optional[int]=4096		,     __a				:	Optional[Any]=16		,     __a				:	Any=0.0		,     __a				:	Union[str, Any]=0.0		,     __a				:	Dict=True		,     __a				:	str=True		,     __a				:	str="gelu"		,     __a				:	Dict=1024		,     __a				:	Dict=0.1		,     __a				:	Tuple=0.0		,     __a				:	str=0.0		,     __a				:	Any=0.02		,     __a				:	Optional[Any]=58100		,     __a				:	Union[str, Any]=False		,     __a				:	Union[str, Any]=58100		,     __a				:	Optional[Any]=0		,     __a				:	int=0		,     __a				:	Dict=True		,     **__a				:	List[str]		,     )			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							Any				=     vocab_size
												__lowercase      :							str				=     decoder_vocab_size or vocab_size
												__lowercase      :							Any				=     max_position_embeddings
												__lowercase      :							Optional[Any]				=     d_model
												__lowercase      :							int				=     encoder_ffn_dim
												__lowercase      :							List[str]				=     encoder_layers
												__lowercase      :							Union[str, Any]				=     encoder_attention_heads
												__lowercase      :							Tuple				=     decoder_ffn_dim
												__lowercase      :							Any				=     decoder_layers
												__lowercase      :							Tuple				=     decoder_attention_heads
												__lowercase      :							Optional[Any]				=     dropout
												__lowercase      :							Dict				=     attention_dropout
												__lowercase      :							Optional[Any]				=     activation_dropout
												__lowercase      :							Optional[int]				=     activation_function
												__lowercase      :							Union[str, Any]				=     init_std
												__lowercase      :							Union[str, Any]				=     encoder_layerdrop
												__lowercase      :							Dict				=     decoder_layerdrop
												__lowercase      :							Optional[Any]				=     use_cache
												__lowercase      :							Union[str, Any]				=     encoder_layers
												__lowercase      :							List[str]				=     scale_embedding  # scale factor will be sqrt(d_model) if True
												__lowercase      :							List[Any]				=     share_encoder_decoder_embeddings
												super().__init__(
												    pad_token_id=__a		,     eos_token_id=__a		,     is_encoder_decoder=__a		,     decoder_start_token_id=__a		,     forced_eos_token_id=__a		,     **__a		,     )







class 		lowerCAmelCase     ( __a			):

					'''simple docstring'''

					@property
					# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
					def       lowerCAmelCase				(  self				:	Tuple							)			->  Mapping[str, Mapping[int, str]]:


												"""simple docstring"""

												if self.task in ["default", "seq2seq-lm"]:
																			__lowercase      :							Any				=     OrderedDict(
																			    [
																			        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
																			        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
																			    ]							)

																			if self.use_past:
																										__lowercase      :							List[str]				=     {0: """batch"""}
																										__lowercase      :							Optional[int]				=     {0: """batch""", 1: """past_decoder_sequence + sequence"""}
																			else:
																										__lowercase      :							int				=     {0: """batch""", 1: """decoder_sequence"""}
																										__lowercase      :							List[str]				=     {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.
																			__lowercase      :							List[Any]				=     OrderedDict(
																			    [
																			        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
																			        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
																			    ]							)
																			if self.use_past:
																										__lowercase			,			__lowercase      :							List[str]				=     self.num_layers
																										for i in range(__a							):
																																	__lowercase      :							Optional[Any]				=     {0: """batch""", 2: """past_sequence + sequence"""}
																																	__lowercase      :							Tuple				=     {0: """batch""", 2: """past_sequence + sequence"""}
												else:
																			__lowercase      :							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
					# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
					def       lowerCAmelCase				(  self				:	Dict							)			->  Mapping[str, Mapping[int, str]]:


												"""simple docstring"""

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



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


												"""simple docstring"""

												__lowercase      :							Dict				=     self._generate_dummy_inputs_for_encoder_and_decoder(
												    __a		,     __a		,     __a		,     __a		,     __a							)

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

																			__lowercase      :							Optional[int]				=     torch.cat(
																			    [common_inputs["""decoder_attention_mask"""], torch.ones(__a		,     __a							)]		,     dim=1							)

																			__lowercase      :							Union[str, Any]				=     []
																			# If the number of encoder and decoder layers are present in the model configuration, both are considered
																			__lowercase			,			__lowercase      :							Any				=     self.num_layers
																			__lowercase      :							str				=     min(__a		,     __a							)
																			__lowercase      :							int				=     max(__a		,     __a							) - min_num_layers
																			__lowercase      :							List[str]				=     """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.
																			__lowercase      :							Union[str, Any]				=     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       lowerCAmelCase				(  self				:	Optional[int]		,     __a				:	PreTrainedTokenizer		,     __a				:	int = -1		,     __a				:	int = -1		,     __a				:	bool = False		,     __a				:	Optional[TensorType] = None		,     )			->  Mapping[str, Any]:


												"""simple docstring"""

												__lowercase      :							Tuple				=     self._generate_dummy_inputs_for_encoder_and_decoder(
												    __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
																			__lowercase			,			__lowercase      :							Any				=     common_inputs["""input_ids"""].shape
																			# Not using the same length for past_key_values
																			__lowercase      :							Optional[Any]				=     seqlen + 2
																			__lowercase			,			__lowercase      :							Dict				=     self.num_layers
																			__lowercase			,			__lowercase      :							Optional[int]				=     self.num_attention_heads
																			__lowercase      :							List[str]				=     (
																			    batch,
																			    num_encoder_attention_heads,
																			    past_key_values_length,
																			    self._config.hidden_size // num_encoder_attention_heads,
																			)

																			__lowercase      :							str				=     common_inputs["""attention_mask"""].dtype
																			__lowercase      :							Union[str, Any]				=     torch.cat(
																			    [common_inputs["""attention_mask"""], torch.ones(__a		,     __a		,     dtype=__a							)]		,     dim=1							)
																			__lowercase      :							Union[str, Any]				=     [
																			    (torch.zeros(__a							), torch.zeros(__a							)) for _ in range(__a							)
																			]
												return common_inputs



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


												"""simple docstring"""

												__lowercase      :							Union[str, 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
												__lowercase      :							str				=     tokenizer.num_special_tokens_to_add(__a							)
												__lowercase      :							Any				=     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
												__lowercase      :							int				=     [""" """.join([tokenizer.unk_token]							) * seq_length] * batch_size
												__lowercase      :							Union[str, Any]				=     dict(tokenizer(__a		,     return_tensors=__a							)							)
												return common_inputs



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


												"""simple docstring"""

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

												else:
																			__lowercase      :							Optional[Any]				=     self._generate_dummy_inputs_for_causal_lm(
																			    __a		,     batch_size=__a		,     seq_length=__a		,     is_pair=__a		,     framework=__a							)

												return common_inputs



					def       lowerCAmelCase				(  self				:	Union[str, Any]		,     __a				:	Dict		,     __a				:	Any		,     __a				:	Optional[Any]		,     __a				:	str							)			->  int:


												"""simple docstring"""

												if self.task in ["default", "seq2seq-lm"]:
																			__lowercase      :							Tuple				=     super()._flatten_past_key_values_(__a		,     __a		,     __a		,     __a							)
												else:
																			__lowercase      :							List[str]				=     super(__a		,     self							)._flatten_past_key_values_(
																			    __a		,     __a		,     __a		,     __a							)



					@property
					def       lowerCAmelCase				(  self				:	str							)			->  float:


												"""simple docstring"""

												return 1E-4







from typing import TYPE_CHECKING

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


lowerCamelCase : int						   =       {
    '''configuration_funnel''': ['''FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FunnelConfig'''],
    '''convert_funnel_original_tf_checkpoint_to_pytorch''': [],
    '''tokenization_funnel''': ['''FunnelTokenizer'''],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	lowerCamelCase : List[str]						   =       ['''FunnelTokenizerFast''']

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	lowerCamelCase : Union[str, Any]						   =       [
	    '''FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST''',
	    '''FunnelBaseModel''',
	    '''FunnelForMaskedLM''',
	    '''FunnelForMultipleChoice''',
	    '''FunnelForPreTraining''',
	    '''FunnelForQuestionAnswering''',
	    '''FunnelForSequenceClassification''',
	    '''FunnelForTokenClassification''',
	    '''FunnelModel''',
	    '''FunnelPreTrainedModel''',
	    '''load_tf_weights_in_funnel''',
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	lowerCamelCase : str						   =       [
	    '''TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST''',
	    '''TFFunnelBaseModel''',
	    '''TFFunnelForMaskedLM''',
	    '''TFFunnelForMultipleChoice''',
	    '''TFFunnelForPreTraining''',
	    '''TFFunnelForQuestionAnswering''',
	    '''TFFunnelForSequenceClassification''',
	    '''TFFunnelForTokenClassification''',
	    '''TFFunnelModel''',
	    '''TFFunnelPreTrainedModel''',
	]


if TYPE_CHECKING:
	from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
	from .tokenization_funnel import FunnelTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_funnel_fast import FunnelTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_funnel import (
		    FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
		    FunnelBaseModel,
		    FunnelForMaskedLM,
		    FunnelForMultipleChoice,
		    FunnelForPreTraining,
		    FunnelForQuestionAnswering,
		    FunnelForSequenceClassification,
		    FunnelForTokenClassification,
		    FunnelModel,
		    FunnelPreTrainedModel,
		    load_tf_weights_in_funnel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_funnel import (
		    TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFFunnelBaseModel,
		    TFFunnelForMaskedLM,
		    TFFunnelForMultipleChoice,
		    TFFunnelForPreTraining,
		    TFFunnelForQuestionAnswering,
		    TFFunnelForSequenceClassification,
		    TFFunnelForTokenClassification,
		    TFFunnelModel,
		    TFFunnelPreTrainedModel,
		)

else:
	import sys

	lowerCamelCase : Any						   =       _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)