



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


UpperCAmelCase__       =	logging.get_logger(__name__)

UpperCAmelCase__       =	"""▁"""

UpperCAmelCase__       =	{"""vocab_file""": """sentencepiece.bpe.model"""}

UpperCAmelCase__       =	{
    """vocab_file""": {
        """facebook/xglm-564M""": """https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model""",
    }
}

UpperCAmelCase__       =	{
    """facebook/xglm-564M""": 2048,
}







class       __lowerCAmelCase   (      A						):
			UpperCamelCase        =					VOCAB_FILES_NAMES
			UpperCamelCase        =					PRETRAINED_VOCAB_FILES_MAP
			UpperCamelCase        =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			UpperCamelCase        =					["input_ids", "attention_mask"]



			def __init__(						self       :  int			,		A       :  Optional[int]			,		A       :  str="<s>"			,		A       :  Optional[Any]="</s>"			,		A       :  Optional[int]="</s>"			,		A       :  List[Any]="<s>"			,		A       :  Tuple="<unk>"			,		A       :  List[str]="<pad>"			,		A       :  Optional[Dict[str, Any]] = None			,		**A       :  List[Any]			,		)       ->   None:



						"""simple docstring"""






						_UpperCAmelCase			=       {} if sp_model_kwargs is None else sp_model_kwargs

						# Compatibility with the original tokenizer
						_UpperCAmelCase			=       7
						_UpperCAmelCase			=       [F"<madeupword{i}>" for i in range(self.num_madeup_words)]

						_UpperCAmelCase			=       kwargs.get('additional_special_tokens'			,		[])
						kwargs["additional_special_tokens"] += [
						    word for word in madeup_words if word not in kwargs["additional_special_tokens"]
						]

						super().__init__(
						    bos_token=A			,		eos_token=A			,		unk_token=A			,		sep_token=A			,		cls_token=A			,		pad_token=A			,		sp_model_kwargs=self.sp_model_kwargs			,		**A			,		)

						_UpperCAmelCase			=       spm.SentencePieceProcessor(**self.sp_model_kwargs)
						self.sp_model.Load(str(A))
						_UpperCAmelCase			=       vocab_file

						# Original fairseq vocab and spm vocab must be "aligned":
						# Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
						# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
						# fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
						# spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

						# The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
						_UpperCAmelCase			=       1

						# Mimic fairseq token-to-id alignment for the first 4 token
						_UpperCAmelCase			=       {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3}

						_UpperCAmelCase			=       len(self.sp_model)
						_UpperCAmelCase			=       {F"<madeupword{i}>": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words)}
						self.fairseq_tokens_to_ids.update(A)

						_UpperCAmelCase			=       {v: k for k, v in self.fairseq_tokens_to_ids.items()}



			def __getstate__(						self       :  Tuple)       ->   Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       self.__dict__.copy()
						_UpperCAmelCase			=       None
						_UpperCAmelCase			=       self.sp_model.serialized_model_proto()
						return state



			def __setstate__(						self       :  Optional[Any]			,		A       :  Optional[int])       ->   Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       d

						# for backward compatibility
						if not hasattr(self			,		'sp_model_kwargs'):
									_UpperCAmelCase			=       {}

						_UpperCAmelCase			=       spm.SentencePieceProcessor(**self.sp_model_kwargs)
						self.sp_model.LoadFromSerializedProto(self.sp_model_proto)



			def    _lowerCamelCase	(						self       :  Any			,		A       :  List[int]			,		A       :  Optional[List[int]] = None)       ->   List[int]:



						"""simple docstring"""






						if token_ids_a is None:
									return [self.sep_token_id] + token_ids_a
						_UpperCAmelCase			=       [self.sep_token_id]
						return sep + token_ids_a + sep + sep + token_ids_a



			def    _lowerCamelCase	(						self       :  Dict			,		A       :  List[int]			,		A       :  Optional[List[int]] = None			,		A       :  bool = False)       ->   List[int]:



						"""simple docstring"""






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

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



			def    _lowerCamelCase	(						self       :  Tuple			,		A       :  List[int]			,		A       :  Optional[List[int]] = None)       ->   List[int]:



						"""simple docstring"""






						_UpperCAmelCase			=       [self.sep_token_id]

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



			@property
			def    _lowerCamelCase	(						self       :  int)       ->   Optional[int]:



						"""simple docstring"""






						return len(self.sp_model) + self.fairseq_offset + self.num_madeup_words



			def    _lowerCamelCase	(						self       :  str)       ->   Dict:



						"""simple docstring"""






						_UpperCAmelCase			=       {self.convert_ids_to_tokens(A): i for i in range(self.vocab_size)}
						vocab.update(self.added_tokens_encoder)
						return vocab



			def    _lowerCamelCase	(						self       :  str			,		A       :  str)       ->   List[str]:



						"""simple docstring"""






						return self.sp_model.encode(A			,		out_type=A)



			def    _lowerCamelCase	(						self       :  Optional[Any]			,		A       :  Optional[int])       ->   int:



						"""simple docstring"""






						if token in self.fairseq_tokens_to_ids:
									return self.fairseq_tokens_to_ids[token]
						_UpperCAmelCase			=       self.sp_model.PieceToId(A)

						# Need to return unknown token if the SP model returned 0
						return spm_id + self.fairseq_offset if spm_id else self.unk_token_id



			def    _lowerCamelCase	(						self       :  int			,		A       :  int)       ->   Tuple:



						"""simple docstring"""






						if index in self.fairseq_ids_to_tokens:
									return self.fairseq_ids_to_tokens[index]
						return self.sp_model.IdToPiece(index - self.fairseq_offset)



			def    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  List[str])       ->   List[Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       ''.join(A).replace(A			,		' ').strip()
						return out_string



			def    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  str			,		A       :  Optional[str] = None)       ->   Tuple[str]:



						"""simple docstring"""






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

						if os.path.abspath(self.vocab_file) != os.path.abspath(A) and os.path.isfile(self.vocab_file):
									copyfile(self.vocab_file			,		A)
						elif not os.path.isfile(self.vocab_file):
									with open(A			,		'wb') as fi:
												_UpperCAmelCase			=       self.sp_model.serialized_model_proto()
												fi.write(A)

						return (out_vocab_file,)





import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
class       __lowerCAmelCase   (      A    ,						unittest.TestCase						):
			UpperCamelCase        =					CLIPTokenizer
			UpperCamelCase        =					CLIPTokenizerFast
			UpperCamelCase        =					True
			UpperCamelCase        =					{}
			UpperCamelCase        =					False



			def    _lowerCamelCase	(						self       :  List[str])       ->   List[str]:



						"""simple docstring"""






						super().setUp()

						# fmt: off
						_UpperCAmelCase			=       ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>']
						# fmt: on
						_UpperCAmelCase			=       dict(zip(A			,		range(len(A))))
						_UpperCAmelCase			=       ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>']
						_UpperCAmelCase			=       {'unk_token': '<unk>'}

						_UpperCAmelCase			=       os.path.join(self.tmpdirname			,		VOCAB_FILES_NAMES['vocab_file'])
						_UpperCAmelCase			=       os.path.join(self.tmpdirname			,		VOCAB_FILES_NAMES['merges_file'])
						with open(self.vocab_file			,		'w'			,		encoding='utf-8') as fp:
									fp.write(json.dumps(A) + '\n')
						with open(self.merges_file			,		'w'			,		encoding='utf-8') as fp:
									fp.write('\n'.join(A))



			def    _lowerCamelCase	(						self       :  Optional[Any]			,		**A       :  str)       ->   Optional[int]:



						"""simple docstring"""






						kwargs.update(self.special_tokens_map)
						return CLIPTokenizer.from_pretrained(self.tmpdirname			,		**A)



			def    _lowerCamelCase	(						self       :  Any			,		**A       :  Dict)       ->   Union[str, Any]:



						"""simple docstring"""






						kwargs.update(self.special_tokens_map)
						return CLIPTokenizerFast.from_pretrained(self.tmpdirname			,		**A)



			def    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  Union[str, Any])       ->   int:



						"""simple docstring"""






						_UpperCAmelCase			=       'lower newer'
						_UpperCAmelCase			=       'lower newer'
						return input_text, output_text



			def    _lowerCamelCase	(						self       :  Dict)       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       CLIPTokenizer(self.vocab_file			,		self.merges_file			,		**self.special_tokens_map)
						_UpperCAmelCase			=       'lower newer'
						_UpperCAmelCase			=       ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>']
						_UpperCAmelCase			=       tokenizer.tokenize(A)
						self.assertListEqual(A			,		A)

						_UpperCAmelCase			=       tokens + [tokenizer.unk_token]
						_UpperCAmelCase			=       [10, 2, 16, 9, 3, 2, 16, 20]
						self.assertListEqual(tokenizer.convert_tokens_to_ids(A)			,		A)



			@require_ftfy
			def    _lowerCamelCase	(						self       :  List[Any])       ->   Dict:



						"""simple docstring"""






						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
									with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"):
												_UpperCAmelCase			=       self.tokenizer_class.from_pretrained(A			,		**A)
												_UpperCAmelCase			=       self.rust_tokenizer_class.from_pretrained(A			,		**A)

												_UpperCAmelCase			=       'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.'
												_UpperCAmelCase			=       tokenizer_s.tokenize(A)
												_UpperCAmelCase			=       tokenizer_r.tokenize(A)

												self.assertListEqual(A			,		A)

												# Test that the tokenization is identical on an example containing a character (Latin Small Letter A
												# with Tilde) encoded in 2 different ways
												_UpperCAmelCase			=       'xa\u0303y' + ' ' + 'x\xe3y'
												_UpperCAmelCase			=       tokenizer_s.tokenize(A)
												_UpperCAmelCase			=       tokenizer_r.tokenize(A)

												self.assertListEqual(A			,		A)

												# Test that the tokenization is identical on unicode of space type
												_UpperCAmelCase			=       [
												    '\u0009',  # (horizontal tab, '\t')
												    '\u000B',  # (vertical tab)
												    '\u000C',  # (form feed)
												    '\u0020',  # (space, ' ')
												    '\u200E',  # (left-to-right mark):w
												    '\u200F',  # (right-to-left mark)
												]
												for unicode_seq in spaces_unicodes:
															_UpperCAmelCase			=       tokenizer_s.tokenize(A)
															_UpperCAmelCase			=       tokenizer_r.tokenize(A)

															self.assertListEqual(A			,		A)

												# Test that the tokenization is identical on unicode of line break type
												_UpperCAmelCase			=       [
												    '\u000A',  # (line feed, '\n')
												    '\r\n',  # (carriage return and line feed, '\r\n')
												    '\u000D',  # (carriage return, '\r')
												    '\r',  # (carriage return, '\r')
												    '\u000D',  # (carriage return, '\r')
												    '\u2028',  # (line separator)
												    '\u2029',  # (paragraph separator)
												    # "\u0085", # (next line)
												]

												# The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
												# it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
												# space (and thus into an empty list).

												for unicode_seq in line_break_unicodes:
															_UpperCAmelCase			=       tokenizer_s.tokenize(A)
															_UpperCAmelCase			=       tokenizer_r.tokenize(A)

															self.assertListEqual(A			,		A)



			def    _lowerCamelCase	(						self       :  str)       ->   Optional[Any]:



						"""simple docstring"""






						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
									with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"):
												_UpperCAmelCase			=       'hello'  # `hello` is a token in the vocabulary of `pretrained_name`
												_UpperCAmelCase			=       F"{text_of_1_token} {text_of_1_token}"

												_UpperCAmelCase			=       self.rust_tokenizer_class.from_pretrained(
												    A			,		use_fast=A			,		)
												_UpperCAmelCase			=       tokenizer_r(A			,		return_offsets_mapping=A			,		add_special_tokens=A)
												self.assertEqual(encoding.offset_mapping[0]			,		(0, len(A)))
												self.assertEqual(
												    encoding.offset_mapping[1]			,		(len(A) + 1, len(A) + 1 + len(A))			,		)

												_UpperCAmelCase			=       F" {text}"

												_UpperCAmelCase			=       self.rust_tokenizer_class.from_pretrained(
												    A			,		use_fast=A			,		)
												_UpperCAmelCase			=       tokenizer_r(A			,		return_offsets_mapping=A			,		add_special_tokens=A)
												self.assertEqual(encoding.offset_mapping[0]			,		(1, 1 + len(A)))
												self.assertEqual(
												    encoding.offset_mapping[1]			,		(1 + len(A) + 1, 1 + len(A) + 1 + len(A))			,		)



			def    _lowerCamelCase	(						self       :  Tuple)       ->   str:



						"""simple docstring"""






						with self.assertRaises(A) as context:
									self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer')

						self.assertTrue(
						    context.exception.args[0].startswith(
						        'The `backend_tokenizer` provided does not match the expected format.'))



			@require_ftfy
			def    _lowerCamelCase	(						self       :  int)       ->   int:



						"""simple docstring"""






						super().test_tokenization_python_rust_equals()



			def    _lowerCamelCase	(						self       :  Union[str, Any])       ->   Any:



						"""simple docstring"""






						pass
