



import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      FalconForCausalLM,
      FalconForQuestionAnswering,
      FalconForSequenceClassification,
      FalconForTokenClassification,
      FalconModel,
  )







class 		snake_case      :



   '''simple docstring'''






   def __init__(					self							:			List[str]				,		lowerCAmelCase							:			List[str]				,		lowerCAmelCase							:			Optional[Any]=3				,		lowerCAmelCase							:			List[Any]=7				,		lowerCAmelCase							:			List[str]=True				,		lowerCAmelCase							:			Tuple=True				,		lowerCAmelCase							:			Union[str, Any]=False				,		lowerCAmelCase							:			Optional[Any]=True				,		lowerCAmelCase							:			Dict=99				,		lowerCAmelCase							:			List[str]=32				,		lowerCAmelCase							:			Optional[Any]=5				,		lowerCAmelCase							:			List[str]=4				,		lowerCAmelCase							:			Optional[int]=37				,		lowerCAmelCase							:			Optional[int]="gelu"				,		lowerCAmelCase							:			int=0.1				,		lowerCAmelCase							:			Optional[Any]=0.1				,		lowerCAmelCase							:			Union[str, Any]=512				,		lowerCAmelCase							:			str=16				,		lowerCAmelCase							:			Optional[int]=2				,		lowerCAmelCase							:			Optional[int]=0.02				,		lowerCAmelCase							:			Dict=3				,		lowerCAmelCase							:			Any=4				,		lowerCAmelCase							:			List[str]=None				,		)				->				Optional[int]:

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					parent
    _snake_case    :  str					=					batch_size
    _snake_case    :  str					=					seq_length
    _snake_case    :  str					=					is_training
    _snake_case    :  int					=					use_input_mask
    _snake_case    :  List[str]					=					use_token_type_ids
    _snake_case    :  Optional[int]					=					use_labels
    _snake_case    :  Dict					=					vocab_size
    _snake_case    :  Dict					=					hidden_size
    _snake_case    :  str					=					num_hidden_layers
    _snake_case    :  Dict					=					num_attention_heads
    _snake_case    :  str					=					intermediate_size
    _snake_case    :  Dict					=					hidden_act
    _snake_case    :  Dict					=					hidden_dropout_prob
    _snake_case    :  str					=					attention_probs_dropout_prob
    _snake_case    :  Union[str, Any]					=					max_position_embeddings
    _snake_case    :  Optional[Any]					=					type_vocab_size
    _snake_case    :  Tuple					=					type_sequence_label_size
    _snake_case    :  Tuple					=					initializer_range
    _snake_case    :  Tuple					=					num_labels
    _snake_case    :  Union[str, Any]					=					num_choices
    _snake_case    :  Any					=					scope

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

    """simple docstring"""







    _snake_case    :  List[str]					=					ids_tensor([self.batch_size, self.seq_length]				,		self.vocab_size)

    _snake_case    :  Optional[int]					=					None
    if self.use_input_mask:
     _snake_case    :  int					=					random_attention_mask([self.batch_size, self.seq_length])

    _snake_case    :  Union[str, Any]					=					None

    _snake_case    :  int					=					None
    _snake_case    :  Tuple					=					None
    _snake_case    :  Any					=					None
    if self.use_labels:
     _snake_case    :  Tuple					=					ids_tensor([self.batch_size]				,		self.type_sequence_label_size)
     _snake_case    :  List[Any]					=					ids_tensor([self.batch_size, self.seq_length]				,		self.num_labels)
     _snake_case    :  Any					=					ids_tensor([self.batch_size]				,		self.num_choices)

    _snake_case    :  List[str]					=					self.get_config()

    return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels

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

    """simple docstring"""







    return FalconConfig(
        vocab_size=self.vocab_size				,		hidden_size=self.hidden_size				,		num_hidden_layers=self.num_hidden_layers				,		num_attention_heads=self.num_attention_heads				,		intermediate_size=self.intermediate_size				,		hidden_act=self.hidden_act				,		hidden_dropout_prob=self.hidden_dropout_prob				,		attention_probs_dropout_prob=self.attention_probs_dropout_prob				,		max_position_embeddings=self.max_position_embeddings				,		type_vocab_size=self.type_vocab_size				,		is_decoder=lowerCAmelCase				,		initializer_range=self.initializer_range				,		pad_token_id=1				,		new_decoder_architecture=lowerCAmelCase				,		)

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

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					FalconModel(config=lowerCAmelCase)
    model.to(lowerCAmelCase)
    model.eval()
    _snake_case    :  Dict					=					model(lowerCAmelCase				,		attention_mask=lowerCAmelCase)
    _snake_case    :  Any					=					model(lowerCAmelCase)
    self.parent.assertEqual(result.last_hidden_state.shape				,		(self.batch_size, self.seq_length, self.hidden_size))

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

    """simple docstring"""







    _snake_case    :  Dict					=					True
    _snake_case    :  Optional[Any]					=					FalconModel(lowerCAmelCase)
    model.to(lowerCAmelCase)
    model.eval()
    _snake_case    :  Dict					=					model(
        lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		encoder_hidden_states=lowerCAmelCase				,		encoder_attention_mask=lowerCAmelCase				,		)
    _snake_case    :  str					=					model(
        lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		encoder_hidden_states=lowerCAmelCase				,		)
    _snake_case    :  Optional[Any]					=					model(lowerCAmelCase				,		attention_mask=lowerCAmelCase)
    self.parent.assertEqual(result.last_hidden_state.shape				,		(self.batch_size, self.seq_length, self.hidden_size))

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

    """simple docstring"""







    _snake_case    :  Optional[int]					=					FalconForCausalLM(config=lowerCAmelCase)
    model.to(lowerCAmelCase)
    model.eval()
    _snake_case    :  List[str]					=					model(lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		labels=lowerCAmelCase)
    self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.seq_length, self.vocab_size))

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

    """simple docstring"""







    _snake_case    :  Optional[int]					=					True
    _snake_case    :  Union[str, Any]					=					True
    _snake_case    :  Optional[int]					=					FalconForCausalLM(config=lowerCAmelCase)
    model.to(lowerCAmelCase)
    model.eval()

    # first forward pass
    _snake_case    :  Union[str, Any]					=					model(
        lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		encoder_hidden_states=lowerCAmelCase				,		encoder_attention_mask=lowerCAmelCase				,		use_cache=lowerCAmelCase				,		)
    _snake_case    :  str					=					outputs.past_key_values

    # create hypothetical multiple next token and extent to next_input_ids
    _snake_case    :  Any					=					ids_tensor((self.batch_size, 3)				,		config.vocab_size)
    _snake_case    :  Tuple					=					ids_tensor((self.batch_size, 3)				,		vocab_size=2)

    # append to next input_ids and
    _snake_case    :  Union[str, Any]					=					torch.cat([input_ids, next_tokens]				,		dim=-1)
    _snake_case    :  int					=					torch.cat([input_mask, next_mask]				,		dim=-1)

    _snake_case    :  List[Any]					=					model(
        lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		encoder_hidden_states=lowerCAmelCase				,		encoder_attention_mask=lowerCAmelCase				,		output_hidden_states=lowerCAmelCase				,		)["""hidden_states"""][0]
    _snake_case    :  int					=					model(
        lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		encoder_hidden_states=lowerCAmelCase				,		encoder_attention_mask=lowerCAmelCase				,		past_key_values=lowerCAmelCase				,		output_hidden_states=lowerCAmelCase				,		)["""hidden_states"""][0]

    # select random slice
    _snake_case    :  Tuple					=					ids_tensor((1,)				,		output_from_past.shape[-1]).item()
    _snake_case    :  Union[str, Any]					=					output_from_no_past[:, -3:, random_slice_idx].detach()
    _snake_case    :  Dict					=					output_from_past[:, :, random_slice_idx].detach()

    self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1])

    # test that outputs are equal for slice
    self.parent.assertTrue(torch.allclose(lowerCAmelCase				,		lowerCAmelCase				,		atol=1E-3))


   def      UpperCamelCase_  (					self							:			Dict)				->				str:

    """simple docstring"""







    _snake_case    :  List[Any]					=					self.prepare_config_and_inputs()
    (
        _snake_case    
    )    :  Any					=					config_and_inputs
    _snake_case    :  Optional[int]					=					{"""input_ids""": input_ids, """attention_mask""": input_mask}
    return config, inputs_dict







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



   '''simple docstring'''






   snake_case_					: Union[str, Any]          =		(
       (
           FalconModel,
           FalconForCausalLM,
           FalconForSequenceClassification,
           FalconForTokenClassification,
           FalconForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   snake_case_					: Optional[int]          =		(FalconForCausalLM,) if is_torch_available() else ()
   snake_case_					: Optional[int]          =		(
       {
           """feature-extraction""": FalconModel,
           """text-classification""": FalconForSequenceClassification,
           """text-generation""": FalconForCausalLM,
           """question-answering""": FalconForQuestionAnswering,
           """token-classification""": FalconForTokenClassification,
           """zero-shot""": FalconForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   snake_case_					: List[Any]          =		False
   snake_case_					: List[Any]          =		False

   def      UpperCamelCase_  (					self							:			Tuple)				->				str:

    """simple docstring"""







    _snake_case    :  Dict					=					FalconModelTester(self)
    _snake_case    :  str					=					ConfigTester(self				,		config_class=lowerCAmelCase				,		hidden_size=37)

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

    """simple docstring"""







    self.config_tester.run_common_tests()

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

    """simple docstring"""







    _snake_case    :  Optional[int]					=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*lowerCAmelCase)

   def      UpperCamelCase_  (					self							:			int)				->				Tuple:

    """simple docstring"""







    _snake_case    :  str					=					self.model_tester.prepare_config_and_inputs()
    for alibi in [True, False]:
     _snake_case    :  Union[str, Any]					=					alibi
     self.model_tester.create_and_check_model(lowerCAmelCase				,		*lowerCAmelCase)

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

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					self.model_tester.prepare_config_and_inputs_for_common()
    _snake_case    :  str					=					3
    _snake_case    :  Any					=					input_dict["""input_ids"""]
    _snake_case    :  List[str]					=					input_ids.ne(1).to(lowerCAmelCase)
    _snake_case    :  List[str]					=					ids_tensor([self.model_tester.batch_size]				,		self.model_tester.type_sequence_label_size)
    _snake_case    :  Optional[Any]					=					FalconForSequenceClassification(lowerCAmelCase)
    model.to(lowerCAmelCase)
    model.eval()
    _snake_case    :  int					=					model(lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		labels=lowerCAmelCase)
    self.assertEqual(result.logits.shape				,		(self.model_tester.batch_size, self.model_tester.num_labels))

   def      UpperCamelCase_  (					self							:			int)				->				Optional[int]:

    """simple docstring"""







    _snake_case    :  int					=					self.model_tester.prepare_config_and_inputs_for_common()
    _snake_case    :  int					=					3
    _snake_case    :  str					=					"""single_label_classification"""
    _snake_case    :  Optional[int]					=					input_dict["""input_ids"""]
    _snake_case    :  List[Any]					=					input_ids.ne(1).to(lowerCAmelCase)
    _snake_case    :  Tuple					=					ids_tensor([self.model_tester.batch_size]				,		self.model_tester.type_sequence_label_size)
    _snake_case    :  Dict					=					FalconForSequenceClassification(lowerCAmelCase)
    model.to(lowerCAmelCase)
    model.eval()
    _snake_case    :  List[str]					=					model(lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		labels=lowerCAmelCase)
    self.assertEqual(result.logits.shape				,		(self.model_tester.batch_size, self.model_tester.num_labels))

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

    """simple docstring"""







    _snake_case    :  List[str]					=					self.model_tester.prepare_config_and_inputs_for_common()
    _snake_case    :  str					=					input_dict["""input_ids"""]
    _snake_case    :  Tuple					=					FalconForCausalLM(lowerCAmelCase)
    model.to(lowerCAmelCase)
    model.eval()
    _snake_case    :  int					=					model(lowerCAmelCase				,		use_cache=lowerCAmelCase)
    _snake_case    :  Union[str, Any]					=					input_ids.shape[0]
    _snake_case    :  Any					=					model._convert_to_rw_cache(result.past_key_values)
    _snake_case    :  Optional[int]					=					model._convert_cache_to_standard_format(lowerCAmelCase				,		lowerCAmelCase)
    for layer in range(len(lowerCAmelCase)):
     for tensor_idx in range(2):
      self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3)
      self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4)
      self.assertTrue(
          torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]))

   def      UpperCamelCase_  (					self							:			int)				->				Tuple:

    """simple docstring"""







    _snake_case    :  Any					=					self.model_tester.prepare_config_and_inputs_for_common()
    _snake_case    :  Union[str, Any]					=					3
    _snake_case    :  Tuple					=					"""multi_label_classification"""
    _snake_case    :  int					=					input_dict["""input_ids"""]
    _snake_case    :  Tuple					=					input_ids.ne(1).to(lowerCAmelCase)
    _snake_case    :  int					=					ids_tensor(
        [self.model_tester.batch_size, config.num_labels]				,		self.model_tester.type_sequence_label_size).to(torch.float)
    _snake_case    :  Optional[int]					=					FalconForSequenceClassification(lowerCAmelCase)
    model.to(lowerCAmelCase)
    model.eval()
    _snake_case    :  Union[str, Any]					=					model(lowerCAmelCase				,		attention_mask=lowerCAmelCase				,		labels=lowerCAmelCase)
    self.assertEqual(result.logits.shape				,		(self.model_tester.batch_size, self.model_tester.num_labels))


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

    """simple docstring"""







    for model_class in self.all_generative_model_classes:
     _snake_case    :  int					=					self.model_tester.prepare_config_and_inputs_for_common()

     # If it doesn't support cache, pass the test
     if not hasattr(lowerCAmelCase				,		"""use_cache"""):
      return

     _snake_case    :  Any					=					model_class(lowerCAmelCase).to(lowerCAmelCase)
     if "use_cache" not in inputs:
      _snake_case    :  Dict					=					True
     _snake_case    :  Tuple					=					model(**lowerCAmelCase)

     # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
     if "past_key_values" not in outputs:
      return

     _snake_case    :  int					=					(
         getattr(lowerCAmelCase				,		"""decoder_layers"""				,		lowerCAmelCase)
         or getattr(lowerCAmelCase				,		"""num_decoder_layers"""				,		lowerCAmelCase)
         or config.num_hidden_layers
     )
     _snake_case    :  Optional[int]					=					getattr(lowerCAmelCase				,		"""num_kv_heads"""				,		config.num_attention_heads)
     _snake_case    :  Optional[int]					=					getattr(lowerCAmelCase				,		"""d_model"""				,		config.hidden_size)
     _snake_case    :  Dict					=					embed_dim // num_attention_heads

     _snake_case    :  List[str]					=					outputs["""past_key_values"""]
     self.assertEqual(len(lowerCAmelCase)				,		lowerCAmelCase)

     _snake_case    :  Any					=					inputs["""input_ids"""].shape
     for i in range(lowerCAmelCase):
      if config.new_decoder_architecture:
       _snake_case    :  Optional[int]					=					config.num_attention_heads
      elif config.multi_query:
       _snake_case    :  Dict					=					1
      self.assertEqual(len(past_kv[0])				,		2)  # K V for the decoder = 2
      self.assertEqual(
          past_kv[i][0].shape				,		(batch_size, num_attention_heads, seq_length, per_head_embed_dim))
      self.assertEqual(
          past_kv[i][1].shape				,		(batch_size, num_attention_heads, seq_length, per_head_embed_dim))







@require_torch
class 		snake_case      (	unittest.TestCase  ):



   '''simple docstring'''






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

    """simple docstring"""







    _snake_case    :  Optional[Any]					=					AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b""")
    _snake_case    :  int					=					FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b""")
    model.eval()
    model.to(lowerCAmelCase)
    _snake_case    :  Union[str, Any]					=					tokenizer("""My favorite food is"""				,		return_tensors="""pt""").to(lowerCAmelCase)

    _snake_case    :  int					=					(
        """My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."""
    )

    _snake_case    :  List[str]					=					model.generate(**lowerCAmelCase				,		do_sample=lowerCAmelCase				,		max_new_tokens=19)
    _snake_case    :  Optional[int]					=					tokenizer.batch_decode(lowerCAmelCase)[0]

    self.assertEqual(lowerCAmelCase				,		lowerCAmelCase)

   @slow
   def      UpperCamelCase_  (					self							:			Optional[int])				->				Optional[int]:

    """simple docstring"""







    for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
     _snake_case    :  Optional[int]					=					AutoTokenizer.from_pretrained(lowerCAmelCase)
     _snake_case    :  Union[str, Any]					=					FalconForCausalLM.from_pretrained(lowerCAmelCase)
     model.eval()
     model.to(lowerCAmelCase)
     _snake_case    :  Any					=					tokenizer("""My favorite food is"""				,		return_tensors="""pt""").to(lowerCAmelCase)

     # We just test that these run without errors - the models are randomly initialized
     # and so the actual text outputs will be garbage
     model.generate(**lowerCAmelCase				,		do_sample=lowerCAmelCase				,		max_new_tokens=4)
     model.generate(**lowerCAmelCase				,		do_sample=lowerCAmelCase				,		max_new_tokens=4)
     model.generate(**lowerCAmelCase				,		num_beams=2				,		max_new_tokens=4)


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

    """simple docstring"""







    with torch.no_grad():
     for repo in [
         "Rocketknight1/falcon-rw-1b",
         "Rocketknight1/tiny-random-falcon-7b",
         "Rocketknight1/tiny-random-falcon-40b",
     ]:
      _snake_case    :  Optional[int]					=					AutoTokenizer.from_pretrained(lowerCAmelCase)
      _snake_case    :  Optional[int]					=					FalconForCausalLM.from_pretrained(lowerCAmelCase)
      model.eval()
      model.to(device=lowerCAmelCase)
      _snake_case    :  Any					=					tokenizer("""My favorite food is"""				,		return_tensors="""pt""").to(lowerCAmelCase)

      # Test results are the same with and without cache
      _snake_case    :  Dict					=					model.generate(**lowerCAmelCase				,		do_sample=lowerCAmelCase				,		max_new_tokens=20				,		use_cache=lowerCAmelCase)
      _snake_case    :  Dict					=					model.generate(**lowerCAmelCase				,		do_sample=lowerCAmelCase				,		max_new_tokens=20				,		use_cache=lowerCAmelCase)
      self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0)





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