Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yiguanxian opened this issue Apr 23, 2024 · 2 comments
Open

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

yiguanxian opened this issue Apr 23, 2024 · 2 comments

Comments

@yiguanxian
Copy link

【现象】
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)

@yiguanxian yiguanxian changed the title 千问qwen1.5解码错误 千问qwen1.5-14B-chat解码错误 Apr 23, 2024
@yiguanxian
Copy link
Author

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

@yiguanxian
Copy link
Author

yiguanxian commented Apr 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant