



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







		'''simple docstring'''





		def 					UpperCAmelCase    (	self						:  Any			,_snake_case						:  Optional[Any]			,_snake_case						:  Tuple						)					->		Dict:






				"""simple docstring"""



				pass







		def 					UpperCAmelCase    (	self						:  Any						)					->		Union[str, Any]:






				"""simple docstring"""



				pass







		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		str:






				"""simple docstring"""



				pass







		def 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  Dict			,_snake_case						:  Dict			,_snake_case						:  Optional[Any]			,_snake_case						:  Dict			,_snake_case						:  Optional[Any]=None			,**_snake_case						:  Union[str, Any]						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    str				=			VisionTextDualEncoderConfig.from_vision_text_configs(_snake_case			,_snake_case						)

				lowercase__   :    Tuple				=			TFVisionTextDualEncoderModel(_snake_case						)

				lowercase__   :    Union[str, Any]				=			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 					UpperCAmelCase    (	self						:  int			,_snake_case						:  Any			,_snake_case						:  Any			,_snake_case						:  Any			,_snake_case						:  int			,_snake_case						:  List[str]=None			,**_snake_case						:  str						)					->		int:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    str				=			self.get_vision_text_model(_snake_case			,_snake_case						)
				lowercase__   :    str				=			TFVisionTextDualEncoderModel(vision_model=_snake_case			,text_model=_snake_case						)

				lowercase__   :    Dict				=			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 					UpperCAmelCase    (	self						:  Optional[Any]			,_snake_case						:  int			,_snake_case						:  Tuple			,_snake_case						:  Optional[Any]			,_snake_case						:  List[Any]			,_snake_case						:  str=None			,**_snake_case						:  str						)					->		Dict:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    List[str]				=			self.get_vision_text_model(_snake_case			,_snake_case						)
				lowercase__   :    str				=			{'''vision_model''': vision_model, '''text_model''': text_model}
				lowercase__   :    List[Any]				=			TFVisionTextDualEncoderModel.from_vision_text_pretrained(**_snake_case						)

				lowercase__   :    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 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  Tuple			,_snake_case						:  Any			,_snake_case						:  List[str]			,_snake_case						:  List[Any]			,_snake_case						:  Any=None			,**_snake_case						:  List[str]						)					->		str:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    int				=			self.get_vision_text_model(_snake_case			,_snake_case						)
				lowercase__   :    Optional[Any]				=			TFVisionTextDualEncoderModel(vision_model=_snake_case			,text_model=_snake_case						)

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

				with tempfile.TemporaryDirectory() as tmpdirname:
						model.save_pretrained(_snake_case						)
						lowercase__   :    List[str]				=			TFVisionTextDualEncoderModel.from_pretrained(_snake_case						)

						lowercase__   :    Any				=			model(input_ids=_snake_case			,pixel_values=_snake_case			,attention_mask=_snake_case						)
						lowercase__   :    Dict				=			after_output[0].numpy()
						lowercase__   :    Optional[int]				=			np.amax(np.abs(out_a - out_a						)						)
						self.assertLessEqual(_snake_case			,1e-5						)







		def 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  Dict			,_snake_case						:  str			,_snake_case						:  str			,_snake_case						:  Optional[Any]			,_snake_case						:  Optional[int]=None			,**_snake_case						:  str						)					->		Any:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    str				=			self.get_vision_text_model(_snake_case			,_snake_case						)
				lowercase__   :    Dict				=			TFVisionTextDualEncoderModel(vision_model=_snake_case			,text_model=_snake_case						)

				lowercase__   :    Optional[Any]				=			model(
				    input_ids=_snake_case			,pixel_values=_snake_case			,attention_mask=_snake_case			,output_attentions=_snake_case						)

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

				lowercase__   :    List[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 					UpperCAmelCase    (	self						:  Optional[int]			,_snake_case						:  np.ndarray			,_snake_case						:  np.ndarray			,_snake_case						:  float						)					->		List[Any]:






				"""simple docstring"""



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







		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		Dict:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			self.prepare_config_and_inputs()
				self.check_vision_text_dual_encoder_model(**_snake_case						)







		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			self.prepare_config_and_inputs()
				self.check_model_from_pretrained_configs(**_snake_case						)







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		Any:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			self.prepare_config_and_inputs()
				self.check_vision_text_dual_encoder_from_pretrained(**_snake_case						)







		def 					UpperCAmelCase    (	self						:  List[str]						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			self.prepare_config_and_inputs()
				self.check_save_load(**_snake_case						)







		def 					UpperCAmelCase    (	self						:  Union[str, Any]						)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			self.prepare_config_and_inputs()
				self.check_vision_text_output_attention(**_snake_case						)







		@slow
		def 					UpperCAmelCase    (	self						:  Any						)					->		Optional[Any]:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    List[Any]				=			self.get_pretrained_model_and_inputs()

				lowercase__   :    str				=			model_a(**_snake_case						)
				lowercase__   :    Optional[int]				=			outputs[0].numpy()

				with tempfile.TemporaryDirectory() as tmp_dirname:
						model_a.save_pretrained(_snake_case						)
						lowercase__   :    Dict				=			TFVisionTextDualEncoderModel.from_pretrained(_snake_case						)

						lowercase__   :    Tuple				=			model_a(**_snake_case						)
						lowercase__   :    Optional[int]				=			after_outputs[0].numpy()
						lowercase__   :    List[str]				=			np.amax(np.abs(out_a - out_a						)						)
						self.assertLessEqual(_snake_case			,1e-5						)





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







		'''simple docstring'''





		def 					UpperCAmelCase    (	self						:  Union[str, Any]						)					->		Optional[int]:






				"""simple docstring"""



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

				return model, inputs







		def 					UpperCAmelCase    (	self						:  Optional[Any]			,_snake_case						:  Dict			,_snake_case						:  List[str]						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			TFViTModel(_snake_case			,name='''vision_model'''						)
				lowercase__   :    Optional[int]				=			TFBertModel(_snake_case			,name='''text_model'''						)
				return vision_model, text_model







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		Dict:






				"""simple docstring"""



				lowercase__   :    Tuple				=			TFViTModelTester(self						)
				lowercase__   :    List[str]				=			TFBertModelTester(self						)
				lowercase__   :    Optional[Any]				=			vit_model_tester.prepare_config_and_inputs()
				lowercase__   :    str				=			bert_model_tester.prepare_config_and_inputs()

				lowercase__		,			lowercase__		,			lowercase__   :    Any				=			vision_config_and_inputs

				(
				    (
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			
				)   :    Optional[Any]				=			text_config_and_inputs

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





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







		'''simple docstring'''





		def 					UpperCAmelCase    (	self						:  Tuple						)					->		Dict:






				"""simple docstring"""



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

				return model, inputs







		def 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  Any			,_snake_case						:  Optional[int]			,_snake_case						:  Union[str, Any]			,_snake_case						:  Optional[int]			,_snake_case						:  Dict=None			,**_snake_case						:  str						)					->		str:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    List[Any]				=			self.get_vision_text_model(_snake_case			,_snake_case						)
				lowercase__   :    Union[str, Any]				=			TFVisionTextDualEncoderModel(vision_model=_snake_case			,text_model=_snake_case						)

				lowercase__   :    str				=			model(
				    input_ids=_snake_case			,pixel_values=_snake_case			,attention_mask=_snake_case			,output_attentions=_snake_case						)

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

				lowercase__   :    Optional[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 					UpperCAmelCase    (	self						:  Any			,_snake_case						:  str			,_snake_case						:  List[Any]						)					->		int:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			TFDeiTModel(_snake_case			,name='''vision_model'''						)
				lowercase__   :    int				=			TFRobertaModel(_snake_case			,name='''text_model'''						)
				return vision_model, text_model







		def 					UpperCAmelCase    (	self						:  str						)					->		str:






				"""simple docstring"""



				lowercase__   :    Tuple				=			TFDeiTModelTester(self						)
				lowercase__   :    Optional[Any]				=			TFRobertaModelTester(self						)
				lowercase__   :    List[str]				=			vit_model_tester.prepare_config_and_inputs()
				lowercase__   :    Dict				=			bert_model_tester.prepare_config_and_inputs()

				lowercase__		,			lowercase__		,			lowercase__   :    int				=			vision_config_and_inputs

				(
				    (
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			
				)   :    Optional[Any]				=			text_config_and_inputs

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





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







		'''simple docstring'''





		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		Any:






				"""simple docstring"""



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

				return model, inputs







		def 					UpperCAmelCase    (	self						:  Any			,_snake_case						:  List[str]			,_snake_case						:  int						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    Tuple				=			TFCLIPVisionModel(_snake_case			,name='''vision_model'''						)
				lowercase__   :    Optional[int]				=			TFBertModel(_snake_case			,name='''text_model'''						)
				return vision_model, text_model







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Dict:






				"""simple docstring"""



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

				lowercase__		,			lowercase__   :    Tuple				=			vision_config_and_inputs

				(
				    (
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			(
				    lowercase__
				)		,			
				)   :    str				=			text_config_and_inputs

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





@require_vision
@require_tf
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		@slow
		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		int:






				"""simple docstring"""



				lowercase__   :    Optional[int]				=			TFVisionTextDualEncoderModel.from_pretrained(
				    '''clip-italian/clip-italian'''			,logit_scale_init_value=1.0			,from_pt=_snake_case						)
				lowercase__   :    Optional[Any]				=			VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian'''						)

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

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

				lowercase__   :    List[Any]				=			np.array([[1.228_4727, 0.310_4122]]						)

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



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCAmelCase							:  Optional[int]   	=						{"""configuration_wavlm""": ["""WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """WavLMConfig"""]}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCAmelCase							:  Any   	=						[
						    """WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """WavLMForAudioFrameClassification""",
						    """WavLMForCTC""",
						    """WavLMForSequenceClassification""",
						    """WavLMForXVector""",
						    """WavLMModel""",
						    """WavLMPreTrainedModel""",
						]

if TYPE_CHECKING:
						from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_wavlm import (
												    WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST,
												    WavLMForAudioFrameClassification,
												    WavLMForCTC,
												    WavLMForSequenceClassification,
												    WavLMForXVector,
												    WavLMModel,
												    WavLMPreTrainedModel,
												)

else:
						import sys

						lowerCAmelCase							:  Union[str, Any]   	=						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






