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     lowerCAmelCase_			(   _SCREAMING_SNAKE_CASE     :				Optional[Any]			):
						if isinstance(_SCREAMING_SNAKE_CASE		, collections.abc.Iterable			):
												return x
						return (x, x)

@require_flax
class        UpperCamelCase						:





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase						)				->				Union[str, Any]:
												pass





						def        A_							(self						)				->				List[str]:
												pass





						def        A_							(self						)				->				Optional[int]:
												pass





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





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase=None			,     **__UpperCamelCase						)				->				Tuple:
												UpperCamelCase_		:							List[str]  =		VisionTextDualEncoderConfig.from_vision_text_configs(__UpperCamelCase			,     __UpperCamelCase						)

												UpperCamelCase_		:							Any  =		FlaxVisionTextDualEncoderModel(__UpperCamelCase						)

												UpperCamelCase_		:							Dict  =		model(input_ids=__UpperCamelCase			,     pixel_values=__UpperCamelCase			,     attention_mask=__UpperCamelCase						)

												self.assertEqual(output["""text_embeds"""].shape			,     (input_ids.shape[0], config.projection_dim)						)
												self.assertEqual(output["""image_embeds"""].shape			,     (pixel_values.shape[0], config.projection_dim)						)





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase=None			,     **__UpperCamelCase						)				->				List[str]:
												UpperCamelCase_,UpperCamelCase_		:							Optional[Any]  =		self.get_vision_text_model(__UpperCamelCase			,     __UpperCamelCase						)
												UpperCamelCase_		:							Union[str, Any]  =		{"""vision_model""": vision_model, """text_model""": text_model}
												UpperCamelCase_		:							str  =		FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__UpperCamelCase						)

												UpperCamelCase_		:							Tuple  =		model(input_ids=__UpperCamelCase			,     pixel_values=__UpperCamelCase			,     attention_mask=__UpperCamelCase						)

												self.assertEqual(output["""text_embeds"""].shape			,     (input_ids.shape[0], model.config.projection_dim)						)
												self.assertEqual(output["""image_embeds"""].shape			,     (pixel_values.shape[0], model.config.projection_dim)						)





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase=None			,     **__UpperCamelCase						)				->				Optional[int]:
												UpperCamelCase_,UpperCamelCase_		:							Dict  =		self.get_vision_text_model(__UpperCamelCase			,     __UpperCamelCase						)
												UpperCamelCase_		:							Optional[Any]  =		{"""vision_model""": vision_model, """text_model""": text_model}
												UpperCamelCase_		:							List[Any]  =		FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__UpperCamelCase						)

												UpperCamelCase_		:							str  =		model(input_ids=__UpperCamelCase			,     pixel_values=__UpperCamelCase			,     attention_mask=__UpperCamelCase						)
												UpperCamelCase_		:							Dict  =		output[0]

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

																		UpperCamelCase_		:							List[str]  =		model(input_ids=__UpperCamelCase			,     pixel_values=__UpperCamelCase			,     attention_mask=__UpperCamelCase						)
																		UpperCamelCase_		:							Optional[int]  =		after_output[0]
																		UpperCamelCase_		:							Dict  =		np.amax(np.abs(out_a - out_a						)						)
																		self.assertLessEqual(__UpperCamelCase			,     1E-3						)





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase=None			,     **__UpperCamelCase						)				->				List[str]:
												UpperCamelCase_,UpperCamelCase_		:							Dict  =		self.get_vision_text_model(__UpperCamelCase			,     __UpperCamelCase						)
												UpperCamelCase_		:							Optional[int]  =		{"""vision_model""": vision_model, """text_model""": text_model}
												UpperCamelCase_		:							Tuple  =		FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__UpperCamelCase						)

												UpperCamelCase_		:							Tuple  =		model(
												    input_ids=__UpperCamelCase			,     pixel_values=__UpperCamelCase			,     attention_mask=__UpperCamelCase			,     output_attentions=__UpperCamelCase						)

												UpperCamelCase_		:							Any  =		output.vision_model_output.attentions
												self.assertEqual(len(__UpperCamelCase						)			,     vision_config.num_hidden_layers						)

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

												UpperCamelCase_		:							List[str]  =		output.text_model_output.attentions
												self.assertEqual(len(__UpperCamelCase						)			,     text_config.num_hidden_layers						)

												self.assertEqual(
												    text_attentions[0].shape[-3:]			,     (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])			,     )





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				Optional[int]:
												pt_model.to(__UpperCamelCase						)
												pt_model.eval()

												# prepare inputs
												UpperCamelCase_		:							Tuple  =		inputs_dict
												UpperCamelCase_		:							Tuple  =		{k: torch.tensor(v.tolist()						) for k, v in flax_inputs.items()}

												with torch.no_grad():
																		UpperCamelCase_		:							Any  =		pt_model(**__UpperCamelCase						).to_tuple()

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

												# PT -> Flax
												with tempfile.TemporaryDirectory() as tmpdirname:
																		pt_model.save_pretrained(__UpperCamelCase						)
																		UpperCamelCase_		:							List[Any]  =		FlaxVisionTextDualEncoderModel.from_pretrained(__UpperCamelCase			,     from_pt=__UpperCamelCase						)

												UpperCamelCase_		:							List[str]  =		fx_model_loaded(**__UpperCamelCase						).to_tuple()
												self.assertEqual(len(__UpperCamelCase						)			,     len(__UpperCamelCase						)			,     """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(__UpperCamelCase			,     pt_output.numpy()			,     4E-2						)

												# Flax -> PT
												with tempfile.TemporaryDirectory() as tmpdirname:
																		fx_model.save_pretrained(__UpperCamelCase						)
																		UpperCamelCase_		:							Any  =		VisionTextDualEncoderModel.from_pretrained(__UpperCamelCase			,     from_flax=__UpperCamelCase						)

												pt_model_loaded.to(__UpperCamelCase						)
												pt_model_loaded.eval()

												with torch.no_grad():
																		UpperCamelCase_		:							Tuple  =		pt_model_loaded(**__UpperCamelCase						).to_tuple()

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





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				Dict:
												UpperCamelCase_		:							List[str]  =		VisionTextDualEncoderConfig.from_vision_text_configs(__UpperCamelCase			,     __UpperCamelCase						)

												UpperCamelCase_		:							Dict  =		VisionTextDualEncoderModel(__UpperCamelCase						)
												UpperCamelCase_		:							List[str]  =		FlaxVisionTextDualEncoderModel(__UpperCamelCase						)

												UpperCamelCase_		:							Dict  =		convert_pytorch_state_dict_to_flax(pt_model.state_dict()			,     __UpperCamelCase						)
												UpperCamelCase_		:							Dict  =		fx_state

												self.check_pt_flax_equivalence(__UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				Any:
												UpperCamelCase_		:							Optional[Any]  =		VisionTextDualEncoderConfig.from_vision_text_configs(__UpperCamelCase			,     __UpperCamelCase						)

												UpperCamelCase_		:							List[Any]  =		VisionTextDualEncoderModel(__UpperCamelCase						)
												UpperCamelCase_		:							Union[str, Any]  =		FlaxVisionTextDualEncoderModel(__UpperCamelCase						)

												UpperCamelCase_		:							List[Any]  =		load_flax_weights_in_pytorch_model(__UpperCamelCase			,     fx_model.params						)

												self.check_pt_flax_equivalence(__UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)





						def        A_							(self						)				->				Optional[Any]:
												UpperCamelCase_		:							List[str]  =		self.prepare_config_and_inputs()
												self.check_model_from_pretrained_configs(**__UpperCamelCase						)





						def        A_							(self						)				->				int:
												UpperCamelCase_		:							Any  =		self.prepare_config_and_inputs()
												self.check_vision_text_dual_encoder_from_pretrained(**__UpperCamelCase						)





						def        A_							(self						)				->				Dict:
												UpperCamelCase_		:							Dict  =		self.prepare_config_and_inputs()
												self.check_save_load(**__UpperCamelCase						)





						def        A_							(self						)				->				List[str]:
												UpperCamelCase_		:							Union[str, Any]  =		self.prepare_config_and_inputs()
												self.check_vision_text_output_attention(**__UpperCamelCase						)





						@is_pt_flax_cross_test
						def        A_							(self						)				->				Optional[Any]:
												UpperCamelCase_		:							List[Any]  =		self.prepare_config_and_inputs()
												UpperCamelCase_		:							Optional[int]  =		config_inputs_dict.pop("""vision_config"""						)
												UpperCamelCase_		:							Optional[int]  =		config_inputs_dict.pop("""text_config"""						)

												UpperCamelCase_		:							Tuple  =		config_inputs_dict

												self.check_equivalence_pt_to_flax(__UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)
												self.check_equivalence_flax_to_pt(__UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)





						@slow
						def        A_							(self						)				->				List[Any]:
												UpperCamelCase_,UpperCamelCase_		:							Any  =		self.get_pretrained_model_and_inputs()

												UpperCamelCase_		:							List[Any]  =		model_a(**__UpperCamelCase						)
												UpperCamelCase_		:							Tuple  =		outputs[0]

												with tempfile.TemporaryDirectory() as tmp_dirname:
																		model_a.save_pretrained(__UpperCamelCase						)
																		UpperCamelCase_		:							Tuple  =		FlaxVisionTextDualEncoderModel.from_pretrained(__UpperCamelCase						)

																		UpperCamelCase_		:							Optional[int]  =		model_a(**__UpperCamelCase						)
																		UpperCamelCase_		:							Optional[Any]  =		after_outputs[0]
																		UpperCamelCase_		:							Tuple  =		np.amax(np.abs(out_a - out_a						)						)
																		self.assertLessEqual(__UpperCamelCase			,     1E-5						)


@require_flax
class        UpperCamelCase						(				__a	,	unittest.TestCase				):





						def        A_							(self						)				->				Any:
												UpperCamelCase_		:							Any  =		FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
												    """hf-internal-testing/tiny-random-vit"""			,     """hf-internal-testing/tiny-bert"""			,     vision_from_pt=__UpperCamelCase			,     text_from_pt=__UpperCamelCase			,     )
												UpperCamelCase_		:							int  =		13
												UpperCamelCase_		:							Tuple  =		floats_tensor(
												    [
												        batch_size,
												        model.config.vision_config.num_channels,
												        model.config.vision_config.image_size,
												        model.config.vision_config.image_size,
												    ]						)
												UpperCamelCase_		:							int  =		ids_tensor([batch_size, 4]			,     model.config.text_config.vocab_size						)
												UpperCamelCase_		:							Union[str, Any]  =		random_attention_mask([batch_size, 4]						)
												UpperCamelCase_		:							Dict  =		{"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

												return model, inputs





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase						)				->				Dict:
												UpperCamelCase_		:							Optional[int]  =		FlaxViTModel(__UpperCamelCase						)
												UpperCamelCase_		:							str  =		FlaxBertModel(__UpperCamelCase						)
												return vision_model, text_model





						def        A_							(self						)				->				Dict:
												UpperCamelCase_		:							Dict  =		FlaxViTModelTester(self						)
												UpperCamelCase_		:							Optional[int]  =		FlaxBertModelTester(self						)
												UpperCamelCase_		:							Optional[int]  =		vit_model_tester.prepare_config_and_inputs()
												UpperCamelCase_		:							Dict  =		bert_model_tester.prepare_config_and_inputs()

												UpperCamelCase_,UpperCamelCase_		:							Optional[int]  =		vision_config_and_inputs

												UpperCamelCase_,UpperCamelCase_,UpperCamelCase_,UpperCamelCase_		:							Optional[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        UpperCamelCase						(				__a	,	unittest.TestCase				):





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

												return model, inputs





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase						)				->				List[Any]:
												UpperCamelCase_		:							Union[str, Any]  =		FlaxCLIPVisionModel(__UpperCamelCase						)
												UpperCamelCase_		:							List[Any]  =		FlaxBertModel(__UpperCamelCase						)
												return vision_model, text_model





						def        A_							(self						)				->				Any:
												UpperCamelCase_		:							Optional[Any]  =		FlaxCLIPVisionModelTester(self						)
												UpperCamelCase_		:							Dict  =		FlaxBertModelTester(self						)
												UpperCamelCase_		:							Tuple  =		clip_model_tester.prepare_config_and_inputs()
												UpperCamelCase_		:							Union[str, Any]  =		bert_model_tester.prepare_config_and_inputs()

												UpperCamelCase_,UpperCamelCase_		:							Dict  =		vision_config_and_inputs

												UpperCamelCase_,UpperCamelCase_,UpperCamelCase_,UpperCamelCase_		:							List[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        UpperCamelCase						(				unittest.TestCase				):





						@slow
						def        A_							(self						)				->				Union[str, Any]:
												UpperCamelCase_		:							List[Any]  =		FlaxVisionTextDualEncoderModel.from_pretrained("""clip-italian/clip-italian"""			,     logit_scale_init_value=1.0						)
												UpperCamelCase_		:							Union[str, Any]  =		VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian"""						)

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

												UpperCamelCase_		:							Optional[Any]  =		model(**__UpperCamelCase						)

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

												UpperCamelCase_		:							int  =		np.array([[1.2_284_727, 0.3_104_122]]						)

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







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

import sentencepiece as spm

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


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


SCREAMING_SNAKE_CASE		:     Union[str, Any]         =	"▁"

SCREAMING_SNAKE_CASE		:     Dict         =	{"vocab_file": "spiece.model"}

SCREAMING_SNAKE_CASE		:     List[Any]         =	{
    "vocab_file": {
        "google/reformer-crime-and-punishment": (
            "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model"
        )
    }
}

SCREAMING_SNAKE_CASE		:     Any         =	{
    "google/reformer-crime-and-punishment": 524288,
}

class        UpperCamelCase						(				__a				):
						a__		:Optional[Any]      =			VOCAB_FILES_NAMES
						a__		:List[Any]      =			PRETRAINED_VOCAB_FILES_MAP
						a__		:Union[str, Any]      =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						a__		:List[Any]      =			['''input_ids''', '''attention_mask''']





						def __init__(self			,     __UpperCamelCase			,     __UpperCamelCase="</s>"			,     __UpperCamelCase="<unk>"			,     __UpperCamelCase=[]			,     __UpperCamelCase = None			,     **__UpperCamelCase			,     )				->				None:
												UpperCamelCase_		:							List[str]  =		{} if sp_model_kwargs is None else sp_model_kwargs

												super().__init__(
												    eos_token=__UpperCamelCase			,     unk_token=__UpperCamelCase			,     additional_special_tokens=__UpperCamelCase			,     sp_model_kwargs=self.sp_model_kwargs			,     **__UpperCamelCase			,     )

												UpperCamelCase_		:							int  =		vocab_file
												UpperCamelCase_		:							str  =		spm.SentencePieceProcessor(**self.sp_model_kwargs						)
												self.sp_model.Load(__UpperCamelCase						)





						@property
						def        A_							(self						)				->				Dict:
												return self.sp_model.get_piece_size()





						def        A_							(self						)				->				Dict[str, int]:
												UpperCamelCase_		:							str  =		{self.convert_ids_to_tokens(__UpperCamelCase						): i for i in range(self.vocab_size						)}
												vocab.update(self.added_tokens_encoder						)
												return vocab





						def __getstate__(self						)				->				Any:
												UpperCamelCase_		:							Dict  =		self.__dict__.copy()
												UpperCamelCase_		:							List[str]  =		None
												return state





						def __setstate__(self			,     __UpperCamelCase						)				->				List[Any]:
												UpperCamelCase_		:							List[Any]  =		d

												# for backward compatibility
												if not hasattr(self			,     """sp_model_kwargs"""						):
																		UpperCamelCase_		:							Any  =		{}

												UpperCamelCase_		:							Dict  =		spm.SentencePieceProcessor(**self.sp_model_kwargs						)
												self.sp_model.Load(self.vocab_file						)





						def        A_							(self			,     __UpperCamelCase						)				->				List[str]:
												return self.sp_model.encode(__UpperCamelCase			,     out_type=__UpperCamelCase						)





						def        A_							(self			,     __UpperCamelCase						)				->				Union[str, Any]:
												return self.sp_model.piece_to_id(__UpperCamelCase						)





						def        A_							(self			,     __UpperCamelCase						)				->				Dict:
												if index < self.sp_model.get_piece_size():
																		UpperCamelCase_		:							Union[str, Any]  =		self.sp_model.IdToPiece(__UpperCamelCase						)
												return token





						def        A_							(self			,     __UpperCamelCase						)				->				int:
												UpperCamelCase_		:							Dict  =		[]
												UpperCamelCase_		:							Union[str, Any]  =		""""""
												for token in tokens:
																		# make sure that special tokens are not decoded using sentencepiece model
																		if token in self.all_special_tokens:
																								out_string += self.sp_model.decode(__UpperCamelCase						) + token
																								UpperCamelCase_		:							Optional[Any]  =		[]
																		else:
																								current_sub_tokens.append(__UpperCamelCase						)
												out_string += self.sp_model.decode(__UpperCamelCase						)
												return out_string.strip()





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase = None						)				->				Tuple[str]:
												if not os.path.isdir(__UpperCamelCase						):
																		logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''						)
																		return
												UpperCamelCase_		:							Tuple  =		os.path.join(
												    __UpperCamelCase			,     (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]						)

												if os.path.abspath(self.vocab_file						) != os.path.abspath(__UpperCamelCase						) and os.path.isfile(self.vocab_file						):
																		copyfile(self.vocab_file			,     __UpperCamelCase						)
												elif not os.path.isfile(self.vocab_file						):
																		with open(__UpperCamelCase			,     """wb"""						) as fi:
																								UpperCamelCase_		:							int  =		self.sp_model.serialized_model_proto()
																								fi.write(__UpperCamelCase						)

												return (out_vocab_file,)






