
"""simple docstring"""







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       A_      (       lowercase   )  ->				Any:


     """simple docstring"""







     if isinstance(lowercase    ,			collections.abc.Iterable   ):
          return x
     return (x, x)



@require_tf
class 		UpperCAmelCase_						:
 """simple docstring"""




 def 	a   (			self							:			Dict  , a_							:			Dict  , a_							:			int			)->       Union[str, Any]:



      """simple docstring"""

      pass




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



      """simple docstring"""

      pass




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



      """simple docstring"""

      pass




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



      """simple docstring"""

      UpperCAmelCase_    :     Tuple            =	VisionTextDualEncoderConfig.from_vision_text_configs(a_  , a_			)

      UpperCAmelCase_    :     int            =	TFVisionTextDualEncoderModel(a_			)

      UpperCAmelCase_    :     Dict            =	model(input_ids=a_  , pixel_values=a_  , attention_mask=a_			)

      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 	a   (			self							:			Optional[int]  , a_							:			List[Any]  , a_							:			Union[str, Any]  , a_							:			List[Any]  , a_							:			str  , a_							:			Tuple=None  , **a_							:			Optional[Any]			)->       Optional[Any]:



      """simple docstring"""

      UpperCAmelCase_       ,UpperCAmelCase_    :     str            =	self.get_vision_text_model(a_  , a_			)
      UpperCAmelCase_    :     Union[str, Any]            =	TFVisionTextDualEncoderModel(vision_model=a_  , text_model=a_			)

      UpperCAmelCase_    :     Tuple            =	model(input_ids=a_  , pixel_values=a_  , attention_mask=a_			)

      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 	a   (			self							:			Union[str, Any]  , a_							:			Optional[Any]  , a_							:			List[str]  , a_							:			Tuple  , a_							:			Tuple  , a_							:			Optional[int]=None  , **a_							:			List[Any]			)->       List[Any]:



      """simple docstring"""

      UpperCAmelCase_       ,UpperCAmelCase_    :     List[Any]            =	self.get_vision_text_model(a_  , a_			)
      UpperCAmelCase_    :     Optional[int]            =	{"""vision_model""": vision_model, """text_model""": text_model}
      UpperCAmelCase_    :     Dict            =	TFVisionTextDualEncoderModel.from_vision_text_pretrained(**a_			)

      UpperCAmelCase_    :     Tuple            =	model(input_ids=a_  , pixel_values=a_  , attention_mask=a_			)

      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 	a   (			self							:			Any  , a_							:			Any  , a_							:			Tuple  , a_							:			List[Any]  , a_							:			Dict  , a_							:			Tuple=None  , **a_							:			Dict			)->       int:



      """simple docstring"""

      UpperCAmelCase_       ,UpperCAmelCase_    :     str            =	self.get_vision_text_model(a_  , a_			)
      UpperCAmelCase_    :     List[str]            =	TFVisionTextDualEncoderModel(vision_model=a_  , text_model=a_			)

      UpperCAmelCase_    :     int            =	model(input_ids=a_  , pixel_values=a_  , attention_mask=a_			)
      UpperCAmelCase_    :     List[str]            =	output[0].numpy()

      with tempfile.TemporaryDirectory() as tmpdirname:
           model.save_pretrained(a_			)
           UpperCAmelCase_    :     List[Any]            =	TFVisionTextDualEncoderModel.from_pretrained(a_			)

           UpperCAmelCase_    :     str            =	model(input_ids=a_  , pixel_values=a_  , attention_mask=a_			)
           UpperCAmelCase_    :     Union[str, Any]            =	after_output[0].numpy()
           UpperCAmelCase_    :     Optional[int]            =	np.amax(np.abs(out_a - out_a			)			)
           self.assertLessEqual(a_  , 1E-5			)




 def 	a   (			self							:			str  , a_							:			List[str]  , a_							:			str  , a_							:			Union[str, Any]  , a_							:			str  , a_							:			List[Any]=None  , **a_							:			List[Any]			)->       str:



      """simple docstring"""

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

      UpperCAmelCase_    :     Optional[int]            =	model(
          input_ids=a_  , pixel_values=a_  , attention_mask=a_  , output_attentions=a_			)

      UpperCAmelCase_    :     int            =	output.vision_model_output.attentions
      self.assertEqual(len(a_			)  , vision_config.num_hidden_layers			)

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

      UpperCAmelCase_    :     Optional[Any]            =	output.text_model_output.attentions
      self.assertEqual(len(a_			)  , 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 	a   (			self							:			List[str]  , a_							:			np.ndarray  , a_							:			np.ndarray  , a_							:			float			)->       Any:



      """simple docstring"""

      UpperCAmelCase_    :     Optional[Any]            =	np.abs((a - b)			).max()
      self.assertLessEqual(a_  , a_  , f'''Difference between torch and flax is {diff} (>= {tol}).'''			)




 def 	a   (			self							:			str			)->       str:



      """simple docstring"""

      UpperCAmelCase_    :     Optional[int]            =	self.prepare_config_and_inputs()
      self.check_vision_text_dual_encoder_model(**a_			)




 def 	a   (			self							:			Optional[Any]			)->       Any:



      """simple docstring"""

      UpperCAmelCase_    :     str            =	self.prepare_config_and_inputs()
      self.check_model_from_pretrained_configs(**a_			)




 def 	a   (			self							:			Dict			)->       int:



      """simple docstring"""

      UpperCAmelCase_    :     List[str]            =	self.prepare_config_and_inputs()
      self.check_vision_text_dual_encoder_from_pretrained(**a_			)




 def 	a   (			self							:			List[str]			)->       Tuple:



      """simple docstring"""

      UpperCAmelCase_    :     Dict            =	self.prepare_config_and_inputs()
      self.check_save_load(**a_			)




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



      """simple docstring"""

      UpperCAmelCase_    :     Dict            =	self.prepare_config_and_inputs()
      self.check_vision_text_output_attention(**a_			)






 @slow
 def 	a   (			self							:			Union[str, Any]			)->       str:



      """simple docstring"""

      UpperCAmelCase_       ,UpperCAmelCase_    :     List[Any]            =	self.get_pretrained_model_and_inputs()

      UpperCAmelCase_    :     List[Any]            =	model_a(**a_			)
      UpperCAmelCase_    :     List[Any]            =	outputs[0].numpy()

      with tempfile.TemporaryDirectory() as tmp_dirname:
           model_a.save_pretrained(a_			)
           UpperCAmelCase_    :     str            =	TFVisionTextDualEncoderModel.from_pretrained(a_			)

           UpperCAmelCase_    :     Union[str, Any]            =	model_a(**a_			)
           UpperCAmelCase_    :     List[Any]            =	after_outputs[0].numpy()
           UpperCAmelCase_    :     Dict            =	np.amax(np.abs(out_a - out_a			)			)
           self.assertLessEqual(a_  , 1E-5			)



@require_tf
class 		UpperCAmelCase_						(lowerCamelCase_	,			unittest.TestCase    ):
 """simple docstring"""




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



      """simple docstring"""

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

      return model, inputs




 def 	a   (			self							:			Any  , a_							:			Any  , a_							:			Any			)->       int:



      """simple docstring"""

      UpperCAmelCase_    :     Optional[int]            =	TFViTModel(a_  , name="""vision_model"""			)
      UpperCAmelCase_    :     int            =	TFBertModel(a_  , name="""text_model"""			)
      return vision_model, text_model






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



      """simple docstring"""

      UpperCAmelCase_    :     int            =	TFViTModelTester(self			)
      UpperCAmelCase_    :     List[str]            =	TFBertModelTester(self			)
      UpperCAmelCase_    :     List[str]            =	vit_model_tester.prepare_config_and_inputs()
      UpperCAmelCase_    :     Optional[int]            =	bert_model_tester.prepare_config_and_inputs()

      UpperCAmelCase_       ,UpperCAmelCase_       ,UpperCAmelCase_    :     List[str]            =	vision_config_and_inputs

      (
          (
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,
      )    :     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_tf
class 		UpperCAmelCase_						(lowerCamelCase_	,			unittest.TestCase    ):
 """simple docstring"""




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



      """simple docstring"""

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

      return model, inputs




 def 	a   (			self							:			Any  , a_							:			Optional[int]  , a_							:			Any  , a_							:			Union[str, Any]  , a_							:			Any  , a_							:			List[str]=None  , **a_							:			Optional[Any]			)->       Any:



      """simple docstring"""

      UpperCAmelCase_       ,UpperCAmelCase_    :     List[str]            =	self.get_vision_text_model(a_  , a_			)
      UpperCAmelCase_    :     List[str]            =	TFVisionTextDualEncoderModel(vision_model=a_  , text_model=a_			)

      UpperCAmelCase_    :     Dict            =	model(
          input_ids=a_  , pixel_values=a_  , attention_mask=a_  , output_attentions=a_			)

      UpperCAmelCase_    :     Optional[int]            =	output.vision_model_output.attentions
      self.assertEqual(len(a_			)  , 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)
      UpperCAmelCase_    :     List[str]            =	to_atuple(vision_model.config.image_size			)
      UpperCAmelCase_    :     List[str]            =	to_atuple(vision_model.config.patch_size			)
      UpperCAmelCase_    :     Any            =	(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
      UpperCAmelCase_    :     List[Any]            =	num_patches + 2
      self.assertEqual(vision_attentions[0].shape[-3:]  , (vision_config.num_attention_heads, seq_len, seq_len)			)

      UpperCAmelCase_    :     Optional[int]            =	output.text_model_output.attentions
      self.assertEqual(len(a_			)  , 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 	a   (			self							:			Any  , a_							:			int  , a_							:			List[str]			)->       Tuple:



      """simple docstring"""

      UpperCAmelCase_    :     Dict            =	TFDeiTModel(a_  , name="""vision_model"""			)
      UpperCAmelCase_    :     Optional[Any]            =	TFRobertaModel(a_  , name="""text_model"""			)
      return vision_model, text_model






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



      """simple docstring"""

      UpperCAmelCase_    :     List[str]            =	TFDeiTModelTester(self			)
      UpperCAmelCase_    :     str            =	TFRobertaModelTester(self			)
      UpperCAmelCase_    :     List[str]            =	vit_model_tester.prepare_config_and_inputs()
      UpperCAmelCase_    :     Union[str, Any]            =	bert_model_tester.prepare_config_and_inputs()

      UpperCAmelCase_       ,UpperCAmelCase_       ,UpperCAmelCase_    :     Dict            =	vision_config_and_inputs

      (
          (
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,
      )    :     Optional[int]            =	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 		UpperCAmelCase_						(lowerCamelCase_	,			unittest.TestCase    ):
 """simple docstring"""




 def 	a   (			self							:			List[Any]			)->       Dict:



      """simple docstring"""

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

      return model, inputs




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



      """simple docstring"""

      UpperCAmelCase_    :     Optional[int]            =	TFCLIPVisionModel(a_  , name="""vision_model"""			)
      UpperCAmelCase_    :     List[str]            =	TFBertModel(a_  , name="""text_model"""			)
      return vision_model, text_model






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



      """simple docstring"""

      UpperCAmelCase_    :     Union[str, Any]            =	TFCLIPVisionModelTester(self			)
      UpperCAmelCase_    :     List[str]            =	TFBertModelTester(self			)
      UpperCAmelCase_    :     Any            =	clip_model_tester.prepare_config_and_inputs()
      UpperCAmelCase_    :     str            =	bert_model_tester.prepare_config_and_inputs()

      UpperCAmelCase_       ,UpperCAmelCase_    :     Union[str, Any]            =	vision_config_and_inputs

      (
          (
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,(
          UpperCAmelCase_
      )       ,
      )    :     List[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 		UpperCAmelCase_						(unittest.TestCase    ):
 """simple docstring"""




 @slow
 def 	a   (			self							:			List[str]			)->       Dict:



      """simple docstring"""

      UpperCAmelCase_    :     int            =	TFVisionTextDualEncoderModel.from_pretrained(
          """clip-italian/clip-italian"""  , logit_scale_init_value=1.0  , from_pt=a_			)
      UpperCAmelCase_    :     Any            =	VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian"""			)

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

      UpperCAmelCase_    :     Union[str, Any]            =	model(**a_			)

      # 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])  , )

      UpperCAmelCase_    :     Tuple            =	np.array([[1.2284727, 0.3104122]]			)

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



"""simple docstring"""







import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Audio, ClassLabel, Features
from .base import TaskTemplate



@dataclass(frozen=lowerCamelCase_    )
class 		UpperCAmelCase_						(lowerCamelCase_    ):
 """simple docstring"""




 UpperCamelCase_    :  str					 =     field(default="""audio-classification"""	,			metadata={"""include_in_asdict_even_if_is_default""": True}    )
 UpperCamelCase_    :  ClassVar[Features]					 =     Features({"""audio""": Audio()}    )
 UpperCamelCase_    :  ClassVar[Features]					 =     Features({"""labels""": ClassLabel}    )
 UpperCamelCase_    :  str					 =     "audio"
 UpperCamelCase_    :  str					 =     "labels"




 def 	a   (			self							:			List[str]  , a_							:			Union[str, Any]			)->       Optional[int]:



      """simple docstring"""

      if self.label_column not in features:
           raise ValueError(f'''Column {self.label_column} is not present in features.'''			)
      if not isinstance(features[self.label_column]  , a_			):
           raise ValueError(f'''Column {self.label_column} is not a ClassLabel.'''			)
      UpperCAmelCase_    :     Any            =	copy.deepcopy(self			)
      UpperCAmelCase_    :     List[str]            =	self.label_schema.copy()
      UpperCAmelCase_    :     List[str]            =	features[self.label_column]
      UpperCAmelCase_    :     Optional[Any]            =	label_schema
      return task_template






 @property
 def 	a   (			self							:			Optional[Any]			)->       Dict[str, str]:



      """simple docstring"""

      return {
          self.audio_column: "audio",
          self.label_column: "labels",
      }

