


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_					(  snake_case					)  ->    List[Any]:






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





@require_tf
class   __a      :
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    Optional[Any]:




        '''simple docstring'''
        pass
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        pass
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        pass
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=None    ,			**lowerCAmelCase__			) ->    str:




        '''simple docstring'''
        lowercase__:       str        =       VisionTextDualEncoderConfig.from_vision_text_configs(a__    ,			a__			)

        lowercase__:       Tuple        =       TFVisionTextDualEncoderModel(a__			)

        lowercase__:       Optional[Any]        =       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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=None    ,			**lowerCAmelCase__			) ->    str:




        '''simple docstring'''
        lowercase__      ,						lowercase__:       Union[str, Any]        =       self.get_vision_text_model(a__    ,			a__			)
        lowercase__:       Tuple        =       TFVisionTextDualEncoderModel(vision_model=a__    ,			text_model=a__			)

        lowercase__:       Any        =       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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=None    ,			**lowerCAmelCase__			) ->    int:




        '''simple docstring'''
        lowercase__      ,						lowercase__:       Optional[Any]        =       self.get_vision_text_model(a__    ,			a__			)
        lowercase__:       Union[str, Any]        =       {'vision_model': vision_model, 'text_model': text_model}
        lowercase__:       Dict        =       TFVisionTextDualEncoderModel.from_vision_text_pretrained(**a__			)

        lowercase__:       Optional[int]        =       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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=None    ,			**lowerCAmelCase__			) ->    Any:




        '''simple docstring'''
        lowercase__      ,						lowercase__:       Dict        =       self.get_vision_text_model(a__    ,			a__			)
        lowercase__:       Tuple        =       TFVisionTextDualEncoderModel(vision_model=a__    ,			text_model=a__			)

        lowercase__:       List[Any]        =       model(input_ids=a__    ,			pixel_values=a__    ,			attention_mask=a__			)
        lowercase__:       int        =       output[0].numpy()

        with tempfile.TemporaryDirectory() as tmpdirname:
               model.save_pretrained(a__			)
               lowercase__:       Tuple        =       TFVisionTextDualEncoderModel.from_pretrained(a__			)

               lowercase__:       int        =       model(input_ids=a__    ,			pixel_values=a__    ,			attention_mask=a__			)
               lowercase__:       Any        =       after_output[0].numpy()
               lowercase__:       Tuple        =       np.amax(np.abs(out_a - out_a			)			)
               self.assertLessEqual(a__    ,			1E-5			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=None    ,			**lowerCAmelCase__			) ->    Dict:




        '''simple docstring'''
        lowercase__      ,						lowercase__:       str        =       self.get_vision_text_model(a__    ,			a__			)
        lowercase__:       Optional[int]        =       TFVisionTextDualEncoderModel(vision_model=a__    ,			text_model=a__			)

        lowercase__:       List[Any]        =       model(
            input_ids=a__    ,			pixel_values=a__    ,			attention_mask=a__    ,			output_attentions=a__			)

        lowercase__:       Tuple        =       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)
        lowercase__:       int        =       to_atuple(vision_model.config.image_size			)
        lowercase__:       Tuple        =       to_atuple(vision_model.config.patch_size			)
        lowercase__:       Optional[int]        =       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
        lowercase__:       Dict        =       num_patches + 1
        self.assertEqual(vision_attentions[0].shape[-3:]    ,			(vision_config.num_attention_heads, seq_len, seq_len)			)

        lowercase__:       List[str]        =       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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    List[str]:




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




        '''simple docstring'''
        lowercase__:       str        =       self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_model(**a__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[str]:




        '''simple docstring'''
        lowercase__:       Dict        =       self.prepare_config_and_inputs()
        self.check_model_from_pretrained_configs(**a__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       List[Any]        =       self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_from_pretrained(**a__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[str]:




        '''simple docstring'''
        lowercase__:       Tuple        =       self.prepare_config_and_inputs()
        self.check_save_load(**a__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       int        =       self.prepare_config_and_inputs()
        self.check_vision_text_output_attention(**a__			)
 @slow
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    int:




        '''simple docstring'''
        lowercase__      ,						lowercase__:       List[str]        =       self.get_pretrained_model_and_inputs()

        lowercase__:       Dict        =       model_a(**a__			)
        lowercase__:       Optional[Any]        =       outputs[0].numpy()

        with tempfile.TemporaryDirectory() as tmp_dirname:
               model_a.save_pretrained(a__			)
               lowercase__:       Any        =       TFVisionTextDualEncoderModel.from_pretrained(a__			)

               lowercase__:       Union[str, Any]        =       model_a(**a__			)
               lowercase__:       Dict        =       after_outputs[0].numpy()
               lowercase__:       Optional[int]        =       np.amax(np.abs(out_a - out_a			)			)
               self.assertLessEqual(a__    ,			1E-5			)





@require_tf
class   __a      (   lowercase_	,	unittest.TestCase				):
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[str]:




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

        return model, inputs
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    Optional[Any]:




        '''simple docstring'''
        lowercase__:       Optional[int]        =       TFViTModel(a__    ,			name='vision_model'			)
        lowercase__:       Optional[int]        =       TFBertModel(a__    ,			name='text_model'			)
        return vision_model, text_model
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    str:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       TFViTModelTester(self			)
        lowercase__:       Optional[int]        =       TFBertModelTester(self			)
        lowercase__:       Tuple        =       vit_model_tester.prepare_config_and_inputs()
        lowercase__:       List[Any]        =       bert_model_tester.prepare_config_and_inputs()

        lowercase__      ,						lowercase__      ,						lowercase__:       Dict        =       vision_config_and_inputs

        (
            (
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,										
        ):       Optional[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   __a      (   lowercase_	,	unittest.TestCase				):
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[str]:




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

        return model, inputs
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=None    ,			**lowerCAmelCase__			) ->    Dict:




        '''simple docstring'''
        lowercase__      ,						lowercase__:       Optional[int]        =       self.get_vision_text_model(a__    ,			a__			)
        lowercase__:       int        =       TFVisionTextDualEncoderModel(vision_model=a__    ,			text_model=a__			)

        lowercase__:       Any        =       model(
            input_ids=a__    ,			pixel_values=a__    ,			attention_mask=a__    ,			output_attentions=a__			)

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

        lowercase__:       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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       Any        =       TFDeiTModel(a__    ,			name='vision_model'			)
        lowercase__:       Any        =       TFRobertaModel(a__    ,			name='text_model'			)
        return vision_model, text_model
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        lowercase__:       str        =       TFDeiTModelTester(self			)
        lowercase__:       str        =       TFRobertaModelTester(self			)
        lowercase__:       Union[str, Any]        =       vit_model_tester.prepare_config_and_inputs()
        lowercase__:       List[str]        =       bert_model_tester.prepare_config_and_inputs()

        lowercase__      ,						lowercase__      ,						lowercase__:       Tuple        =       vision_config_and_inputs

        (
            (
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,										
        ):       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_tf
class   __a      (   lowercase_	,	unittest.TestCase				):
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[str]:




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

        return model, inputs
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       str        =       TFCLIPVisionModel(a__    ,			name='vision_model'			)
        lowercase__:       Tuple        =       TFBertModel(a__    ,			name='text_model'			)
        return vision_model, text_model
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        lowercase__:       Optional[Any]        =       TFCLIPVisionModelTester(self			)
        lowercase__:       Dict        =       TFBertModelTester(self			)
        lowercase__:       Optional[int]        =       clip_model_tester.prepare_config_and_inputs()
        lowercase__:       Tuple        =       bert_model_tester.prepare_config_and_inputs()

        lowercase__      ,						lowercase__:       int        =       vision_config_and_inputs

        (
            (
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,						(
            lowercase__				
        )      ,										
        ):       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_vision
@require_tf
class   __a      (   unittest.TestCase				):
 @slow
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        lowercase__:       Optional[Any]        =       TFVisionTextDualEncoderModel.from_pretrained(
            'clip-italian/clip-italian'    ,			logit_scale_init_value=1.0    ,			from_pt=a__			)
        lowercase__:       List[str]        =       VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'			)

        lowercase__:       Optional[Any]        =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
        lowercase__:       Tuple        =       processor(
            text=['una foto di un gatto', 'una foto di un cane']    ,			images=a__    ,			padding=a__    ,			return_tensors='np'			)

        lowercase__:       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])    ,			)

        lowercase__:       Optional[Any]        =       np.array([[1.2_2_8_4_7_2_7, 0.3_1_0_4_1_2_2]]			)

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






from __future__ import annotations




def        snake_case_					(  snake_case      ,							snake_case					)  ->    list[int]:
       lowercase__:       Tuple        =       0
       lowercase__:       str        =       len(snake_case					) - 1

       while i < j:
              if nums[i] + nums[j] == target:
                     return [i, j]
              elif nums[i] + nums[j] < target:
                     lowercase__:       str        =       i + 1
              else:
                     lowercase__:       Dict        =       j - 1

       return []


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(F'''{two_pointer([2, 7, 11, 15], 9) = }''')


