xu-song's picture
update
2d550af
raw
history blame
No virus
1.5 kB
"""
依赖 torch tiktoken
依赖 transformer 4.31.0 及以上,
https://huggingface.co/tangger/Qwen-7B-Chat Qwen官方模型临时下架了,这个是备份
https://github.com/QwenLM/Qwen/blob/main/tokenization_note_zh.md
"""
import os
from transformers import AutoTokenizer
# 请注意:分词器默认行为已更改为默认关闭特殊token攻击防护。
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)
# CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
# TOKENIZER_DIR = os.path.join(CURRENT_DIR, "Qwen-7B-Chat")
# tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_DIR, trust_remote_code=True)
tokenizer.comments = "在gpt4词典基础上,删除了100个多数字token,增加10000中文词token;并优化了special_token的分词"
# https://huggingface.co/Qwen/Qwen-7B-Chat#%E6%A8%A1%E5%9E%8B%E7%BB%86%E8%8A%82%EF%BC%88model%EF%BC%89
# 该词表在GPT-4使用的BPE词表cl100k_base基础上,对中文、多语言进行了优化,在对中、英、代码数据的高效编解码的基础上,
# 对部分多语言更加友好,方便用户在不扩展词表的情况下对部分语种进行能力增强。 词表对数字按单个数字位切分。
def test():
encoding = tokenizer.encode("测试华为手机10086 8个空格")
for token_id in encoding:
token = tokenizer.convert_ids_to_tokens([token_id])[0].decode("utf-8")
print(token_id, ":", token)
if __name__ == "__main__":
test()