"""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  _A(		lowerCAmelCase							):
							if isinstance(lowerCAmelCase			,  collections.abc.Iterable							):
														return x
							return (x, x)
@require_tf
class 						__UpperCAmelCase    :







			'''simple docstring'''





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_							):







										'''simple docstring'''


										pass





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										pass





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										pass





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_=None   ,      **snake_case_							):







										'''simple docstring'''


										A__       :     Optional[int]								=			VisionTextDualEncoderConfig.from_vision_text_configs(snake_case_   ,      snake_case_							)

										A__       :     Dict								=			TFVisionTextDualEncoderModel(snake_case_							)

										A__       :     Optional[int]								=			model(input_ids=snake_case_   ,      pixel_values=snake_case_   ,      attention_mask=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 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_=None   ,      **snake_case_							):







										'''simple docstring'''


										A__				,							A__       :     Optional[int]								=			self.get_vision_text_model(snake_case_   ,      snake_case_							)
										A__       :     int								=			TFVisionTextDualEncoderModel(vision_model=snake_case_   ,      text_model=snake_case_							)

										A__       :     Tuple								=			model(input_ids=snake_case_   ,      pixel_values=snake_case_   ,      attention_mask=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 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_=None   ,      **snake_case_							):







										'''simple docstring'''


										A__				,							A__       :     List[str]								=			self.get_vision_text_model(snake_case_   ,      snake_case_							)
										A__       :     Dict								=			{"""vision_model""": vision_model, """text_model""": text_model}
										A__       :     List[str]								=			TFVisionTextDualEncoderModel.from_vision_text_pretrained(**snake_case_							)

										A__       :     List[Any]								=			model(input_ids=snake_case_   ,      pixel_values=snake_case_   ,      attention_mask=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 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_=None   ,      **snake_case_							):







										'''simple docstring'''


										A__				,							A__       :     List[Any]								=			self.get_vision_text_model(snake_case_   ,      snake_case_							)
										A__       :     Tuple								=			TFVisionTextDualEncoderModel(vision_model=snake_case_   ,      text_model=snake_case_							)

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

										with tempfile.TemporaryDirectory() as tmpdirname:
																	model.save_pretrained(snake_case_							)
																	A__       :     Dict								=			TFVisionTextDualEncoderModel.from_pretrained(snake_case_							)

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





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_=None   ,      **snake_case_							):







										'''simple docstring'''


										A__				,							A__       :     Any								=			self.get_vision_text_model(snake_case_   ,      snake_case_							)
										A__       :     Any								=			TFVisionTextDualEncoderModel(vision_model=snake_case_   ,      text_model=snake_case_							)

										A__       :     Optional[int]								=			model(
										    input_ids=snake_case_   ,      pixel_values=snake_case_   ,      attention_mask=snake_case_   ,      output_attentions=snake_case_							)

										A__       :     Tuple								=			output.vision_model_output.attentions
										self.assertEqual(len(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)
										A__       :     Union[str, Any]								=			to_atuple(vision_model.config.image_size							)
										A__       :     Optional[int]								=			to_atuple(vision_model.config.patch_size							)
										A__       :     List[str]								=			(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
										A__       :     List[str]								=			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(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 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_							):







										'''simple docstring'''


										A__       :     List[Any]								=			np.abs((a - b)							).max()
										self.assertLessEqual(snake_case_   ,      snake_case_   ,      F'''Difference between torch and flax is {diff} (>= {tol}).'''							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[Any]								=			self.prepare_config_and_inputs()
										self.check_vision_text_dual_encoder_model(**snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[int]								=			self.prepare_config_and_inputs()
										self.check_model_from_pretrained_configs(**snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			self.prepare_config_and_inputs()
										self.check_vision_text_dual_encoder_from_pretrained(**snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			self.prepare_config_and_inputs()
										self.check_save_load(**snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     str								=			self.prepare_config_and_inputs()
										self.check_vision_text_output_attention(**snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__				,							A__       :     int								=			self.get_pretrained_model_and_inputs()

										A__       :     Union[str, Any]								=			model_a(**snake_case_							)
										A__       :     Union[str, Any]								=			outputs[0].numpy()

										with tempfile.TemporaryDirectory() as tmp_dirname:
																	model_a.save_pretrained(snake_case_							)
																	A__       :     Optional[int]								=			TFVisionTextDualEncoderModel.from_pretrained(snake_case_							)

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





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







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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

										return model, inputs





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_							):







										'''simple docstring'''


										A__       :     Tuple								=			TFViTModel(snake_case_   ,      name="""vision_model"""							)
										A__       :     Dict								=			TFBertModel(snake_case_   ,      name="""text_model"""							)
										return vision_model, text_model





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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

										A__				,							A__				,							A__       :     Tuple								=			vision_config_and_inputs

										(
										    (
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							
										)       :     List[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 						__UpperCAmelCase    (__A							,   unittest.TestCase     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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

										return model, inputs





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_=None   ,      **snake_case_							):







										'''simple docstring'''


										A__				,							A__       :     List[str]								=			self.get_vision_text_model(snake_case_   ,      snake_case_							)
										A__       :     List[str]								=			TFVisionTextDualEncoderModel(vision_model=snake_case_   ,      text_model=snake_case_							)

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

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

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







										'''simple docstring'''


										A__       :     Dict								=			TFDeiTModel(snake_case_   ,      name="""vision_model"""							)
										A__       :     Any								=			TFRobertaModel(snake_case_   ,      name="""text_model"""							)
										return vision_model, text_model





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Union[str, Any]								=			TFDeiTModelTester(self							)
										A__       :     Optional[int]								=			TFRobertaModelTester(self							)
										A__       :     Tuple								=			vit_model_tester.prepare_config_and_inputs()
										A__       :     int								=			bert_model_tester.prepare_config_and_inputs()

										A__				,							A__				,							A__       :     Union[str, Any]								=			vision_config_and_inputs

										(
										    (
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							
										)       :     List[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 						__UpperCAmelCase    (__A							,   unittest.TestCase     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Dict								=			TFVisionTextDualEncoderModel.from_vision_text_pretrained(
										    """Rocketknight1/tiny-random-clip-tf"""   ,      """hf-internal-testing/tiny-random-bert"""							)
										A__       :     Dict								=			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__       :     int								=			ids_tensor([batch_size, 4]   ,      model.text_model.config.vocab_size							)
										A__       :     List[str]								=			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 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_							):







										'''simple docstring'''


										A__       :     str								=			TFCLIPVisionModel(snake_case_   ,      name="""vision_model"""							)
										A__       :     Any								=			TFBertModel(snake_case_   ,      name="""text_model"""							)
										return vision_model, text_model





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[Any]								=			TFCLIPVisionModelTester(self							)
										A__       :     int								=			TFBertModelTester(self							)
										A__       :     Optional[Any]								=			clip_model_tester.prepare_config_and_inputs()
										A__       :     Optional[int]								=			bert_model_tester.prepare_config_and_inputs()

										A__				,							A__       :     Union[str, Any]								=			vision_config_and_inputs

										(
										    (
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							
										)       :     List[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 						__UpperCAmelCase    (unittest.TestCase     ):







			'''simple docstring'''





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Tuple								=			TFVisionTextDualEncoderModel.from_pretrained(
										    """clip-italian/clip-italian"""   ,      logit_scale_init_value=1.0   ,      from_pt=snake_case_							)
										A__       :     List[str]								=			VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian"""							)

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

										A__       :     Any								=			model(**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])   ,      )

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

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






"""simple docstring"""



import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCamelCase								=		logging.get_logger(__name__)

_UpperCamelCase								=		{
    "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json",
    # See all SEW models at https://huggingface.co/models?filter=sew
}
class 						__UpperCAmelCase    (__A     ):







			'''simple docstring'''
			_UpperCamelCase  :						Dict					     =				'sew'





			def __init__(			self   ,      snake_case_=32   ,      snake_case_=768   ,      snake_case_=12   ,      snake_case_=12   ,      snake_case_=3_072   ,      snake_case_=2   ,      snake_case_="gelu"   ,      snake_case_=0.1   ,      snake_case_=0.1   ,      snake_case_=0.1   ,      snake_case_=0.0   ,      snake_case_=0.1   ,      snake_case_=0.1   ,      snake_case_=0.02   ,      snake_case_=1E-5   ,      snake_case_="group"   ,      snake_case_="gelu"   ,      snake_case_=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)   ,      snake_case_=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)   ,      snake_case_=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)   ,      snake_case_=False   ,      snake_case_=128   ,      snake_case_=16   ,      snake_case_=True   ,      snake_case_=0.05   ,      snake_case_=10   ,      snake_case_=2   ,      snake_case_=0.0   ,      snake_case_=10   ,      snake_case_=0   ,      snake_case_="mean"   ,      snake_case_=False   ,      snake_case_=False   ,      snake_case_=256   ,      snake_case_=0   ,      snake_case_=1   ,      snake_case_=2   ,      **snake_case_   ,      ):







										'''simple docstring'''


										super().__init__(**snake_case_   ,      pad_token_id=snake_case_   ,      bos_token_id=snake_case_   ,      eos_token_id=snake_case_							)
										A__       :     Dict								=			hidden_size
										A__       :     Dict								=			feat_extract_norm
										A__       :     str								=			feat_extract_activation
										A__       :     Optional[Any]								=			list(snake_case_							)
										A__       :     str								=			list(snake_case_							)
										A__       :     Any								=			list(snake_case_							)
										A__       :     Any								=			conv_bias
										A__       :     Any								=			num_conv_pos_embeddings
										A__       :     Any								=			num_conv_pos_embedding_groups
										A__       :     str								=			len(self.conv_dim							)
										A__       :     Tuple								=			num_hidden_layers
										A__       :     int								=			intermediate_size
										A__       :     Union[str, Any]								=			squeeze_factor
										A__       :     Union[str, Any]								=			hidden_act
										A__       :     List[str]								=			num_attention_heads
										A__       :     List[str]								=			hidden_dropout
										A__       :     Dict								=			attention_dropout
										A__       :     Tuple								=			activation_dropout
										A__       :     Optional[int]								=			feat_proj_dropout
										A__       :     Optional[Any]								=			final_dropout
										A__       :     int								=			layerdrop
										A__       :     List[Any]								=			layer_norm_eps
										A__       :     int								=			initializer_range
										A__       :     Dict								=			vocab_size

										if (
										    (len(self.conv_stride							) != self.num_feat_extract_layers)
										    or (len(self.conv_kernel							) != self.num_feat_extract_layers)
										    or (len(self.conv_dim							) != self.num_feat_extract_layers)
										):
																	raise ValueError(
																	    """Configuration for convolutional layers is incorrect."""
																	    """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"""
																	    F'''but is `len(config.conv_dim) = {len(self.conv_dim							)}`, `len(config.conv_stride)'''
																	    F'''= {len(self.conv_stride							)}`, `len(config.conv_kernel) = {len(self.conv_kernel							)}`.'''							)

										# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
										A__       :     Optional[Any]								=			apply_spec_augment
										A__       :     int								=			mask_time_prob
										A__       :     Tuple								=			mask_time_length
										A__       :     Optional[Any]								=			mask_time_min_masks
										A__       :     Any								=			mask_feature_prob
										A__       :     List[Any]								=			mask_feature_length
										A__       :     Any								=			mask_feature_min_masks

										# ctc loss
										A__       :     str								=			ctc_loss_reduction
										A__       :     List[Any]								=			ctc_zero_infinity

										# sequence classification
										A__       :     Union[str, Any]								=			use_weighted_layer_sum
										A__       :     str								=			classifier_proj_size





			@property
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										return functools.reduce(operator.mul   ,      self.conv_stride   ,      1							)





