


'''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 			_UpperCamelCase					(						UpperCamelCase__    ):
			if isinstance(_UpperCAmelCase       ,					collections.abc.Iterable    ):
						return x
			return (x, x)


@require_tf
class 						_snake_case    :




					def  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase):
								pass




					def  snake_case__  (					self):
								pass




					def  snake_case__  (					self):
								pass




					def  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase=None							,							**_lowerCamelCase):
								UpperCAmelCase__     :    str         =   VisionTextDualEncoderConfig.from_vision_text_configs(__SCREAMING_SNAKE_CASE							,							__SCREAMING_SNAKE_CASE)

								UpperCAmelCase__     :    List[str]         =   TFVisionTextDualEncoderModel(__SCREAMING_SNAKE_CASE)

								UpperCAmelCase__     :    int         =   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  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase=None							,							**_lowerCamelCase):
								UpperCAmelCase__      ,				UpperCAmelCase__     :    str         =   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__     :    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  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase=None							,							**_lowerCamelCase):
								UpperCAmelCase__      ,				UpperCAmelCase__     :    Optional[int]         =   self.get_vision_text_model(__SCREAMING_SNAKE_CASE							,							__SCREAMING_SNAKE_CASE)
								UpperCAmelCase__     :    Tuple         =   {"""vision_model""": vision_model, """text_model""": text_model}
								UpperCAmelCase__     :    List[str]         =   TFVisionTextDualEncoderModel.from_vision_text_pretrained(**__SCREAMING_SNAKE_CASE)

								UpperCAmelCase__     :    Union[str, Any]         =   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  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase=None							,							**_lowerCamelCase):
								UpperCAmelCase__      ,				UpperCAmelCase__     :    Union[str, Any]         =   self.get_vision_text_model(__SCREAMING_SNAKE_CASE							,							__SCREAMING_SNAKE_CASE)
								UpperCAmelCase__     :    Dict         =   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)
								UpperCAmelCase__     :    int         =   output[0].numpy()

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

											UpperCAmelCase__     :    Union[str, Any]         =   model(input_ids=__SCREAMING_SNAKE_CASE							,							pixel_values=__SCREAMING_SNAKE_CASE							,							attention_mask=__SCREAMING_SNAKE_CASE)
											UpperCAmelCase__     :    Union[str, Any]         =   after_output[0].numpy()
											UpperCAmelCase__     :    Union[str, Any]         =   np.amax(np.abs(out_a - out_a))
											self.assertLessEqual(__SCREAMING_SNAKE_CASE							,							1e-5)




					def  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase=None							,							**_lowerCamelCase):
								UpperCAmelCase__      ,				UpperCAmelCase__     :    Dict         =   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__     :    str         =   model(
								    input_ids=__SCREAMING_SNAKE_CASE							,							pixel_values=__SCREAMING_SNAKE_CASE							,							attention_mask=__SCREAMING_SNAKE_CASE							,							output_attentions=__SCREAMING_SNAKE_CASE)

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

								UpperCAmelCase__     :    Tuple         =   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  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase):
								UpperCAmelCase__     :    Optional[int]         =   np.abs((a - b)).max()
								self.assertLessEqual(__SCREAMING_SNAKE_CASE							,							__SCREAMING_SNAKE_CASE							,							f'''Difference between torch and flax is {diff} (>= {tol}).''')




					def  snake_case__  (					self):
								UpperCAmelCase__     :    Optional[Any]         =   self.prepare_config_and_inputs()
								self.check_vision_text_dual_encoder_model(**__SCREAMING_SNAKE_CASE)




					def  snake_case__  (					self):
								UpperCAmelCase__     :    Optional[int]         =   self.prepare_config_and_inputs()
								self.check_model_from_pretrained_configs(**__SCREAMING_SNAKE_CASE)




					def  snake_case__  (					self):
								UpperCAmelCase__     :    str         =   self.prepare_config_and_inputs()
								self.check_vision_text_dual_encoder_from_pretrained(**__SCREAMING_SNAKE_CASE)




					def  snake_case__  (					self):
								UpperCAmelCase__     :    Optional[int]         =   self.prepare_config_and_inputs()
								self.check_save_load(**__SCREAMING_SNAKE_CASE)




					def  snake_case__  (					self):
								UpperCAmelCase__     :    Optional[Any]         =   self.prepare_config_and_inputs()
								self.check_vision_text_output_attention(**__SCREAMING_SNAKE_CASE)




					@slow
					def  snake_case__  (					self):
								UpperCAmelCase__      ,				UpperCAmelCase__     :    Optional[Any]         =   self.get_pretrained_model_and_inputs()

								UpperCAmelCase__     :    Optional[int]         =   model_a(**__SCREAMING_SNAKE_CASE)
								UpperCAmelCase__     :    Tuple         =   outputs[0].numpy()

								with tempfile.TemporaryDirectory() as tmp_dirname:
											model_a.save_pretrained(__SCREAMING_SNAKE_CASE)
											UpperCAmelCase__     :    Any         =   TFVisionTextDualEncoderModel.from_pretrained(__SCREAMING_SNAKE_CASE)

											UpperCAmelCase__     :    Dict         =   model_a(**__SCREAMING_SNAKE_CASE)
											UpperCAmelCase__     :    Dict         =   after_outputs[0].numpy()
											UpperCAmelCase__     :    Any         =   np.amax(np.abs(out_a - out_a))
											self.assertLessEqual(__SCREAMING_SNAKE_CASE							,							1e-5)


@require_tf
class 						_snake_case    (   __UpperCAmelCase				,      unittest.TestCase		):




					def  snake_case__  (					self):
								UpperCAmelCase__     :    List[Any]         =   TFVisionTextDualEncoderModel.from_vision_text_pretrained(
								    """hf-internal-testing/tiny-random-vit"""							,							"""hf-internal-testing/tiny-random-bert""")
								UpperCAmelCase__     :    Optional[int]         =   13
								UpperCAmelCase__     :    int         =   floats_tensor(
								    [
								        batch_size,
								        model.vision_model.config.num_channels,
								        model.vision_model.config.image_size,
								        model.vision_model.config.image_size,
								    ])
								UpperCAmelCase__     :    List[str]         =   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  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase):
								UpperCAmelCase__     :    str         =   TFViTModel(__SCREAMING_SNAKE_CASE							,							name="""vision_model""")
								UpperCAmelCase__     :    Tuple         =   TFBertModel(__SCREAMING_SNAKE_CASE							,							name="""text_model""")
								return vision_model, text_model




					def  snake_case__  (					self):
								UpperCAmelCase__     :    Union[str, Any]         =   TFViTModelTester(self)
								UpperCAmelCase__     :    List[str]         =   TFBertModelTester(self)
								UpperCAmelCase__     :    Union[str, Any]         =   vit_model_tester.prepare_config_and_inputs()
								UpperCAmelCase__     :    Tuple         =   bert_model_tester.prepare_config_and_inputs()

								UpperCAmelCase__      ,				UpperCAmelCase__      ,				UpperCAmelCase__     :    List[Any]         =   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 						_snake_case    (   __UpperCAmelCase				,      unittest.TestCase		):




					def  snake_case__  (					self):
								UpperCAmelCase__     :    str         =   TFVisionTextDualEncoderModel.from_vision_text_pretrained(
								    """Rocketknight1/tiny-random-deit-tf"""							,							"""hf-internal-testing/tiny-random-roberta""")
								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__     :    Optional[Any]         =   ids_tensor([batch_size, 4]							,							model.text_model.config.vocab_size)
								UpperCAmelCase__     :    List[str]         =   random_attention_mask([batch_size, 4])
								UpperCAmelCase__     :    str         =   {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

								return model, inputs




					def  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase=None							,							**_lowerCamelCase):
								UpperCAmelCase__      ,				UpperCAmelCase__     :    List[str]         =   self.get_vision_text_model(__SCREAMING_SNAKE_CASE							,							__SCREAMING_SNAKE_CASE)
								UpperCAmelCase__     :    str         =   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__     :    List[Any]         =   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__     :    Tuple         =   to_atuple(vision_model.config.image_size)
								UpperCAmelCase__     :    Optional[Any]         =   to_atuple(vision_model.config.patch_size)
								UpperCAmelCase__     :    Any         =   (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
								UpperCAmelCase__     :    Tuple         =   num_patches + 2
								self.assertEqual(vision_attentions[0].shape[-3:]							,							(vision_config.num_attention_heads, seq_len, seq_len))

								UpperCAmelCase__     :    Tuple         =   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  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase):
								UpperCAmelCase__     :    Dict         =   TFDeiTModel(__SCREAMING_SNAKE_CASE							,							name="""vision_model""")
								UpperCAmelCase__     :    Optional[int]         =   TFRobertaModel(__SCREAMING_SNAKE_CASE							,							name="""text_model""")
								return vision_model, text_model




					def  snake_case__  (					self):
								UpperCAmelCase__     :    Tuple         =   TFDeiTModelTester(self)
								UpperCAmelCase__     :    Union[str, Any]         =   TFRobertaModelTester(self)
								UpperCAmelCase__     :    List[str]         =   vit_model_tester.prepare_config_and_inputs()
								UpperCAmelCase__     :    Optional[Any]         =   bert_model_tester.prepare_config_and_inputs()

								UpperCAmelCase__      ,				UpperCAmelCase__      ,				UpperCAmelCase__     :    Optional[int]         =   vision_config_and_inputs

								(
								    (
								    UpperCAmelCase__   
								)      ,				(
								    UpperCAmelCase__   
								)      ,				(
								    UpperCAmelCase__   
								)      ,				(
								    UpperCAmelCase__   
								)      ,				(
								    UpperCAmelCase__   
								)      ,				(
								    UpperCAmelCase__   
								)      ,				(
								    UpperCAmelCase__   
								)      ,				   
								)     :    Tuple         =   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    (   __UpperCAmelCase				,      unittest.TestCase		):




					def  snake_case__  (					self):
								UpperCAmelCase__     :    Union[str, Any]         =   TFVisionTextDualEncoderModel.from_vision_text_pretrained(
								    """Rocketknight1/tiny-random-clip-tf"""							,							"""hf-internal-testing/tiny-random-bert""")
								UpperCAmelCase__     :    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__     :    List[Any]         =   random_attention_mask([batch_size, 4])
								UpperCAmelCase__     :    List[Any]         =   {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

								return model, inputs




					def  snake_case__  (					self							,							_lowerCamelCase							,							_lowerCamelCase):
								UpperCAmelCase__     :    List[Any]         =   TFCLIPVisionModel(__SCREAMING_SNAKE_CASE							,							name="""vision_model""")
								UpperCAmelCase__     :    Union[str, Any]         =   TFBertModel(__SCREAMING_SNAKE_CASE							,							name="""text_model""")
								return vision_model, text_model




					def  snake_case__  (					self):
								UpperCAmelCase__     :    Optional[Any]         =   TFCLIPVisionModelTester(self)
								UpperCAmelCase__     :    Tuple         =   TFBertModelTester(self)
								UpperCAmelCase__     :    Optional[Any]         =   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__   
								)      ,				   
								)     :    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 						_snake_case    (   unittest.TestCase		):




					@slow
					def  snake_case__  (					self):
								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__     :    Tuple         =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
								UpperCAmelCase__     :    List[str]         =   processor(
								    text=["""una foto di un gatto""", """una foto di un cane"""]							,							images=__SCREAMING_SNAKE_CASE							,							padding=__SCREAMING_SNAKE_CASE							,							return_tensors="""np""")

								UpperCAmelCase__     :    List[str]         =   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__     :    Dict         =   np.array([[1.2284727, 0.3104122]])

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




from __future__ import annotations

from typing import Any





def      __snake_case	(			_UpperCAmelCase				):
    if not postfix_notation:
        return 0

    __a						       =				{'''+''', '''-''', '''*''', '''/'''}
    __a						       =				[]

    for token in postfix_notation:
        if token in operations:
            __a       ,   __a						       =				stack.pop(), stack.pop()
            if token == "+":
                stack.append(a + b				)
            elif token == "-":
                stack.append(a - b				)
            elif token == "*":
                stack.append(a * b				)
            else:
                if a * b < 0 and a % b != 0:
                    stack.append(a // b + 1				)
                else:
                    stack.append(a // b				)
        else:
            stack.append(int(_UpperCAmelCase				)				)

    return stack.pop()


if __name__ == "__main__":
 import doctest

 doctest.testmod()





