[Bug] Literal EOS strings in SFT content are treated as termination tokens

未关闭
#2,134 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 4 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
huggingface, python

调研方向

Start at OpenaiTokenizeFunctionConfig.build and reproduce the issue with the GLM-5.2 tokenizer and the provided messages. Trace how rendered content is tokenized and how labels are masked; done means literal EOS strings become ordinary text while template-added response terminators remain supervised EOS tokens, with regression coverage and affected caches handled.

由索引模型根据 Issue 内容生成。

描述

XTuner can encode literal EOS strings in SFT content as actual termination tokens and include them in the training loss. For example, an assistant explaining <|endoftext|> should generate that spelling as ordinary text, while the template-added response terminator should remain a special token.

Reproduction

Verified with XTuner commit e7299bbc, Transformers 5.14.1, and the GLM-5.2 tokenizer:

from transformers import AutoTokenizer
from xtuner.v1.datasets import OpenaiTokenizeFunctionConfig

tokenizer = AutoTokenizer.from_pretrained(
    "zai-org/GLM-5.2", trust_remote_code=True
)
tokenize_fn = OpenaiTokenizeFunctionConfig(
    chat_template="glm5.2"
).build(tokenizer)

sample = tokenize_fn({
    "messages": [
        {"role": "user", "content": "解释 GPT-2 的结束标记。"},
        {
            "role": "assistant",
            "content": "GPT-2 使用 <|endoftext|> 作为结束标记。",
        },
    ]
})

print([
    (i, sample["labels"][i])
    for i, token_id in enumerate(sample["input_ids"])
    if token_id == tokenizer.eos_token_id
])

Output:

[(28, 154820), (33, 154820)]

Both the literal inside the sentence and the actual response terminator become supervised EOS targets. Only the latter should be an EOS token.

Impact and scope

This produces incorrect EOS supervision and can cause unexpected early termination during inference. Tool calls can also be affected when their arguments contain EOS literals—for example, inside code or shell commands—potentially leaving the generated call incomplete.

The underlying behavior is not specific to GLM-5.2: Hugging Face tokenizers recognize registered special-token strings even with add_special_tokens=False. Other models can therefore be affected when their EOS spellings appear in training content; the exact markers depend on the tokenizer.

Proposed fix

Implement EOS-aware handling directly in the rendering/tokenization pipeline:

  • Track template-inserted response terminators and preserve them as special tokens.
  • Encode literal EOS occurrences in message content, reasoning, and tool arguments as ordinary subwords, while preserving other control tokens and model-specific turn boundaries.
  • Preserve existing loss-mask semantics, invalidate affected caches, and add regression tests.

Globally enabling split_special_tokens=True on the rendered conversation would also split genuine template controls, so the handling should be selective.

Would this approach be suitable? If maintainers agree, I would be happy to prepare a PR with the implementation and regression tests.

主要语言
Python
星标
5.2k
派生
466
平均合并
5 天 8 小时
30 天内合并 PR
44

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

InternLM/xtuner 的其他 Issue

查看 InternLM/xtuner 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。