



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


lowercase  							=					logging.get_logger(__name__)

lowercase  							=					{
    '''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__			(      A    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					'''marian'''
							_UpperCAmelCase      				=					['''past_key_values''']
							_UpperCAmelCase      				=					{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






							def __init__(  self	,       snake_case=58101	,       snake_case=None	,       snake_case=1024	,       snake_case=12	,       snake_case=4096	,       snake_case=16	,       snake_case=12	,       snake_case=4096	,       snake_case=16	,       snake_case=0.0	,       snake_case=0.0	,       snake_case=True	,       snake_case=True	,       snake_case="gelu"	,       snake_case=1024	,       snake_case=0.1	,       snake_case=0.0	,       snake_case=0.0	,       snake_case=0.02	,       snake_case=58100	,       snake_case=False	,       snake_case=58100	,       snake_case=0	,       snake_case=0	,       snake_case=True	,       **snake_case	,       )			->							Dict:
									_UpperCAmelCase							 =		vocab_size
									_UpperCAmelCase							 =		decoder_vocab_size or 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
									_UpperCAmelCase							 =		share_encoder_decoder_embeddings
									super().__init__(
									    pad_token_id=snake_case	,       eos_token_id=snake_case	,       is_encoder_decoder=snake_case	,       decoder_start_token_id=snake_case	,       forced_eos_token_id=snake_case	,       **snake_case	,       )







class 			lowercase__			(      A    ):
							'''simple docstring'''







							@property
							# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
							def        lowerCamelCase_   (  self						)			->							Mapping[str, Mapping[int, str]]:
									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_(snake_case	,       direction='inputs'						)
									elif self.task == "causal-lm":
											# TODO: figure this case out.
											_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(snake_case						):
															_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        lowerCamelCase_   (  self						)			->							Mapping[str, Mapping[int, str]]:
									if self.task in ["default", "seq2seq-lm"]:
											_UpperCAmelCase							 =		super().outputs
									else:
											_UpperCAmelCase							 =		super(snake_case	,       self						).outputs
											if self.use_past:
													_UpperCAmelCase ,					_UpperCAmelCase							 =		self.num_layers
													for i in range(snake_case						):
															_UpperCAmelCase							 =		{0: 'batch', 2: 'past_sequence + sequence'}
															_UpperCAmelCase							 =		{0: 'batch', 2: 'past_sequence + sequence'}
									return common_outputs






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case = -1	,       snake_case = -1	,       snake_case = False	,       snake_case = None	,       )			->							Mapping[str, Any]:
									_UpperCAmelCase							 =		self._generate_dummy_inputs_for_encoder_and_decoder(
									    snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)

									# Generate decoder inputs
									_UpperCAmelCase							 =		seq_length if not self.use_past else 1
									_UpperCAmelCase							 =		self._generate_dummy_inputs_for_encoder_and_decoder(
									    snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)
									_UpperCAmelCase							 =		{f'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
									_UpperCAmelCase							 =		dict(**snake_case	,       **snake_case						)

									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 + 3
											_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(snake_case	,       snake_case						)]	,       dim=1						)

											_UpperCAmelCase							 =		[]
											# If the number of encoder and decoder layers are present in the model configuration, both are considered
											_UpperCAmelCase ,					_UpperCAmelCase							 =		self.num_layers
											_UpperCAmelCase							 =		min(snake_case	,       snake_case						)
											_UpperCAmelCase							 =		max(snake_case	,       snake_case						) - min_num_layers
											_UpperCAmelCase							 =		'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

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






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case = -1	,       snake_case = -1	,       snake_case = False	,       snake_case = None	,       )			->							Mapping[str, Any]:
									_UpperCAmelCase							 =		self._generate_dummy_inputs_for_encoder_and_decoder(
									    snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)

									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
											# Not using the same length for past_key_values
											_UpperCAmelCase							 =		seqlen + 2
											_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(snake_case	,       snake_case	,       dtype=snake_case						)]	,       dim=1						)
											_UpperCAmelCase							 =		[
											    (torch.zeros(snake_case						), torch.zeros(snake_case						)) for _ in range(snake_case						)
											]
									return common_inputs






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case = -1	,       snake_case = -1	,       snake_case = False	,       snake_case = 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							 =		compute_effective_axis_dimension(
									    snake_case	,       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(snake_case						)
									_UpperCAmelCase							 =		compute_effective_axis_dimension(
									    snake_case	,       fixed_dimension=OnnxConfig.default_fixed_sequence	,       num_token_to_add=snake_case						)

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






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case = -1	,       snake_case = -1	,       snake_case = False	,       snake_case = None	,       )			->							Mapping[str, Any]:
									if self.task in ["default", "seq2seq-lm"]:
											_UpperCAmelCase							 =		self._generate_dummy_inputs_for_default_and_seqaseq_lm(
											    snake_case	,       batch_size=snake_case	,       seq_length=snake_case	,       is_pair=snake_case	,       framework=snake_case						)

									else:
											_UpperCAmelCase							 =		self._generate_dummy_inputs_for_causal_lm(
											    snake_case	,       batch_size=snake_case	,       seq_length=snake_case	,       is_pair=snake_case	,       framework=snake_case						)

									return common_inputs






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							str:
									if self.task in ["default", "seq2seq-lm"]:
											_UpperCAmelCase							 =		super()._flatten_past_key_values_(snake_case	,       snake_case	,       snake_case	,       snake_case						)
									else:
											_UpperCAmelCase							 =		super(snake_case	,       self						)._flatten_past_key_values_(
											    snake_case	,       snake_case	,       snake_case	,       snake_case						)






							@property
							def        lowerCamelCase_   (  self						)			->							float:
									return 1E-4







"""simple docstring"""





import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
					import torch




@is_pipeline_test
class 			lowercase__			(      unittest.TestCase    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
							_UpperCAmelCase      				=					TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case						)			->							Dict:
									_UpperCAmelCase							 =		TextaTextGenerationPipeline(model=snake_case	,       tokenizer=snake_case						)
									return generator, ["Something to write", "Something else"]






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case						)			->							Dict:
									_UpperCAmelCase							 =		generator('Something there'						)
									self.assertEqual(snake_case	,       [{'generated_text': ANY(snake_case						)}]						)
									# These are encoder decoder, they don't just append to incoming string
									self.assertFalse(outputs[0]['generated_text'].startswith('Something there'						)						)

									_UpperCAmelCase							 =		generator(['This is great !', 'Something else']	,       num_return_sequences=2	,       do_sample=snake_case						)
									self.assertEqual(
									    snake_case	,       [
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									    ]	,       )

									_UpperCAmelCase							 =		generator(
									    ['This is great !', 'Something else']	,       num_return_sequences=2	,       batch_size=2	,       do_sample=snake_case						)
									self.assertEqual(
									    snake_case	,       [
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									        [{'generated_text': ANY(snake_case						)}, {'generated_text': ANY(snake_case						)}],
									    ]	,       )

									with self.assertRaises(snake_case						):
											generator(4						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		pipeline('text2text-generation'	,       model='patrickvonplaten/t5-tiny-random'	,       framework='pt'						)
									# do_sample=False necessary for reproducibility
									_UpperCAmelCase							 =		generator('Something there'	,       do_sample=snake_case						)
									self.assertEqual(snake_case	,       [{'generated_text': ''}]						)

									_UpperCAmelCase							 =		3
									_UpperCAmelCase							 =		generator(
									    'Something there'	,       num_return_sequences=snake_case	,       num_beams=snake_case	,       )
									_UpperCAmelCase							 =		[
									    {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide Beide'},
									    {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide'},
									    {'generated_text': ''},
									]
									self.assertEqual(snake_case	,       snake_case						)

									_UpperCAmelCase							 =		generator('This is a test'	,       do_sample=snake_case	,       num_return_sequences=2	,       return_tensors=snake_case						)
									self.assertEqual(
									    snake_case	,       [
									        {'generated_token_ids': ANY(torch.Tensor						)},
									        {'generated_token_ids': ANY(torch.Tensor						)},
									    ]	,       )
									_UpperCAmelCase							 =		generator.model.config.eos_token_id
									_UpperCAmelCase							 =		'<pad>'
									_UpperCAmelCase							 =		generator(
									    ['This is a test', 'This is a second test']	,       do_sample=snake_case	,       num_return_sequences=2	,       batch_size=2	,       return_tensors=snake_case	,       )
									self.assertEqual(
									    snake_case	,       [
									        [
									            {'generated_token_ids': ANY(torch.Tensor						)},
									            {'generated_token_ids': ANY(torch.Tensor						)},
									        ],
									        [
									            {'generated_token_ids': ANY(torch.Tensor						)},
									            {'generated_token_ids': ANY(torch.Tensor						)},
									        ],
									    ]	,       )






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Any:
									_UpperCAmelCase							 =		pipeline('text2text-generation'	,       model='patrickvonplaten/t5-tiny-random'	,       framework='tf'						)
									# do_sample=False necessary for reproducibility
									_UpperCAmelCase							 =		generator('Something there'	,       do_sample=snake_case						)
									self.assertEqual(snake_case	,       [{'generated_text': ''}]						)


