|
import importlib |
|
from enum import Enum, auto |
|
|
|
|
|
""" |
|
Interface: |
|
- |
|
|
|
tokenizer.parent = "" |
|
tokenizer.type = TokenizerType.ByteBPE.name |
|
tokenizer.implementation = TokenizerImpl.SentencePiece.name # https://github.com/facebookresearch/llama/blob/main/llama/tokenizer.py |
|
tokenizer.comments = "split all numbers into individual digits, " \ |
|
"and fallback to bytes to decompose unknown UTF-8 characters" |
|
""" |
|
|
|
Animal = Enum('Animal', 'ANT BEE CAT DOG') |
|
|
|
uniq_tokenizers = [ |
|
"" |
|
] |
|
|
|
all_tokenizers = [ |
|
"gpt_35_turbo", |
|
"gpt2", |
|
"gpt2_chinese", |
|
"bert_chinese", |
|
"moss", |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"gpt_nexo_20b", |
|
|
|
|
|
|
|
|
|
"chatglm_6b", |
|
|
|
|
|
"llama", |
|
"chinese_llama_lora_7b", |
|
|
|
|
|
|
|
"baichuan_7b", |
|
"qwen" |
|
] |
|
|
|
|
|
|
|
class TokenizerType(Enum): |
|
""" |
|
- https://huggingface.co/docs/transformers/tokenizer_summary |
|
- https://github.com/EleutherAI/gpt-neox/blob/main/megatron/tokenizer/tokenizer.py |
|
- https://github.com/google/sentencepiece/blob/3863f7648e5d8edb571ac592f3ac4f5f0695275a/src/sentencepiece_model.proto#L48 |
|
- UNIGRAM = 1; // Unigram language model with dynamic algorithm |
|
- BPE = 2; // Byte Pair Encoding |
|
- WORD = 3; // Delimitered by whitespace. |
|
- CHAR = 4; // tokenizes into character sequence |
|
""" |
|
BPE = auto() |
|
ByteBPE = auto() |
|
GPT2BPETokenizer = auto() |
|
BERTTokenizer = auto() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TokenizerImpl(Enum): |
|
""" |
|
""" |
|
SentencePiece = auto() |
|
|
|
|
|
|
|
|
|
GPT2Tokenizer = auto() |
|
BertTokenizer = auto() |
|
|
|
|
|
|
|
def load_tokener(model_name): |
|
tokenizer = importlib.import_module("." + model_name, 'vocab').tokenizer |
|
return tokenizer |
|
|
|
if __name__ == "__main__": |
|
pass |