


'''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 (     __a  )    ->      str:



					'''simple docstring'''






					if isinstance(__a					,   collections.abc.Iterable  ):
										return x
					return (x, x)
@require_tf
class   lowercase      :

		"""simple docstring"""

		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							pass




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							pass




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							pass




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_=None       ,  **UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Optional[Any]				   =					VisionTextDualEncoderConfig.from_vision_text_configs(UpperCamelCase_       ,  UpperCamelCase_				)

							UpperCamelCase__  :Any				   =					TFVisionTextDualEncoderModel(UpperCamelCase_				)

							UpperCamelCase__  :List[str]				   =					model(input_ids=UpperCamelCase_       ,  pixel_values=UpperCamelCase_       ,  attention_mask=UpperCamelCase_				)

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

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :Tuple				   =					self.get_vision_text_model(UpperCamelCase_       ,  UpperCamelCase_				)
							UpperCamelCase__  :Any				   =					TFVisionTextDualEncoderModel(vision_model=UpperCamelCase_       ,  text_model=UpperCamelCase_				)

							UpperCamelCase__  :List[str]				   =					model(input_ids=UpperCamelCase_       ,  pixel_values=UpperCamelCase_       ,  attention_mask=UpperCamelCase_				)

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

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :Optional[int]				   =					self.get_vision_text_model(UpperCamelCase_       ,  UpperCamelCase_				)
							UpperCamelCase__  :int				   =					{'''vision_model''': vision_model, '''text_model''': text_model}
							UpperCamelCase__  :Tuple				   =					TFVisionTextDualEncoderModel.from_vision_text_pretrained(**UpperCamelCase_				)

							UpperCamelCase__  :Any				   =					model(input_ids=UpperCamelCase_       ,  pixel_values=UpperCamelCase_       ,  attention_mask=UpperCamelCase_				)

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

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :List[str]				   =					self.get_vision_text_model(UpperCamelCase_       ,  UpperCamelCase_				)
							UpperCamelCase__  :Optional[Any]				   =					TFVisionTextDualEncoderModel(vision_model=UpperCamelCase_       ,  text_model=UpperCamelCase_				)

							UpperCamelCase__  :Union[str, Any]				   =					model(input_ids=UpperCamelCase_       ,  pixel_values=UpperCamelCase_       ,  attention_mask=UpperCamelCase_				)
							UpperCamelCase__  :int				   =					output[0].numpy()

							with tempfile.TemporaryDirectory() as tmpdirname:
												model.save_pretrained(UpperCamelCase_				)
												UpperCamelCase__  :Dict				   =					TFVisionTextDualEncoderModel.from_pretrained(UpperCamelCase_				)

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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_=None       ,  **UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :List[str]				   =					self.get_vision_text_model(UpperCamelCase_       ,  UpperCamelCase_				)
							UpperCamelCase__  :Tuple				   =					TFVisionTextDualEncoderModel(vision_model=UpperCamelCase_       ,  text_model=UpperCamelCase_				)

							UpperCamelCase__  :Optional[Any]				   =					model(
							    input_ids=UpperCamelCase_       ,  pixel_values=UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  output_attentions=UpperCamelCase_				)

							UpperCamelCase__  :List[Any]				   =					output.vision_model_output.attentions
							self.assertEqual(len(UpperCamelCase_				)       ,  vision_config.num_hidden_layers				)

							# in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
							UpperCamelCase__  :Any				   =					to_atuple(vision_model.config.image_size				)
							UpperCamelCase__  :Union[str, Any]				   =					to_atuple(vision_model.config.patch_size				)
							UpperCamelCase__  :Any				   =					(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
							UpperCamelCase__  :Tuple				   =					num_patches + 1
							self.assertEqual(vision_attentions[0].shape[-3:]       ,  (vision_config.num_attention_heads, seq_len, seq_len)				)

							UpperCamelCase__  :Any				   =					output.text_model_output.attentions
							self.assertEqual(len(UpperCamelCase_				)       ,  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       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :int				   =					np.abs((a - b)				).max()
							self.assertLessEqual(UpperCamelCase_       ,  UpperCamelCase_       ,  F'''Difference between torch and flax is {diff} (>= {tol}).'''				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					self.prepare_config_and_inputs()
							self.check_vision_text_dual_encoder_model(**UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Any				   =					self.prepare_config_and_inputs()
							self.check_model_from_pretrained_configs(**UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					self.prepare_config_and_inputs()
							self.check_vision_text_dual_encoder_from_pretrained(**UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Optional[Any]				   =					self.prepare_config_and_inputs()
							self.check_save_load(**UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Dict				   =					self.prepare_config_and_inputs()
							self.check_vision_text_output_attention(**UpperCamelCase_				)




		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :Any				   =					self.get_pretrained_model_and_inputs()

							UpperCamelCase__  :Dict				   =					model_a(**UpperCamelCase_				)
							UpperCamelCase__  :Optional[int]				   =					outputs[0].numpy()

							with tempfile.TemporaryDirectory() as tmp_dirname:
												model_a.save_pretrained(UpperCamelCase_				)
												UpperCamelCase__  :int				   =					TFVisionTextDualEncoderModel.from_pretrained(UpperCamelCase_				)

												UpperCamelCase__  :List[Any]				   =					model_a(**UpperCamelCase_				)
												UpperCamelCase__  :Optional[Any]				   =					after_outputs[0].numpy()
												UpperCamelCase__  :Union[str, Any]				   =					np.amax(np.abs(out_a - out_a				)				)
												self.assertLessEqual(UpperCamelCase_       ,  1e-5				)



@require_tf
class   lowercase      (				A__      ,						unittest.TestCase   ):

		"""simple docstring"""

		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


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

							return model, inputs




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					TFViTModel(UpperCamelCase_       ,  name='''vision_model'''				)
							UpperCamelCase__  :List[Any]				   =					TFBertModel(UpperCamelCase_       ,  name='''text_model'''				)
							return vision_model, text_model




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :List[Any]				   =					TFViTModelTester(self				)
							UpperCamelCase__  :Union[str, Any]				   =					TFBertModelTester(self				)
							UpperCamelCase__  :Optional[Any]				   =					vit_model_tester.prepare_config_and_inputs()
							UpperCamelCase__  :Dict				   =					bert_model_tester.prepare_config_and_inputs()

							UpperCamelCase__   ,							UpperCamelCase__   ,							UpperCamelCase__  :Any				   =					vision_config_and_inputs

							(
							    (
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							
							)  :Tuple				   =					text_config_and_inputs

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



@require_tf
class   lowercase      (				A__      ,						unittest.TestCase   ):

		"""simple docstring"""

		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


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

							return model, inputs




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_=None       ,  **UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :List[Any]				   =					self.get_vision_text_model(UpperCamelCase_       ,  UpperCamelCase_				)
							UpperCamelCase__  :Optional[int]				   =					TFVisionTextDualEncoderModel(vision_model=UpperCamelCase_       ,  text_model=UpperCamelCase_				)

							UpperCamelCase__  :Any				   =					model(
							    input_ids=UpperCamelCase_       ,  pixel_values=UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  output_attentions=UpperCamelCase_				)

							UpperCamelCase__  :List[str]				   =					output.vision_model_output.attentions
							self.assertEqual(len(UpperCamelCase_				)       ,  vision_config.num_hidden_layers				)

							# in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
							UpperCamelCase__  :int				   =					to_atuple(vision_model.config.image_size				)
							UpperCamelCase__  :Any				   =					to_atuple(vision_model.config.patch_size				)
							UpperCamelCase__  :Union[str, Any]				   =					(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
							UpperCamelCase__  :int				   =					num_patches + 2
							self.assertEqual(vision_attentions[0].shape[-3:]       ,  (vision_config.num_attention_heads, seq_len, seq_len)				)

							UpperCamelCase__  :Any				   =					output.text_model_output.attentions
							self.assertEqual(len(UpperCamelCase_				)       ,  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       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Optional[Any]				   =					TFDeiTModel(UpperCamelCase_       ,  name='''vision_model'''				)
							UpperCamelCase__  :List[Any]				   =					TFRobertaModel(UpperCamelCase_       ,  name='''text_model'''				)
							return vision_model, text_model




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :List[Any]				   =					TFDeiTModelTester(self				)
							UpperCamelCase__  :Dict				   =					TFRobertaModelTester(self				)
							UpperCamelCase__  :int				   =					vit_model_tester.prepare_config_and_inputs()
							UpperCamelCase__  :Optional[int]				   =					bert_model_tester.prepare_config_and_inputs()

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

							(
							    (
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							
							)  :Tuple				   =					text_config_and_inputs

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



@require_tf
class   lowercase      (				A__      ,						unittest.TestCase   ):

		"""simple docstring"""

		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


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

							return model, inputs




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Optional[int]				   =					TFCLIPVisionModel(UpperCamelCase_       ,  name='''vision_model'''				)
							UpperCamelCase__  :str				   =					TFBertModel(UpperCamelCase_       ,  name='''text_model'''				)
							return vision_model, text_model




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :List[str]				   =					TFCLIPVisionModelTester(self				)
							UpperCamelCase__  :List[str]				   =					TFBertModelTester(self				)
							UpperCamelCase__  :Union[str, Any]				   =					clip_model_tester.prepare_config_and_inputs()
							UpperCamelCase__  :List[str]				   =					bert_model_tester.prepare_config_and_inputs()

							UpperCamelCase__   ,							UpperCamelCase__  :int				   =					vision_config_and_inputs

							(
							    (
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							
							)  :Optional[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_vision
@require_tf
class   lowercase      (				unittest.TestCase   ):

		"""simple docstring"""

		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Union[str, Any]				   =					TFVisionTextDualEncoderModel.from_pretrained(
							    '''clip-italian/clip-italian'''       ,  logit_scale_init_value=1.0       ,  from_pt=UpperCamelCase_				)
							UpperCamelCase__  :List[Any]				   =					VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian'''				)

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

							UpperCamelCase__  :Dict				   =					model(**UpperCamelCase_				)

							# verify the logits
							self.assertEqual(outputs.logits_per_image.shape       ,  (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])				)
							self.assertEqual(
							    outputs.logits_per_text.shape       ,  (inputs.input_ids.shape[0], inputs.pixel_values.shape[0])       ,  )

							UpperCamelCase__  :Dict				   =					np.array([[1.2284727, 0.3104122]]				)

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



'''simple docstring'''
from __future__ import annotations

from math import pi, sqrt


def      a (     __a					,   __a  )    ->      tuple:



					'''simple docstring'''






					if inductance <= 0:
										raise ValueError('''Inductance cannot be 0 or negative'''  )

					elif capacitance <= 0:
										raise ValueError('''Capacitance cannot be 0 or negative'''  )

					else:
										return (
										    "Resonant frequency",
										    float(1 / (2 * pi * (sqrt(inductance * capacitance  )))  ),
										)


if __name__ == "__main__":
		import doctest

		doctest.testmod()