



from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
     from transformers import (
         TFBertModel,
         TFCLIPVisionModel,
         TFDeiTModel,
         TFRobertaModel,
         TFVisionTextDualEncoderModel,
         TFViTModel,
         VisionTextDualEncoderConfig,
     )

if is_vision_available():
     from PIL import Image

     from transformers import VisionTextDualEncoderProcessor

def     __lowercase(	UpperCAmelCase__ ):


      """simple docstring"""



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





@require_tf
class    lowerCamelCase__  :
     """simple docstring"""



     def 						_a      (self				,			__a				,			__a    ):






           '''simple docstring'''



           pass







     def 						_a      (self    ):






           '''simple docstring'''



           pass







     def 						_a      (self    ):






           '''simple docstring'''



           pass







     def 						_a      (self				,			__a				,			__a				,			__a				,			__a				,			__a=None				,			**__a    ):






           '''simple docstring'''



           lowerCamelCase								=		VisionTextDualEncoderConfig.from_vision_text_configs(__a				,			__a    )

           lowerCamelCase								=		TFVisionTextDualEncoderModel(__a    )

           lowerCamelCase								=		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				,			__a				,			__a				,			__a				,			__a				,			__a=None				,			**__a    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.get_vision_text_model(__a				,			__a    )
           lowerCamelCase								=		TFVisionTextDualEncoderModel(vision_model=__a				,			text_model=__a    )

           lowerCamelCase								=		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				,			__a				,			__a				,			__a				,			__a				,			__a=None				,			**__a    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.get_vision_text_model(__a				,			__a    )
           lowerCamelCase								=		{"vision_model": vision_model, "text_model": text_model}
           lowerCamelCase								=		TFVisionTextDualEncoderModel.from_vision_text_pretrained(**__a    )

           lowerCamelCase								=		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				,			__a				,			__a				,			__a				,			__a				,			__a=None				,			**__a    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.get_vision_text_model(__a				,			__a    )
           lowerCamelCase								=		TFVisionTextDualEncoderModel(vision_model=__a				,			text_model=__a    )

           lowerCamelCase								=		model(input_ids=__a				,			pixel_values=__a				,			attention_mask=__a    )
           lowerCamelCase								=		output[0].numpy()

           with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_pretrained(__a    )
                 lowerCamelCase								=		TFVisionTextDualEncoderModel.from_pretrained(__a    )

                 lowerCamelCase								=		model(input_ids=__a				,			pixel_values=__a				,			attention_mask=__a    )
                 lowerCamelCase								=		after_output[0].numpy()
                 lowerCamelCase								=		np.amax(np.abs(out_a - out_a    )    )
                 self.assertLessEqual(__a				,			1E-5    )







     def 						_a      (self				,			__a				,			__a				,			__a				,			__a				,			__a=None				,			**__a    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.get_vision_text_model(__a				,			__a    )
           lowerCamelCase								=		TFVisionTextDualEncoderModel(vision_model=__a				,			text_model=__a    )

           lowerCamelCase								=		model(
               input_ids=__a				,			pixel_values=__a				,			attention_mask=__a				,			output_attentions=__a    )

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

           lowerCamelCase								=		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				,			__a				,			__a				,			__a    ):






           '''simple docstring'''



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







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.prepare_config_and_inputs()
           self.check_vision_text_dual_encoder_model(**__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.prepare_config_and_inputs()
           self.check_model_from_pretrained_configs(**__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.prepare_config_and_inputs()
           self.check_vision_text_dual_encoder_from_pretrained(**__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.prepare_config_and_inputs()
           self.check_save_load(**__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		self.prepare_config_and_inputs()
           self.check_vision_text_output_attention(**__a    )







     @slow
     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.get_pretrained_model_and_inputs()

           lowerCamelCase								=		model_a(**__a    )
           lowerCamelCase								=		outputs[0].numpy()

           with tempfile.TemporaryDirectory() as tmp_dirname:
                 model_a.save_pretrained(__a    )
                 lowerCamelCase								=		TFVisionTextDualEncoderModel.from_pretrained(__a    )

                 lowerCamelCase								=		model_a(**__a    )
                 lowerCamelCase								=		after_outputs[0].numpy()
                 lowerCamelCase								=		np.amax(np.abs(out_a - out_a    )    )
                 self.assertLessEqual(__a				,			1E-5    )





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



     def 						_a      (self    ):






           '''simple docstring'''



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

           return model, inputs







     def 						_a      (self				,			__a				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		TFViTModel(__a				,			name="vision_model"    )
           lowerCamelCase								=		TFBertModel(__a				,			name="text_model"    )
           return vision_model, text_model







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		TFViTModelTester(self    )
           lowerCamelCase								=		TFBertModelTester(self    )
           lowerCamelCase								=		vit_model_tester.prepare_config_and_inputs()
           lowerCamelCase								=		bert_model_tester.prepare_config_and_inputs()

           lowerCamelCase			,							lowerCamelCase			,							lowerCamelCase								=		vision_config_and_inputs

           (
               (
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							
           )								=		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    lowerCamelCase__  (						UpperCAmelCase_       ,    unittest.TestCase):
     """simple docstring"""



     def 						_a      (self    ):






           '''simple docstring'''



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

           return model, inputs







     def 						_a      (self				,			__a				,			__a				,			__a				,			__a				,			__a=None				,			**__a    ):






           '''simple docstring'''



           lowerCamelCase			,							lowerCamelCase								=		self.get_vision_text_model(__a				,			__a    )
           lowerCamelCase								=		TFVisionTextDualEncoderModel(vision_model=__a				,			text_model=__a    )

           lowerCamelCase								=		model(
               input_ids=__a				,			pixel_values=__a				,			attention_mask=__a				,			output_attentions=__a    )

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

           lowerCamelCase								=		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				,			__a				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		TFDeiTModel(__a				,			name="vision_model"    )
           lowerCamelCase								=		TFRobertaModel(__a				,			name="text_model"    )
           return vision_model, text_model







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		TFDeiTModelTester(self    )
           lowerCamelCase								=		TFRobertaModelTester(self    )
           lowerCamelCase								=		vit_model_tester.prepare_config_and_inputs()
           lowerCamelCase								=		bert_model_tester.prepare_config_and_inputs()

           lowerCamelCase			,							lowerCamelCase			,							lowerCamelCase								=		vision_config_and_inputs

           (
               (
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							
           )								=		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    lowerCamelCase__  (						UpperCAmelCase_       ,    unittest.TestCase):
     """simple docstring"""



     def 						_a      (self    ):






           '''simple docstring'''



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

           return model, inputs







     def 						_a      (self				,			__a				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		TFCLIPVisionModel(__a				,			name="vision_model"    )
           lowerCamelCase								=		TFBertModel(__a				,			name="text_model"    )
           return vision_model, text_model







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		TFCLIPVisionModelTester(self    )
           lowerCamelCase								=		TFBertModelTester(self    )
           lowerCamelCase								=		clip_model_tester.prepare_config_and_inputs()
           lowerCamelCase								=		bert_model_tester.prepare_config_and_inputs()

           lowerCamelCase			,							lowerCamelCase								=		vision_config_and_inputs

           (
               (
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							(
               lowerCamelCase
           )			,							
           )								=		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    lowerCamelCase__  (						unittest.TestCase):
     """simple docstring"""



     @slow
     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		TFVisionTextDualEncoderModel.from_pretrained(
               "clip-italian/clip-italian"				,			logit_scale_init_value=1.0				,			from_pt=__a    )
           lowerCamelCase								=		VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian"    )

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

           lowerCamelCase								=		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])				,			)

           lowerCamelCase								=		np.array([[1.228_4727, 0.310_4122]]    )

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




from __future__ import annotations

from collections import deque
from collections.abc import Iterator
from dataclasses import dataclass





@dataclass
class    lowerCamelCase__  :
     """simple docstring"""



     _A       =			42
     _A       =			42





class    lowerCamelCase__  :
     """simple docstring"""



     def __init__(self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		[[] for _ in range(__a    )]
           lowerCamelCase								=		size







     def __getitem__(self				,			__a    ):






           '''simple docstring'''



           return iter(self._graph[vertex]    )







     @property
     def 						_a      (self    ):






           '''simple docstring'''



           return self._size







     def 						_a      (self				,			__a				,			__a				,			__a    ):






           '''simple docstring'''



           if weight not in (0, 1):
                 raise ValueError("Edge weight must be either 0 or 1."    )

           if to_vertex < 0 or to_vertex >= self.size:
                 raise ValueError("Vertex indexes must be in [0; size)."    )

           self._graph[from_vertex].append(Edge(__a				,			__a    )    )







     def 						_a      (self				,			__a				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		deque([start_vertex]    )
           lowerCamelCase								=		[None] * self.size
           lowerCamelCase								=		0

           while queue:
                 lowerCamelCase								=		queue.popleft()
                 lowerCamelCase								=		distances[current_vertex]
                 if current_distance is None:
                       continue

                 for edge in self[current_vertex]:
                       lowerCamelCase								=		current_distance + edge.weight
                       lowerCamelCase								=		distances[edge.destination_vertex]
                       if (
                           isinstance(__a				,			__a    )
                           and new_distance >= dest_vertex_distance
                       ):
                             continue
                       lowerCamelCase								=		new_distance
                       if edge.weight == 0:
                             queue.appendleft(edge.destination_vertex    )
                       else:
                             queue.append(edge.destination_vertex    )

           if distances[finish_vertex] is None:
                 raise ValueError("No path from start_vertex to finish_vertex."    )

           return distances[finish_vertex]


if __name__ == "__main__":
     import doctest

     doctest.testmod()