

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

lowerCAmelCase  : Optional[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 			_A		(	__magic_name__):
  SCREAMING_SNAKE_CASE     :   Optional[Any]							=	'''marian'''
  SCREAMING_SNAKE_CASE     :   Any							=	['''past_key_values''']
  SCREAMING_SNAKE_CASE     :   List[Any]							=	{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}

  def __init__(					self			,       _SCREAMING_SNAKE_CASE=5_8101			,       _SCREAMING_SNAKE_CASE=None			,       _SCREAMING_SNAKE_CASE=1024			,       _SCREAMING_SNAKE_CASE=12			,       _SCREAMING_SNAKE_CASE=4096			,       _SCREAMING_SNAKE_CASE=16			,       _SCREAMING_SNAKE_CASE=12			,       _SCREAMING_SNAKE_CASE=4096			,       _SCREAMING_SNAKE_CASE=16			,       _SCREAMING_SNAKE_CASE=0.0			,       _SCREAMING_SNAKE_CASE=0.0			,       _SCREAMING_SNAKE_CASE=True			,       _SCREAMING_SNAKE_CASE=True			,       _SCREAMING_SNAKE_CASE="gelu"			,       _SCREAMING_SNAKE_CASE=1024			,       _SCREAMING_SNAKE_CASE=0.1			,       _SCREAMING_SNAKE_CASE=0.0			,       _SCREAMING_SNAKE_CASE=0.0			,       _SCREAMING_SNAKE_CASE=0.02			,       _SCREAMING_SNAKE_CASE=5_8100			,       _SCREAMING_SNAKE_CASE=False			,       _SCREAMING_SNAKE_CASE=5_8100			,       _SCREAMING_SNAKE_CASE=0			,       _SCREAMING_SNAKE_CASE=0			,       _SCREAMING_SNAKE_CASE=True			,       **_SCREAMING_SNAKE_CASE			,       ):





        """simple docstring"""







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

class 			_A		(	__magic_name__):

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





        """simple docstring"""







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

              if self.use_past:
                    SCREAMING_SNAKE_CASE_					:	Any     							=  {0: 'batch'}
                    SCREAMING_SNAKE_CASE_					:	Union[str, Any]     							=  {0: 'batch', 1: 'past_decoder_sequence + sequence'}
              else:
                    SCREAMING_SNAKE_CASE_					:	str     							=  {0: 'batch', 1: 'decoder_sequence'}
                    SCREAMING_SNAKE_CASE_					:	List[Any]     							=  {0: 'batch', 1: 'decoder_sequence'}

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





        """simple docstring"""







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

  def 			UpperCAmelCase     (					self			,       _SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE = -1			,       _SCREAMING_SNAKE_CASE = -1			,       _SCREAMING_SNAKE_CASE = False			,       _SCREAMING_SNAKE_CASE = None			,       ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_					:	Optional[Any]     							=  self._generate_dummy_inputs_for_encoder_and_decoder(
            lowercase__			,       lowercase__			,       lowercase__			,       lowercase__			,       lowercase__       )

        # Generate decoder inputs
        SCREAMING_SNAKE_CASE_					:	Optional[Any]     							=  seq_length if not self.use_past else 1
        SCREAMING_SNAKE_CASE_					:	Optional[Any]     							=  self._generate_dummy_inputs_for_encoder_and_decoder(
            lowercase__			,       lowercase__			,       lowercase__			,       lowercase__			,       lowercase__       )
        SCREAMING_SNAKE_CASE_					:	Optional[int]     							=  {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
        SCREAMING_SNAKE_CASE_					:	Union[str, Any]     							=  dict(**lowercase__			,       **lowercase__       )

        if self.use_past:
              if not is_torch_available():
                    raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'       )
              else:
                    import torch
              SCREAMING_SNAKE_CASE_							,    SCREAMING_SNAKE_CASE_					:	List[str]     							=  common_inputs['input_ids'].shape
              SCREAMING_SNAKE_CASE_					:	Dict     							=  common_inputs['decoder_input_ids'].shape[1]
              SCREAMING_SNAKE_CASE_							,    SCREAMING_SNAKE_CASE_					:	List[str]     							=  self.num_attention_heads
              SCREAMING_SNAKE_CASE_					:	Tuple     							=  (
                  batch,
                  num_encoder_attention_heads,
                  encoder_seq_length,
                  self._config.hidden_size // num_encoder_attention_heads,
              )
              SCREAMING_SNAKE_CASE_					:	Optional[int]     							=  decoder_seq_length + 3
              SCREAMING_SNAKE_CASE_					:	List[Any]     							=  (
                  batch,
                  num_decoder_attention_heads,
                  decoder_past_length,
                  self._config.hidden_size // num_decoder_attention_heads,
              )

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

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

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

  def 			UpperCAmelCase     (					self			,       _SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE = -1			,       _SCREAMING_SNAKE_CASE = -1			,       _SCREAMING_SNAKE_CASE = False			,       _SCREAMING_SNAKE_CASE = None			,       ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_					:	Union[str, Any]     							=  self._generate_dummy_inputs_for_encoder_and_decoder(
            lowercase__			,       lowercase__			,       lowercase__			,       lowercase__			,       lowercase__       )

        if self.use_past:
              if not is_torch_available():
                    raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'       )
              else:
                    import torch
              SCREAMING_SNAKE_CASE_							,    SCREAMING_SNAKE_CASE_					:	int     							=  common_inputs['input_ids'].shape
              # Not using the same length for past_key_values
              SCREAMING_SNAKE_CASE_					:	List[Any]     							=  seqlen + 2
              SCREAMING_SNAKE_CASE_							,    SCREAMING_SNAKE_CASE_					:	Union[str, Any]     							=  self.num_layers
              SCREAMING_SNAKE_CASE_							,    SCREAMING_SNAKE_CASE_					:	List[Any]     							=  self.num_attention_heads
              SCREAMING_SNAKE_CASE_					:	Optional[int]     							=  (
                  batch,
                  num_encoder_attention_heads,
                  past_key_values_length,
                  self._config.hidden_size // num_encoder_attention_heads,
              )

              SCREAMING_SNAKE_CASE_					:	Tuple     							=  common_inputs['attention_mask'].dtype
              SCREAMING_SNAKE_CASE_					:	Optional[int]     							=  torch.cat(
                  [common_inputs['attention_mask'], torch.ones(lowercase__			,       lowercase__			,       dtype=lowercase__       )]			,       dim=1       )
              SCREAMING_SNAKE_CASE_					:	List[Any]     							=  [
                  (torch.zeros(lowercase__       ), torch.zeros(lowercase__       )) for _ in range(lowercase__       )
              ]
        return common_inputs

  def 			UpperCAmelCase     (					self			,       _SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE = -1			,       _SCREAMING_SNAKE_CASE = -1			,       _SCREAMING_SNAKE_CASE = False			,       _SCREAMING_SNAKE_CASE = None			,       ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_					:	Union[str, Any]     							=  compute_effective_axis_dimension(
            lowercase__			,       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
        SCREAMING_SNAKE_CASE_					:	Optional[int]     							=  tokenizer.num_special_tokens_to_add(lowercase__       )
        SCREAMING_SNAKE_CASE_					:	Any     							=  compute_effective_axis_dimension(
            lowercase__			,       fixed_dimension=OnnxConfig.default_fixed_sequence			,       num_token_to_add=lowercase__       )

        # Generate dummy inputs according to compute batch and sequence
        SCREAMING_SNAKE_CASE_					:	Union[str, Any]     							=  [' '.join([tokenizer.unk_token]       ) * seq_length] * batch_size
        SCREAMING_SNAKE_CASE_					:	List[str]     							=  dict(tokenizer(lowercase__			,       return_tensors=lowercase__       )       )
        return common_inputs

  def 			UpperCAmelCase     (					self			,       _SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE = -1			,       _SCREAMING_SNAKE_CASE = -1			,       _SCREAMING_SNAKE_CASE = False			,       _SCREAMING_SNAKE_CASE = None			,       ):





        """simple docstring"""







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

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

        return common_inputs

  def 			UpperCAmelCase     (					self			,       _SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE			,       _SCREAMING_SNAKE_CASE       ):





        """simple docstring"""







        if self.task in ["default", "seq2seq-lm"]:
              SCREAMING_SNAKE_CASE_					:	Union[str, Any]     							=  super()._flatten_past_key_values_(lowercase__			,       lowercase__			,       lowercase__			,       lowercase__       )
        else:
              SCREAMING_SNAKE_CASE_					:	List[str]     							=  super(lowercase__			,       self       )._flatten_past_key_values_(
                  lowercase__			,       lowercase__			,       lowercase__			,       lowercase__       )



  @property
  def 			UpperCAmelCase     (					self       ):





        """simple docstring"""







        return 1e-4







from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
				import tensorflow as tf

				from transformers import TFBlipTextModel
				from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST



class        lowerCamelCase__				:





			def __init__(       self	:	Optional[Any]  ,  lowercase__	:	int  ,  lowercase__	:	List[str]=12  ,  lowercase__	:	Optional[int]=7  ,  lowercase__	:	List[Any]=True  ,  lowercase__	:	str=True  ,  lowercase__	:	Optional[Any]=True  ,  lowercase__	:	List[str]=99  ,  lowercase__	:	Optional[int]=32  ,  lowercase__	:	Tuple=32  ,  lowercase__	:	Dict=2  ,  lowercase__	:	Optional[int]=4  ,  lowercase__	:	str=37  ,  lowercase__	:	Optional[Any]=0.1  ,  lowercase__	:	List[str]=0.1  ,  lowercase__	:	Union[str, Any]=5_12  ,  lowercase__	:	List[str]=0.0_2  ,  lowercase__	:	Tuple=0  ,  lowercase__	:	List[Any]=None  ,  ):
										_lowerCAmelCase							=							parent
										_lowerCAmelCase							=							batch_size
										_lowerCAmelCase							=							seq_length
										_lowerCAmelCase							=							is_training
										_lowerCAmelCase							=							use_input_mask
										_lowerCAmelCase							=							use_labels
										_lowerCAmelCase							=							vocab_size
										_lowerCAmelCase							=							hidden_size
										_lowerCAmelCase							=							projection_dim
										_lowerCAmelCase							=							num_hidden_layers
										_lowerCAmelCase							=							num_attention_heads
										_lowerCAmelCase							=							intermediate_size
										_lowerCAmelCase							=							dropout
										_lowerCAmelCase							=							attention_dropout
										_lowerCAmelCase							=							max_position_embeddings
										_lowerCAmelCase							=							initializer_range
										_lowerCAmelCase							=							scope
										_lowerCAmelCase							=							bos_token_id





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size						)

										_lowerCAmelCase							=							None
										if self.use_input_mask:
																	_lowerCAmelCase							=							random_attention_mask([self.batch_size, self.seq_length]						)

										if input_mask is not None:
																	_lowerCAmelCase							=							input_mask.numpy()
																	_lowerCAmelCase			,		_lowerCAmelCase							=							input_mask.shape
																	_lowerCAmelCase							=							np.random.randint(1  ,  seq_length - 1  ,  size=(batch_size,)						)
																	for batch_idx, start_index in enumerate(lowercase__						):
																								_lowerCAmelCase							=							1
																								_lowerCAmelCase							=							0

										_lowerCAmelCase							=							self.get_config()

										return config, input_ids, tf.convert_to_tensor(lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										return BlipTextConfig(
										    vocab_size=self.vocab_size  ,  hidden_size=self.hidden_size  ,  projection_dim=self.projection_dim  ,  num_hidden_layers=self.num_hidden_layers  ,  num_attention_heads=self.num_attention_heads  ,  intermediate_size=self.intermediate_size  ,  dropout=self.dropout  ,  attention_dropout=self.attention_dropout  ,  max_position_embeddings=self.max_position_embeddings  ,  initializer_range=self.initializer_range  ,  bos_token_id=self.bos_token_id  ,  )





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple  ,  lowercase__	:	int  ,  lowercase__	:	List[Any]  ,  lowercase__	:	Union[str, Any]						):
										_lowerCAmelCase							=							TFBlipTextModel(config=lowercase__						)
										_lowerCAmelCase							=							model(lowercase__  ,  attention_mask=lowercase__  ,  training=lowercase__						)
										_lowerCAmelCase							=							model(lowercase__  ,  training=lowercase__						)
										self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)						)
										self.parent.assertEqual(result.pooler_output.shape  ,  (self.batch_size, self.hidden_size)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							self.prepare_config_and_inputs()
										_lowerCAmelCase			,		_lowerCAmelCase			,		_lowerCAmelCase							=							config_and_inputs
										_lowerCAmelCase							=							{'input_ids': input_ids, 'attention_mask': input_mask}
										return config, inputs_dict







@require_tf
class        lowerCamelCase__				(   UpperCAmelCase       ,unittest.TestCase				):
			UpperCamelCase__    							=(TFBlipTextModel,) if is_tf_available() else ()
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										_lowerCAmelCase							=							BlipTextModelTester(self						)
										_lowerCAmelCase							=							ConfigTester(self  ,  config_class=lowercase__  ,  hidden_size=37						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										self.config_tester.run_common_tests()





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										pass





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										pass





			@unittest.skip(reason='Blip does not use inputs_embeds'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										pass





			@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										pass





			@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										pass





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_lowerCAmelCase							=							TFBlipTextModel.from_pretrained(lowercase__						)
																	self.assertIsNotNone(lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any  ,  lowercase__	:	Any=True						):
										super().test_pt_tf_model_equivalence(allow_missing_keys=lowercase__						)






