

'''simple docstring'''



import json
import os
from typing import Optional, Tuple

import regex as re

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_UpperCamelCase						:  Union[str, Any]							       =				logging.get_logger(__name__)

_UpperCamelCase						:  List[str]							       =				{
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
}

_UpperCamelCase						:  List[Any]							       =				{
    'vocab_file': {'ctrl': 'https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-vocab.json'},
    'merges_file': {'ctrl': 'https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-merges.txt'},
}

_UpperCamelCase						:  List[str]							       =				{
    'ctrl': 256,
}

_UpperCamelCase						:  Optional[int]							       =				{
    'Pregnancy': 16_8629,
    'Christianity': 7675,
    'Explain': 10_6423,
    'Fitness': 6_3440,
    'Saving': 6_3163,
    'Ask': 2_7171,
    'Ass': 9_5985,
    'Joke': 16_3509,
    'Questions': 4_5622,
    'Thoughts': 4_9605,
    'Retail': 5_2342,
    'Feminism': 16_4338,
    'Writing': 1_1992,
    'Atheism': 19_2263,
    'Netflix': 4_8616,
    'Computing': 3_9639,
    'Opinion': 4_3213,
    'Alone': 4_4967,
    'Funny': 5_8917,
    'Gaming': 4_0358,
    'Human': 4088,
    'India': 1331,
    'Joker': 7_7138,
    'Diet': 3_6206,
    'Legal': 1_1859,
    'Norman': 4939,
    'Tip': 7_2689,
    'Weight': 5_2343,
    'Movies': 4_6273,
    'Running': 2_3425,
    'Science': 2090,
    'Horror': 3_7793,
    'Confession': 6_0572,
    'Finance': 1_2250,
    'Politics': 1_6360,
    'Scary': 19_1985,
    'Support': 1_2654,
    'Technologies': 3_2516,
    'Teenage': 6_6160,
    'Event': 3_2769,
    'Learned': 6_7460,
    'Notion': 18_2770,
    'Wikipedia': 3_7583,
    'Books': 6665,
    'Extract': 7_6050,
    'Confessions': 10_2701,
    'Conspiracy': 7_5932,
    'Links': 6_3674,
    'Narcissus': 15_0425,
    'Relationship': 5_4766,
    'Relationships': 13_4796,
    'Reviews': 4_1671,
    'News': 4256,
    'Translation': 2_6820,
    'multilingual': 12_8406,
}


def       snake_case   (	snake_case      : Any			) ->  Tuple:


    """simple docstring"""


    lowerCAmelCase 						=							set()
    lowerCAmelCase 						=							word[0]
    for char in word[1:]:
        pairs.add((prev_char, char)			)
        lowerCAmelCase 						=							char

    lowerCAmelCase 						=							set(A__			)
    return pairs



class 				_snake_case					(   __SCREAMING_SNAKE_CASE			):
  SCREAMING_SNAKE_CASE			:       Dict				=     VOCAB_FILES_NAMES
  SCREAMING_SNAKE_CASE			:       Optional[Any]				=     PRETRAINED_VOCAB_FILES_MAP
  SCREAMING_SNAKE_CASE			:       Optional[int]				=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  SCREAMING_SNAKE_CASE			:       Dict				=     CONTROL_CODES

  def __init__(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE="<unk>"      , **_SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      super().__init__(unk_token=lowerCAmelCase__      , **lowerCAmelCase__	)

      with open(lowerCAmelCase__      , encoding='utf-8'	) as vocab_handle:
          lowerCAmelCase 						=							json.load(lowerCAmelCase__	)
      lowerCAmelCase 						=							{v: k for k, v in self.encoder.items()}
      with open(lowerCAmelCase__      , encoding='utf-8'	) as merges_handle:
          lowerCAmelCase 						=							merges_handle.read().split('\n'	)[1:-1]
      lowerCAmelCase 						=							[tuple(merge.split()	) for merge in merges]
      lowerCAmelCase 						=							dict(zip(lowerCAmelCase__      , range(len(lowerCAmelCase__	)	)	)	)
      lowerCAmelCase 						=							{}

  @property
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return len(self.encoder	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return dict(self.encoder      , **self.added_tokens_encoder	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      if token in self.cache:
          return self.cache[token]
      lowerCAmelCase 						=							tuple(lowerCAmelCase__	)
      lowerCAmelCase 						=							tuple(list(word[:-1]	) + [word[-1] + '</w>']	)
      lowerCAmelCase 						=							get_pairs(lowerCAmelCase__	)

      if not pairs:
          return token

      while True:
          lowerCAmelCase 						=							min(lowerCAmelCase__      , key=lambda _SCREAMING_SNAKE_CASE	: self.bpe_ranks.get(lowerCAmelCase__      , float('inf'	)	)	)
          if bigram not in self.bpe_ranks:
              break
          lowerCAmelCase 						=							bigram
          lowerCAmelCase 						=							[]
          lowerCAmelCase 						=							0
          while i < len(lowerCAmelCase__	):
              try:
                  lowerCAmelCase 						=							word.index(lowerCAmelCase__      , lowerCAmelCase__	)
              except ValueError:
                  new_word.extend(word[i:]	)
                  break
              else:
                  new_word.extend(word[i:j]	)
                  lowerCAmelCase 						=							j

              if word[i] == first and i < len(lowerCAmelCase__	) - 1 and word[i + 1] == second:
                  new_word.append(first + second	)
                  i += 2
              else:
                  new_word.append(word[i]	)
                  i += 1
          lowerCAmelCase 						=							tuple(lowerCAmelCase__	)
          lowerCAmelCase 						=							new_word
          if len(lowerCAmelCase__	) == 1:
              break
          else:
              lowerCAmelCase 						=							get_pairs(lowerCAmelCase__	)
      lowerCAmelCase 						=							'@@ '.join(lowerCAmelCase__	)
      lowerCAmelCase 						=							word[:-4]
      lowerCAmelCase 						=							word
      return word

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							[]

      lowerCAmelCase 						=							re.findall(R'\S+\n?'      , lowerCAmelCase__	)

      for token in words:
          split_tokens.extend(list(self.bpe(lowerCAmelCase__	).split(' '	)	)	)
      return split_tokens

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      return self.encoder.get(lowerCAmelCase__      , self.encoder.get(self.unk_token	)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      return self.decoder.get(lowerCAmelCase__      , self.unk_token	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							' '.join(lowerCAmelCase__	).replace('@@ '      , ''	).strip()
      return out_string


  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE = None	):





      '''simple docstring'''

      if not os.path.isdir(lowerCAmelCase__	):
          logger.error(F'Vocabulary path ({save_directory}) should be a directory'	)
          return
      lowerCAmelCase 						=							os.path.join(
          lowerCAmelCase__      , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']	)
      lowerCAmelCase 						=							os.path.join(
          lowerCAmelCase__      , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file']	)

      with open(lowerCAmelCase__      , 'w'      , encoding='utf-8'	) as f:
          f.write(json.dumps(self.encoder      , indent=2      , sort_keys=lowerCAmelCase__      , ensure_ascii=lowerCAmelCase__	) + '\n'	)

      lowerCAmelCase 						=							0
      with open(lowerCAmelCase__      , 'w'      , encoding='utf-8'	) as writer:
          writer.write('#version: 0.2\n'	)
          for bpe_tokens, token_index in sorted(self.bpe_ranks.items()      , key=lambda _SCREAMING_SNAKE_CASE	: kv[1]	):
              if index != token_index:
                  logger.warning(
                      F'Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.'
                      ' Please check that the tokenizer is not corrupted!'	)
                  lowerCAmelCase 						=							token_index
              writer.write(' '.join(lowerCAmelCase__	) + '\n'	)
              index += 1

      return vocab_file, merge_file

  # def decode(self, token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=True):
  #     filtered_tokens = ' '.join(self.convert_ids_to_tokens(token_ids, skip_special_tokens=skip_special_tokens))
  #     tokens_generated_so_far = re.sub('(@@ )', '', string=filtered_tokens)
  #     tokens_generated_so_far = re.sub('(@@ ?$)', '', string=tokens_generated_so_far)
  #     return ''.join(tokens_generated_so_far)









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       )       )





