-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from LlmKira/dev
(feat): Novelai tokenizer re-implement || New LLM
- Loading branch information
Showing
28 changed files
with
3,950 additions
and
950 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,3 +177,4 @@ cython_debug/ | |
/playground/art_assert/ | ||
/playground/unpack/ | ||
/playground/boom-train/ | ||
/frontend/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import json | ||
import os | ||
import pathlib | ||
import zlib | ||
from typing import Dict, List, Optional | ||
|
||
import requests | ||
from json_repair import repair_json | ||
from pydantic import BaseModel, model_validator | ||
from tokenizers import Tokenizer, pre_tokenizers, Regex, decoders | ||
from tokenizers.models import BPE | ||
|
||
# https://novelai.net/tokenizer/compressed/llama3nai_tokenizer.def?v=2&static=true | ||
|
||
model_name = "clip_tokenizer" | ||
model_full_name = f"{model_name}.def" | ||
url = f"https://novelai.net/tokenizer/compressed/{model_full_name}?v=2&static=true" | ||
if not os.path.exists(model_full_name): | ||
print(f"Downloading {model_full_name} from {url}") | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
# write down | ||
with open(model_full_name, "wb") as f: | ||
f.write(response.content) | ||
|
||
|
||
class TokenizerSetting(BaseModel): | ||
class TokenizerConfig(BaseModel): | ||
splitRegex: str | ||
maxEncodeChars: Optional[int] = None | ||
maxNoWhitespaceChars: Optional[int] = None | ||
ignoreMerges: Optional[bool] = False | ||
|
||
config: TokenizerConfig | ||
specialTokens: List[str] | ||
vocab: Dict[str, int] | ||
merges: list | ||
|
||
@model_validator(mode="after") | ||
def ensure(self): | ||
self.merges = [tuple(merge) for merge in self.merges] | ||
return self | ||
|
||
|
||
# 读取和解压文件 | ||
file = pathlib.Path(__file__).parent.joinpath(model_full_name) | ||
encoded_data = file.read_bytes() | ||
decompress_obj = zlib.decompressobj(-zlib.MAX_WBITS) | ||
decode = decompress_obj.decompress(encoded_data) | ||
|
||
# 修复和解析 JSON | ||
repaired_json = repair_json(decode.decode('utf-8'), return_objects=True) | ||
json.dump(repaired_json, open(f"{model_name}.json", "w"), indent=2) | ||
tokenizer_setting = TokenizerSetting.model_validate(repaired_json) | ||
|
||
# 创建 tokenizer | ||
tokenizer = Tokenizer(BPE( | ||
vocab=tokenizer_setting.vocab, | ||
merges=tokenizer_setting.merges, | ||
ignore_merges=tokenizer_setting.config.ignoreMerges | ||
)) | ||
|
||
# 设置特殊 tokens | ||
tokenizer.add_special_tokens(tokenizer_setting.specialTokens) | ||
print(tokenizer.token_to_id(" ")) | ||
if tokenizer_setting.config.maxEncodeChars: | ||
tokenizer.enable_truncation(max_length=tokenizer_setting.config.maxEncodeChars) | ||
# 设置 normalizer | ||
# tokenizer.normalizer = normalizers.Sequence([]) | ||
|
||
# 设置 pre_tokenizer | ||
pre_zus = [ | ||
pre_tokenizers.Split( | ||
behavior="merged_with_next", | ||
pattern=Regex(tokenizer_setting.config.splitRegex) | ||
), | ||
] | ||
if tokenizer.token_to_id(" ") is None: | ||
pre_zus.append(pre_tokenizers.ByteLevel(add_prefix_space=False, use_regex=False)) | ||
pre_tokenizer = pre_tokenizers.Sequence(pre_zus) | ||
|
||
tokenizer.pre_tokenizer = pre_tokenizer | ||
tokenizer.decoder = decoders.ByteLevel() | ||
|
||
# 使用 tokenizer | ||
text = "Hello, World! This is a test." | ||
encoded = tokenizer.encode(text, add_special_tokens=True) | ||
print(f"Pre-tokenized text: {pre_tokenizer.pre_tokenize_str(text)}") | ||
print(f"Encoded tokens: {encoded.tokens}") | ||
print(f"Token IDs: {encoded.ids}") | ||
|
||
# 解码 | ||
decoded = tokenizer.decode(encoded.ids) | ||
print(f"Decoded text:{decoded}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "novelai-python" | ||
version = "0.4.17" | ||
version = "0.5.0" | ||
description = "NovelAI Python Binding With Pydantic" | ||
authors = [ | ||
{ name = "sudoskys", email = "[email protected]" }, | ||
|
@@ -26,6 +26,8 @@ dependencies = [ | |
"ftfy>=6.2.0", | ||
"regex>=2023.12.25", | ||
"tokenizers>=0.15.2", | ||
"json-repair>=0.29.4", | ||
"robust-downloader>=0.0.2", | ||
] | ||
requires-python = ">=3.9" | ||
readme = "README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from enum import Enum | ||
from typing import Optional, Union | ||
|
||
|
||
class TextLLMModel(Enum): | ||
NEO_2B = "2.7B" | ||
J_6B = "6B" | ||
J_6B_V3 = "6B-v3" | ||
J_6B_V4 = "6B-v4" | ||
GENJI_PYTHON_6B = "genji-python-6b" | ||
GENJI_JP_6B = "genji-jp-6b" | ||
GENJI_JP_6B_V2 = "genji-jp-6b-v2" | ||
EUTERPE_V0 = "euterpe-v0" | ||
EUTERPE_V2 = "euterpe-v2" | ||
KRAKE_V1 = "krake-v1" | ||
KRAKE_V2 = "krake-v2" | ||
BLUE = "blue" | ||
RED = "red" | ||
GREEN = "green" | ||
PURPLE = "purple" | ||
PINK = "pink" | ||
YELLOW = "yellow" | ||
WHITE = "white" | ||
BLACK = "black" | ||
CASSANDRA = "cassandra" | ||
COMMENT_BOT = "hypebot" | ||
INFILL = "infillmodel" | ||
CLIO = "clio-v1" | ||
KAYRA = "kayra-v1" | ||
ERATO = "llama-3-erato-v1" | ||
|
||
|
||
class TextTokenizerGroup(object): | ||
GENJI = "genji_tokenizer.def" | ||
PILE = "pile_tokenizer.def" | ||
PILE_NAI = "pile_tokenizer.def" | ||
NAI_INLINE = "gpt2_tokenizer.def" | ||
NERDSTASH_V2 = "nerdstash_tokenizer_v2.def" | ||
NERDSTASH = "nerdstash_tokenizer.def" | ||
LLAMA3 = "llama3_tokenizer.def" | ||
GPT2 = "gpt2_tokenizer.def" | ||
CLIP = "clip_tokenizer.def" | ||
|
||
|
||
TextLLMModelTypeAlias = Union[TextLLMModel, str] | ||
|
||
TOKENIZER_MODEL_MAP = { | ||
TextLLMModel.GENJI_JP_6B_V2: TextTokenizerGroup.GENJI, | ||
TextLLMModel.CASSANDRA: TextTokenizerGroup.PILE, | ||
TextLLMModel.KRAKE_V2: TextTokenizerGroup.PILE, | ||
TextLLMModel.INFILL: TextTokenizerGroup.NAI_INLINE, | ||
TextLLMModel.KAYRA: TextTokenizerGroup.NERDSTASH_V2, | ||
TextLLMModel.BLUE: TextTokenizerGroup.NERDSTASH_V2, | ||
TextLLMModel.PINK: TextTokenizerGroup.NERDSTASH_V2, | ||
TextLLMModel.YELLOW: TextTokenizerGroup.NERDSTASH_V2, | ||
TextLLMModel.RED: TextTokenizerGroup.NERDSTASH_V2, | ||
TextLLMModel.GREEN: TextTokenizerGroup.NERDSTASH_V2, | ||
TextLLMModel.BLACK: TextTokenizerGroup.NERDSTASH_V2, | ||
TextLLMModel.CLIO: TextTokenizerGroup.NERDSTASH, | ||
TextLLMModel.PURPLE: TextTokenizerGroup.LLAMA3, | ||
TextLLMModel.WHITE: TextTokenizerGroup.LLAMA3, | ||
TextLLMModel.ERATO: TextTokenizerGroup.LLAMA3, | ||
} | ||
|
||
COLORS_LLM = [ | ||
TextLLMModel.BLUE, | ||
TextLLMModel.RED, | ||
TextLLMModel.GREEN, | ||
TextLLMModel.PURPLE, | ||
TextLLMModel.PINK, | ||
TextLLMModel.YELLOW, | ||
TextLLMModel.WHITE, | ||
TextLLMModel.BLACK, | ||
] | ||
|
||
|
||
def get_llm_group(model: TextLLMModel) -> Optional[TextTokenizerGroup]: | ||
if isinstance(model, str): | ||
model = TextLLMModel(model) | ||
return TOKENIZER_MODEL_MAP.get(model, None) | ||
|
||
|
||
def get_tokenizer_model(model: TextLLMModel) -> str: | ||
if isinstance(model, str): | ||
model = TextLLMModel(model) | ||
group = TOKENIZER_MODEL_MAP.get(model, TextTokenizerGroup.GPT2) | ||
return group | ||
|
||
|
||
def get_tokenizer_model_url(model: TextLLMModel) -> str: | ||
model_name = get_tokenizer_model(model) | ||
if not model_name.endswith(".def"): | ||
model_name = f"{model_name}.def" | ||
return f"https://novelai.net/tokenizer/compressed/{model_name}?v=2&static=true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.