



from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


a__    =	logging.get_logger(__name__)

a__    =	{
    """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json""",
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}







class 		snake_case						(	SCREAMING_SNAKE_CASE_  ):



   '''simple docstring'''






   snake_case_					: List[Any]          =		"""blenderbot-small"""
   snake_case_					: List[Any]          =		["""past_key_values"""]
   snake_case_					: Union[str, Any]          =		{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

   def __init__(					self							:			str				,		lowerCAmelCase							:			Union[str, Any]=5_0265				,		lowerCAmelCase							:			Any=512				,		lowerCAmelCase							:			List[str]=8				,		lowerCAmelCase							:			List[Any]=2048				,		lowerCAmelCase							:			Dict=16				,		lowerCAmelCase							:			Any=8				,		lowerCAmelCase							:			Union[str, Any]=2048				,		lowerCAmelCase							:			Tuple=16				,		lowerCAmelCase							:			List[Any]=0.0				,		lowerCAmelCase							:			Any=0.0				,		lowerCAmelCase							:			Dict=True				,		lowerCAmelCase							:			List[str]=True				,		lowerCAmelCase							:			Optional[Any]="gelu"				,		lowerCAmelCase							:			Optional[Any]=512				,		lowerCAmelCase							:			Optional[Any]=0.1				,		lowerCAmelCase							:			Any=0.0				,		lowerCAmelCase							:			str=0.0				,		lowerCAmelCase							:			Tuple=0.02				,		lowerCAmelCase							:			Optional[int]=1				,		lowerCAmelCase							:			Dict=False				,		lowerCAmelCase							:			Tuple=0				,		lowerCAmelCase							:			Union[str, Any]=1				,		lowerCAmelCase							:			Optional[int]=2				,		lowerCAmelCase							:			Optional[int]=2				,		**lowerCAmelCase							:			Tuple				,		)				->				int:

    """simple docstring"""







    _snake_case    :  str					=					vocab_size
    _snake_case    :  Any					=					max_position_embeddings
    _snake_case    :  Optional[Any]					=					d_model
    _snake_case    :  Optional[Any]					=					encoder_ffn_dim
    _snake_case    :  Union[str, Any]					=					encoder_layers
    _snake_case    :  Tuple					=					encoder_attention_heads
    _snake_case    :  Any					=					decoder_ffn_dim
    _snake_case    :  List[Any]					=					decoder_layers
    _snake_case    :  List[str]					=					decoder_attention_heads
    _snake_case    :  int					=					dropout
    _snake_case    :  int					=					attention_dropout
    _snake_case    :  Optional[int]					=					activation_dropout
    _snake_case    :  Tuple					=					activation_function
    _snake_case    :  str					=					init_std
    _snake_case    :  Optional[int]					=					encoder_layerdrop
    _snake_case    :  Tuple					=					decoder_layerdrop
    _snake_case    :  str					=					use_cache
    _snake_case    :  Optional[Any]					=					encoder_layers
    _snake_case    :  int					=					scale_embedding  # scale factor will be sqrt(d_model) if True

    super().__init__(
        pad_token_id=lowerCAmelCase				,		bos_token_id=lowerCAmelCase				,		eos_token_id=lowerCAmelCase				,		is_encoder_decoder=lowerCAmelCase				,		decoder_start_token_id=lowerCAmelCase				,		forced_eos_token_id=lowerCAmelCase				,		**lowerCAmelCase				,		)







class 		snake_case						(	SCREAMING_SNAKE_CASE_  ):



   '''simple docstring'''






   @property
   def      UpperCamelCase_  (					self							:			Optional[int])				->				Mapping[str, Mapping[int, str]]:

    """simple docstring"""







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

     if self.use_past:
      _snake_case    :  List[Any]					=					{0: """batch"""}
      _snake_case    :  int					=					{0: """batch""", 1: """past_decoder_sequence + sequence"""}
     else:
      _snake_case    :  Optional[int]					=					{0: """batch""", 1: """decoder_sequence"""}
      _snake_case    :  Tuple					=					{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.
     _snake_case    :  List[Any]					=					OrderedDict(
         [
             ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
             ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
         ])
     if self.use_past:
      _snake_case	,    _snake_case    :  List[Any]					=					self.num_layers
      for i in range(lowerCAmelCase):
       _snake_case    :  int					=					{0: """batch""", 2: """past_sequence + sequence"""}
       _snake_case    :  List[str]					=					{0: """batch""", 2: """past_sequence + sequence"""}
    else:
     _snake_case    :  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
   def      UpperCamelCase_  (					self							:			str)				->				Mapping[str, Mapping[int, str]]:

    """simple docstring"""







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

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

    """simple docstring"""







    _snake_case    :  int					=					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
        lowerCAmelCase				,		lowerCAmelCase				,		lowerCAmelCase				,		lowerCAmelCase				,		lowerCAmelCase)

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

     _snake_case    :  Dict					=					torch.cat(
         [common_inputs["""decoder_attention_mask"""], torch.ones(lowerCAmelCase				,		lowerCAmelCase)]				,		dim=1)

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

    """simple docstring"""







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

     _snake_case    :  Optional[int]					=					common_inputs["""attention_mask"""].dtype
     _snake_case    :  List[str]					=					torch.cat(
         [common_inputs["""attention_mask"""], torch.ones(lowerCAmelCase				,		lowerCAmelCase				,		dtype=lowerCAmelCase)]				,		dim=1)
     _snake_case    :  Dict					=					[
         (torch.zeros(lowerCAmelCase), torch.zeros(lowerCAmelCase)) for _ in range(lowerCAmelCase)
     ]
    return common_inputs

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

    """simple docstring"""







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

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

    """simple docstring"""







    if self.task in ["default", "seq2seq-lm"]:
     _snake_case    :  List[Any]					=					self._generate_dummy_inputs_for_default_and_seqaseq_lm(
         lowerCAmelCase				,		batch_size=lowerCAmelCase				,		seq_length=lowerCAmelCase				,		is_pair=lowerCAmelCase				,		framework=lowerCAmelCase)

    elif self.task == "causal-lm":
     _snake_case    :  Optional[int]					=					self._generate_dummy_inputs_for_causal_lm(
         lowerCAmelCase				,		batch_size=lowerCAmelCase				,		seq_length=lowerCAmelCase				,		is_pair=lowerCAmelCase				,		framework=lowerCAmelCase)
    else:
     _snake_case    :  Optional[int]					=					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         lowerCAmelCase				,		batch_size=lowerCAmelCase				,		seq_length=lowerCAmelCase				,		is_pair=lowerCAmelCase				,		framework=lowerCAmelCase)

    return common_inputs


   def      UpperCamelCase_  (					self							:			str				,		lowerCAmelCase							:			Tuple				,		lowerCAmelCase							:			str				,		lowerCAmelCase							:			Optional[int]				,		lowerCAmelCase							:			Dict)				->				Tuple:

    """simple docstring"""







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





from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
  from transformers import (
      TFBertModel,
      TFCLIPVisionModel,
      TFDeiTModel,
      TFRobertaModel,
      TFVisionTextDualEncoderModel,
      TFViTModel,
      VisionTextDualEncoderConfig,
  )

if is_vision_available():
  from PIL import Image

  from transformers import VisionTextDualEncoderProcessor



def       lowercase       (   SCREAMING_SNAKE_CASE__       :	Dict				)  ->     Optional[int]:
 if isinstance(SCREAMING_SNAKE_CASE__				,       collections.abc.Iterable				):
  return x
 return (x, x)







@require_tf
class 		snake_case						:



   '''simple docstring'''






   def      UpperCamelCase_  (					self							:			List[str]				,		lowerCAmelCase							:			Tuple				,		lowerCAmelCase							:			List[str])				->				Any:

    """simple docstring"""







    pass

   def      UpperCamelCase_  (					self							:			Dict)				->				Optional[Any]:

    """simple docstring"""







    pass

   def      UpperCamelCase_  (					self							:			str)				->				Union[str, Any]:

    """simple docstring"""







    pass

   def      UpperCamelCase_  (					self							:			Dict				,		lowerCAmelCase							:			Dict				,		lowerCAmelCase							:			Tuple				,		lowerCAmelCase							:			Optional[int]				,		lowerCAmelCase							:			Any				,		lowerCAmelCase							:			str=None				,		**lowerCAmelCase							:			List[Any])				->				List[str]:

    """simple docstring"""







    _snake_case    :  Dict					=					VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase				,		lowerCAmelCase)

    _snake_case    :  Optional[int]					=					TFVisionTextDualEncoderModel(lowerCAmelCase)

    _snake_case    :  Dict					=					model(input_ids=lowerCAmelCase				,		pixel_values=lowerCAmelCase				,		attention_mask=lowerCAmelCase)

    self.assertEqual(output["""text_embeds"""].shape				,		(input_ids.shape[0], config.projection_dim))
    self.assertEqual(output["""image_embeds"""].shape				,		(pixel_values.shape[0], config.projection_dim))

   def      UpperCamelCase_  (					self							:			List[str]				,		lowerCAmelCase							:			Optional[Any]				,		lowerCAmelCase							:			Dict				,		lowerCAmelCase							:			Union[str, Any]				,		lowerCAmelCase							:			List[str]				,		lowerCAmelCase							:			Any=None				,		**lowerCAmelCase							:			Any)				->				Any:

    """simple docstring"""







    _snake_case	,    _snake_case    :  Dict					=					self.get_vision_text_model(lowerCAmelCase				,		lowerCAmelCase)
    _snake_case    :  Optional[int]					=					TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase				,		text_model=lowerCAmelCase)

    _snake_case    :  List[str]					=					model(input_ids=lowerCAmelCase				,		pixel_values=lowerCAmelCase				,		attention_mask=lowerCAmelCase)

    self.assertEqual(output["""text_embeds"""].shape				,		(input_ids.shape[0], model.config.projection_dim))
    self.assertEqual(output["""image_embeds"""].shape				,		(pixel_values.shape[0], model.config.projection_dim))

   def      UpperCamelCase_  (					self							:			Optional[int]				,		lowerCAmelCase							:			str				,		lowerCAmelCase							:			List[Any]				,		lowerCAmelCase							:			Tuple				,		lowerCAmelCase							:			List[Any]				,		lowerCAmelCase							:			List[Any]=None				,		**lowerCAmelCase							:			List[Any])				->				Tuple:

    """simple docstring"""







    _snake_case	,    _snake_case    :  List[Any]					=					self.get_vision_text_model(lowerCAmelCase				,		lowerCAmelCase)
    _snake_case    :  Optional[Any]					=					{"""vision_model""": vision_model, """text_model""": text_model}
    _snake_case    :  str					=					TFVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase)

    _snake_case    :  List[Any]					=					model(input_ids=lowerCAmelCase				,		pixel_values=lowerCAmelCase				,		attention_mask=lowerCAmelCase)

    self.assertEqual(output["""text_embeds"""].shape				,		(input_ids.shape[0], model.config.projection_dim))
    self.assertEqual(output["""image_embeds"""].shape				,		(pixel_values.shape[0], model.config.projection_dim))

   def      UpperCamelCase_  (					self							:			Optional[Any]				,		lowerCAmelCase							:			Any				,		lowerCAmelCase							:			List[Any]				,		lowerCAmelCase							:			Tuple				,		lowerCAmelCase							:			List[str]				,		lowerCAmelCase							:			Union[str, Any]=None				,		**lowerCAmelCase							:			int)				->				Tuple:

    """simple docstring"""







    _snake_case	,    _snake_case    :  int					=					self.get_vision_text_model(lowerCAmelCase				,		lowerCAmelCase)
    _snake_case    :  Union[str, Any]					=					TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase				,		text_model=lowerCAmelCase)

    _snake_case    :  str					=					model(input_ids=lowerCAmelCase				,		pixel_values=lowerCAmelCase				,		attention_mask=lowerCAmelCase)
    _snake_case    :  Optional[Any]					=					output[0].numpy()

    with tempfile.TemporaryDirectory() as tmpdirname:
     model.save_pretrained(lowerCAmelCase)
     _snake_case    :  Optional[Any]					=					TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase)

     _snake_case    :  Tuple					=					model(input_ids=lowerCAmelCase				,		pixel_values=lowerCAmelCase				,		attention_mask=lowerCAmelCase)
     _snake_case    :  Optional[int]					=					after_output[0].numpy()
     _snake_case    :  List[Any]					=					np.amax(np.abs(out_a - out_a))
     self.assertLessEqual(lowerCAmelCase				,		1E-5)

   def      UpperCamelCase_  (					self							:			Optional[int]				,		lowerCAmelCase							:			str				,		lowerCAmelCase							:			Optional[int]				,		lowerCAmelCase							:			Union[str, Any]				,		lowerCAmelCase							:			Union[str, Any]				,		lowerCAmelCase							:			Optional[int]=None				,		**lowerCAmelCase							:			Dict)				->				Any:

    """simple docstring"""







    _snake_case	,    _snake_case    :  List[str]					=					self.get_vision_text_model(lowerCAmelCase				,		lowerCAmelCase)
    _snake_case    :  Any					=					TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase				,		text_model=lowerCAmelCase)

    _snake_case    :  Tuple					=					model(
        input_ids=lowerCAmelCase				,		pixel_values=lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		output_attentions=lowerCAmelCase)

    _snake_case    :  List[Any]					=					output.vision_model_output.attentions
    self.assertEqual(len(lowerCAmelCase)				,		vision_config.num_hidden_layers)

    # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
    _snake_case    :  Any					=					to_atuple(vision_model.config.image_size)
    _snake_case    :  int					=					to_atuple(vision_model.config.patch_size)
    _snake_case    :  List[str]					=					(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
    _snake_case    :  Optional[int]					=					num_patches + 1
    self.assertEqual(vision_attentions[0].shape[-3:]				,		(vision_config.num_attention_heads, seq_len, seq_len))

    _snake_case    :  int					=					output.text_model_output.attentions
    self.assertEqual(len(lowerCAmelCase)				,		text_config.num_hidden_layers)

    self.assertEqual(
        text_attentions[0].shape[-3:]				,		(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])				,		)

   def      UpperCamelCase_  (					self							:			str				,		lowerCAmelCase							:			np.ndarray				,		lowerCAmelCase							:			np.ndarray				,		lowerCAmelCase							:			float)				->				Dict:

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					np.abs((a - b)).max()
    self.assertLessEqual(lowerCAmelCase				,		lowerCAmelCase				,		F'''Difference between torch and flax is {diff} (>= {tol}).''')

   def      UpperCamelCase_  (					self							:			Any)				->				int:

    """simple docstring"""







    _snake_case    :  Any					=					self.prepare_config_and_inputs()
    self.check_vision_text_dual_encoder_model(**lowerCAmelCase)

   def      UpperCamelCase_  (					self							:			List[str])				->				Optional[Any]:

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					self.prepare_config_and_inputs()
    self.check_model_from_pretrained_configs(**lowerCAmelCase)

   def      UpperCamelCase_  (					self							:			str)				->				Union[str, Any]:

    """simple docstring"""







    _snake_case    :  List[Any]					=					self.prepare_config_and_inputs()
    self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase)

   def      UpperCamelCase_  (					self							:			str)				->				Union[str, Any]:

    """simple docstring"""







    _snake_case    :  Optional[Any]					=					self.prepare_config_and_inputs()
    self.check_save_load(**lowerCAmelCase)

   def      UpperCamelCase_  (					self							:			Union[str, Any])				->				List[Any]:

    """simple docstring"""







    _snake_case    :  Tuple					=					self.prepare_config_and_inputs()
    self.check_vision_text_output_attention(**lowerCAmelCase)


   @slow
   def      UpperCamelCase_  (					self							:			str)				->				int:

    """simple docstring"""







    _snake_case	,    _snake_case    :  Dict					=					self.get_pretrained_model_and_inputs()

    _snake_case    :  Optional[Any]					=					model_a(**lowerCAmelCase)
    _snake_case    :  int					=					outputs[0].numpy()

    with tempfile.TemporaryDirectory() as tmp_dirname:
     model_a.save_pretrained(lowerCAmelCase)
     _snake_case    :  Union[str, Any]					=					TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase)

     _snake_case    :  Dict					=					model_a(**lowerCAmelCase)
     _snake_case    :  int					=					after_outputs[0].numpy()
     _snake_case    :  str					=					np.amax(np.abs(out_a - out_a))
     self.assertLessEqual(lowerCAmelCase				,		1E-5)







@require_tf
class 		snake_case						(	SCREAMING_SNAKE_CASE_					,unittest.TestCase  ):



   '''simple docstring'''






   def      UpperCamelCase_  (					self							:			List[Any])				->				Tuple:

    """simple docstring"""







    _snake_case    :  List[Any]					=					TFVisionTextDualEncoderModel.from_vision_text_pretrained(
        """hf-internal-testing/tiny-random-vit"""				,		"""hf-internal-testing/tiny-random-bert""")
    _snake_case    :  Union[str, Any]					=					13
    _snake_case    :  int					=					floats_tensor(
        [
            batch_size,
            model.vision_model.config.num_channels,
            model.vision_model.config.image_size,
            model.vision_model.config.image_size,
        ])
    _snake_case    :  Any					=					ids_tensor([batch_size, 4]				,		model.text_model.config.vocab_size)
    _snake_case    :  Tuple					=					random_attention_mask([batch_size, 4])
    _snake_case    :  Tuple					=					{"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

    return model, inputs

   def      UpperCamelCase_  (					self							:			List[str]				,		lowerCAmelCase							:			List[Any]				,		lowerCAmelCase							:			List[Any])				->				Dict:

    """simple docstring"""







    _snake_case    :  List[str]					=					TFViTModel(lowerCAmelCase				,		name="""vision_model""")
    _snake_case    :  List[Any]					=					TFBertModel(lowerCAmelCase				,		name="""text_model""")
    return vision_model, text_model


   def      UpperCamelCase_  (					self							:			List[str])				->				Union[str, Any]:

    """simple docstring"""







    _snake_case    :  Optional[Any]					=					TFViTModelTester(self)
    _snake_case    :  Optional[Any]					=					TFBertModelTester(self)
    _snake_case    :  Optional[int]					=					vit_model_tester.prepare_config_and_inputs()
    _snake_case    :  int					=					bert_model_tester.prepare_config_and_inputs()

    _snake_case	,    _snake_case	,    _snake_case    :  Dict					=					vision_config_and_inputs

    (
        (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    
    )    :  List[str]					=					text_config_and_inputs

    return {
        "text_config": text_config,
        "vision_config": vision_config,
        "pixel_values": pixel_values,
        "attention_mask": input_mask,
        "input_ids": input_ids,
        "text_token_type_ids": token_type_ids,
        "text_sequence_labels": sequence_labels,
        "text_token_labels": token_labels,
        "text_choice_labels": choice_labels,
    }







@require_tf
class 		snake_case						(	SCREAMING_SNAKE_CASE_					,unittest.TestCase  ):



   '''simple docstring'''






   def      UpperCamelCase_  (					self							:			str)				->				Tuple:

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					TFVisionTextDualEncoderModel.from_vision_text_pretrained(
        """Rocketknight1/tiny-random-deit-tf"""				,		"""hf-internal-testing/tiny-random-roberta""")
    _snake_case    :  int					=					13
    _snake_case    :  List[str]					=					floats_tensor(
        [
            batch_size,
            model.vision_model.config.num_channels,
            model.vision_model.config.image_size,
            model.vision_model.config.image_size,
        ])
    _snake_case    :  Union[str, Any]					=					ids_tensor([batch_size, 4]				,		model.text_model.config.vocab_size)
    _snake_case    :  int					=					random_attention_mask([batch_size, 4])
    _snake_case    :  List[str]					=					{"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

    return model, inputs

   def      UpperCamelCase_  (					self							:			str				,		lowerCAmelCase							:			str				,		lowerCAmelCase							:			Tuple				,		lowerCAmelCase							:			int				,		lowerCAmelCase							:			str				,		lowerCAmelCase							:			int=None				,		**lowerCAmelCase							:			Dict)				->				Optional[int]:

    """simple docstring"""







    _snake_case	,    _snake_case    :  Any					=					self.get_vision_text_model(lowerCAmelCase				,		lowerCAmelCase)
    _snake_case    :  int					=					TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase				,		text_model=lowerCAmelCase)

    _snake_case    :  Optional[Any]					=					model(
        input_ids=lowerCAmelCase				,		pixel_values=lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		output_attentions=lowerCAmelCase)

    _snake_case    :  Dict					=					output.vision_model_output.attentions
    self.assertEqual(len(lowerCAmelCase)				,		vision_config.num_hidden_layers)

    # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
    _snake_case    :  List[Any]					=					to_atuple(vision_model.config.image_size)
    _snake_case    :  str					=					to_atuple(vision_model.config.patch_size)
    _snake_case    :  str					=					(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
    _snake_case    :  List[Any]					=					num_patches + 2
    self.assertEqual(vision_attentions[0].shape[-3:]				,		(vision_config.num_attention_heads, seq_len, seq_len))

    _snake_case    :  Dict					=					output.text_model_output.attentions
    self.assertEqual(len(lowerCAmelCase)				,		text_config.num_hidden_layers)

    self.assertEqual(
        text_attentions[0].shape[-3:]				,		(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])				,		)

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

    """simple docstring"""







    _snake_case    :  Optional[int]					=					TFDeiTModel(lowerCAmelCase				,		name="""vision_model""")
    _snake_case    :  Optional[int]					=					TFRobertaModel(lowerCAmelCase				,		name="""text_model""")
    return vision_model, text_model


   def      UpperCamelCase_  (					self							:			List[str])				->				int:

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					TFDeiTModelTester(self)
    _snake_case    :  Tuple					=					TFRobertaModelTester(self)
    _snake_case    :  Optional[int]					=					vit_model_tester.prepare_config_and_inputs()
    _snake_case    :  Optional[Any]					=					bert_model_tester.prepare_config_and_inputs()

    _snake_case	,    _snake_case	,    _snake_case    :  List[Any]					=					vision_config_and_inputs

    (
        (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    
    )    :  Dict					=					text_config_and_inputs

    return {
        "text_config": text_config,
        "vision_config": vision_config,
        "pixel_values": pixel_values,
        "attention_mask": input_mask,
        "input_ids": input_ids,
        "text_token_type_ids": token_type_ids,
        "text_sequence_labels": sequence_labels,
        "text_token_labels": token_labels,
        "text_choice_labels": choice_labels,
    }







@require_tf
class 		snake_case						(	SCREAMING_SNAKE_CASE_					,unittest.TestCase  ):



   '''simple docstring'''






   def      UpperCamelCase_  (					self							:			Union[str, Any])				->				Union[str, Any]:

    """simple docstring"""







    _snake_case    :  str					=					TFVisionTextDualEncoderModel.from_vision_text_pretrained(
        """Rocketknight1/tiny-random-clip-tf"""				,		"""hf-internal-testing/tiny-random-bert""")
    _snake_case    :  str					=					13
    _snake_case    :  Dict					=					floats_tensor(
        [
            batch_size,
            model.vision_model.config.num_channels,
            model.vision_model.config.image_size,
            model.vision_model.config.image_size,
        ])
    _snake_case    :  Optional[int]					=					ids_tensor([batch_size, 4]				,		model.text_model.config.vocab_size)
    _snake_case    :  str					=					random_attention_mask([batch_size, 4])
    _snake_case    :  Optional[int]					=					{"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

    return model, inputs

   def      UpperCamelCase_  (					self							:			Optional[Any]				,		lowerCAmelCase							:			Optional[Any]				,		lowerCAmelCase							:			Any)				->				Optional[Any]:

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					TFCLIPVisionModel(lowerCAmelCase				,		name="""vision_model""")
    _snake_case    :  List[Any]					=					TFBertModel(lowerCAmelCase				,		name="""text_model""")
    return vision_model, text_model


   def      UpperCamelCase_  (					self							:			str)				->				List[Any]:

    """simple docstring"""







    _snake_case    :  List[str]					=					TFCLIPVisionModelTester(self)
    _snake_case    :  int					=					TFBertModelTester(self)
    _snake_case    :  Any					=					clip_model_tester.prepare_config_and_inputs()
    _snake_case    :  str					=					bert_model_tester.prepare_config_and_inputs()

    _snake_case	,    _snake_case    :  Optional[Any]					=					vision_config_and_inputs

    (
        (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    (
        _snake_case
    )	,    
    )    :  Union[str, Any]					=					text_config_and_inputs

    return {
        "text_config": text_config,
        "vision_config": vision_config,
        "pixel_values": pixel_values,
        "attention_mask": input_mask,
        "input_ids": input_ids,
        "text_token_type_ids": token_type_ids,
        "text_sequence_labels": sequence_labels,
        "text_token_labels": token_labels,
        "text_choice_labels": choice_labels,
    }







@require_vision
@require_tf
class 		snake_case						(	unittest.TestCase  ):



   '''simple docstring'''






   @slow
   def      UpperCamelCase_  (					self							:			Dict)				->				str:

    """simple docstring"""







    _snake_case    :  Optional[int]					=					TFVisionTextDualEncoderModel.from_pretrained(
        """clip-italian/clip-italian"""				,		logit_scale_init_value=1.0				,		from_pt=lowerCAmelCase)
    _snake_case    :  str					=					VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian""")

    _snake_case    :  List[str]					=					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
    _snake_case    :  List[Any]					=					processor(
        text=["""una foto di un gatto""", """una foto di un cane"""]				,		images=lowerCAmelCase				,		padding=lowerCAmelCase				,		return_tensors="""np""")

    _snake_case    :  Optional[int]					=					model(**lowerCAmelCase)

    # verify the logits
    self.assertEqual(outputs.logits_per_image.shape				,		(inputs.pixel_values.shape[0], inputs.input_ids.shape[0]))
    self.assertEqual(
        outputs.logits_per_text.shape				,		(inputs.input_ids.shape[0], inputs.pixel_values.shape[0])				,		)

    _snake_case    :  int					=					np.array([[1.2_284_727, 0.3_104_122]])

    self.assertTrue(np.allclose(outputs.logits_per_image.numpy()				,		lowerCAmelCase				,		atol=1E-3))
