关于tokenizer对特殊字符的处理
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 12.3k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
你好,感谢你们的出色工作。
我在使用模型的 tokenizer 时遇到了一些问题。当我使用 tokenizer 对包含连续的空格和特殊字符(如 \n, \t 等)进行 encode 操作时,我发现 tokenizer 会忽略周围有多个空格的特殊字符,或者会将这些特殊字符转为多个空格,这使得再次进行 decode 操作后,文本中这些特殊字符会丢失。测试的代码如下:
tokenizer = AutoTokenizer.from_pretrained("fnlp/moss-moon-003-sft", trust_remote_code=True)
text = "a \n b\n \nc \n "
# input_ids: [64, 106058, 106058, 65, 106057, 66, 220, 56599]
input_ids = tokenizer.encode(text, add_special_tokens=False)
# 输出: "a b c \n "
print(tokenizer.decode(input_ids))
我按照这样测试其他模型的 tokenizer 时均未发现这个现象,其他模型的 tokenizer 对文本经过 encode 和 decode 后均会还原为和原来一致的文本。
这个问题是我对 MOSS 进行微调后发现的,我发现微调后的 MOSS 模型在生成代码时,当遇到代码中有换行加缩进的时候,微调模型从来不会生成换行,而原本的 MOSS 在这种情况下是会生成换行的。在我检查了 tokenizer 后发现了这一问题,请问应该如何解决呢?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided Python reproduction with AutoTokenizer and the fnlp/moss-moon-003-sft model, then inspect the tokenizer's encode/decode behavior for spaces, newlines, and tabs. Done means the round trip preserves these characters and fine-tuned generation can retain newlines with indentation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100