"""simple docstring"""



from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
   from transformers import (
       TFBertModel,
       TFCLIPVisionModel,
       TFDeiTModel,
       TFRobertaModel,
       TFVisionTextDualEncoderModel,
       TFViTModel,
       VisionTextDualEncoderConfig,
   )

if is_vision_available():
   from PIL import Image

   from transformers import VisionTextDualEncoderProcessor



def 					_lowerCamelCase(					a							):
       if isinstance(a ,  collections.abc.Iterable							):
              return x
       return (x, x)






@require_tf
class 				snake_case__     :


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase		):
        pass


 def 		a__ (				self		):
        pass


 def 		a__ (				self		):
        pass


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase=None  ,						**lowerCamelCase		):
        __a     				=  VisionTextDualEncoderConfig.from_vision_text_configs(lowerCamelCase  ,						lowerCamelCase		)

        __a     				=  TFVisionTextDualEncoderModel(lowerCamelCase		)

        __a     				=  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 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase=None  ,						**lowerCamelCase		):
        __a				, __a     				=  self.get_vision_text_model(lowerCamelCase  ,						lowerCamelCase		)
        __a     				=  TFVisionTextDualEncoderModel(vision_model=lowerCamelCase  ,						text_model=lowerCamelCase		)

        __a     				=  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 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase=None  ,						**lowerCamelCase		):
        __a				, __a     				=  self.get_vision_text_model(lowerCamelCase  ,						lowerCamelCase		)
        __a     				=  {"vision_model": vision_model, "text_model": text_model}
        __a     				=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCamelCase		)

        __a     				=  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 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase=None  ,						**lowerCamelCase		):
        __a				, __a     				=  self.get_vision_text_model(lowerCamelCase  ,						lowerCamelCase		)
        __a     				=  TFVisionTextDualEncoderModel(vision_model=lowerCamelCase  ,						text_model=lowerCamelCase		)

        __a     				=  model(input_ids=lowerCamelCase  ,						pixel_values=lowerCamelCase  ,						attention_mask=lowerCamelCase		)
        __a     				=  output[0].numpy()

        with tempfile.TemporaryDirectory() as tmpdirname:
               model.save_pretrained(lowerCamelCase		)
               __a     				=  TFVisionTextDualEncoderModel.from_pretrained(lowerCamelCase		)

               __a     				=  model(input_ids=lowerCamelCase  ,						pixel_values=lowerCamelCase  ,						attention_mask=lowerCamelCase		)
               __a     				=  after_output[0].numpy()
               __a     				=  np.amax(np.abs(out_a - out_a		)		)
               self.assertLessEqual(lowerCamelCase  ,						1E-5		)


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase=None  ,						**lowerCamelCase		):
        __a				, __a     				=  self.get_vision_text_model(lowerCamelCase  ,						lowerCamelCase		)
        __a     				=  TFVisionTextDualEncoderModel(vision_model=lowerCamelCase  ,						text_model=lowerCamelCase		)

        __a     				=  model(
            input_ids=lowerCamelCase  ,						pixel_values=lowerCamelCase  ,						attention_mask=lowerCamelCase  ,						output_attentions=lowerCamelCase		)

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

        __a     				=  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 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase		):
        __a     				=  np.abs((a - b)		).max()
        self.assertLessEqual(lowerCamelCase  ,						lowerCamelCase  ,						F"Difference between torch and flax is {diff} (>= {tol})."		)


 def 		a__ (				self		):
        __a     				=  self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_model(**lowerCamelCase		)


 def 		a__ (				self		):
        __a     				=  self.prepare_config_and_inputs()
        self.check_model_from_pretrained_configs(**lowerCamelCase		)


 def 		a__ (				self		):
        __a     				=  self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_from_pretrained(**lowerCamelCase		)


 def 		a__ (				self		):
        __a     				=  self.prepare_config_and_inputs()
        self.check_save_load(**lowerCamelCase		)


 def 		a__ (				self		):
        __a     				=  self.prepare_config_and_inputs()
        self.check_vision_text_output_attention(**lowerCamelCase		)






 @slow
 def 		a__ (				self		):
        __a				, __a     				=  self.get_pretrained_model_and_inputs()

        __a     				=  model_a(**lowerCamelCase		)
        __a     				=  outputs[0].numpy()

        with tempfile.TemporaryDirectory() as tmp_dirname:
               model_a.save_pretrained(lowerCamelCase		)
               __a     				=  TFVisionTextDualEncoderModel.from_pretrained(lowerCamelCase		)

               __a     				=  model_a(**lowerCamelCase		)
               __a     				=  after_outputs[0].numpy()
               __a     				=  np.amax(np.abs(out_a - out_a		)		)
               self.assertLessEqual(lowerCamelCase  ,						1E-5		)






@require_tf
class 				snake_case__     (   snake_case_,		unittest.TestCase   ):


 def 		a__ (				self		):
        __a     				=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
            "hf-internal-testing/tiny-random-vit"  ,						"hf-internal-testing/tiny-random-bert"		)
        __a     				=  13
        __a     				=  floats_tensor(
            [
                batch_size,
                model.vision_model.config.num_channels,
                model.vision_model.config.image_size,
                model.vision_model.config.image_size,
            ]		)
        __a     				=  ids_tensor([batch_size, 4]  ,						model.text_model.config.vocab_size		)
        __a     				=  random_attention_mask([batch_size, 4]		)
        __a     				=  {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

        return model, inputs


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase		):
        __a     				=  TFViTModel(lowerCamelCase  ,						name="vision_model"		)
        __a     				=  TFBertModel(lowerCamelCase  ,						name="text_model"		)
        return vision_model, text_model






 def 		a__ (				self		):
        __a     				=  TFViTModelTester(self		)
        __a     				=  TFBertModelTester(self		)
        __a     				=  vit_model_tester.prepare_config_and_inputs()
        __a     				=  bert_model_tester.prepare_config_and_inputs()

        __a				, __a				, __a     				=  vision_config_and_inputs

        (
            (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, 
        )     				=  text_config_and_inputs

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






@require_tf
class 				snake_case__     (   snake_case_,		unittest.TestCase   ):


 def 		a__ (				self		):
        # DeiT repo doesn't have TF weights, but we don't actually use the weights at all so let's
        # just reinitialize it.
        __a     				=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
            "Rocketknight1/tiny-random-deit-tf"  ,						"hf-internal-testing/tiny-random-roberta"		)
        __a     				=  13
        __a     				=  floats_tensor(
            [
                batch_size,
                model.vision_model.config.num_channels,
                model.vision_model.config.image_size,
                model.vision_model.config.image_size,
            ]		)
        __a     				=  ids_tensor([batch_size, 4]  ,						model.text_model.config.vocab_size		)
        __a     				=  random_attention_mask([batch_size, 4]		)
        __a     				=  {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

        return model, inputs


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase=None  ,						**lowerCamelCase		):
        __a				, __a     				=  self.get_vision_text_model(lowerCamelCase  ,						lowerCamelCase		)
        __a     				=  TFVisionTextDualEncoderModel(vision_model=lowerCamelCase  ,						text_model=lowerCamelCase		)

        __a     				=  model(
            input_ids=lowerCamelCase  ,						pixel_values=lowerCamelCase  ,						attention_mask=lowerCamelCase  ,						output_attentions=lowerCamelCase		)

        __a     				=  output.vision_model_output.attentions
        self.assertEqual(len(lowerCamelCase		)  ,						vision_config.num_hidden_layers		)

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

        __a     				=  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 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase		):
        __a     				=  TFDeiTModel(lowerCamelCase  ,						name="vision_model"		)
        __a     				=  TFRobertaModel(lowerCamelCase  ,						name="text_model"		)
        return vision_model, text_model






 def 		a__ (				self		):
        __a     				=  TFDeiTModelTester(self		)
        __a     				=  TFRobertaModelTester(self		)
        __a     				=  vit_model_tester.prepare_config_and_inputs()
        __a     				=  bert_model_tester.prepare_config_and_inputs()

        __a				, __a				, __a     				=  vision_config_and_inputs

        (
            (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, 
        )     				=  text_config_and_inputs

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






@require_tf
class 				snake_case__     (   snake_case_,		unittest.TestCase   ):


 def 		a__ (				self		):
        __a     				=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
            "Rocketknight1/tiny-random-clip-tf"  ,						"hf-internal-testing/tiny-random-bert"		)
        __a     				=  13
        __a     				=  floats_tensor(
            [
                batch_size,
                model.vision_model.config.num_channels,
                model.vision_model.config.image_size,
                model.vision_model.config.image_size,
            ]		)
        __a     				=  ids_tensor([batch_size, 4]  ,						model.text_model.config.vocab_size		)
        __a     				=  random_attention_mask([batch_size, 4]		)
        __a     				=  {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

        return model, inputs


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase		):
        __a     				=  TFCLIPVisionModel(lowerCamelCase  ,						name="vision_model"		)
        __a     				=  TFBertModel(lowerCamelCase  ,						name="text_model"		)
        return vision_model, text_model






 def 		a__ (				self		):
        __a     				=  TFCLIPVisionModelTester(self		)
        __a     				=  TFBertModelTester(self		)
        __a     				=  clip_model_tester.prepare_config_and_inputs()
        __a     				=  bert_model_tester.prepare_config_and_inputs()

        __a				, __a     				=  vision_config_and_inputs

        (
            (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, (
            __a
        )				, 
        )     				=  text_config_and_inputs

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






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


 @slow
 def 		a__ (				self		):
        __a     				=  TFVisionTextDualEncoderModel.from_pretrained(
            "clip-italian/clip-italian"  ,						logit_scale_init_value=1.0  ,						from_pt=lowerCamelCase		)
        __a     				=  VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian"		)

        __a     				=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
        __a     				=  processor(
            text=["una foto di un gatto", "una foto di un cane"]  ,						images=lowerCamelCase  ,						padding=lowerCamelCase  ,						return_tensors="np"		)

        __a     				=  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])  ,						)

        __a     				=  np.array([[1.228_4727, 0.310_4122]]		)

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

"""simple docstring"""



import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
   from .tokenization_camembert import CamembertTokenizer
else:
   SCREAMING_SNAKE_CASE__:List[Any]			=	None


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

SCREAMING_SNAKE_CASE__:Tuple			=	{"""vocab_file""": """sentencepiece.bpe.model""", """tokenizer_file""": """tokenizer.json"""}

SCREAMING_SNAKE_CASE__:Optional[int]			=	{
    """vocab_file""": {
        """camembert-base""": """https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model""",
    },
    """tokenizer_file""": {
        """camembert-base""": """https://huggingface.co/camembert-base/resolve/main/tokenizer.json""",
    },
}

SCREAMING_SNAKE_CASE__:List[str]			=	{
    """camembert-base""": 512,
}

SCREAMING_SNAKE_CASE__:str			=	"""▁"""






class 				snake_case__     (   snake_case_   ):
 _snake_case   :			List[Any]           =		VOCAB_FILES_NAMES
 _snake_case   :			List[Any]           =		PRETRAINED_VOCAB_FILES_MAP
 _snake_case   :			Optional[int]           =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 _snake_case   :			Any           =		["""input_ids""", """attention_mask"""]
 _snake_case   :			str           =		CamembertTokenizer


 def __init__(				self  ,						lowerCamelCase=None  ,						lowerCamelCase=None  ,						lowerCamelCase="<s>"  ,						lowerCamelCase="</s>"  ,						lowerCamelCase="</s>"  ,						lowerCamelCase="<s>"  ,						lowerCamelCase="<unk>"  ,						lowerCamelCase="<pad>"  ,						lowerCamelCase="<mask>"  ,						lowerCamelCase=["<s>NOTUSED", "</s>NOTUSED"]  ,						**lowerCamelCase  ,						):
        # Mask token behave like a normal word, i.e. include the space before it
        __a     				=  AddedToken(lowerCamelCase  ,						lstrip=lowerCamelCase  ,						rstrip=lowerCamelCase		) if isinstance(lowerCamelCase  ,						lowerCamelCase		) else mask_token

        super().__init__(
            lowerCamelCase  ,						tokenizer_file=lowerCamelCase  ,						bos_token=lowerCamelCase  ,						eos_token=lowerCamelCase  ,						sep_token=lowerCamelCase  ,						cls_token=lowerCamelCase  ,						unk_token=lowerCamelCase  ,						pad_token=lowerCamelCase  ,						mask_token=lowerCamelCase  ,						additional_special_tokens=lowerCamelCase  ,						**lowerCamelCase  ,						)

        __a     				=  vocab_file
        __a     				=  False if not self.vocab_file else True


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase = None		):

        if token_ids_a is None:
               return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
        __a     				=  [self.cls_token_id]
        __a     				=  [self.sep_token_id]
        return cls + token_ids_a + sep + sep + token_ids_a + sep


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase = None		):
        __a     				=  [self.sep_token_id]
        __a     				=  [self.cls_token_id]

        if token_ids_a is None:
               return len(cls + token_ids_a + sep		) * [0]
        return len(cls + token_ids_a + sep + sep + token_ids_a + sep		) * [0]






 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase = None		):
        if not self.can_save_slow_tokenizer:
               raise ValueError(
                   "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow "
                   "tokenizer."		)

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

        if os.path.abspath(self.vocab_file		) != os.path.abspath(lowerCamelCase		):
               copyfile(self.vocab_file  ,						lowerCamelCase		)

        return (out_vocab_file,)
