


from typing import List, Optional, Tuple, Union

import torch

from ...schedulers import DDIMScheduler
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput







class 	__lowercase    (__SCREAMING_SNAKE_CASE  ):





							"""simple docstring"""







							def __init__(      self			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										super().__init__()

										# make sure scheduler can always be converted to DDIM
										SCREAMING_SNAKE_CASE_		:     Tuple					=					DDIMScheduler.from_config(scheduler.config       )

										self.register_modules(unet=lowerCAmelCase__			,						scheduler=lowerCAmelCase__       )






							@torch.no_grad()
							def __call__(      self			,						lowerCAmelCase__ = 1			,						lowerCAmelCase__ = None			,						lowerCAmelCase__ = 0.0			,						lowerCAmelCase__ = 5_0			,						lowerCAmelCase__ = None			,						lowerCAmelCase__ = "pil"			,						lowerCAmelCase__ = True			,						):





										"""simple docstring"""




										if isinstance(self.unet.config.sample_size			,						lowerCAmelCase__       ):
													SCREAMING_SNAKE_CASE_		:     Any					=					(
													    batch_size,
													    self.unet.config.in_channels,
													    self.unet.config.sample_size,
													    self.unet.config.sample_size,
													)
										else:
													SCREAMING_SNAKE_CASE_		:     Dict					=					(batch_size, self.unet.config.in_channels, *self.unet.config.sample_size)

										if isinstance(lowerCAmelCase__			,						lowerCAmelCase__       ) and len(lowerCAmelCase__       ) != batch_size:
													raise ValueError(
													    F'''You have passed a list of generators of length {len(lowerCAmelCase__       )}, but requested an effective batch'''
													    F''' size of {batch_size}. Make sure the batch size matches the length of the generators.'''       )

										SCREAMING_SNAKE_CASE_		:     List[Any]					=					randn_tensor(lowerCAmelCase__			,						generator=lowerCAmelCase__			,						device=self.device			,						dtype=self.unet.dtype       )

										# set step values
										self.scheduler.set_timesteps(lowerCAmelCase__       )

										for t in self.progress_bar(self.scheduler.timesteps       ):
													# 1. predict noise model_output
													SCREAMING_SNAKE_CASE_		:     List[str]					=					self.unet(lowerCAmelCase__			,						lowerCAmelCase__       ).sample

													# 2. predict previous mean of image x_t-1 and add variance depending on eta
													# eta corresponds to η in paper and should be between [0, 1]
													# do x_t -> x_t-1
													SCREAMING_SNAKE_CASE_		:     List[str]					=					self.scheduler.step(
													    lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						eta=lowerCAmelCase__			,						use_clipped_model_output=lowerCAmelCase__			,						generator=lowerCAmelCase__       ).prev_sample

										SCREAMING_SNAKE_CASE_		:     List[Any]					=					(image / 2 + 0.5).clamp(0			,						1       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					image.cpu().permute(0			,						2			,						3			,						1       ).numpy()
										if output_type == "pil":
													SCREAMING_SNAKE_CASE_		:     str					=					self.numpy_to_pil(lowerCAmelCase__       )

										if not return_dict:
													return (image,)

										return ImagePipelineOutput(images=lowerCAmelCase__       )









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 						a__			(				A__				):
			if isinstance(A__,      collections.abc.Iterable				):
						return x
			return (x, x)







@require_flax
class 	__lowercase    :





							"""simple docstring"""







							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										pass






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										pass






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										pass






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




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






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





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase__			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     List[str]					=					FlaxVisionTextDualEncoderModel(lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Tuple					=					model(input_ids=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						attention_mask=lowerCAmelCase__       )

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





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					self.get_vision_text_model(lowerCAmelCase__			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     str					=					{'vision_model': vision_model, 'text_model': text_model}
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					model(input_ids=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						attention_mask=lowerCAmelCase__       )

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





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     int					=					self.get_vision_text_model(lowerCAmelCase__			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Dict					=					{'vision_model': vision_model, 'text_model': text_model}
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					model(input_ids=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						attention_mask=lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					output[0]

										with tempfile.TemporaryDirectory() as tmpdirname:
													model.save_pretrained(lowerCAmelCase__       )
													SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase__       )

													SCREAMING_SNAKE_CASE_		:     Tuple					=					model(input_ids=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						attention_mask=lowerCAmelCase__       )
													SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					after_output[0]
													SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					np.amax(np.abs(out_a - out_a       )       )
													self.assertLessEqual(lowerCAmelCase__			,						1E-3       )






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





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					self.get_vision_text_model(lowerCAmelCase__			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     List[str]					=					{'vision_model': vision_model, 'text_model': text_model}
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     List[str]					=					model(
										    input_ids=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						attention_mask=lowerCAmelCase__			,						output_attentions=lowerCAmelCase__       )

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

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

										SCREAMING_SNAKE_CASE_		:     Tuple					=					output.text_model_output.attentions
										self.assertEqual(len(lowerCAmelCase__       )			,						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			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										pt_model.to(lowerCAmelCase__       )
										pt_model.eval()

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

										with torch.no_grad():
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					pt_model(**lowerCAmelCase__       ).to_tuple()

										SCREAMING_SNAKE_CASE_		:     Dict					=					fx_model(**lowerCAmelCase__       ).to_tuple()
										self.assertEqual(len(lowerCAmelCase__       )			,						len(lowerCAmelCase__       )			,						'Output lengths differ between Flax and PyTorch'       )
										for fx_output, pt_output in zip(fx_outputs[:4]			,						pt_outputs[:4]       ):
													self.assert_almost_equals(lowerCAmelCase__			,						pt_output.numpy()			,						4E-2       )

										# PT -> Flax
										with tempfile.TemporaryDirectory() as tmpdirname:
													pt_model.save_pretrained(lowerCAmelCase__       )
													SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase__			,						from_pt=lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Dict					=					fx_model_loaded(**lowerCAmelCase__       ).to_tuple()
										self.assertEqual(len(lowerCAmelCase__       )			,						len(lowerCAmelCase__       )			,						'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(lowerCAmelCase__			,						pt_output.numpy()			,						4E-2       )

										# Flax -> PT
										with tempfile.TemporaryDirectory() as tmpdirname:
													fx_model.save_pretrained(lowerCAmelCase__       )
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					VisionTextDualEncoderModel.from_pretrained(lowerCAmelCase__			,						from_flax=lowerCAmelCase__       )

										pt_model_loaded.to(lowerCAmelCase__       )
										pt_model_loaded.eval()

										with torch.no_grad():
													SCREAMING_SNAKE_CASE_		:     Optional[int]					=					pt_model_loaded(**lowerCAmelCase__       ).to_tuple()

										self.assertEqual(len(lowerCAmelCase__       )			,						len(lowerCAmelCase__       )			,						'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(lowerCAmelCase__			,						pt_output_loaded.numpy()			,						4E-2       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[Any]					=					VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase__			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					VisionTextDualEncoderModel(lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					FlaxVisionTextDualEncoderModel(lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     str					=					convert_pytorch_state_dict_to_flax(pt_model.state_dict()			,						lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					fx_state

										self.check_pt_flax_equivalence(lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase__			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					VisionTextDualEncoderModel(lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     str					=					FlaxVisionTextDualEncoderModel(lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     int					=					load_flax_weights_in_pytorch_model(lowerCAmelCase__			,						fx_model.params       )

										self.check_pt_flax_equivalence(lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     str					=					self.prepare_config_and_inputs()
										self.check_model_from_pretrained_configs(**lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					self.prepare_config_and_inputs()
										self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					self.prepare_config_and_inputs()
										self.check_save_load(**lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					self.prepare_config_and_inputs()
										self.check_vision_text_output_attention(**lowerCAmelCase__       )






							@is_pt_flax_cross_test
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					self.prepare_config_and_inputs()
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					config_inputs_dict.pop('vision_config'       )
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					config_inputs_dict.pop('text_config'       )

										SCREAMING_SNAKE_CASE_		:     Any					=					config_inputs_dict

										self.check_equivalence_pt_to_flax(lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       )
										self.check_equivalence_flax_to_pt(lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       )






							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     Dict					=					self.get_pretrained_model_and_inputs()

										SCREAMING_SNAKE_CASE_		:     int					=					model_a(**lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					outputs[0]

										with tempfile.TemporaryDirectory() as tmp_dirname:
													model_a.save_pretrained(lowerCAmelCase__       )
													SCREAMING_SNAKE_CASE_		:     int					=					FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase__       )

													SCREAMING_SNAKE_CASE_		:     str					=					model_a(**lowerCAmelCase__       )
													SCREAMING_SNAKE_CASE_		:     Any					=					after_outputs[0]
													SCREAMING_SNAKE_CASE_		:     str					=					np.amax(np.abs(out_a - out_a       )       )
													self.assertLessEqual(lowerCAmelCase__			,						1E-5       )







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





							"""simple docstring"""







							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
										    'hf-internal-testing/tiny-random-vit'			,						'hf-internal-testing/tiny-bert'			,						vision_from_pt=lowerCAmelCase__			,						text_from_pt=lowerCAmelCase__			,						)
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					1_3
										SCREAMING_SNAKE_CASE_		:     str					=					floats_tensor(
										    [
										        batch_size,
										        model.config.vision_config.num_channels,
										        model.config.vision_config.image_size,
										        model.config.vision_config.image_size,
										    ]       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					ids_tensor([batch_size, 4]			,						model.config.text_config.vocab_size       )
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					random_attention_mask([batch_size, 4]       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					{'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

										return model, inputs






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     int					=					FlaxViTModel(lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					FlaxBertModel(lowerCAmelCase__       )
										return vision_model, text_model






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




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

										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     Any					=					vision_config_and_inputs

										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     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_torch
class 	__lowercase    (__SCREAMING_SNAKE_CASE	,       unittest.TestCase  ):





							"""simple docstring"""







							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     str					=					FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
										    'hf-internal-testing/tiny-random-clip'			,						'hf-internal-testing/tiny-bert'			,						vision_from_pt=lowerCAmelCase__			,						text_from_pt=lowerCAmelCase__			,						)
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					1_3
										SCREAMING_SNAKE_CASE_		:     Dict					=					floats_tensor(
										    [
										        batch_size,
										        model.config.vision_config.num_channels,
										        model.config.vision_config.image_size,
										        model.config.vision_config.image_size,
										    ]       )
										SCREAMING_SNAKE_CASE_		:     int					=					ids_tensor([batch_size, 4]			,						model.config.text_config.vocab_size       )
										SCREAMING_SNAKE_CASE_		:     Dict					=					random_attention_mask([batch_size, 4]       )
										SCREAMING_SNAKE_CASE_		:     int					=					{'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

										return model, inputs






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					FlaxCLIPVisionModel(lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Any					=					FlaxBertModel(lowerCAmelCase__       )
										return vision_model, text_model






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					FlaxCLIPVisionModelTester(self       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					FlaxBertModelTester(self       )
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					clip_model_tester.prepare_config_and_inputs()
										SCREAMING_SNAKE_CASE_		:     int					=					bert_model_tester.prepare_config_and_inputs()

										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     int					=					vision_config_and_inputs

										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     str					=					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 	__lowercase    (unittest.TestCase  ):





							"""simple docstring"""







							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[str]					=					FlaxVisionTextDualEncoderModel.from_pretrained('clip-italian/clip-italian'			,						logit_scale_init_value=1.0       )
										SCREAMING_SNAKE_CASE_		:     str					=					VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'       )

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

										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					model(**lowerCAmelCase__       )

										# 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])			,						)

										SCREAMING_SNAKE_CASE_		:     Tuple					=					np.array([[1.2_284_727, 0.3_104_122]]       )

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





