ztxz16 / fastllm

纯c++的全平台llm加速库,支持python调用,chatglm-6B级模型单卡可达10000+token / s,支持glm, llama, moss基座,手机端流畅运行

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

千问qwen1.5-14B-chat解码错误

yiguanxian opened this issue · comments

【现象】
qwen1.5-14B-Chat模型在解码时报UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 1: unexpected end of data。
【描述】
模型输入是:假设f(x)=x,那么f(x)1到2的积分是多少。模型输出的tokenId包含11995、18137,这两个tokenId会导致上述异常。它们在词表中对应的是特殊字符,解码tokenId的方法是:model = pyfastllm.create_llm(model_path); model.weight.tokenizer.decode([11995, 18137])。另外尝试用原始模型的tokenizer解码是可以解码的,只是显示出来的是人类无法理解的字符,它不抛上述解码异常。我觉得1是要处理解码异常的问题,2是生成的tokenId应该是有问题的,即使它们被正常解码出来了,它们似乎与问题也不太相关。
【flm模型转换方法】
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained(xxx, trust_remote_code = True)
model = AutoModel.from_pretrained(xxx, trust_remote_code = True)
from fastllm_pytools import llm
model = llm.from_hf(model, tokenizer, dtype = "int8")
model.save("model.flm")
【模型推理方法】
prompt_input = “假设f(x)=x,那么f(x)1到2的积分是多少”
model = pyfastllm.create_llm("model.flm")
input_ids = model.weight.tokenizer.encode(prompt_input)
handle = model.launch_response(input_ids)
...
resp_token = model.fetch_response(handle)
content = model.weight.tokenizer.decode([resp_token])
print(content)

我看了下是因为有的解码需要两个或多个token一起解码才行,而model.fetch_response每次只会得到一个token

已解决。原因是:有的需要两个或多个tokenid才能解码出有意义的字符,单个tokenid解码出来的可能是无意义的乱码。解决办法:1.通过try-catch捕获UnicodeDecodeError异常,然后将多个tokenid拼在一起解码。2.通过ret_byte=model.weight.tokenizer.decode_byte([token_id]); res = ret_byte.decode(errors='ignore')去解码。建议使用方法2。