






"""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		(							UpperCAmelCase__       :					Optional[int]      )		->str:
						if isinstance(UpperCAmelCase__,						collections.abc.Iterable      ):
												return x
						return (x, x)






@require_tf
class 							__SCREAMING_SNAKE_CASE						:


							def 						_UpperCamelCase       (			self      :						List[Any]			,    snake_case      :						List[Any]			,    snake_case      :						Any							):
													'''simple docstring'''





													pass


							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													pass


							def 						_UpperCamelCase       (			self      :						str							):
													'''simple docstring'''





													pass


							def 						_UpperCamelCase       (			self      :						Union[str, Any]			,    snake_case      :						str			,    snake_case      :						int			,    snake_case      :						int			,    snake_case      :						List[Any]			,    snake_case      :						str=None			,    **snake_case      :						Union[str, Any]							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  VisionTextDualEncoderConfig.from_vision_text_configs(snake_case			,    snake_case							)

													A__						:  List[Any]		  =  TFVisionTextDualEncoderModel(snake_case							)

													A__						:  Optional[int]		  =  model(input_ids=snake_case			,    pixel_values=snake_case			,    attention_mask=snake_case							)

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


							def 						_UpperCamelCase       (			self      :						int			,    snake_case      :						List[str]			,    snake_case      :						Optional[int]			,    snake_case      :						Dict			,    snake_case      :						Optional[int]			,    snake_case      :						Any=None			,    **snake_case      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  Tuple		  =  self.get_vision_text_model(snake_case			,    snake_case							)
													A__						:  Union[str, Any]		  =  TFVisionTextDualEncoderModel(vision_model=snake_case			,    text_model=snake_case							)

													A__						:  Dict		  =  model(input_ids=snake_case			,    pixel_values=snake_case			,    attention_mask=snake_case							)

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


							def 						_UpperCamelCase       (			self      :						Dict			,    snake_case      :						Dict			,    snake_case      :						Union[str, Any]			,    snake_case      :						Tuple			,    snake_case      :						str			,    snake_case      :						Union[str, Any]=None			,    **snake_case      :						Tuple							):
													'''simple docstring'''





													A__						:  Optional[int]		  =  self.get_vision_text_model(snake_case			,    snake_case							)
													A__						:  Tuple		  =  {"""vision_model""": vision_model, """text_model""": text_model}
													A__						:  Dict		  =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(**snake_case							)

													A__						:  int		  =  model(input_ids=snake_case			,    pixel_values=snake_case			,    attention_mask=snake_case							)

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


							def 						_UpperCamelCase       (			self      :						List[Any]			,    snake_case      :						Optional[int]			,    snake_case      :						int			,    snake_case      :						Any			,    snake_case      :						List[str]			,    snake_case      :						Optional[int]=None			,    **snake_case      :						Tuple							):
													'''simple docstring'''





													A__						:  Tuple		  =  self.get_vision_text_model(snake_case			,    snake_case							)
													A__						:  Dict		  =  TFVisionTextDualEncoderModel(vision_model=snake_case			,    text_model=snake_case							)

													A__						:  List[Any]		  =  model(input_ids=snake_case			,    pixel_values=snake_case			,    attention_mask=snake_case							)
													A__						:  Union[str, Any]		  =  output[0].numpy()

													with tempfile.TemporaryDirectory() as tmpdirname:
																			model.save_pretrained(snake_case							)
																			A__						:  Any		  =  TFVisionTextDualEncoderModel.from_pretrained(snake_case							)

																			A__						:  int		  =  model(input_ids=snake_case			,    pixel_values=snake_case			,    attention_mask=snake_case							)
																			A__						:  Union[str, Any]		  =  after_output[0].numpy()
																			A__						:  Tuple		  =  np.amax(np.abs(out_a - out_a							)							)
																			self.assertLessEqual(snake_case			,    1e-5							)


							def 						_UpperCamelCase       (			self      :						List[str]			,    snake_case      :						Any			,    snake_case      :						Any			,    snake_case      :						int			,    snake_case      :						Dict			,    snake_case      :						Optional[int]=None			,    **snake_case      :						List[Any]							):
													'''simple docstring'''





													A__						:  List[str]		  =  self.get_vision_text_model(snake_case			,    snake_case							)
													A__						:  Optional[Any]		  =  TFVisionTextDualEncoderModel(vision_model=snake_case			,    text_model=snake_case							)

													A__						:  Tuple		  =  model(
													    input_ids=snake_case			,    pixel_values=snake_case			,    attention_mask=snake_case			,    output_attentions=snake_case							)

													A__						:  Optional[int]		  =  output.vision_model_output.attentions
													self.assertEqual(len(snake_case							)			,    vision_config.num_hidden_layers							)

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

													A__						:  Tuple		  =  output.text_model_output.attentions
													self.assertEqual(len(snake_case							)			,    text_config.num_hidden_layers							)

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


							def 						_UpperCamelCase       (			self      :						str			,    snake_case      :						np.ndarray			,    snake_case      :						np.ndarray			,    snake_case      :						float							):
													'''simple docstring'''





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


							def 						_UpperCamelCase       (			self      :						Optional[int]							):
													'''simple docstring'''





													A__						:  Tuple		  =  self.prepare_config_and_inputs()
													self.check_vision_text_dual_encoder_model(**snake_case							)


							def 						_UpperCamelCase       (			self      :						Any							):
													'''simple docstring'''





													A__						:  List[Any]		  =  self.prepare_config_and_inputs()
													self.check_model_from_pretrained_configs(**snake_case							)


							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													A__						:  Optional[int]		  =  self.prepare_config_and_inputs()
													self.check_vision_text_dual_encoder_from_pretrained(**snake_case							)


							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													A__						:  Tuple		  =  self.prepare_config_and_inputs()
													self.check_save_load(**snake_case							)


							def 						_UpperCamelCase       (			self      :						int							):
													'''simple docstring'''





													A__						:  str		  =  self.prepare_config_and_inputs()
													self.check_vision_text_output_attention(**snake_case							)


							@slow
							def 						_UpperCamelCase       (			self      :						Optional[int]							):
													'''simple docstring'''





													A__						:  Any		  =  self.get_pretrained_model_and_inputs()

													A__						:  int		  =  model_a(**snake_case							)
													A__						:  Dict		  =  outputs[0].numpy()

													with tempfile.TemporaryDirectory() as tmp_dirname:
																			model_a.save_pretrained(snake_case							)
																			A__						:  List[str]		  =  TFVisionTextDualEncoderModel.from_pretrained(snake_case							)

																			A__						:  List[Any]		  =  model_a(**snake_case							)
																			A__						:  List[Any]		  =  after_outputs[0].numpy()
																			A__						:  Optional[Any]		  =  np.amax(np.abs(out_a - out_a							)							)
																			self.assertLessEqual(snake_case			,    1e-5							)






@require_tf
class 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase       ,		unittest.TestCase		):


							def 						_UpperCamelCase       (			self      :						Optional[int]							):
													'''simple docstring'''





													A__						:  List[Any]		  =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
													    """hf-internal-testing/tiny-random-vit"""			,    """hf-internal-testing/tiny-random-bert"""							)
													A__						:  Optional[int]		  =  13
													A__						:  Any		  =  floats_tensor(
													    [
													        batch_size,
													        model.vision_model.config.num_channels,
													        model.vision_model.config.image_size,
													        model.vision_model.config.image_size,
													    ]							)
													A__						:  Any		  =  ids_tensor([batch_size, 4]			,    model.text_model.config.vocab_size							)
													A__						:  Dict		  =  random_attention_mask([batch_size, 4]							)
													A__						:  str		  =  {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

													return model, inputs


							def 						_UpperCamelCase       (			self      :						Any			,    snake_case      :						Tuple			,    snake_case      :						Union[str, Any]							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  TFViTModel(snake_case			,    name="""vision_model"""							)
													A__						:  Optional[int]		  =  TFBertModel(snake_case			,    name="""text_model"""							)
													return vision_model, text_model


							def 						_UpperCamelCase       (			self      :						List[Any]							):
													'''simple docstring'''





													A__						:  str		  =  TFViTModelTester(self							)
													A__						:  str		  =  TFBertModelTester(self							)
													A__						:  str		  =  vit_model_tester.prepare_config_and_inputs()
													A__						:  str		  =  bert_model_tester.prepare_config_and_inputs()

													A__						:  Any		  =  vision_config_and_inputs

													(
													    A__  
													)						:  Optional[Any]		  =  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 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase       ,		unittest.TestCase		):


							def 						_UpperCamelCase       (			self      :						Dict							):
													'''simple docstring'''





													A__						:  Union[str, Any]		  =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
													    """Rocketknight1/tiny-random-deit-tf"""			,    """hf-internal-testing/tiny-random-roberta"""							)
													A__						:  List[str]		  =  13
													A__						:  Optional[int]		  =  floats_tensor(
													    [
													        batch_size,
													        model.vision_model.config.num_channels,
													        model.vision_model.config.image_size,
													        model.vision_model.config.image_size,
													    ]							)
													A__						:  List[str]		  =  ids_tensor([batch_size, 4]			,    model.text_model.config.vocab_size							)
													A__						:  Dict		  =  random_attention_mask([batch_size, 4]							)
													A__						:  Dict		  =  {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

													return model, inputs


							def 						_UpperCamelCase       (			self      :						Union[str, Any]			,    snake_case      :						List[Any]			,    snake_case      :						Union[str, Any]			,    snake_case      :						List[str]			,    snake_case      :						str			,    snake_case      :						Union[str, Any]=None			,    **snake_case      :						List[Any]							):
													'''simple docstring'''





													A__						:  List[str]		  =  self.get_vision_text_model(snake_case			,    snake_case							)
													A__						:  Optional[Any]		  =  TFVisionTextDualEncoderModel(vision_model=snake_case			,    text_model=snake_case							)

													A__						:  int		  =  model(
													    input_ids=snake_case			,    pixel_values=snake_case			,    attention_mask=snake_case			,    output_attentions=snake_case							)

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

													A__						:  Dict		  =  output.text_model_output.attentions
													self.assertEqual(len(snake_case							)			,    text_config.num_hidden_layers							)

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


							def 						_UpperCamelCase       (			self      :						Optional[int]			,    snake_case      :						List[Any]			,    snake_case      :						Tuple							):
													'''simple docstring'''





													A__						:  Tuple		  =  TFDeiTModel(snake_case			,    name="""vision_model"""							)
													A__						:  Optional[int]		  =  TFRobertaModel(snake_case			,    name="""text_model"""							)
													return vision_model, text_model


							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  TFDeiTModelTester(self							)
													A__						:  Optional[int]		  =  TFRobertaModelTester(self							)
													A__						:  Union[str, Any]		  =  vit_model_tester.prepare_config_and_inputs()
													A__						:  Any		  =  bert_model_tester.prepare_config_and_inputs()

													A__						:  Tuple		  =  vision_config_and_inputs

													(
													    A__  
													)						:  Optional[int]		  =  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 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase       ,		unittest.TestCase		):


							def 						_UpperCamelCase       (			self      :						List[Any]							):
													'''simple docstring'''





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

													return model, inputs


							def 						_UpperCamelCase       (			self      :						Dict			,    snake_case      :						Dict			,    snake_case      :						Any							):
													'''simple docstring'''





													A__						:  Dict		  =  TFCLIPVisionModel(snake_case			,    name="""vision_model"""							)
													A__						:  str		  =  TFBertModel(snake_case			,    name="""text_model"""							)
													return vision_model, text_model


							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													A__						:  Optional[int]		  =  TFCLIPVisionModelTester(self							)
													A__						:  List[str]		  =  TFBertModelTester(self							)
													A__						:  str		  =  clip_model_tester.prepare_config_and_inputs()
													A__						:  Union[str, Any]		  =  bert_model_tester.prepare_config_and_inputs()

													A__						:  Tuple		  =  vision_config_and_inputs

													(
													    A__  
													)						:  str		  =  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 							__SCREAMING_SNAKE_CASE						(				unittest.TestCase		):


							@slow
							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													A__						:  str		  =  TFVisionTextDualEncoderModel.from_pretrained(
													    """clip-italian/clip-italian"""			,    logit_scale_init_value=1.0			,    from_pt=snake_case							)
													A__						:  str		  =  VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian"""							)

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

													A__						:  Optional[Any]		  =  model(**snake_case							)

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

													A__						:  int		  =  np.array([[1.2284727, 0.3104122]]							)

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














"""simple docstring"""



import json
import os
from functools import lru_cache
from typing import List, Optional, Tuple

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


A_    =							logging.get_logger(__name__)


A_    =							{'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt'''}

A_    =							{
    '''vocab_file''': {
        '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json''',
        '''allenai/longformer-large-4096''': (
            '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json'''
        ),
        '''allenai/longformer-large-4096-finetuned-triviaqa''': (
            '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json'''
        ),
        '''allenai/longformer-base-4096-extra.pos.embd.only''': (
            '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json'''
        ),
        '''allenai/longformer-large-4096-extra.pos.embd.only''': (
            '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json'''
        ),
    },
    '''merges_file''': {
        '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt''',
        '''allenai/longformer-large-4096''': (
            '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt'''
        ),
        '''allenai/longformer-large-4096-finetuned-triviaqa''': (
            '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt'''
        ),
        '''allenai/longformer-base-4096-extra.pos.embd.only''': (
            '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt'''
        ),
        '''allenai/longformer-large-4096-extra.pos.embd.only''': (
            '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt'''
        ),
    },
}

A_    =							{
    '''allenai/longformer-base-4096''': 4096,
    '''allenai/longformer-large-4096''': 4096,
    '''allenai/longformer-large-4096-finetuned-triviaqa''': 4096,
    '''allenai/longformer-base-4096-extra.pos.embd.only''': 4096,
    '''allenai/longformer-large-4096-extra.pos.embd.only''': 4096,
}




@lru_cache()
# Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode
def      _lowerCAmelCase		(							)		->List[str]:
						A__						:  Tuple		  =  (
						    list(range(ord("""!"""      ),						ord("""~"""      ) + 1      )      ) + list(range(ord("""¡"""      ),						ord("""¬"""      ) + 1      )      ) + list(range(ord("""®"""      ),						ord("""ÿ"""      ) + 1      )      )
						)
						A__						:  int		  =  bs[:]
						A__						:  Optional[Any]		  =  0
						for b in range(2**8      ):
												if b not in bs:
																		bs.append(UpperCAmelCase__      )
																		cs.append(2**8 + n      )
																		n += 1
						A__						:  Optional[Any]		  =  [chr(UpperCAmelCase__      ) for n in cs]
						return dict(zip(UpperCAmelCase__,						UpperCAmelCase__      )      )




def      _lowerCAmelCase		(							UpperCAmelCase__       :					int      )		->Tuple:
						A__						:  Tuple		  =  set()
						A__						:  Tuple		  =  word[0]
						for char in word[1:]:
												pairs.add((prev_char, char)      )
												A__						:  str		  =  char
						return pairs






class 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase		):
							snake_case_     =							VOCAB_FILES_NAMES
							snake_case_     =							PRETRAINED_VOCAB_FILES_MAP
							snake_case_     =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							snake_case_     =							['input_ids', 'attention_mask']


							def __init__(			self      :						List[str]			,    snake_case      :						List[str]			,    snake_case      :						str			,    snake_case      :						Dict="replace"			,    snake_case      :						Optional[int]="<s>"			,    snake_case      :						Any="</s>"			,    snake_case      :						List[Any]="</s>"			,    snake_case      :						str="<s>"			,    snake_case      :						Tuple="<unk>"			,    snake_case      :						List[str]="<pad>"			,    snake_case      :						Optional[Any]="<mask>"			,    snake_case      :						Tuple=False			,    **snake_case      :						Dict			,    ):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  AddedToken(snake_case			,    lstrip=snake_case			,    rstrip=snake_case							) if isinstance(snake_case			,    snake_case							) else bos_token
													A__						:  Optional[Any]		  =  AddedToken(snake_case			,    lstrip=snake_case			,    rstrip=snake_case							) if isinstance(snake_case			,    snake_case							) else eos_token
													A__						:  int		  =  AddedToken(snake_case			,    lstrip=snake_case			,    rstrip=snake_case							) if isinstance(snake_case			,    snake_case							) else sep_token
													A__						:  str		  =  AddedToken(snake_case			,    lstrip=snake_case			,    rstrip=snake_case							) if isinstance(snake_case			,    snake_case							) else cls_token
													A__						:  Optional[Any]		  =  AddedToken(snake_case			,    lstrip=snake_case			,    rstrip=snake_case							) if isinstance(snake_case			,    snake_case							) else unk_token
													A__						:  Optional[int]		  =  AddedToken(snake_case			,    lstrip=snake_case			,    rstrip=snake_case							) if isinstance(snake_case			,    snake_case							) else pad_token

													# Mask token behave like a normal word, i.e. include the space before it
													A__						:  Union[str, Any]		  =  AddedToken(snake_case			,    lstrip=snake_case			,    rstrip=snake_case							) if isinstance(snake_case			,    snake_case							) else mask_token

													super().__init__(
													    errors=snake_case			,    bos_token=snake_case			,    eos_token=snake_case			,    unk_token=snake_case			,    sep_token=snake_case			,    cls_token=snake_case			,    pad_token=snake_case			,    mask_token=snake_case			,    add_prefix_space=snake_case			,    **snake_case			,    )

													with open(snake_case			,    encoding="""utf-8"""							) as vocab_handle:
																			A__						:  Tuple		  =  json.load(snake_case							)
													A__						:  Dict		  =  {v: k for k, v in self.encoder.items()}
													A__						:  Dict		  =  errors  # how to handle errors in decoding
													A__						:  Dict		  =  bytes_to_unicode()
													A__						:  Optional[int]		  =  {v: k for k, v in self.byte_encoder.items()}
													with open(snake_case			,    encoding="""utf-8"""							) as merges_handle:
																			A__						:  List[str]		  =  merges_handle.read().split("""\n"""							)[1:-1]
													A__						:  Dict		  =  [tuple(merge.split()							) for merge in bpe_merges]
													A__						:  Union[str, Any]		  =  dict(zip(snake_case			,    range(len(snake_case							)							)							)							)
													A__						:  Tuple		  =  {}
													A__						:  Optional[int]		  =  add_prefix_space

													# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
													A__						:  Union[str, Any]		  =  re.compile(r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""							)


							@property
							def 						_UpperCamelCase       (			self      :						List[Any]							):
													'''simple docstring'''





													return len(self.encoder							)


							def 						_UpperCamelCase       (			self      :						List[Any]							):
													'''simple docstring'''





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


							def 						_UpperCamelCase       (			self      :						str			,    snake_case      :						Tuple							):
													'''simple docstring'''





													if token in self.cache:
																			return self.cache[token]
													A__						:  str		  =  tuple(snake_case							)
													A__						:  Any		  =  get_pairs(snake_case							)

													if not pairs:
																			return token

													while True:
																			A__						:  int		  =  min(snake_case			,    key=lambda snake_case							: self.bpe_ranks.get(snake_case			,    float("""inf"""							)							)							)
																			if bigram not in self.bpe_ranks:
																									break
																			A__  ,  A__						:  int		  =  bigram
																			A__						:  List[str]		  =  []
																			A__						:  Dict		  =  0
																			while i < len(snake_case							):
																									try:
																															A__						:  int		  =  word.index(snake_case			,    snake_case							)
																									except ValueError:
																															new_word.extend(word[i:]							)
																															break
																									else:
																															new_word.extend(word[i:j]							)
																															A__						:  str		  =  j

																									if word[i] == first and i < len(snake_case							) - 1 and word[i + 1] == second:
																															new_word.append(first + second							)
																															i += 2
																									else:
																															new_word.append(word[i]							)
																															i += 1
																			A__						:  Any		  =  tuple(snake_case							)
																			A__						:  Optional[Any]		  =  new_word
																			if len(snake_case							) == 1:
																									break
																			else:
																									A__						:  int		  =  get_pairs(snake_case							)
													A__						:  Dict		  =  """ """.join(snake_case							)
													A__						:  int		  =  word
													return word


							def 						_UpperCamelCase       (			self      :						int			,    snake_case      :						List[str]							):
													'''simple docstring'''





													A__						:  int		  =  []
													for token in re.findall(self.pat			,    snake_case							):
																			A__						:  Union[str, Any]		  =  """""".join(
																			    self.byte_encoder[b] for b in token.encode("""utf-8"""							)							)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
																			bpe_tokens.extend(bpe_token for bpe_token in self.bpe(snake_case							).split(""" """							)							)
													return bpe_tokens


							def 						_UpperCamelCase       (			self      :						Optional[Any]			,    snake_case      :						Dict							):
													'''simple docstring'''





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


							def 						_UpperCamelCase       (			self      :						Optional[int]			,    snake_case      :						int							):
													'''simple docstring'''





													return self.decoder.get(snake_case							)


							def 						_UpperCamelCase       (			self      :						Union[str, Any]			,    snake_case      :						Union[str, Any]							):
													'''simple docstring'''





													A__						:  Union[str, Any]		  =  """""".join(snake_case							)
													A__						:  Optional[Any]		  =  bytearray([self.byte_decoder[c] for c in text]							).decode("""utf-8"""			,    errors=self.errors							)
													return text


							def 						_UpperCamelCase       (			self      :						List[str]			,    snake_case      :						str			,    snake_case      :						Optional[str] = None							):
													'''simple docstring'''





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

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

													A__						:  Optional[int]		  =  0
													with open(snake_case			,    """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 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!"""							)
																															A__						:  Dict		  =  token_index
																									writer.write(""" """.join(snake_case							) + """\n"""							)
																									index += 1

													return vocab_file, merge_file


							def 						_UpperCamelCase       (			self      :						List[Any]			,    snake_case      :						List[int]			,    snake_case      :						Optional[List[int]] = None							):
													'''simple docstring'''





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


							def 						_UpperCamelCase       (			self      :						int			,    snake_case      :						List[int]			,    snake_case      :						Optional[List[int]] = None			,    snake_case      :						bool = False							):
													'''simple docstring'''





													if already_has_special_tokens:
																			return super().get_special_tokens_mask(
																			    token_ids_a=snake_case			,    token_ids_a=snake_case			,    already_has_special_tokens=snake_case							)

													if token_ids_a is None:
																			return [1] + ([0] * len(snake_case							)) + [1]
													return [1] + ([0] * len(snake_case							)) + [1, 1] + ([0] * len(snake_case							)) + [1]


							def 						_UpperCamelCase       (			self      :						Union[str, Any]			,    snake_case      :						List[int]			,    snake_case      :						Optional[List[int]] = None							):
													'''simple docstring'''





													A__						:  int		  =  [self.sep_token_id]
													A__						:  int		  =  [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 						_UpperCamelCase       (			self      :						str			,    snake_case      :						Optional[Any]			,    snake_case      :						List[str]=False			,    **snake_case      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  kwargs.pop("""add_prefix_space"""			,    self.add_prefix_space							)
													if (is_split_into_words or add_prefix_space) and (len(snake_case							) > 0 and not text[0].isspace()):
																			A__						:  str		  =  """ """ + text
													return (text, kwargs)






