






from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

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

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


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

if is_vision_available():
	from PIL import Image

	from transformers import VisionTextDualEncoderProcessor



def  a__ (__lowercase							:List[Any]   )		-> Union[str, Any]:
				if isinstance(__lowercase					,  collections.abc.Iterable   ):
								return x
				return (x, x)






@require_tf
class 						UpperCAmelCase__ :







		def  A__      (					self       ,A__       ,A__		):
						pass







		def  A__      (					self		):
						pass







		def  A__      (					self		):
						pass







		def  A__      (					self       ,A__       ,A__       ,A__       ,A__       ,A__=None       ,**A__		):
						_A	:				int   =      VisionTextDualEncoderConfig.from_vision_text_configs(A__       ,A__		)

						_A	:				List[Any]   =      TFVisionTextDualEncoderModel(A__		)

						_A	:				Dict   =      model(input_ids=A__       ,pixel_values=A__       ,attention_mask=A__		)

						self.assertEqual(output['''text_embeds'''].shape       ,(input_ids.shape[0], config.projection_dim)		)
						self.assertEqual(output['''image_embeds'''].shape       ,(pixel_values.shape[0], config.projection_dim)		)







		def  A__      (					self       ,A__       ,A__       ,A__       ,A__       ,A__=None       ,**A__		):
						_A  ,				_A	:				int   =      self.get_vision_text_model(A__       ,A__		)
						_A	:				List[str]   =      TFVisionTextDualEncoderModel(vision_model=A__       ,text_model=A__		)

						_A	:				Tuple   =      model(input_ids=A__       ,pixel_values=A__       ,attention_mask=A__		)

						self.assertEqual(output['''text_embeds'''].shape       ,(input_ids.shape[0], model.config.projection_dim)		)
						self.assertEqual(output['''image_embeds'''].shape       ,(pixel_values.shape[0], model.config.projection_dim)		)







		def  A__      (					self       ,A__       ,A__       ,A__       ,A__       ,A__=None       ,**A__		):
						_A  ,				_A	:				Dict   =      self.get_vision_text_model(A__       ,A__		)
						_A	:				int   =      {'''vision_model''': vision_model, '''text_model''': text_model}
						_A	:				str   =      TFVisionTextDualEncoderModel.from_vision_text_pretrained(**A__		)

						_A	:				List[str]   =      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__		):
						_A  ,				_A	:				Any   =      self.get_vision_text_model(A__       ,A__		)
						_A	:				Optional[int]   =      TFVisionTextDualEncoderModel(vision_model=A__       ,text_model=A__		)

						_A	:				Dict   =      model(input_ids=A__       ,pixel_values=A__       ,attention_mask=A__		)
						_A	:				Optional[Any]   =      output[0].numpy()

						with tempfile.TemporaryDirectory() as tmpdirname:
										model.save_pretrained(A__		)
										_A	:				str   =      TFVisionTextDualEncoderModel.from_pretrained(A__		)

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







		def  A__      (					self       ,A__       ,A__       ,A__       ,A__       ,A__=None       ,**A__		):
						_A  ,				_A	:				Dict   =      self.get_vision_text_model(A__       ,A__		)
						_A	:				Tuple   =      TFVisionTextDualEncoderModel(vision_model=A__       ,text_model=A__		)

						_A	:				List[Any]   =      model(
						    input_ids=A__       ,pixel_values=A__       ,attention_mask=A__       ,output_attentions=A__		)

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

						_A	:				Any   =      output.text_model_output.attentions
						self.assertEqual(len(A__		)       ,text_config.num_hidden_layers		)

						self.assertEqual(
						    text_attentions[0].shape[-3:]       ,(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])       ,)







		def  A__      (					self       ,A__       ,A__       ,A__		):
						_A	:				Union[str, Any]   =      np.abs((a - b)		).max()
						self.assertLessEqual(A__       ,A__       ,f"""Difference between torch and flax is {diff} (>= {tol})."""		)







		def  A__      (					self		):
						_A	:				str   =      self.prepare_config_and_inputs()
						self.check_vision_text_dual_encoder_model(**A__		)







		def  A__      (					self		):
						_A	:				Union[str, Any]   =      self.prepare_config_and_inputs()
						self.check_model_from_pretrained_configs(**A__		)







		def  A__      (					self		):
						_A	:				int   =      self.prepare_config_and_inputs()
						self.check_vision_text_dual_encoder_from_pretrained(**A__		)







		def  A__      (					self		):
						_A	:				int   =      self.prepare_config_and_inputs()
						self.check_save_load(**A__		)







		def  A__      (					self		):
						_A	:				Tuple   =      self.prepare_config_and_inputs()
						self.check_vision_text_output_attention(**A__		)







		@slow
		def  A__      (					self		):
						_A  ,				_A	:				Optional[Any]   =      self.get_pretrained_model_and_inputs()

						_A	:				int   =      model_a(**A__		)
						_A	:				List[str]   =      outputs[0].numpy()

						with tempfile.TemporaryDirectory() as tmp_dirname:
										model_a.save_pretrained(A__		)
										_A	:				List[Any]   =      TFVisionTextDualEncoderModel.from_pretrained(A__		)

										_A	:				Dict   =      model_a(**A__		)
										_A	:				Optional[Any]   =      after_outputs[0].numpy()
										_A	:				Optional[Any]   =      np.amax(np.abs(out_a - out_a		)		)
										self.assertLessEqual(A__       ,1E-5		)






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







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

						return model, inputs







		def  A__      (					self       ,A__       ,A__		):
						_A	:				Union[str, Any]   =      TFViTModel(A__       ,name='''vision_model'''		)
						_A	:				List[str]   =      TFBertModel(A__       ,name='''text_model'''		)
						return vision_model, text_model







		def  A__      (					self		):
						_A	:				Union[str, Any]   =      TFViTModelTester(self		)
						_A	:				int   =      TFBertModelTester(self		)
						_A	:				Union[str, Any]   =      vit_model_tester.prepare_config_and_inputs()
						_A	:				List[Any]   =      bert_model_tester.prepare_config_and_inputs()

						_A  ,				_A  ,				_A	:				Any   =      vision_config_and_inputs

						(
						    (
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				
						)	:				int   =      text_config_and_inputs

						return {
						    "text_config": text_config,
						    "vision_config": vision_config,
						    "pixel_values": pixel_values,
						    "attention_mask": input_mask,
						    "input_ids": input_ids,
						    "text_token_type_ids": token_type_ids,
						    "text_sequence_labels": sequence_labels,
						    "text_token_labels": token_labels,
						    "text_choice_labels": choice_labels,
						}






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







		def  A__      (					self		):
						# DeiT repo doesn't have TF weights, but we don't actually use the weights at all so let's
						# just reinitialize it.
						_A	:				int   =      TFVisionTextDualEncoderModel.from_vision_text_pretrained(
						    '''Rocketknight1/tiny-random-deit-tf'''       ,'''hf-internal-testing/tiny-random-roberta'''		)
						_A	:				Any   =      13
						_A	:				Tuple   =      floats_tensor(
						    [
						        batch_size,
						        model.vision_model.config.num_channels,
						        model.vision_model.config.image_size,
						        model.vision_model.config.image_size,
						    ]		)
						_A	:				Union[str, Any]   =      ids_tensor([batch_size, 4]       ,model.text_model.config.vocab_size		)
						_A	:				Optional[int]   =      random_attention_mask([batch_size, 4]		)
						_A	:				Any   =      {'''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__		):
						_A  ,				_A	:				List[str]   =      self.get_vision_text_model(A__       ,A__		)
						_A	:				Any   =      TFVisionTextDualEncoderModel(vision_model=A__       ,text_model=A__		)

						_A	:				Any   =      model(
						    input_ids=A__       ,pixel_values=A__       ,attention_mask=A__       ,output_attentions=A__		)

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

						_A	:				int   =      output.text_model_output.attentions
						self.assertEqual(len(A__		)       ,text_config.num_hidden_layers		)

						self.assertEqual(
						    text_attentions[0].shape[-3:]       ,(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])       ,)







		def  A__      (					self       ,A__       ,A__		):
						_A	:				Optional[int]   =      TFDeiTModel(A__       ,name='''vision_model'''		)
						_A	:				Any   =      TFRobertaModel(A__       ,name='''text_model'''		)
						return vision_model, text_model







		def  A__      (					self		):
						_A	:				Dict   =      TFDeiTModelTester(self		)
						_A	:				str   =      TFRobertaModelTester(self		)
						_A	:				int   =      vit_model_tester.prepare_config_and_inputs()
						_A	:				int   =      bert_model_tester.prepare_config_and_inputs()

						_A  ,				_A  ,				_A	:				List[Any]   =      vision_config_and_inputs

						(
						    (
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				(
						    _A
						)  ,				
						)	:				Union[str, Any]   =      text_config_and_inputs

						return {
						    "text_config": text_config,
						    "vision_config": vision_config,
						    "pixel_values": pixel_values,
						    "attention_mask": input_mask,
						    "input_ids": input_ids,
						    "text_token_type_ids": token_type_ids,
						    "text_sequence_labels": sequence_labels,
						    "text_token_labels": token_labels,
						    "text_choice_labels": choice_labels,
						}






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







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

						return model, inputs







		def  A__      (					self       ,A__       ,A__		):
						_A	:				str   =      TFCLIPVisionModel(A__       ,name='''vision_model'''		)
						_A	:				Any   =      TFBertModel(A__       ,name='''text_model'''		)
						return vision_model, text_model







		def  A__      (					self		):
						_A	:				Optional[int]   =      TFCLIPVisionModelTester(self		)
						_A	:				Union[str, Any]   =      TFBertModelTester(self		)
						_A	:				Tuple   =      clip_model_tester.prepare_config_and_inputs()
						_A	:				Optional[Any]   =      bert_model_tester.prepare_config_and_inputs()

						_A  ,				_A	:				List[str]   =      vision_config_and_inputs

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







		@slow
		def  A__      (					self		):
						_A	:				Optional[int]   =      TFVisionTextDualEncoderModel.from_pretrained(
						    '''clip-italian/clip-italian'''       ,logit_scale_init_value=1.0       ,from_pt=A__		)
						_A	:				Union[str, Any]   =      VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian'''		)

						_A	:				int   =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
						_A	:				Union[str, Any]   =      processor(
						    text=['''una foto di un gatto''', '''una foto di un cane''']       ,images=A__       ,padding=A__       ,return_tensors='''np'''		)

						_A	:				str   =      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])       ,)

						_A	:				Any   =      np.array([[1.2_28_47_27, 0.3_10_41_22]]		)

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











from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


_UpperCamelCase			:	Any								=logging.get_logger(__name__)


if is_vision_available():
	import PIL






class 						UpperCAmelCase__ (							__snake_case							):
		__snake_case      :       Union[str, Any]     =   ["pixel_values"]







		def __init__(					self       ,A__ = True       ,A__ = None       ,A__ = PILImageResampling.BICUBIC       ,A__ = True       ,A__ = None       ,A__ = True       ,A__ = 1 / 255       ,A__ = True       ,A__ = None       ,A__ = None       ,A__ = True       ,**A__       ,):
						super().__init__(**A__		)
						_A	:				List[str]   =      size if size is not None else {'''shortest_edge''': 224}
						_A	:				int   =      get_size_dict(A__       ,default_to_square=A__		)
						_A	:				Optional[int]   =      crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
						_A	:				Dict   =      get_size_dict(A__       ,default_to_square=A__       ,param_name='''crop_size'''		)

						_A	:				List[str]   =      do_resize
						_A	:				List[str]   =      size
						_A	:				Tuple   =      resample
						_A	:				List[Any]   =      do_center_crop
						_A	:				Dict   =      crop_size
						_A	:				Any   =      do_rescale
						_A	:				Optional[Any]   =      rescale_factor
						_A	:				Dict   =      do_normalize
						_A	:				Optional[int]   =      image_mean if image_mean is not None else OPENAI_CLIP_MEAN
						_A	:				Optional[int]   =      image_std if image_std is not None else OPENAI_CLIP_STD
						_A	:				Any   =      do_convert_rgb







		def  A__      (					self       ,A__       ,A__       ,A__ = PILImageResampling.BICUBIC       ,A__ = None       ,**A__       ,):
						_A	:				List[Any]   =      get_size_dict(A__       ,default_to_square=A__		)
						if "shortest_edge" not in size:
										raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"""		)
						_A	:				Any   =      get_resize_output_image_size(A__       ,size=size['''shortest_edge''']       ,default_to_square=A__		)
						return resize(A__       ,size=A__       ,resample=A__       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__       ,A__ = None       ,**A__       ,):
						_A	:				Union[str, Any]   =      get_size_dict(A__		)
						if "height" not in size or "width" not in size:
										raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}"""		)
						return center_crop(A__       ,size=(size['''height'''], size['''width'''])       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__       ,A__ = None       ,**A__       ,):
						return rescale(A__       ,scale=A__       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__       ,A__       ,A__ = None       ,**A__       ,):
						return normalize(A__       ,mean=A__       ,std=A__       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = ChannelDimension.FIRST       ,**A__       ,):
						_A	:				str   =      do_resize if do_resize is not None else self.do_resize
						_A	:				List[str]   =      size if size is not None else self.size
						_A	:				List[str]   =      get_size_dict(A__       ,param_name='''size'''       ,default_to_square=A__		)
						_A	:				Optional[Any]   =      resample if resample is not None else self.resample
						_A	:				int   =      do_center_crop if do_center_crop is not None else self.do_center_crop
						_A	:				Any   =      crop_size if crop_size is not None else self.crop_size
						_A	:				int   =      get_size_dict(A__       ,param_name='''crop_size'''       ,default_to_square=A__		)
						_A	:				int   =      do_rescale if do_rescale is not None else self.do_rescale
						_A	:				List[str]   =      rescale_factor if rescale_factor is not None else self.rescale_factor
						_A	:				Tuple   =      do_normalize if do_normalize is not None else self.do_normalize
						_A	:				Union[str, Any]   =      image_mean if image_mean is not None else self.image_mean
						_A	:				int   =      image_std if image_std is not None else self.image_std
						_A	:				Optional[Any]   =      do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

						_A	:				Optional[Any]   =      make_list_of_images(A__		)

						if not valid_images(A__		):
										raise ValueError(
										    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
										    '''torch.Tensor, tf.Tensor or jax.ndarray.'''		)

						if do_resize and size is None:
										raise ValueError('''Size must be specified if do_resize is True.'''		)

						if do_center_crop and crop_size is None:
										raise ValueError('''Crop size must be specified if do_center_crop is True.'''		)

						if do_rescale and rescale_factor is None:
										raise ValueError('''Rescale factor must be specified if do_rescale is True.'''		)

						if do_normalize and (image_mean is None or image_std is None):
										raise ValueError('''Image mean and std must be specified if do_normalize is True.'''		)

						# PIL RGBA images are converted to RGB
						if do_convert_rgb:
										_A	:				int   =      [convert_to_rgb(A__		) for image in images]

						# All transformations expect numpy arrays.
						_A	:				int   =      [to_numpy_array(A__		) for image in images]

						if do_resize:
										_A	:				List[str]   =      [self.resize(image=A__       ,size=A__       ,resample=A__		) for image in images]

						if do_center_crop:
										_A	:				Union[str, Any]   =      [self.center_crop(image=A__       ,size=A__		) for image in images]

						if do_rescale:
										_A	:				Tuple   =      [self.rescale(image=A__       ,scale=A__		) for image in images]

						if do_normalize:
										_A	:				Dict   =      [self.normalize(image=A__       ,mean=A__       ,std=A__		) for image in images]

						_A	:				Dict   =      [to_channel_dimension_format(A__       ,A__		) for image in images]

						_A	:				Dict   =      {'''pixel_values''': images}
						return BatchFeature(data=A__       ,tensor_type=A__		)



