






'''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


lowerCAmelCase__          =						logging.get_logger(__name__)

lowerCAmelCase__          =						{
    '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       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''marian'''
	_lowerCamelCase              =  ['''past_key_values''']
	_lowerCamelCase              =  {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}

	def __init__(      self   :						str  ,					UpperCAmelCase_   :						Optional[int]=58_101  ,					UpperCAmelCase_   :						Dict=None  ,					UpperCAmelCase_   :						Dict=1_024  ,					UpperCAmelCase_   :						List[Any]=12  ,					UpperCAmelCase_   :						Any=4_096  ,					UpperCAmelCase_   :						List[Any]=16  ,					UpperCAmelCase_   :						Optional[Any]=12  ,					UpperCAmelCase_   :						Any=4_096  ,					UpperCAmelCase_   :						str=16  ,					UpperCAmelCase_   :						int=0.0  ,					UpperCAmelCase_   :						str=0.0  ,					UpperCAmelCase_   :						Union[str, Any]=True  ,					UpperCAmelCase_   :						str=True  ,					UpperCAmelCase_   :						int="gelu"  ,					UpperCAmelCase_   :						Tuple=1_024  ,					UpperCAmelCase_   :						List[Any]=0.1  ,					UpperCAmelCase_   :						Tuple=0.0  ,					UpperCAmelCase_   :						Optional[Any]=0.0  ,					UpperCAmelCase_   :						List[Any]=0.02  ,					UpperCAmelCase_   :						List[Any]=58_100  ,					UpperCAmelCase_   :						Any=False  ,					UpperCAmelCase_   :						Optional[int]=58_100  ,					UpperCAmelCase_   :						List[str]=0  ,					UpperCAmelCase_   :						Any=0  ,					UpperCAmelCase_   :						Union[str, Any]=True  ,					**UpperCAmelCase_   :						Optional[int]  ,					):
			UpperCamelCase__				:					Tuple					=					vocab_size
			UpperCamelCase__				:					str					=					decoder_vocab_size or vocab_size
			UpperCamelCase__				:					Tuple					=					max_position_embeddings
			UpperCamelCase__				:					Union[str, Any]					=					d_model
			UpperCamelCase__				:					Optional[int]					=					encoder_ffn_dim
			UpperCamelCase__				:					Optional[Any]					=					encoder_layers
			UpperCamelCase__				:					Union[str, Any]					=					encoder_attention_heads
			UpperCamelCase__				:					Optional[int]					=					decoder_ffn_dim
			UpperCamelCase__				:					List[str]					=					decoder_layers
			UpperCamelCase__				:					Optional[int]					=					decoder_attention_heads
			UpperCamelCase__				:					Dict					=					dropout
			UpperCamelCase__				:					str					=					attention_dropout
			UpperCamelCase__				:					Tuple					=					activation_dropout
			UpperCamelCase__				:					Optional[int]					=					activation_function
			UpperCamelCase__				:					Any					=					init_std
			UpperCamelCase__				:					Optional[Any]					=					encoder_layerdrop
			UpperCamelCase__				:					int					=					decoder_layerdrop
			UpperCamelCase__				:					Optional[Any]					=					use_cache
			UpperCamelCase__				:					Tuple					=					encoder_layers
			UpperCamelCase__				:					Optional[int]					=					scale_embedding  # scale factor will be sqrt(d_model) if True
			UpperCamelCase__				:					Union[str, Any]					=					share_encoder_decoder_embeddings
			super().__init__(
			    pad_token_id=UpperCAmelCase_  ,					eos_token_id=UpperCAmelCase_  ,					is_encoder_decoder=UpperCAmelCase_  ,					decoder_start_token_id=UpperCAmelCase_  ,					forced_eos_token_id=UpperCAmelCase_  ,					**UpperCAmelCase_  ,					)







class       __lowercase					(__lowerCamelCase    ):

	@property
	# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
	def      __UpperCamelCase							(      self   :						List[str]):
			if self.task in ["default", "seq2seq-lm"]:
					UpperCamelCase__				:					Dict					=					OrderedDict(
					    [
					        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
					        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
					    ])

					if self.use_past:
							UpperCamelCase__				:					Optional[int]					=					{0: 'batch'}
							UpperCamelCase__				:					List[Any]					=					{0: 'batch', 1: 'past_decoder_sequence + sequence'}
					else:
							UpperCamelCase__				:					Optional[Any]					=					{0: 'batch', 1: 'decoder_sequence'}
							UpperCamelCase__				:					List[Any]					=					{0: 'batch', 1: 'decoder_sequence'}

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

	def      __UpperCamelCase							(      self   :						List[Any]  ,					UpperCAmelCase_   :						PreTrainedTokenizer  ,					UpperCAmelCase_   :						int = -1  ,					UpperCAmelCase_   :						int = -1  ,					UpperCAmelCase_   :						bool = False  ,					UpperCAmelCase_   :						Optional[TensorType] = None  ,					):
			UpperCamelCase__				:					Optional[Any]					=					self._generate_dummy_inputs_for_encoder_and_decoder(
			    UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_)

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

					UpperCamelCase__				:					int					=					torch.cat(
					    [common_inputs['decoder_attention_mask'], torch.ones(UpperCAmelCase_  ,					UpperCAmelCase_)]  ,					dim=1)

					UpperCamelCase__				:					Tuple					=					[]
					# If the number of encoder and decoder layers are present in the model configuration, both are considered
					UpperCamelCase__,	UpperCamelCase__				:					Union[str, Any]					=					self.num_layers
					UpperCamelCase__				:					Optional[Any]					=					min(UpperCAmelCase_  ,					UpperCAmelCase_)
					UpperCamelCase__				:					Optional[int]					=					max(UpperCAmelCase_  ,					UpperCAmelCase_) - min_num_layers
					UpperCamelCase__				:					Any					=					'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.
					UpperCamelCase__				:					Union[str, Any]					=					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

	def      __UpperCamelCase							(      self   :						Tuple  ,					UpperCAmelCase_   :						PreTrainedTokenizer  ,					UpperCAmelCase_   :						int = -1  ,					UpperCAmelCase_   :						int = -1  ,					UpperCAmelCase_   :						bool = False  ,					UpperCAmelCase_   :						Optional[TensorType] = None  ,					):
			UpperCamelCase__				:					Optional[Any]					=					self._generate_dummy_inputs_for_encoder_and_decoder(
			    UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					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
					UpperCamelCase__,	UpperCamelCase__				:					Optional[Any]					=					common_inputs['input_ids'].shape
					# Not using the same length for past_key_values
					UpperCamelCase__				:					str					=					seqlen + 2
					UpperCamelCase__,	UpperCamelCase__				:					Optional[Any]					=					self.num_layers
					UpperCamelCase__,	UpperCamelCase__				:					Optional[Any]					=					self.num_attention_heads
					UpperCamelCase__				:					Union[str, Any]					=					(
					    batch,
					    num_encoder_attention_heads,
					    past_key_values_length,
					    self._config.hidden_size // num_encoder_attention_heads,
					)

					UpperCamelCase__				:					str					=					common_inputs['attention_mask'].dtype
					UpperCamelCase__				:					int					=					torch.cat(
					    [common_inputs['attention_mask'], torch.ones(UpperCAmelCase_  ,					UpperCAmelCase_  ,					dtype=UpperCAmelCase_)]  ,					dim=1)
					UpperCamelCase__				:					Optional[int]					=					[
					    (torch.zeros(UpperCAmelCase_), torch.zeros(UpperCAmelCase_)) for _ in range(UpperCAmelCase_)
					]
			return common_inputs

	def      __UpperCamelCase							(      self   :						Dict  ,					UpperCAmelCase_   :						PreTrainedTokenizer  ,					UpperCAmelCase_   :						int = -1  ,					UpperCAmelCase_   :						int = -1  ,					UpperCAmelCase_   :						bool = False  ,					UpperCAmelCase_   :						Optional[TensorType] = None  ,					):
			# 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__				:					Any					=					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
			UpperCamelCase__				:					Optional[int]					=					tokenizer.num_special_tokens_to_add(UpperCAmelCase_)
			UpperCamelCase__				:					List[str]					=					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
			UpperCamelCase__				:					Tuple					=					[' '.join([tokenizer.unk_token]) * seq_length] * batch_size
			UpperCamelCase__				:					int					=					dict(tokenizer(UpperCAmelCase_  ,					return_tensors=UpperCAmelCase_))
			return common_inputs

	def      __UpperCamelCase							(      self   :						List[str]  ,					UpperCAmelCase_   :						PreTrainedTokenizer  ,					UpperCAmelCase_   :						int = -1  ,					UpperCAmelCase_   :						int = -1  ,					UpperCAmelCase_   :						bool = False  ,					UpperCAmelCase_   :						Optional[TensorType] = None  ,					):
			if self.task in ["default", "seq2seq-lm"]:
					UpperCamelCase__				:					Tuple					=					self._generate_dummy_inputs_for_default_and_seqaseq_lm(
					    UpperCAmelCase_  ,					batch_size=UpperCAmelCase_  ,					seq_length=UpperCAmelCase_  ,					is_pair=UpperCAmelCase_  ,					framework=UpperCAmelCase_)

			else:
					UpperCamelCase__				:					int					=					self._generate_dummy_inputs_for_causal_lm(
					    UpperCAmelCase_  ,					batch_size=UpperCAmelCase_  ,					seq_length=UpperCAmelCase_  ,					is_pair=UpperCAmelCase_  ,					framework=UpperCAmelCase_)

			return common_inputs

	def      __UpperCamelCase							(      self   :						str  ,					UpperCAmelCase_   :						Any  ,					UpperCAmelCase_   :						List[str]  ,					UpperCAmelCase_   :						List[str]  ,					UpperCAmelCase_   :						Dict):
			if self.task in ["default", "seq2seq-lm"]:
					UpperCamelCase__				:					Any					=					super()._flatten_past_key_values_(UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_)
			else:
					UpperCamelCase__				:					List[str]					=					super(UpperCAmelCase_  ,					self)._flatten_past_key_values_(
					    UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_)





	@property
	def      __UpperCamelCase							(      self   :						Union[str, Any]):
			return 1e-4












'''simple docstring'''




def       __UpperCAmelCase						(  lowerCamelCase_     ,      lowerCamelCase_)							->     list[str]:
		return [sentence[i : i + ngram_size] for i in range(len(lowerCamelCase_) - ngram_size + 1)]


if __name__ == "__main__":
		from doctest import testmod

		testmod()




