






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__	:			List[str]			   = logging.get_logger(__name__)

A__	:			Optional[int]			   = {
    '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       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				lowercase__											=       """marian"""
				lowercase__											=       ["""past_key_values"""]
				lowercase__											=       {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}




				def __init__(     self						:    List[str],  lowerCamelCase						:    List[str]=58_101,  lowerCamelCase						:    List[str]=None,  lowerCamelCase						:    int=1_024,  lowerCamelCase						:    Union[str, Any]=12,  lowerCamelCase						:    int=4_096,  lowerCamelCase						:    List[str]=16,  lowerCamelCase						:    str=12,  lowerCamelCase						:    List[str]=4_096,  lowerCamelCase						:    List[Any]=16,  lowerCamelCase						:    Dict=0.0,  lowerCamelCase						:    Optional[Any]=0.0,  lowerCamelCase						:    int=True,  lowerCamelCase						:    int=True,  lowerCamelCase						:    str="gelu",  lowerCamelCase						:    str=1_024,  lowerCamelCase						:    Any=0.1,  lowerCamelCase						:    Tuple=0.0,  lowerCamelCase						:    List[str]=0.0,  lowerCamelCase						:    List[str]=0.02,  lowerCamelCase						:    List[str]=58_100,  lowerCamelCase						:    Dict=False,  lowerCamelCase						:    List[Any]=58_100,  lowerCamelCase						:    Optional[Any]=0,  lowerCamelCase						:    Any=0,  lowerCamelCase						:    str=True,  **lowerCamelCase						:    Union[str, Any],  ):

								'''simple docstring'''
								lowercase__    =			vocab_size
								lowercase__    =			decoder_vocab_size or vocab_size
								lowercase__    =			max_position_embeddings
								lowercase__    =			d_model
								lowercase__    =			encoder_ffn_dim
								lowercase__    =			encoder_layers
								lowercase__    =			encoder_attention_heads
								lowercase__    =			decoder_ffn_dim
								lowercase__    =			decoder_layers
								lowercase__    =			decoder_attention_heads
								lowercase__    =			dropout
								lowercase__    =			attention_dropout
								lowercase__    =			activation_dropout
								lowercase__    =			activation_function
								lowercase__    =			init_std
								lowercase__    =			encoder_layerdrop
								lowercase__    =			decoder_layerdrop
								lowercase__    =			use_cache
								lowercase__    =			encoder_layers
								lowercase__    =			scale_embedding  # scale factor will be sqrt(d_model) if True
								lowercase__    =			share_encoder_decoder_embeddings
								super().__init__(
								    pad_token_id=lowerCamelCase,  eos_token_id=lowerCamelCase,  is_encoder_decoder=lowerCamelCase,  decoder_start_token_id=lowerCamelCase,  forced_eos_token_id=lowerCamelCase,  **lowerCamelCase,  )



class       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				@property
				# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
				def 				lowercase__   (     self						:    Any  ):

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

												if self.use_past:
																lowercase__    =			{0: '''batch'''}
																lowercase__    =			{0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
												else:
																lowercase__    =			{0: '''batch''', 1: '''decoder_sequence'''}
																lowercase__    =			{0: '''batch''', 1: '''decoder_sequence'''}

												if self.use_past:
																self.fill_with_past_key_values_(lowerCamelCase,  direction='''inputs'''  )
								elif self.task == "causal-lm":
												# TODO: figure this case out.
												lowercase__    =			OrderedDict(
												    [
												        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
												        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
												    ]  )
												if self.use_past:
																lowercase__ ,						lowercase__    =			self.num_layers
																for i in range(lowerCamelCase  ):
																				lowercase__    =			{0: '''batch''', 2: '''past_sequence + sequence'''}
																				lowercase__    =			{0: '''batch''', 2: '''past_sequence + sequence'''}
								else:
												lowercase__    =			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 				lowercase__   (     self						:    List[str]  ):

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




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

								'''simple docstring'''
								lowercase__    =			self._generate_dummy_inputs_for_encoder_and_decoder(
								    lowerCamelCase,  lowerCamelCase,  lowerCamelCase,  lowerCamelCase,  lowerCamelCase  )

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

												lowercase__    =			torch.cat(
												    [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCamelCase,  lowerCamelCase  )],  dim=1  )

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

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




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

								'''simple docstring'''
								lowercase__    =			self._generate_dummy_inputs_for_encoder_and_decoder(
								    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
												lowercase__ ,						lowercase__    =			common_inputs['''input_ids'''].shape
												# Not using the same length for past_key_values
												lowercase__    =			seqlen + 2
												lowercase__ ,						lowercase__    =			self.num_layers
												lowercase__ ,						lowercase__    =			self.num_attention_heads
												lowercase__    =			(
												    batch,
												    num_encoder_attention_heads,
												    past_key_values_length,
												    self._config.hidden_size // num_encoder_attention_heads,
												)

												lowercase__    =			common_inputs['''attention_mask'''].dtype
												lowercase__    =			torch.cat(
												    [common_inputs['''attention_mask'''], torch.ones(lowerCamelCase,  lowerCamelCase,  dtype=lowerCamelCase  )],  dim=1  )
												lowercase__    =			[
												    (torch.zeros(lowerCamelCase  ), torch.zeros(lowerCamelCase  )) for _ in range(lowerCamelCase  )
												]
								return common_inputs




				def 				lowercase__   (     self						:    List[Any],  lowerCamelCase						:    PreTrainedTokenizer,  lowerCamelCase						:    int = -1,  lowerCamelCase						:    int = -1,  lowerCamelCase						:    bool = False,  lowerCamelCase						:    Optional[TensorType] = None,  ):

								'''simple docstring'''
								# 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
								lowercase__    =			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
								lowercase__    =			tokenizer.num_special_tokens_to_add(lowerCamelCase  )
								lowercase__    =			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
								lowercase__    =			[''' '''.join([tokenizer.unk_token]  ) * seq_length] * batch_size
								lowercase__    =			dict(tokenizer(lowerCamelCase,  return_tensors=lowerCamelCase  )  )
								return common_inputs




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

								'''simple docstring'''
								if self.task in ["default", "seq2seq-lm"]:
												lowercase__    =			self._generate_dummy_inputs_for_default_and_seqaseq_lm(
												    lowerCamelCase,  batch_size=lowerCamelCase,  seq_length=lowerCamelCase,  is_pair=lowerCamelCase,  framework=lowerCamelCase  )

								else:
												lowercase__    =			self._generate_dummy_inputs_for_causal_lm(
												    lowerCamelCase,  batch_size=lowerCamelCase,  seq_length=lowerCamelCase,  is_pair=lowerCamelCase,  framework=lowerCamelCase  )

								return common_inputs




				def 				lowercase__   (     self						:    List[Any],  lowerCamelCase						:    List[Any],  lowerCamelCase						:    Tuple,  lowerCamelCase						:    int,  lowerCamelCase						:    Tuple  ):

								'''simple docstring'''
								if self.task in ["default", "seq2seq-lm"]:
												lowercase__    =			super()._flatten_past_key_values_(lowerCamelCase,  lowerCamelCase,  lowerCamelCase,  lowerCamelCase  )
								else:
												lowercase__    =			super(lowerCamelCase,  self  )._flatten_past_key_values_(
												    lowerCamelCase,  lowerCamelCase,  lowerCamelCase,  lowerCamelCase  )




				@property
				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								return 1E-4












from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType
class       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				lowercase__											=       ["""image_processor""", """tokenizer"""]
				lowercase__											=       """BlipImageProcessor"""
				lowercase__											=       ("""BertTokenizer""", """BertTokenizerFast""")




				def __init__(     self						:    Dict,  lowerCamelCase						:    Dict,  lowerCamelCase						:    str  ):

								'''simple docstring'''
								lowercase__    =			False
								super().__init__(lowerCamelCase,  lowerCamelCase  )
								lowercase__    =			self.image_processor




				def __call__(     self						:    int,  lowerCamelCase						:    ImageInput = None,  lowerCamelCase						:    Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,  lowerCamelCase						:    bool = True,  lowerCamelCase						:    Union[bool, str, PaddingStrategy] = False,  lowerCamelCase						:    Union[bool, str, TruncationStrategy] = None,  lowerCamelCase						:    Optional[int] = None,  lowerCamelCase						:    int = 0,  lowerCamelCase						:    Optional[int] = None,  lowerCamelCase						:    Optional[bool] = None,  lowerCamelCase						:    bool = False,  lowerCamelCase						:    bool = False,  lowerCamelCase						:    bool = False,  lowerCamelCase						:    bool = False,  lowerCamelCase						:    bool = False,  lowerCamelCase						:    bool = True,  lowerCamelCase						:    Optional[Union[str, TensorType]] = None,  **lowerCamelCase						:    Any,  ):

								'''simple docstring'''
								if images is None and text is None:
												raise ValueError('''You have to specify either images or text.'''  )

								# Get only text
								if images is None:
												lowercase__    =			self.tokenizer
												lowercase__    =			self.tokenizer(
												    text=lowerCamelCase,  add_special_tokens=lowerCamelCase,  padding=lowerCamelCase,  truncation=lowerCamelCase,  max_length=lowerCamelCase,  stride=lowerCamelCase,  pad_to_multiple_of=lowerCamelCase,  return_attention_mask=lowerCamelCase,  return_overflowing_tokens=lowerCamelCase,  return_special_tokens_mask=lowerCamelCase,  return_offsets_mapping=lowerCamelCase,  return_token_type_ids=lowerCamelCase,  return_length=lowerCamelCase,  verbose=lowerCamelCase,  return_tensors=lowerCamelCase,  **lowerCamelCase,  )
												return text_encoding

								# add pixel_values
								lowercase__    =			self.image_processor(lowerCamelCase,  return_tensors=lowerCamelCase  )

								if text is not None:
												lowercase__    =			self.tokenizer(
												    text=lowerCamelCase,  add_special_tokens=lowerCamelCase,  padding=lowerCamelCase,  truncation=lowerCamelCase,  max_length=lowerCamelCase,  stride=lowerCamelCase,  pad_to_multiple_of=lowerCamelCase,  return_attention_mask=lowerCamelCase,  return_overflowing_tokens=lowerCamelCase,  return_special_tokens_mask=lowerCamelCase,  return_offsets_mapping=lowerCamelCase,  return_token_type_ids=lowerCamelCase,  return_length=lowerCamelCase,  verbose=lowerCamelCase,  return_tensors=lowerCamelCase,  **lowerCamelCase,  )
								else:
												lowercase__    =			None

								if text_encoding is not None:
												encoding_image_processor.update(lowerCamelCase  )

								return encoding_image_processor




				def 				lowercase__   (     self						:    Tuple,  *lowerCamelCase						:    Union[str, Any],  **lowerCamelCase						:    Optional[int]  ):

								'''simple docstring'''
								return self.tokenizer.batch_decode(*lowerCamelCase,  **lowerCamelCase  )




				def 				lowercase__   (     self						:    List[str],  *lowerCamelCase						:    int,  **lowerCamelCase						:    List[str]  ):

								'''simple docstring'''
								return self.tokenizer.decode(*lowerCamelCase,  **lowerCamelCase  )




				@property
				def 				lowercase__   (     self						:    List[str]  ):

								'''simple docstring'''
								lowercase__    =			self.tokenizer.model_input_names
								lowercase__    =			self.image_processor.model_input_names
								return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names  )  )




