



"""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       _snake_case						(						UpperCamelCase						:		List[str]						):
 if isinstance(UpperCamelCase			,		collections.abc.Iterable						):
  return x
 return (x, x)



@require_tf
class       SCREAMING_SNAKE_CASE__  :

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      Dict:





    '''simple docstring'''




    pass

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    pass

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





    '''simple docstring'''




    pass

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE=None      ,     **_SCREAMING_SNAKE_CASE						)  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					Union[str, Any]	=	VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					int	=	TFVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					Tuple	=	model(input_ids=_SCREAMING_SNAKE_CASE      ,     pixel_values=_SCREAMING_SNAKE_CASE      ,     attention_mask=_SCREAMING_SNAKE_CASE						)

    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 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE=None      ,     **_SCREAMING_SNAKE_CASE						)  ->      Tuple:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					Optional[Any]	=	self.get_vision_text_model(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					List[Any]	=	TFVisionTextDualEncoderModel(vision_model=_SCREAMING_SNAKE_CASE      ,     text_model=_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					List[str]	=	model(input_ids=_SCREAMING_SNAKE_CASE      ,     pixel_values=_SCREAMING_SNAKE_CASE      ,     attention_mask=_SCREAMING_SNAKE_CASE						)

    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 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE=None      ,     **_SCREAMING_SNAKE_CASE						)  ->      int:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					int	=	self.get_vision_text_model(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					List[str]	=	{"""vision_model""": vision_model, """text_model""": text_model}
    UpperCAmelCase	:					List[Any]	=	TFVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					List[str]	=	model(input_ids=_SCREAMING_SNAKE_CASE      ,     pixel_values=_SCREAMING_SNAKE_CASE      ,     attention_mask=_SCREAMING_SNAKE_CASE						)

    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 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE=None      ,     **_SCREAMING_SNAKE_CASE						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					List[Any]	=	self.get_vision_text_model(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Any	=	TFVisionTextDualEncoderModel(vision_model=_SCREAMING_SNAKE_CASE      ,     text_model=_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					Union[str, Any]	=	model(input_ids=_SCREAMING_SNAKE_CASE      ,     pixel_values=_SCREAMING_SNAKE_CASE      ,     attention_mask=_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Optional[Any]	=	output[0].numpy()

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

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

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE=None      ,     **_SCREAMING_SNAKE_CASE						)  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					Optional[int]	=	self.get_vision_text_model(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					int	=	TFVisionTextDualEncoderModel(vision_model=_SCREAMING_SNAKE_CASE      ,     text_model=_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					Dict	=	model(
        input_ids=_SCREAMING_SNAKE_CASE      ,     pixel_values=_SCREAMING_SNAKE_CASE      ,     attention_mask=_SCREAMING_SNAKE_CASE      ,     output_attentions=_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					Any	=	output.vision_model_output.attentions
    self.assertEqual(len(_SCREAMING_SNAKE_CASE						)      ,     vision_config.num_hidden_layers						)

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

    UpperCAmelCase	:					List[Any]	=	output.text_model_output.attentions
    self.assertEqual(len(_SCREAMING_SNAKE_CASE						)      ,     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 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					str	=	np.abs((a - b)						).max()
    self.assertLessEqual(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     F"Difference between torch and flax is {diff} (>= {tol})."						)

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					Optional[int]	=	self.prepare_config_and_inputs()
    self.check_vision_text_dual_encoder_model(**_SCREAMING_SNAKE_CASE						)

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					List[Any]	=	self.prepare_config_and_inputs()
    self.check_model_from_pretrained_configs(**_SCREAMING_SNAKE_CASE						)

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					Optional[int]	=	self.prepare_config_and_inputs()
    self.check_vision_text_dual_encoder_from_pretrained(**_SCREAMING_SNAKE_CASE						)

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





    '''simple docstring'''




    UpperCAmelCase	:					List[str]	=	self.prepare_config_and_inputs()
    self.check_save_load(**_SCREAMING_SNAKE_CASE						)

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





    '''simple docstring'''




    UpperCAmelCase	:					Optional[int]	=	self.prepare_config_and_inputs()
    self.check_vision_text_output_attention(**_SCREAMING_SNAKE_CASE						)




   @slow
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Tuple:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					Optional[int]	=	self.get_pretrained_model_and_inputs()

    UpperCAmelCase	:					List[Any]	=	model_a(**_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					List[Any]	=	outputs[0].numpy()

    with tempfile.TemporaryDirectory() as tmp_dirname:
     model_a.save_pretrained(_SCREAMING_SNAKE_CASE						)
     UpperCAmelCase	:					Tuple	=	TFVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE						)

     UpperCAmelCase	:					List[str]	=	model_a(**_SCREAMING_SNAKE_CASE						)
     UpperCAmelCase	:					Optional[Any]	=	after_outputs[0].numpy()
     UpperCAmelCase	:					List[Any]	=	np.amax(np.abs(out_a - out_a						)						)
     self.assertLessEqual(_SCREAMING_SNAKE_CASE      ,     1E-5						)



@require_tf
class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__      ,			unittest.TestCase ):

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Dict:





    '''simple docstring'''




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

    return model, inputs

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					Optional[Any]	=	TFViTModel(_SCREAMING_SNAKE_CASE      ,     name="""vision_model"""						)
    UpperCAmelCase	:					Optional[Any]	=	TFBertModel(_SCREAMING_SNAKE_CASE      ,     name="""text_model"""						)
    return vision_model, text_model




   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Tuple:





    '''simple docstring'''




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

    UpperCAmelCase	,		UpperCAmelCase	,		UpperCAmelCase	:					str	=	vision_config_and_inputs

    (
        (
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		
    )	:					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       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__      ,			unittest.TestCase ):

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




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

    return model, inputs

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE=None      ,     **_SCREAMING_SNAKE_CASE						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	,		UpperCAmelCase	:					str	=	self.get_vision_text_model(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					Optional[int]	=	TFVisionTextDualEncoderModel(vision_model=_SCREAMING_SNAKE_CASE      ,     text_model=_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					List[Any]	=	model(
        input_ids=_SCREAMING_SNAKE_CASE      ,     pixel_values=_SCREAMING_SNAKE_CASE      ,     attention_mask=_SCREAMING_SNAKE_CASE      ,     output_attentions=_SCREAMING_SNAKE_CASE						)

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

    UpperCAmelCase	:					List[str]	=	output.text_model_output.attentions
    self.assertEqual(len(_SCREAMING_SNAKE_CASE						)      ,     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 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      List[str]:





    '''simple docstring'''




    UpperCAmelCase	:					Optional[Any]	=	TFDeiTModel(_SCREAMING_SNAKE_CASE      ,     name="""vision_model"""						)
    UpperCAmelCase	:					Union[str, Any]	=	TFRobertaModel(_SCREAMING_SNAKE_CASE      ,     name="""text_model"""						)
    return vision_model, text_model




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





    '''simple docstring'''




    UpperCAmelCase	:					Optional[Any]	=	TFDeiTModelTester(self						)
    UpperCAmelCase	:					List[Any]	=	TFRobertaModelTester(self						)
    UpperCAmelCase	:					List[Any]	=	vit_model_tester.prepare_config_and_inputs()
    UpperCAmelCase	:					Optional[int]	=	bert_model_tester.prepare_config_and_inputs()

    UpperCAmelCase	,		UpperCAmelCase	,		UpperCAmelCase	:					Tuple	=	vision_config_and_inputs

    (
        (
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		
    )	:					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       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__      ,			unittest.TestCase ):

   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Tuple:





    '''simple docstring'''




    UpperCAmelCase	:					List[Any]	=	TFVisionTextDualEncoderModel.from_vision_text_pretrained(
        """Rocketknight1/tiny-random-clip-tf"""      ,     """hf-internal-testing/tiny-random-bert"""						)
    UpperCAmelCase	:					Optional[int]	=	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[Any]	=	ids_tensor([batch_size, 4]      ,     model.text_model.config.vocab_size						)
    UpperCAmelCase	:					Union[str, Any]	=	random_attention_mask([batch_size, 4]						)
    UpperCAmelCase	:					Optional[int]	=	{"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

    return model, inputs

   def 	SCREAMING_SNAKE_CASE     (   self      ,     _SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						)  ->      List[Any]:





    '''simple docstring'''




    UpperCAmelCase	:					str	=	TFCLIPVisionModel(_SCREAMING_SNAKE_CASE      ,     name="""vision_model"""						)
    UpperCAmelCase	:					List[str]	=	TFBertModel(_SCREAMING_SNAKE_CASE      ,     name="""text_model"""						)
    return vision_model, text_model




   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    UpperCAmelCase	:					Dict	=	TFCLIPVisionModelTester(self						)
    UpperCAmelCase	:					Dict	=	TFBertModelTester(self						)
    UpperCAmelCase	:					str	=	clip_model_tester.prepare_config_and_inputs()
    UpperCAmelCase	:					List[str]	=	bert_model_tester.prepare_config_and_inputs()

    UpperCAmelCase	,		UpperCAmelCase	:					Optional[Any]	=	vision_config_and_inputs

    (
        (
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		(
        UpperCAmelCase
    )	,		
    )	:					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_vision
@require_tf
class       SCREAMING_SNAKE_CASE__  (					unittest.TestCase ):

   @slow
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					List[Any]	=	TFVisionTextDualEncoderModel.from_pretrained(
        """clip-italian/clip-italian"""      ,     logit_scale_init_value=1.0      ,     from_pt=_SCREAMING_SNAKE_CASE						)
    UpperCAmelCase	:					List[Any]	=	VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian"""						)

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

    UpperCAmelCase	:					Optional[int]	=	model(**_SCREAMING_SNAKE_CASE						)

    # 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	:					str	=	np.array([[1.228_4727, 0.310_4122]]						)

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





"""simple docstring"""
A:   int								=		range(2, 2_0 + 1)
A:   Any								=		[1_0**k for k in range(ks[-1] + 1)]
A:   dict[int, dict[int, list[list[int]]]]								=		{}

def       _snake_case						(						UpperCamelCase						:		Dict			,		UpperCamelCase						:		Any			,		UpperCamelCase						:		Any			,		UpperCamelCase						:		int						):
 UpperCAmelCase	:					List[str]	=	sum(a_i[j] for j in range(UpperCamelCase			,		len(UpperCamelCase						)						)						)
 UpperCAmelCase	:					str	=	sum(a_i[j] * base[j] for j in range(min(len(UpperCamelCase						)			,		UpperCamelCase						)						)						)

 UpperCAmelCase	,		UpperCAmelCase	:					str	=	0, 0
 UpperCAmelCase	:					Optional[Any]	=	n - i

 UpperCAmelCase	:					Optional[int]	=	memo.get(UpperCamelCase						)

 if sub_memo is not None:
  UpperCAmelCase	:					str	=	sub_memo.get(UpperCamelCase						)

  if jumps is not None and len(UpperCamelCase						) > 0:
   # find and make the largest jump without going over
   UpperCAmelCase	:					Tuple	=	-1
   for _k in range(len(UpperCamelCase						) - 1			,		-1			,		-1						):
    if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
     UpperCAmelCase	:					int	=	_k
     break

   if max_jump >= 0:
    UpperCAmelCase	,		UpperCAmelCase	,		UpperCAmelCase	:					Optional[Any]	=	jumps[max_jump]
    # since the difference between jumps is cached, add c
    UpperCAmelCase	:					List[str]	=	diff + c
    for j in range(min(UpperCamelCase			,		len(UpperCamelCase						)						)						):
     UpperCAmelCase	,		UpperCAmelCase	:					Union[str, Any]	=	divmod(UpperCamelCase			,		10						)
    if new_c > 0:
     add(UpperCamelCase			,		UpperCamelCase			,		UpperCamelCase						)

  else:
   UpperCAmelCase	:					int	=	[]
 else:
  UpperCAmelCase	:					List[str]	=	{c: []}
  UpperCAmelCase	:					str	=	sub_memo

 if dn >= max_dn or c + diff >= base[k]:
  return diff, dn

 if k > ks[0]:
  while True:
   # keep doing smaller jumps
   UpperCAmelCase	,		UpperCAmelCase	:					List[str]	=	next_term(UpperCamelCase			,		k - 1			,		i + dn			,		UpperCamelCase						)
   diff += _diff
   dn += terms_jumped

   if dn >= max_dn or c + diff >= base[k]:
    break
 else:
  # would be too small a jump, just compute sequential terms instead
  UpperCAmelCase	,		UpperCAmelCase	:					int	=	compute(UpperCamelCase			,		UpperCamelCase			,		i + dn			,		UpperCamelCase						)
  diff += _diff
  dn += terms_jumped

 UpperCAmelCase	:					Dict	=	sub_memo[c]

 # keep jumps sorted by # of terms skipped
 UpperCAmelCase	:					str	=	0
 while j < len(UpperCamelCase						):
  if jumps[j][1] > dn:
   break
  j += 1

 # cache the jump for this value digitsum(b) and c
 sub_memo[c].insert(UpperCamelCase			,		(diff, dn, k)						)
 return (diff, dn)

def       _snake_case						(						UpperCamelCase						:		int			,		UpperCamelCase						:		Tuple			,		UpperCamelCase						:		Union[str, Any]			,		UpperCamelCase						:		Any						):
 if i >= n:
  return 0, i
 if k > len(UpperCamelCase						):
  a_i.extend([0 for _ in range(k - len(UpperCamelCase						)						)]						)

 # note: a_i -> b * 10^k + c
 # ds_b -> digitsum(b)
 # ds_c -> digitsum(c)
 UpperCAmelCase	:					List[str]	=	i
 UpperCAmelCase	,		UpperCAmelCase	,		UpperCAmelCase	:					Optional[int]	=	0, 0, 0
 for j in range(len(UpperCamelCase						)						):
  if j >= k:
   ds_b += a_i[j]
  else:
   ds_c += a_i[j]

 while i < n:
  i += 1
  UpperCAmelCase	:					Optional[int]	=	ds_c + ds_b
  diff += addend
  UpperCAmelCase	:					str	=	0
  for j in range(UpperCamelCase						):
   UpperCAmelCase	:					Any	=	a_i[j] + addend
   UpperCAmelCase	,		UpperCAmelCase	:					Any	=	divmod(UpperCamelCase			,		10						)

   ds_c += a_i[j]

  if addend > 0:
   break

 if addend > 0:
  add(UpperCamelCase			,		UpperCamelCase			,		UpperCamelCase						)
 return diff, i - start_i

def       _snake_case						(						UpperCamelCase						:		Union[str, Any]			,		UpperCamelCase						:		str			,		UpperCamelCase						:		Optional[int]						):
 for j in range(UpperCamelCase			,		len(UpperCamelCase						)						):
  UpperCAmelCase	:					Optional[int]	=	digits[j] + addend
  if s >= 10:
   UpperCAmelCase	,		UpperCAmelCase	:					int	=	divmod(UpperCamelCase			,		10						)
   UpperCAmelCase	:					str	=	addend // 10 + quotient
  else:
   UpperCAmelCase	:					Any	=	s
   UpperCAmelCase	:					Union[str, Any]	=	addend // 10

  if addend == 0:
   break

 while addend > 0:
  UpperCAmelCase	,		UpperCAmelCase	:					Any	=	divmod(UpperCamelCase			,		10						)
  digits.append(UpperCamelCase						)

def       _snake_case						(						UpperCamelCase						:		int = 10**15						):
 UpperCAmelCase	:					Dict	=	[1]
 UpperCAmelCase	:					int	=	1
 UpperCAmelCase	:					Tuple	=	0
 while True:
  UpperCAmelCase	,		UpperCAmelCase	:					Tuple	=	next_term(UpperCamelCase			,		20			,		i + dn			,		UpperCamelCase						)
  dn += terms_jumped
  if dn == n - i:
   break

 UpperCAmelCase	:					Any	=	0
 for j in range(len(UpperCamelCase						)						):
  a_n += digits[j] * 10**j
 return a_n


if __name__ == "__main__":
      print(f"""{solution() = }""")
