2024-05-09 13:30:44 +00:00
|
|
|
|
# Test libllama tokenizer == AutoTokenizer.
|
2024-05-17 23:09:13 +00:00
|
|
|
|
# Brute force random words/text generation.
|
2024-05-09 13:30:44 +00:00
|
|
|
|
#
|
|
|
|
|
# Sample usage:
|
|
|
|
|
#
|
|
|
|
|
# python3 tests/test-tokenizer-random.py ./models/ggml-vocab-llama-bpe.gguf ./models/tokenizers/llama-bpe
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
import logging
|
|
|
|
|
import argparse
|
|
|
|
|
import subprocess
|
|
|
|
|
import random
|
2024-06-18 16:40:52 +00:00
|
|
|
|
import unicodedata
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
from typing import Iterator
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
|
|
|
|
import cffi
|
2024-05-17 23:09:13 +00:00
|
|
|
|
from transformers import AutoTokenizer
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
2024-06-18 16:40:52 +00:00
|
|
|
|
|
|
|
|
|
logger = logging.getLogger("test-tokenizer-random")
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LibLlama:
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
DEFAULT_PATH_LLAMA_H = "./include/llama.h"
|
|
|
|
|
DEFAULT_PATH_INCLUDES = ["./ggml/include/", "./include/"]
|
|
|
|
|
DEFAULT_PATH_LIBLLAMA = "./build/src/libllama.so" # CMakeLists.txt: BUILD_SHARED_LIBS ON
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def __init__(self, path_llama_h: str = None, path_includes: list[str] = [], path_libllama: str = None):
|
2024-05-09 13:30:44 +00:00
|
|
|
|
path_llama_h = path_llama_h or self.DEFAULT_PATH_LLAMA_H
|
2024-07-05 17:01:35 +00:00
|
|
|
|
path_includes = path_includes or self.DEFAULT_PATH_INCLUDES
|
2024-05-09 13:30:44 +00:00
|
|
|
|
path_libllama = path_libllama or self.DEFAULT_PATH_LIBLLAMA
|
2024-07-05 17:01:35 +00:00
|
|
|
|
(self.ffi, self.lib) = self._load_libllama_cffi(path_llama_h, path_includes, path_libllama)
|
2024-05-09 13:30:44 +00:00
|
|
|
|
self.lib.llama_backend_init()
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def _load_libllama_cffi(self, path_llama_h: str, path_includes: list[str], path_libllama: str):
|
|
|
|
|
cmd = ["gcc", "-E", "-P", "-D__restrict=", "-D__attribute__(x)=", "-D__asm__(x)="]
|
|
|
|
|
cmd += ["-I" + path for path in path_includes] + [path_llama_h]
|
2024-05-09 13:30:44 +00:00
|
|
|
|
res = subprocess.run(cmd, stdout=subprocess.PIPE)
|
|
|
|
|
assert (res.returncode == 0)
|
|
|
|
|
source = res.stdout.decode()
|
|
|
|
|
ffi = cffi.FFI()
|
|
|
|
|
if True: # workarounds for pycparser
|
|
|
|
|
source = "typedef struct { } __builtin_va_list;" + "\n" + source
|
|
|
|
|
source = source.replace("sizeof (int)", str(ffi.sizeof("int")))
|
|
|
|
|
source = source.replace("sizeof (void *)", str(ffi.sizeof("void*")))
|
|
|
|
|
source = source.replace("sizeof (size_t)", str(ffi.sizeof("size_t")))
|
|
|
|
|
source = source.replace("sizeof(int32_t)", str(ffi.sizeof("int32_t")))
|
|
|
|
|
ffi.cdef(source, override=True)
|
|
|
|
|
lib = ffi.dlopen(path_libllama)
|
|
|
|
|
return (ffi, lib)
|
|
|
|
|
|
|
|
|
|
def model_default_params(self, **kwargs):
|
|
|
|
|
mparams = self.lib.llama_model_default_params()
|
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
|
setattr(mparams, k, v)
|
|
|
|
|
return mparams
|
|
|
|
|
|
|
|
|
|
def context_default_params(self, **kwargs):
|
|
|
|
|
cparams = self.lib.llama_context_default_params()
|
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
|
setattr(cparams, k, v)
|
|
|
|
|
return cparams
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LibLlamaModel:
|
|
|
|
|
|
|
|
|
|
def __init__(self, libllama: LibLlama, path_model: str, mparams={}, cparams={}):
|
|
|
|
|
self.lib = libllama.lib
|
|
|
|
|
self.ffi = libllama.ffi
|
|
|
|
|
if isinstance(mparams, dict):
|
|
|
|
|
mparams = libllama.model_default_params(**mparams)
|
|
|
|
|
self.model = self.lib.llama_load_model_from_file(path_model.encode(), mparams)
|
|
|
|
|
if not self.model:
|
|
|
|
|
raise RuntimeError("error: failed to load model '%s'" % path_model)
|
|
|
|
|
if isinstance(cparams, dict):
|
|
|
|
|
cparams = libllama.context_default_params(**cparams)
|
|
|
|
|
self.ctx = self.lib.llama_new_context_with_model(self.model, cparams)
|
|
|
|
|
if not self.ctx:
|
|
|
|
|
raise RuntimeError("error: failed to create context for model '%s'" % path_model)
|
|
|
|
|
n_tokens_max = self.lib.llama_n_ctx(self.ctx)
|
|
|
|
|
self.token_ids = self.ffi.new("llama_token[]", n_tokens_max)
|
2024-07-05 17:01:35 +00:00
|
|
|
|
self.text_buff = self.ffi.new("uint8_t[]", 1024)
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
|
|
|
|
def free(self):
|
|
|
|
|
if self.ctx:
|
|
|
|
|
self.lib.llama_free(self.ctx)
|
|
|
|
|
if self.model:
|
|
|
|
|
self.lib.llama_free_model(self.model)
|
|
|
|
|
self.ctx = None
|
|
|
|
|
self.model = None
|
|
|
|
|
self.lib = None
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def tokenize(self, text: str, add_special: bool = False, parse_special: bool = False) -> list[int]:
|
2024-05-09 13:30:44 +00:00
|
|
|
|
text = text.encode("utf-8")
|
2024-07-05 17:01:35 +00:00
|
|
|
|
num = self.lib.llama_tokenize(self.model, text, len(text), self.token_ids, len(self.token_ids), add_special, parse_special)
|
|
|
|
|
while num < 0 and len(self.token_ids) < (16 << 20):
|
|
|
|
|
self.token_ids = self.ffi.new("llama_token[]", -2 * num)
|
|
|
|
|
num = self.lib.llama_tokenize(self.model, text, len(text), self.token_ids, len(self.token_ids), add_special, parse_special)
|
2024-05-09 13:30:44 +00:00
|
|
|
|
return list(self.token_ids[0:num])
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def detokenize(self, ids: list[int], remove_special: bool = False, unparse_special: bool = False) -> str:
|
|
|
|
|
if len(self.token_ids) < len(ids):
|
|
|
|
|
self.token_ids = self.ffi.new("llama_token[]", 2 * len(ids))
|
|
|
|
|
for i, id in enumerate(ids):
|
|
|
|
|
self.token_ids[i] = id
|
|
|
|
|
num = self.lib.llama_detokenize(self.model, self.token_ids, len(ids), self.text_buff, len(self.text_buff), remove_special, unparse_special)
|
|
|
|
|
while num < 0 and len(self.text_buff) < (16 << 20):
|
|
|
|
|
self.text_buff = self.ffi.new("uint8_t[]", -2 * num)
|
|
|
|
|
num = self.lib.llama_detokenize(self.model, self.token_ids, len(ids), self.text_buff, len(self.text_buff), remove_special, unparse_special)
|
|
|
|
|
return str(self.ffi.buffer(self.text_buff, num), encoding="utf-8", errors="replace") # replace errors with '\uFFFD'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Tokenizer:
|
|
|
|
|
|
|
|
|
|
def encode(self, text: str) -> list[int]:
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
def decode(self, ids: list[int]) -> str:
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TokenizerGroundtruth (Tokenizer):
|
|
|
|
|
|
|
|
|
|
def __init__(self, dir_tokenizer: str):
|
|
|
|
|
self.model = AutoTokenizer.from_pretrained(dir_tokenizer)
|
|
|
|
|
# guess BOS and EOS
|
|
|
|
|
ids = self.encode("a")
|
|
|
|
|
assert 1 <= len(ids) <= 3
|
|
|
|
|
add_bos_token = len(ids) > 1 and self.model.bos_token_id == ids[0]
|
|
|
|
|
add_eos_token = len(ids) > 1 and self.model.eos_token_id == ids[-1]
|
|
|
|
|
self.add_bos_token = getattr(self.model, "add_bos_token", add_bos_token)
|
|
|
|
|
self.add_eos_token = getattr(self.model, "add_eos_token", add_eos_token)
|
|
|
|
|
# build vocab
|
|
|
|
|
tokens = list(self.model.get_vocab().values())
|
|
|
|
|
self.vocab = self.model.batch_decode(tokens, skip_special_tokens=True)
|
|
|
|
|
self.vocab = list(sorted(self.vocab))
|
|
|
|
|
# tokens and lists
|
|
|
|
|
self.special_tokens = list(self.model.all_special_tokens)
|
|
|
|
|
self.added_tokens = list(self.model.added_tokens_encoder)
|
|
|
|
|
self.bos_token = self.model.bos_token
|
|
|
|
|
self.eos_token = self.model.eos_token
|
|
|
|
|
|
|
|
|
|
def encode(self, text: str) -> list[int]:
|
|
|
|
|
return self.model.encode(text, add_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
def decode(self, ids: list[int]) -> str:
|
|
|
|
|
return self.model.decode(ids, skip_special_tokens=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TokenizerLlamaCpp (Tokenizer):
|
|
|
|
|
|
|
|
|
|
libllama: LibLlama = None
|
|
|
|
|
|
|
|
|
|
def __init__(self, vocab_file: str):
|
|
|
|
|
if not self.libllama:
|
|
|
|
|
self.libllama = LibLlama()
|
|
|
|
|
self.model = LibLlamaModel(self.libllama, vocab_file, mparams=dict(vocab_only=True), cparams=dict(n_ctx=4096))
|
|
|
|
|
|
|
|
|
|
def encode(self, text: str) -> list[int]:
|
|
|
|
|
return self.model.tokenize(text, add_special=True, parse_special=True)
|
|
|
|
|
|
|
|
|
|
def decode(self, ids: list[int]) -> str:
|
|
|
|
|
return self.model.detokenize(ids, remove_special=False, unparse_special=True)
|
|
|
|
|
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
|
|
|
|
def generator_custom_text() -> Iterator[str]:
|
|
|
|
|
"""General tests"""
|
|
|
|
|
yield from [
|
|
|
|
|
"",
|
|
|
|
|
" ",
|
|
|
|
|
" ",
|
|
|
|
|
" ",
|
|
|
|
|
"\t",
|
|
|
|
|
"\n",
|
|
|
|
|
"\n\n",
|
|
|
|
|
"\n\n\n",
|
|
|
|
|
"\t\n",
|
|
|
|
|
"Hello world",
|
|
|
|
|
" Hello world",
|
|
|
|
|
"Hello World",
|
|
|
|
|
" Hello World",
|
|
|
|
|
" Hello World!",
|
|
|
|
|
"Hello, world!",
|
|
|
|
|
" Hello, world!",
|
|
|
|
|
" this is 🦙.cpp",
|
|
|
|
|
"w048 7tuijk dsdfhu",
|
|
|
|
|
"нещо на Български",
|
|
|
|
|
"កាន់តែពិសេសអាចខលចេញ",
|
|
|
|
|
"🚀 (normal) 😶🌫️ (multiple emojis concatenated) ✅ (only emoji that has its own token)",
|
|
|
|
|
"Hello",
|
|
|
|
|
" Hello",
|
|
|
|
|
" Hello",
|
|
|
|
|
" Hello",
|
|
|
|
|
" Hello",
|
|
|
|
|
" Hello\n Hello",
|
|
|
|
|
" (",
|
|
|
|
|
"\n =",
|
|
|
|
|
"' era",
|
|
|
|
|
"Hello, y'all! How are you 😁 ?我想在apple工作1314151天~",
|
|
|
|
|
"3",
|
|
|
|
|
"33",
|
|
|
|
|
"333",
|
|
|
|
|
"3333",
|
|
|
|
|
"33333",
|
|
|
|
|
"333333",
|
|
|
|
|
"3333333",
|
|
|
|
|
"33333333",
|
|
|
|
|
"333333333",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generator_custom_text_edge_cases() -> Iterator[str]:
|
|
|
|
|
"""Edge cases found while debugging"""
|
|
|
|
|
yield from [
|
2024-05-17 23:09:13 +00:00
|
|
|
|
'\x1f-a', # unicode_ranges_control, {0x00001C, 0x00001F}
|
|
|
|
|
'¼-a', # unicode_ranges_digit, 0x00BC
|
|
|
|
|
'½-a', # unicode_ranges_digit, 0x00BD
|
|
|
|
|
'¾-a', # unicode_ranges_digit, 0x00BE
|
|
|
|
|
'a 〇b', # unicode_ranges_digit, 0x3007
|
|
|
|
|
'Ⅵ-a', # unicode_ranges_digit, {0x00002150, 0x0000218F} // Number Forms
|
|
|
|
|
'\uFEFF//', # unicode_ranges_control, 0xFEFF (BOM)
|
|
|
|
|
'Cửa Việt', # llama-3, ignore_merges = true
|
2024-05-20 18:15:57 +00:00
|
|
|
|
'<s>a', # Phi-3 fail
|
2024-05-21 12:39:48 +00:00
|
|
|
|
'<unk><|endoftext|><s>', # Phi-3 fail
|
2024-06-18 16:40:52 +00:00
|
|
|
|
'a\na', # bert fail
|
|
|
|
|
'"`', # falcon
|
|
|
|
|
' \u2e4e', # falcon
|
|
|
|
|
'a\xa0\xa0\x00b', # jina-v2-es
|
|
|
|
|
'one <mask>', # jina-v2-es <mask> lstrip=true
|
|
|
|
|
'a </s> b', # rstrip phi-3
|
|
|
|
|
'a <mask> b', # lstrip jina-v2
|
|
|
|
|
'\xa0aC', # deepseek
|
2024-07-05 17:01:35 +00:00
|
|
|
|
'\u2029 \uA3E4', # deepseek-llm
|
|
|
|
|
"a ?",
|
|
|
|
|
'å', # mpt
|
|
|
|
|
'\U000ac517', # utf-8 encode error, falcon
|
|
|
|
|
'\U000522f4', # utf-8 encode error, starcoder
|
|
|
|
|
"<s><s><unk><s>a<s>b<s>c<unk>d<unk></s>",
|
|
|
|
|
"<s> <s> <unk><s>a<s>b<s>c<unk>d<unk></s>",
|
2024-05-09 13:30:44 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def generator_vocab_words(tokenizer: TokenizerGroundtruth) -> Iterator[str]:
|
2024-06-04 07:17:17 +00:00
|
|
|
|
"""Brute force check all vocab words"""
|
2024-07-05 17:01:35 +00:00
|
|
|
|
yield from tokenizer.vocab
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generator_ascii_lr_strip() -> Iterator[str]:
|
|
|
|
|
WHITESPACES = ["", " ", " "]
|
|
|
|
|
CHARACTERS = list(chr(i) for i in range(1, 0x80)) + [""]
|
|
|
|
|
for char1 in CHARACTERS:
|
|
|
|
|
for char2 in CHARACTERS:
|
|
|
|
|
for lstrip in WHITESPACES:
|
|
|
|
|
for rstrip in WHITESPACES:
|
|
|
|
|
yield lstrip + char1 + char2 + rstrip
|
|
|
|
|
yield lstrip + char1 + rstrip + char2
|
|
|
|
|
yield char1 + lstrip + char2 + rstrip
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generator_apostrophe() -> Iterator[str]:
|
|
|
|
|
WHITESPACES = ["", " ", " "]
|
|
|
|
|
CHARACTERS = list(chr(i) for i in range(1, 0x80)) + [""]
|
|
|
|
|
for char1 in CHARACTERS:
|
|
|
|
|
for char2 in CHARACTERS:
|
|
|
|
|
for lstrip in WHITESPACES:
|
|
|
|
|
for rstrip in WHITESPACES:
|
|
|
|
|
yield char1 + lstrip + "'" + rstrip + char2
|
|
|
|
|
yield char1 + char2 + lstrip + "'" + rstrip + "z"
|
|
|
|
|
yield "a" + lstrip + "'" + rstrip + char1 + char2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generator_added_lr_strip(tokenizer: TokenizerGroundtruth) -> Iterator[str]:
|
|
|
|
|
WHITESPACES = ["", " ", " ", "\n", "\r\n", "\n\n", "\t", "\t\t"]
|
|
|
|
|
all_tokens = list(sorted(set(tokenizer.special_tokens + tokenizer.added_tokens)))
|
2024-06-04 07:17:17 +00:00
|
|
|
|
for token in all_tokens:
|
|
|
|
|
for lstrip in WHITESPACES:
|
|
|
|
|
for rstrip in WHITESPACES:
|
|
|
|
|
yield lstrip + token + rstrip
|
|
|
|
|
yield "a" + lstrip + token + rstrip
|
|
|
|
|
yield lstrip + token + rstrip + "z"
|
|
|
|
|
yield "a" + lstrip + token + rstrip + "z"
|
|
|
|
|
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def generator_random_added_tokens(tokenizer: TokenizerGroundtruth, iterations=100) -> Iterator[str]:
|
|
|
|
|
separations = [" ", "\n", "\t", "-", "!", "one", "1", "<s>", "</s>"]
|
|
|
|
|
all_tokens = list(sorted(set(tokenizer.special_tokens + tokenizer.added_tokens + separations)))
|
2024-05-20 18:15:57 +00:00
|
|
|
|
rand = random.Random()
|
|
|
|
|
for m in range(iterations):
|
|
|
|
|
rand.seed(m)
|
2024-06-04 07:17:17 +00:00
|
|
|
|
words = rand.choices(all_tokens, k=500)
|
2024-06-18 16:40:52 +00:00
|
|
|
|
if words and words[0] == tokenizer.bos_token: # skip spam warning of double BOS
|
2024-05-28 19:46:34 +00:00
|
|
|
|
while len(words) > 1 and words[1] == tokenizer.bos_token: # leave one starting BOS
|
|
|
|
|
words.pop(0)
|
|
|
|
|
if tokenizer.add_bos_token: # drop all starting BOS
|
2024-05-21 12:39:48 +00:00
|
|
|
|
words.pop(0)
|
2024-06-18 16:40:52 +00:00
|
|
|
|
if words and words[-1] == tokenizer.eos_token: # skip spam warning of double EOS
|
|
|
|
|
while len(words) > 1 and words[-2] == tokenizer.eos_token: # leave one trailing EOS
|
|
|
|
|
words.pop(-1)
|
|
|
|
|
if tokenizer.add_bos_token: # drop all trailing EOS
|
|
|
|
|
words.pop(-1)
|
2024-05-20 18:15:57 +00:00
|
|
|
|
yield "".join(words)
|
|
|
|
|
|
|
|
|
|
|
2024-05-17 23:09:13 +00:00
|
|
|
|
def generator_random_chars(iterations=100) -> Iterator[str]:
|
2024-05-09 13:30:44 +00:00
|
|
|
|
"""Brute force random text with simple characters"""
|
|
|
|
|
|
2024-06-18 16:40:52 +00:00
|
|
|
|
NUM_WORDS = 400
|
2024-05-09 13:30:44 +00:00
|
|
|
|
WHITESPACES = list(" " * 20 + "\n" * 5 + "\r\n" * 5 + "\t" * 5)
|
2024-05-17 23:09:13 +00:00
|
|
|
|
CHARS = list(sorted(set("""
|
2024-05-09 13:30:44 +00:00
|
|
|
|
ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
|
|
|
|
abcdefghijklmnopqrstuvwxyz
|
|
|
|
|
ÁÉÍÓÚÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ
|
|
|
|
|
áéíóúàèìòùâêîôûäëïöü
|
|
|
|
|
.-,*/-+ª!"·$%&/()=?¿[]{}<>\\|@#~½¬~;:_
|
2024-05-17 23:09:13 +00:00
|
|
|
|
""")))
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
|
|
|
|
rand = random.Random()
|
|
|
|
|
for m in range(iterations):
|
|
|
|
|
rand.seed(m)
|
|
|
|
|
text = []
|
2024-06-18 16:40:52 +00:00
|
|
|
|
for _ in range(NUM_WORDS):
|
2024-05-09 13:30:44 +00:00
|
|
|
|
k = rand.randint(1, 7)
|
|
|
|
|
word = rand.choices(CHARS, k=k)
|
2024-06-18 16:40:52 +00:00
|
|
|
|
word.append(rand.choice(WHITESPACES))
|
|
|
|
|
text.append("".join(word))
|
|
|
|
|
yield "".join(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generator_unicodes() -> Iterator[str]:
|
|
|
|
|
"""Iterate unicode characters"""
|
|
|
|
|
|
|
|
|
|
MAX_CODEPOINTS = 0x30000 # 0x110000
|
|
|
|
|
|
|
|
|
|
def _valid(cpt):
|
|
|
|
|
if cpt >= 0x30000: # unassigned and supplementary
|
|
|
|
|
return False
|
2024-07-05 17:01:35 +00:00
|
|
|
|
# if cpt == 0x2029: # deepseek-llm
|
|
|
|
|
# return False
|
|
|
|
|
if unicodedata.category(chr(cpt)) in ("Cn", "Cs", "Co"): # undefined, surrogates, private
|
2024-06-18 16:40:52 +00:00
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
characters = [chr(cpt) for cpt in range(0, MAX_CODEPOINTS) if _valid(cpt)]
|
2024-06-18 16:40:52 +00:00
|
|
|
|
|
|
|
|
|
yield from characters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generator_random_unicodes(iterations=100) -> Iterator[str]:
|
|
|
|
|
"""Brute force random text with unicode characters"""
|
|
|
|
|
|
|
|
|
|
NUM_WORDS = 200
|
|
|
|
|
WHITESPACES = list(" " * 20 + "\n" * 5 + "\r\n" * 5 + "\t" * 5)
|
|
|
|
|
|
|
|
|
|
characters = list(generator_unicodes())
|
|
|
|
|
|
|
|
|
|
rand = random.Random()
|
|
|
|
|
for m in range(iterations):
|
|
|
|
|
rand.seed(m)
|
|
|
|
|
text = []
|
|
|
|
|
for _ in range(NUM_WORDS):
|
|
|
|
|
k = rand.randint(1, 7)
|
|
|
|
|
word = rand.choices(characters, k=k)
|
|
|
|
|
word.append(rand.choice(WHITESPACES))
|
|
|
|
|
text.append("".join(word))
|
2024-05-09 13:30:44 +00:00
|
|
|
|
yield "".join(text)
|
|
|
|
|
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def generator_random_vocab_chars(tokenizer: TokenizerGroundtruth, iterations=100) -> Iterator[str]:
|
2024-05-09 13:30:44 +00:00
|
|
|
|
"""Brute force random text with vocab characters"""
|
|
|
|
|
|
2024-05-17 23:09:13 +00:00
|
|
|
|
vocab_chars = set()
|
2024-07-05 17:01:35 +00:00
|
|
|
|
for word in tokenizer.vocab:
|
2024-05-17 23:09:13 +00:00
|
|
|
|
vocab_chars.update(word)
|
|
|
|
|
vocab_chars = list(sorted(vocab_chars))
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
|
|
|
|
rand = random.Random()
|
|
|
|
|
for m in range(iterations):
|
|
|
|
|
rand.seed(m)
|
|
|
|
|
text = rand.choices(vocab_chars, k=1024)
|
|
|
|
|
yield "".join(text)
|
|
|
|
|
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def generator_random_vocab_words(tokenizer: TokenizerGroundtruth, iterations=100) -> Iterator[str]:
|
2024-05-17 23:09:13 +00:00
|
|
|
|
"""Brute force random text from vocab words"""
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
vocab = [w.strip() for w in tokenizer.vocab]
|
2024-05-17 23:09:13 +00:00
|
|
|
|
yield from vocab
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
|
|
|
|
rand = random.Random()
|
|
|
|
|
for m in range(iterations):
|
|
|
|
|
rand.seed(m)
|
|
|
|
|
text = []
|
|
|
|
|
num_words = rand.randint(300, 400)
|
|
|
|
|
for i in range(num_words):
|
|
|
|
|
k = rand.randint(1, 3)
|
2024-05-17 23:09:13 +00:00
|
|
|
|
words = rand.choices(vocab, k=k)
|
2024-05-09 13:30:44 +00:00
|
|
|
|
sep = rand.choice(" \n\r\t")
|
2024-05-17 23:09:13 +00:00
|
|
|
|
text.append("".join(words) + sep)
|
2024-05-09 13:30:44 +00:00
|
|
|
|
yield "".join(text)
|
|
|
|
|
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def compare_tokenizers(tokenizer1: TokenizerGroundtruth, tokenizer2: TokenizerLlamaCpp, generator: Iterator[str]):
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
|
|
|
|
def find_first_mismatch(ids1: list[int], ids2: list[int]):
|
2024-05-17 23:09:13 +00:00
|
|
|
|
for i, (a, b) in enumerate(zip(ids1, ids2)):
|
2024-05-09 13:30:44 +00:00
|
|
|
|
if a != b:
|
|
|
|
|
return i
|
|
|
|
|
if len(ids1) == len(ids2):
|
|
|
|
|
return -1
|
|
|
|
|
return min(len(ids1), len(ids2))
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
def check_detokenizer(text: str, text1: str, text2: str) -> bool:
|
|
|
|
|
if text1 == text2: # equal to TokenizerGroundtruth?
|
|
|
|
|
return True
|
|
|
|
|
# equal to source text?
|
|
|
|
|
if tokenizer1.add_bos_token: # remove BOS
|
|
|
|
|
if text2.startswith(tokenizer1.bos_token):
|
|
|
|
|
text2 = text2[len(tokenizer1.bos_token):]
|
|
|
|
|
if tokenizer1.add_eos_token: # remove EOS
|
|
|
|
|
if text2.endswith(tokenizer1.eos_token):
|
|
|
|
|
text2 = text2[:-len(tokenizer1.eos_token)]
|
|
|
|
|
return text == text2
|
|
|
|
|
|
|
|
|
|
t_encode1 = 0
|
|
|
|
|
t_encode2 = 0
|
|
|
|
|
t_decode1 = 0
|
|
|
|
|
t_decode2 = 0
|
2024-06-18 16:40:52 +00:00
|
|
|
|
t_start = time.perf_counter()
|
2024-07-05 17:01:35 +00:00
|
|
|
|
encode_errors = 0
|
|
|
|
|
decode_errors = 0
|
|
|
|
|
MAX_ERRORS = 10
|
2024-06-18 16:40:52 +00:00
|
|
|
|
|
2024-05-09 13:30:44 +00:00
|
|
|
|
logger.info("%s: %s" % (generator.__name__, "ini"))
|
|
|
|
|
for text in generator:
|
2024-07-05 17:01:35 +00:00
|
|
|
|
# print(repr(text), text.encode())
|
2024-06-18 16:40:52 +00:00
|
|
|
|
# print(repr(text), hex(ord(text[0])), text.encode())
|
|
|
|
|
t0 = time.perf_counter()
|
2024-07-05 17:01:35 +00:00
|
|
|
|
ids1 = tokenizer1.encode(text)
|
2024-06-18 16:40:52 +00:00
|
|
|
|
t1 = time.perf_counter()
|
2024-07-05 17:01:35 +00:00
|
|
|
|
ids2 = tokenizer2.encode(text)
|
2024-06-18 16:40:52 +00:00
|
|
|
|
t2 = time.perf_counter()
|
2024-07-05 17:01:35 +00:00
|
|
|
|
text1 = tokenizer1.decode(ids1)
|
|
|
|
|
t3 = time.perf_counter()
|
|
|
|
|
text2 = tokenizer2.decode(ids1)
|
|
|
|
|
t4 = time.perf_counter()
|
|
|
|
|
t_encode1 += t1 - t0
|
|
|
|
|
t_encode2 += t2 - t1
|
|
|
|
|
t_decode1 += t3 - t2
|
|
|
|
|
t_decode2 += t4 - t3
|
|
|
|
|
if encode_errors < MAX_ERRORS and ids1 != ids2:
|
2024-05-09 13:30:44 +00:00
|
|
|
|
i = find_first_mismatch(ids1, ids2)
|
2024-06-04 07:17:17 +00:00
|
|
|
|
ids1 = list(ids1)[max(0, i - 2) : i + 5 + 1]
|
|
|
|
|
ids2 = list(ids2)[max(0, i - 2) : i + 5 + 1]
|
2024-07-05 17:01:35 +00:00
|
|
|
|
logger.error(" Expected: " + str(ids1))
|
|
|
|
|
logger.error(" Result: " + str(ids2))
|
|
|
|
|
encode_errors += 1
|
|
|
|
|
logger.error(f" {encode_errors=}")
|
|
|
|
|
if decode_errors < MAX_ERRORS and not check_detokenizer(text, text1, text2):
|
|
|
|
|
i = find_first_mismatch(text1, text2)
|
|
|
|
|
text1 = list(text1[max(0, i - 2) : i + 5 + 1])
|
|
|
|
|
text2 = list(text2[max(0, i - 2) : i + 5 + 1])
|
|
|
|
|
logger.error(" Expected: " + " ".join(hex(ord(x)) for x in text1))
|
|
|
|
|
logger.error(" Result: " + " ".join(hex(ord(x)) for x in text2))
|
|
|
|
|
decode_errors += 1
|
|
|
|
|
logger.error(f" {decode_errors=}")
|
|
|
|
|
if encode_errors >= MAX_ERRORS and decode_errors >= MAX_ERRORS:
|
|
|
|
|
logger.error(f" EXIT: {encode_errors=} {decode_errors=}")
|
2024-06-18 16:40:52 +00:00
|
|
|
|
# raise Exception()
|
2024-07-05 17:01:35 +00:00
|
|
|
|
break
|
2024-06-18 16:40:52 +00:00
|
|
|
|
|
|
|
|
|
t_total = time.perf_counter() - t_start
|
2024-07-05 17:01:35 +00:00
|
|
|
|
logger.info(f"{generator.__name__}: end, {t_encode1=:.3f} {t_encode2=:.3f} {t_decode1=:.3f} {t_decode2=:.3f} {t_total=:.3f}")
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
|
|
|
|
|
2024-05-17 23:09:13 +00:00
|
|
|
|
def main(argv: list[str] = None):
|
2024-05-09 13:30:44 +00:00
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
parser.add_argument("vocab_file", help="path to vocab 'gguf' file")
|
|
|
|
|
parser.add_argument("dir_tokenizer", help="directory containing 'tokenizer.model' file")
|
|
|
|
|
parser.add_argument("--verbose", action="store_true", help="increase output verbosity")
|
2024-05-17 23:09:13 +00:00
|
|
|
|
args = parser.parse_args(argv)
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
2024-06-18 16:40:52 +00:00
|
|
|
|
logging.basicConfig(level = logging.DEBUG if args.verbose else logging.INFO)
|
|
|
|
|
logger.info(f"VOCABFILE: '{args.vocab_file}'")
|
2024-05-09 13:30:44 +00:00
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
tokenizer1 = TokenizerGroundtruth(args.dir_tokenizer)
|
|
|
|
|
tokenizer2 = TokenizerLlamaCpp(args.vocab_file)
|
2024-05-17 23:09:13 +00:00
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
# compare_tokenizers(tokenizer1, tokenizer2, generator_custom_text())
|
|
|
|
|
# compare_tokenizers(tokenizer1, tokenizer2, generator_custom_text_edge_cases())
|
|
|
|
|
compare_tokenizers(tokenizer1, tokenizer2, generator_ascii_lr_strip())
|
|
|
|
|
compare_tokenizers(tokenizer1, tokenizer2, generator_apostrophe())
|
|
|
|
|
compare_tokenizers(tokenizer1, tokenizer2, generator_unicodes())
|
|
|
|
|
compare_tokenizers(tokenizer1, tokenizer2, generator_vocab_words(tokenizer1))
|
|
|
|
|
compare_tokenizers(tokenizer1, tokenizer2, generator_added_lr_strip(tokenizer1))
|
|
|
|
|
# compare_tokenizers(tokenizer1, tokenizer2, generator_random_added_tokens(tokenizer1, 10_000))
|
|
|
|
|
# compare_tokenizers(tokenizer1, tokenizer2, generator_random_chars(10_000))
|
|
|
|
|
# compare_tokenizers(tokenizer1, tokenizer2, generator_random_unicodes(10_000))
|
|
|
|
|
# compare_tokenizers(tokenizer1, tokenizer2, generator_random_vocab_chars(tokenizer1, 10_000))
|
|
|
|
|
# compare_tokenizers(tokenizer1, tokenizer2, generator_random_vocab_words(tokenizer1, 5_000))
|
2024-05-28 19:46:34 +00:00
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
tokenizer2.model.free()
|
2024-05-17 23:09:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-05-20 18:15:57 +00:00
|
|
|
|
# main()
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
if True:
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level = logging.DEBUG,
|
|
|
|
|
format = "%(asctime)s.%(msecs)03d %(name)s %(levelname)s %(message)s",
|
|
|
|
|
datefmt = "%Y-%m-%d %H:%M:%S",
|
|
|
|
|
filename = logger.name + ".log",
|
|
|
|
|
filemode = "a"
|
|
|
|
|
)
|
2024-06-18 16:40:52 +00:00
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level = logging.DEBUG,
|
2024-07-05 17:01:35 +00:00
|
|
|
|
format = "%(levelname)s %(message)s",
|
2024-06-18 16:40:52 +00:00
|
|
|
|
)
|
|
|
|
|
|
2024-06-04 07:17:17 +00:00
|
|
|
|
path_tokenizers = "./models/tokenizers/"
|
2024-05-20 18:15:57 +00:00
|
|
|
|
path_vocab_format = "./models/ggml-vocab-%s.gguf"
|
|
|
|
|
|
|
|
|
|
tokenizers = [
|
2024-07-05 17:01:35 +00:00
|
|
|
|
"llama-spm", # SPM
|
|
|
|
|
"phi-3", # SPM
|
|
|
|
|
"gemma", # SPM
|
|
|
|
|
"gemma-2", # SPM
|
|
|
|
|
"baichuan", # SPM
|
|
|
|
|
"bert-bge", # WPM
|
|
|
|
|
"jina-v2-en", # WPM
|
2024-06-18 16:40:52 +00:00
|
|
|
|
"llama-bpe", # BPE
|
2024-07-05 17:01:35 +00:00
|
|
|
|
"phi-2", # BPE
|
|
|
|
|
"deepseek-llm", # BPE
|
|
|
|
|
"deepseek-coder", # BPE
|
2024-06-18 16:40:52 +00:00
|
|
|
|
"falcon", # BPE
|
2024-07-05 17:01:35 +00:00
|
|
|
|
"mpt", # BPE
|
2024-06-18 16:40:52 +00:00
|
|
|
|
"starcoder", # BPE
|
2024-07-05 17:01:35 +00:00
|
|
|
|
"gpt-2", # BPE
|
|
|
|
|
"stablelm2", # BPE
|
|
|
|
|
"refact", # BPE
|
|
|
|
|
"qwen2", # BPE
|
|
|
|
|
"olmo", # BPE
|
2024-06-18 16:40:52 +00:00
|
|
|
|
"jina-v2-es", # BPE
|
|
|
|
|
"jina-v2-de", # BPE
|
|
|
|
|
"smaug-bpe", # BPE
|
2024-07-05 17:01:35 +00:00
|
|
|
|
"poro-chat", # BPE
|
|
|
|
|
"jina-v2-code", # BPE
|
|
|
|
|
"viking", # BPE
|
|
|
|
|
"jais", # BPE
|
2024-05-20 18:15:57 +00:00
|
|
|
|
]
|
|
|
|
|
|
2024-07-05 17:01:35 +00:00
|
|
|
|
logger.info("=" * 50)
|
2024-05-20 18:15:57 +00:00
|
|
|
|
for tokenizer in tokenizers:
|
2024-07-05 17:01:35 +00:00
|
|
|
|
logger.info("-" * 50)
|
2024-06-18 16:40:52 +00:00
|
|
|
|
logger.info(f"TOKENIZER: '{tokenizer}'")
|
2024-05-20 18:15:57 +00:00
|
|
|
|
vocab_file = path_vocab_format % tokenizer
|
|
|
|
|
dir_tokenizer = path_tokenizers + "/" + tokenizer
|
|
|
|
|
main([vocab_file, dir_tokenizer, "--verbose"])
|