



"""simple docstring"""






# Lint as: python3
import os
import re
import urllib.parse
from pathlib import Path
from typing import Callable, List, Optional, Union
from zipfile import ZipFile

from ..utils.file_utils import cached_path, hf_github_url
from ..utils.logging import get_logger
from ..utils.version import Version


__snake_case							:   List[Any]      =    get_logger(__name__)



class 			A__   :




	'''simple docstring'''





	SCREAMING_SNAKE_CASE          =	'dummy_data'
	SCREAMING_SNAKE_CASE          =	'datasets'
	SCREAMING_SNAKE_CASE          =	False

	def __init__(     self:		List[str]       ,			_SCREAMING_SNAKE_CASE:		str       ,			_SCREAMING_SNAKE_CASE:		str       ,			_SCREAMING_SNAKE_CASE:		Union[Version, str]       ,			_SCREAMING_SNAKE_CASE:		Optional[str] = None       ,			_SCREAMING_SNAKE_CASE:		bool = False       ,			_SCREAMING_SNAKE_CASE:		bool = True       ,			_SCREAMING_SNAKE_CASE:		Optional[List[Callable]] = None       ,			)				->       Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       0
				__lowerCAmelCase      :  List[str]												=       dataset_name
				__lowerCAmelCase      :  List[str]												=       cache_dir
				__lowerCAmelCase      :  Any												=       use_local_dummy_data
				__lowerCAmelCase      :  Dict												=       config
				# download_callbacks take a single url as input
				__lowerCAmelCase      :  List[Callable]												=       download_callbacks or []
				# if False, it doesn't load existing files and it returns the paths of the dummy files relative
				# to the dummy_data zip file root
				__lowerCAmelCase      :  Optional[Any]												=       load_existing_dummy_data

				# TODO(PVP, QL) might need to make this more general
				__lowerCAmelCase      :  List[Any]												=       str(_SCREAMING_SNAKE_CASE)
				# to be downloaded
				__lowerCAmelCase      :  int												=       None
				__lowerCAmelCase      :  int												=       None

	@property
	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       Optional[Any]:

				"""simple docstring"""

				if self._dummy_file is None:
							__lowerCAmelCase      :  Optional[int]												=       self.download_dummy_data()
				return self._dummy_file

	@property
	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       Any:

				"""simple docstring"""

				if self.config is not None:
							# structure is dummy / config_name / version_name
							return os.path.join("dummy"       ,			self.config.name       ,			self.version_name)
				# structure is dummy / version_name
				return os.path.join("dummy"       ,			self.version_name)

	@property
	def 							_SCREAMING_SNAKE_CASE   (     self:		Union[str, Any])				->       List[str]:

				"""simple docstring"""

				return os.path.join(self.dummy_data_folder       ,			"dummy_data.zip")

	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       int:

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[int]												=       (
				    self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data
				)

				__lowerCAmelCase      :  List[str]												=       cached_path(
				    _SCREAMING_SNAKE_CASE       ,			cache_dir=self.cache_dir       ,			extract_compressed_file=_SCREAMING_SNAKE_CASE       ,			force_extract=_SCREAMING_SNAKE_CASE)

				return os.path.join(_SCREAMING_SNAKE_CASE       ,			self.dummy_file_name)

	@property
	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       Union[str, Any]:

				"""simple docstring"""

				return os.path.join(self.datasets_scripts_dir       ,			self.dataset_name       ,			self.dummy_zip_file)

	@property
	def 							_SCREAMING_SNAKE_CASE   (     self:		str)				->       Any:

				"""simple docstring"""

				if self._bucket_url is None:
							__lowerCAmelCase      :  str												=       hf_github_url(self.dataset_name       ,			self.dummy_zip_file.replace(os.sep       ,			"/"))
				return self._bucket_url

	@property
	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any])				->       List[Any]:

				"""simple docstring"""

				if os.path.isdir(self.dummy_file):
							return self.dummy_file
				# else cut off path to file -> example `xsum`.
				return "/".join(self.dummy_file.replace(os.sep       ,			"/").split("/")[:-1])

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

				"""simple docstring"""

				if self.load_existing_dummy_data:
							# dummy data is downloaded and tested
							__lowerCAmelCase      :  Optional[int]												=       self.dummy_file
				else:
							# dummy data cannot be downloaded and only the path to dummy file is returned
							__lowerCAmelCase      :  List[Any]												=       self.dummy_file_name

				# special case when data_url is a dict
				if isinstance(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE):
							return self.create_dummy_data_dict(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				elif isinstance(_SCREAMING_SNAKE_CASE       ,			(list, tuple)):
							return self.create_dummy_data_list(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				else:
							return self.create_dummy_data_single(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		int       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			*_SCREAMING_SNAKE_CASE:		str)				->       Union[str, Any]:

				"""simple docstring"""

				return self.download_and_extract(_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		Optional[int])				->       Dict:

				"""simple docstring"""

				return self.download_and_extract(_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		str       ,			_SCREAMING_SNAKE_CASE:		str       ,			*_SCREAMING_SNAKE_CASE:		int       ,			**_SCREAMING_SNAKE_CASE:		Optional[int])				->       Optional[Any]:

				"""simple docstring"""

				return path

	def 							_SCREAMING_SNAKE_CASE   (     self:		Any)				->       Dict:

				"""simple docstring"""

				return {}

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

				"""simple docstring"""

				__lowerCAmelCase      :  Tuple												=       {}
				for key, single_urls in data_url.items():
							for download_callback in self.download_callbacks:
										if isinstance(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE):
													for single_url in single_urls:
																download_callback(_SCREAMING_SNAKE_CASE)
										else:
													__lowerCAmelCase      :  Optional[Any]												=       single_urls
													download_callback(_SCREAMING_SNAKE_CASE)
            # we force the name of each key to be the last file / folder name of the url path
            # if the url has arguments, we need to encode them with urllib.parse.quote_plus
							if isinstance(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE):
										__lowerCAmelCase      :  str												=       [os.path.join(_SCREAMING_SNAKE_CASE       ,			urllib.parse.quote_plus(Path(_SCREAMING_SNAKE_CASE).name)) for x in single_urls]
							else:
										__lowerCAmelCase      :  Optional[Any]												=       single_urls
										__lowerCAmelCase      :  Optional[int]												=       os.path.join(_SCREAMING_SNAKE_CASE       ,			urllib.parse.quote_plus(Path(_SCREAMING_SNAKE_CASE).name))
							__lowerCAmelCase      :  List[str]												=       value

				# make sure that values are unique
				if all(isinstance(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE) for i in dummy_data_dict.values()) and len(set(dummy_data_dict.values())) < len(
				    dummy_data_dict.values()):
							# append key to value to make its name unique
							__lowerCAmelCase      :  List[Any]												=       {key: value + key for key, value in dummy_data_dict.items()}

				return dummy_data_dict

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

				"""simple docstring"""

				__lowerCAmelCase      :  List[Any]												=       []
				# trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one
				__lowerCAmelCase      :  Any												=       all(bool(re.findall("[0-9]{3,}-of-[0-9]{3,}"       ,			_SCREAMING_SNAKE_CASE)) for url in data_url)
				__lowerCAmelCase      :  Union[str, Any]												=       all(
				    url.startswith("https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed") for url in data_url)
				if data_url and (is_tf_records or is_pubmed_records):
							__lowerCAmelCase      :  Optional[int]												=       [data_url[0]] * len(_SCREAMING_SNAKE_CASE)
				for single_url in data_url:
							for download_callback in self.download_callbacks:
										download_callback(_SCREAMING_SNAKE_CASE)
							# we force the name of each key to be the last file / folder name of the url path
							# if the url has arguments, we need to encode them with urllib.parse.quote_plus
							__lowerCAmelCase      :  Tuple												=       os.path.join(_SCREAMING_SNAKE_CASE       ,			urllib.parse.quote_plus(single_url.split("/")[-1]))
							dummy_data_list.append(_SCREAMING_SNAKE_CASE)
				return dummy_data_list

	def 							_SCREAMING_SNAKE_CASE   (     self:		str       ,			_SCREAMING_SNAKE_CASE:		Any       ,			_SCREAMING_SNAKE_CASE:		Any)				->       int:

				"""simple docstring"""

				for download_callback in self.download_callbacks:
							download_callback(_SCREAMING_SNAKE_CASE)
				# we force the name of each key to be the last file / folder name of the url path
				# if the url has arguments, we need to encode them with urllib.parse.quote_plus
				__lowerCAmelCase      :  Optional[Any]												=       os.path.join(_SCREAMING_SNAKE_CASE       ,			urllib.parse.quote_plus(data_url.split("/")[-1]))
				if os.path.exists(_SCREAMING_SNAKE_CASE) or not self.load_existing_dummy_data:
							return value
				else:
							# Backward compatibility, maybe deprecate at one point.
							# For many datasets with single url calls to dl_manager.download_and_extract,
							# the dummy_data.zip file is actually the zipped downloaded file
							# while now we expected the dummy_data.zip file to be a directory containing
							# the downloaded file.
							return path_to_dummy_data

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[str])				->       Dict:

				"""simple docstring"""

				pass

	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       int:

				"""simple docstring"""

				pass

	def 							_SCREAMING_SNAKE_CASE   (     self:		Any       ,			_SCREAMING_SNAKE_CASE:		List[str])				->       List[str]:

				"""simple docstring"""

				def _iter_archive_members(_SCREAMING_SNAKE_CASE:		List[Any]):
							# this preserves the order of the members inside the ZIP archive
							__lowerCAmelCase      :  Union[str, Any]												=       Path(self.dummy_file).parent
							__lowerCAmelCase      :  Tuple												=       path.relative_to(_SCREAMING_SNAKE_CASE)
							with ZipFile(self.local_path_to_dummy_data) as zip_file:
										__lowerCAmelCase      :  Optional[int]												=       zip_file.namelist()
							for member in members:
										if member.startswith(relative_path.as_posix()):
													yield dummy_parent_path.joinpath(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  int												=       Path(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  List[Any]												=       _iter_archive_members(_SCREAMING_SNAKE_CASE) if self.use_local_dummy_data else path.rglob("*")
				for file_path in file_paths:
							if file_path.is_file() and not file_path.name.startswith((".", "__")):
										yield file_path.relative_to(_SCREAMING_SNAKE_CASE).as_posix(), file_path.open("rb")







	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[Any]       ,			_SCREAMING_SNAKE_CASE:		str)				->       Any:

				"""simple docstring"""

				if not isinstance(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE):
							__lowerCAmelCase      :  Optional[Any]												=       [paths]
				for path in paths:
							if os.path.isfile(_SCREAMING_SNAKE_CASE):
										if os.path.basename(_SCREAMING_SNAKE_CASE).startswith((".", "__")):
													return
										yield path
							else:
										for dirpath, dirnames, filenames in os.walk(_SCREAMING_SNAKE_CASE):
													if os.path.basename(_SCREAMING_SNAKE_CASE).startswith((".", "__")):
																continue
													dirnames.sort()
													for filename in sorted(_SCREAMING_SNAKE_CASE):
																if filename.startswith((".", "__")):
																			continue
																yield os.path.join(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)




"""simple docstring"""






import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_flax_cross_test,
    require_flax,
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import is_flax_available, is_torch_available, is_vision_available

from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_flax_bert import FlaxBertModelTester
from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester
from ..vit.test_modeling_flax_vit import FlaxViTModelTester


if is_flax_available():
	from transformers import (
	    FlaxBertModel,
	    FlaxCLIPVisionModel,
	    FlaxVisionTextDualEncoderModel,
	    FlaxViTModel,
	    VisionTextDualEncoderConfig,
	    VisionTextDualEncoderProcessor,
	)
	from transformers.modeling_flax_pytorch_utils import (
	    convert_pytorch_state_dict_to_flax,
	    load_flax_weights_in_pytorch_model,
	)


if is_torch_available():
	import torch

	from transformers import VisionTextDualEncoderModel

if is_vision_available():
	from PIL import Image


def 			_lowercase				(  __snake_case	)						->   List[str]:
			if isinstance(__snake_case					,collections.abc.Iterable	):
						return x
			return (x, x)



@require_flax
class 			A__   :




	'''simple docstring'''





	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[Any]       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			_SCREAMING_SNAKE_CASE:		Optional[Any])				->       str:

				"""simple docstring"""

				pass

	def 							_SCREAMING_SNAKE_CASE   (     self:		str)				->       int:

				"""simple docstring"""

				pass

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[str])				->       Tuple:

				"""simple docstring"""

				pass

	def 							_SCREAMING_SNAKE_CASE   (     self:		Any       ,			_SCREAMING_SNAKE_CASE:		np.ndarray       ,			_SCREAMING_SNAKE_CASE:		np.ndarray       ,			_SCREAMING_SNAKE_CASE:		float)				->       List[Any]:

				"""simple docstring"""

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

	def 							_SCREAMING_SNAKE_CASE   (     self:		str       ,			_SCREAMING_SNAKE_CASE:		List[str]       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			_SCREAMING_SNAKE_CASE:		Any       ,			_SCREAMING_SNAKE_CASE:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		Any=None       ,			**_SCREAMING_SNAKE_CASE:		Tuple)				->       List[str]:

				"""simple docstring"""

				__lowerCAmelCase      :  Tuple												=       VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  List[str]												=       FlaxVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  Optional[int]												=       model(input_ids=_SCREAMING_SNAKE_CASE       ,			pixel_values=_SCREAMING_SNAKE_CASE       ,			attention_mask=_SCREAMING_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 							_SCREAMING_SNAKE_CASE   (     self:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		str       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			_SCREAMING_SNAKE_CASE:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		Optional[Any]=None       ,			**_SCREAMING_SNAKE_CASE:		List[str])				->       Dict:

				"""simple docstring"""

				__lowerCAmelCase						,	__lowerCAmelCase      :  List[Any]												=       self.get_vision_text_model(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Union[str, Any]												=       {"vision_model": vision_model, "text_model": text_model}
				__lowerCAmelCase      :  Optional[Any]												=       FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  Union[str, Any]												=       model(input_ids=_SCREAMING_SNAKE_CASE       ,			pixel_values=_SCREAMING_SNAKE_CASE       ,			attention_mask=_SCREAMING_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 							_SCREAMING_SNAKE_CASE   (     self:		Dict       ,			_SCREAMING_SNAKE_CASE:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		List[str]       ,			_SCREAMING_SNAKE_CASE:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		List[Any]=None       ,			**_SCREAMING_SNAKE_CASE:		Tuple)				->       str:

				"""simple docstring"""

				__lowerCAmelCase						,	__lowerCAmelCase      :  Optional[Any]												=       self.get_vision_text_model(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Optional[Any]												=       {"vision_model": vision_model, "text_model": text_model}
				__lowerCAmelCase      :  Any												=       FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  Optional[Any]												=       model(input_ids=_SCREAMING_SNAKE_CASE       ,			pixel_values=_SCREAMING_SNAKE_CASE       ,			attention_mask=_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Any												=       output[0]

				with tempfile.TemporaryDirectory() as tmpdirname:
							model.save_pretrained(_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase      :  Union[str, Any]												=       FlaxVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE)

							__lowerCAmelCase      :  int												=       model(input_ids=_SCREAMING_SNAKE_CASE       ,			pixel_values=_SCREAMING_SNAKE_CASE       ,			attention_mask=_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase      :  Union[str, Any]												=       after_output[0]
							__lowerCAmelCase      :  Any												=       np.amax(np.abs(out_a - out_a))
							self.assertLessEqual(_SCREAMING_SNAKE_CASE       ,			1e-3)

	def 							_SCREAMING_SNAKE_CASE   (     self:		Tuple       ,			_SCREAMING_SNAKE_CASE:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		Optional[Any]       ,			_SCREAMING_SNAKE_CASE:		int       ,			_SCREAMING_SNAKE_CASE:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		Any=None       ,			**_SCREAMING_SNAKE_CASE:		List[str])				->       Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase						,	__lowerCAmelCase      :  Any												=       self.get_vision_text_model(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Union[str, Any]												=       {"vision_model": vision_model, "text_model": text_model}
				__lowerCAmelCase      :  Optional[Any]												=       FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  Optional[Any]												=       model(
				    input_ids=_SCREAMING_SNAKE_CASE       ,			pixel_values=_SCREAMING_SNAKE_CASE       ,			attention_mask=_SCREAMING_SNAKE_CASE       ,			output_attentions=_SCREAMING_SNAKE_CASE)

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

				__lowerCAmelCase      :  Union[str, Any]												=       output.text_model_output.attentions
				self.assertEqual(len(_SCREAMING_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 							_SCREAMING_SNAKE_CASE   (     self:		List[str]       ,			_SCREAMING_SNAKE_CASE:		Union[str, Any]       ,			_SCREAMING_SNAKE_CASE:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		int)				->       str:

				"""simple docstring"""

				pt_model.to(_SCREAMING_SNAKE_CASE)
				pt_model.eval()

				# prepare inputs
				__lowerCAmelCase      :  Union[str, Any]												=       inputs_dict
				__lowerCAmelCase      :  Union[str, Any]												=       {k: torch.tensor(v.tolist()) for k, v in flax_inputs.items()}

				with torch.no_grad():
							__lowerCAmelCase      :  Any												=       pt_model(**_SCREAMING_SNAKE_CASE).to_tuple()

				__lowerCAmelCase      :  List[Any]												=       fx_model(**_SCREAMING_SNAKE_CASE).to_tuple()
				self.assertEqual(len(_SCREAMING_SNAKE_CASE)       ,			len(_SCREAMING_SNAKE_CASE)       ,			"Output lengths differ between Flax and PyTorch")
				for fx_output, pt_output in zip(fx_outputs[:4]       ,			pt_outputs[:4]):
							self.assert_almost_equals(_SCREAMING_SNAKE_CASE       ,			pt_output.numpy()       ,			4e-2)

				# PT -> Flax
				with tempfile.TemporaryDirectory() as tmpdirname:
							pt_model.save_pretrained(_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase      :  str												=       FlaxVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE       ,			from_pt=_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  int												=       fx_model_loaded(**_SCREAMING_SNAKE_CASE).to_tuple()
				self.assertEqual(len(_SCREAMING_SNAKE_CASE)       ,			len(_SCREAMING_SNAKE_CASE)       ,			"Output lengths differ between Flax and PyTorch")
				for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4]       ,			pt_outputs[:4]):
							self.assert_almost_equals(_SCREAMING_SNAKE_CASE       ,			pt_output.numpy()       ,			4e-2)

				# Flax -> PT
				with tempfile.TemporaryDirectory() as tmpdirname:
							fx_model.save_pretrained(_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase      :  Union[str, Any]												=       VisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE       ,			from_flax=_SCREAMING_SNAKE_CASE)

				pt_model_loaded.to(_SCREAMING_SNAKE_CASE)
				pt_model_loaded.eval()

				with torch.no_grad():
							__lowerCAmelCase      :  Optional[Any]												=       pt_model_loaded(**_SCREAMING_SNAKE_CASE).to_tuple()

				self.assertEqual(len(_SCREAMING_SNAKE_CASE)       ,			len(_SCREAMING_SNAKE_CASE)       ,			"Output lengths differ between Flax and PyTorch")
				for fx_output, pt_output_loaded in zip(fx_outputs[:4]       ,			pt_outputs_loaded[:4]):
							self.assert_almost_equals(_SCREAMING_SNAKE_CASE       ,			pt_output_loaded.numpy()       ,			4e-2)

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

				"""simple docstring"""

				__lowerCAmelCase      :  Union[str, Any]												=       VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  Dict												=       VisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  str												=       FlaxVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  str												=       convert_pytorch_state_dict_to_flax(pt_model.state_dict()       ,			_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Optional[Any]												=       fx_state

				self.check_pt_flax_equivalence(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		Tuple       ,			_SCREAMING_SNAKE_CASE:		Dict)				->       str:

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[int]												=       VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  Dict												=       VisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Tuple												=       FlaxVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  int												=       load_flax_weights_in_pytorch_model(_SCREAMING_SNAKE_CASE       ,			fx_model.params)

				self.check_pt_flax_equivalence(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)

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

				"""simple docstring"""

				__lowerCAmelCase      :  List[str]												=       self.prepare_config_and_inputs()
				self.check_model_from_pretrained_configs(**_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		Dict)				->       int:

				"""simple docstring"""

				__lowerCAmelCase      :  List[Any]												=       self.prepare_config_and_inputs()
				self.check_vision_text_dual_encoder_from_pretrained(**_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any])				->       List[Any]:

				"""simple docstring"""

				__lowerCAmelCase      :  Dict												=       self.prepare_config_and_inputs()
				self.check_save_load(**_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       Dict:

				"""simple docstring"""

				__lowerCAmelCase      :  List[str]												=       self.prepare_config_and_inputs()
				self.check_vision_text_output_attention(**_SCREAMING_SNAKE_CASE)

	@is_pt_flax_cross_test
	def 							_SCREAMING_SNAKE_CASE   (     self:		Union[str, Any])				->       Any:

				"""simple docstring"""

				__lowerCAmelCase      :  Dict												=       self.prepare_config_and_inputs()
				__lowerCAmelCase      :  List[Any]												=       config_inputs_dict.pop("vision_config")
				__lowerCAmelCase      :  str												=       config_inputs_dict.pop("text_config")

				__lowerCAmelCase      :  Union[str, Any]												=       config_inputs_dict

				self.check_equivalence_pt_to_flax(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				self.check_equivalence_flax_to_pt(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)







	@slow
	def 							_SCREAMING_SNAKE_CASE   (     self:		str)				->       Dict:

				"""simple docstring"""

				__lowerCAmelCase						,	__lowerCAmelCase      :  Dict												=       self.get_pretrained_model_and_inputs()

				__lowerCAmelCase      :  Union[str, Any]												=       model_a(**_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  List[str]												=       outputs[0]

				with tempfile.TemporaryDirectory() as tmp_dirname:
							model_a.save_pretrained(_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase      :  List[Any]												=       FlaxVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE)

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






@require_flax
class 			A__   (  __SCREAMING_SNAKE_CASE  ,		unittest.TestCase							):




	'''simple docstring'''





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

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
				    "hf-internal-testing/tiny-random-vit"       ,			"hf-internal-testing/tiny-bert"       ,			vision_from_pt=_SCREAMING_SNAKE_CASE       ,			text_from_pt=_SCREAMING_SNAKE_CASE       ,			)
				__lowerCAmelCase      :  Union[str, Any]												=       13
				__lowerCAmelCase      :  Optional[int]												=       floats_tensor(
				    [
				        batch_size,
				        model.config.vision_config.num_channels,
				        model.config.vision_config.image_size,
				        model.config.vision_config.image_size,
				    ])
				__lowerCAmelCase      :  List[Any]												=       ids_tensor([batch_size, 4]       ,			model.config.text_config.vocab_size)
				__lowerCAmelCase      :  List[Any]												=       random_attention_mask([batch_size, 4])
				__lowerCAmelCase      :  str												=       {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

				return model, inputs

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

				"""simple docstring"""

				__lowerCAmelCase      :  List[str]												=       FlaxViTModel(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Union[str, Any]												=       FlaxBertModel(_SCREAMING_SNAKE_CASE)
				return vision_model, text_model







	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[int])				->       int:

				"""simple docstring"""

				__lowerCAmelCase      :  List[Any]												=       FlaxViTModelTester(self)
				__lowerCAmelCase      :  Optional[Any]												=       FlaxBertModelTester(self)
				__lowerCAmelCase      :  int												=       vit_model_tester.prepare_config_and_inputs()
				__lowerCAmelCase      :  List[str]												=       bert_model_tester.prepare_config_and_inputs()

				__lowerCAmelCase						,	__lowerCAmelCase      :  Tuple												=       vision_config_and_inputs

				__lowerCAmelCase						,	__lowerCAmelCase						,	__lowerCAmelCase						,	__lowerCAmelCase      :  Union[str, Any]												=       text_config_and_inputs

				# make sure that cross attention layers are added
				return {
				    "text_config": text_config,
				    "vision_config": vision_config,
				    "pixel_values": pixel_values,
				    "attention_mask": attention_mask,
				    "input_ids": input_ids,
				    "token_type_ids": token_type_ids,
				}






@require_torch
class 			A__   (  __SCREAMING_SNAKE_CASE  ,		unittest.TestCase							):




	'''simple docstring'''





	def 							_SCREAMING_SNAKE_CASE   (     self:		Dict)				->       List[str]:

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[int]												=       FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
				    "hf-internal-testing/tiny-random-clip"       ,			"hf-internal-testing/tiny-bert"       ,			vision_from_pt=_SCREAMING_SNAKE_CASE       ,			text_from_pt=_SCREAMING_SNAKE_CASE       ,			)
				__lowerCAmelCase      :  Optional[int]												=       13
				__lowerCAmelCase      :  List[str]												=       floats_tensor(
				    [
				        batch_size,
				        model.config.vision_config.num_channels,
				        model.config.vision_config.image_size,
				        model.config.vision_config.image_size,
				    ])
				__lowerCAmelCase      :  Any												=       ids_tensor([batch_size, 4]       ,			model.config.text_config.vocab_size)
				__lowerCAmelCase      :  str												=       random_attention_mask([batch_size, 4])
				__lowerCAmelCase      :  Optional[int]												=       {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

				return model, inputs

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

				"""simple docstring"""

				__lowerCAmelCase      :  int												=       FlaxCLIPVisionModel(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  List[str]												=       FlaxBertModel(_SCREAMING_SNAKE_CASE)
				return vision_model, text_model







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

				"""simple docstring"""

				__lowerCAmelCase      :  List[Any]												=       FlaxCLIPVisionModelTester(self)
				__lowerCAmelCase      :  str												=       FlaxBertModelTester(self)
				__lowerCAmelCase      :  Optional[Any]												=       clip_model_tester.prepare_config_and_inputs()
				__lowerCAmelCase      :  Dict												=       bert_model_tester.prepare_config_and_inputs()

				__lowerCAmelCase						,	__lowerCAmelCase      :  Any												=       vision_config_and_inputs

				__lowerCAmelCase						,	__lowerCAmelCase						,	__lowerCAmelCase						,	__lowerCAmelCase      :  List[Any]												=       text_config_and_inputs

				# make sure that cross attention layers are added
				return {
				    "text_config": text_config,
				    "vision_config": vision_config,
				    "pixel_values": pixel_values,
				    "attention_mask": attention_mask,
				    "input_ids": input_ids,
				    "token_type_ids": token_type_ids,
				}






@require_flax
@require_vision
class 			A__   (  unittest.TestCase							):




	'''simple docstring'''





	@slow
	def 							_SCREAMING_SNAKE_CASE   (     self:		List[str])				->       List[str]:

				"""simple docstring"""

				__lowerCAmelCase      :  Dict												=       FlaxVisionTextDualEncoderModel.from_pretrained("clip-italian/clip-italian"       ,			logit_scale_init_value=1.0)
				__lowerCAmelCase      :  str												=       VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian")

				__lowerCAmelCase      :  Tuple												=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
				__lowerCAmelCase      :  Optional[int]												=       processor(
				    text=["una foto di un gatto", "una foto di un cane"]       ,			images=_SCREAMING_SNAKE_CASE       ,			padding=_SCREAMING_SNAKE_CASE       ,			return_tensors="np")

				__lowerCAmelCase      :  List[str]												=       model(**_SCREAMING_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])       ,			)

				__lowerCAmelCase      :  List[str]												=       np.array([[1.228_4727, 0.310_4122]])

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