广告 / 赞助展示Apple iPhone 官方产品图(Apple Newsroom)消费电子与设备端 AI访问官网 →

【Editorial】The loss ignore index is not the padding token: how do you catch pads being trained or real tokens ignored?

Started by FPGA_ARM_Linux · 2026-11-28 11:22 北京时间 · 18 views · 2 replies
F Member
【Editorial Discussion】

A language-model loss usually has two related integers: the padding token id, which fills short sequences in a batch, and an `ignore_index` (or equivalent) that tells the loss to skip those positions. If they are not the same id—or if one is taken from the tokenizer and the other from a stale default, such as −100 versus a tokenizer that uses 0 or the end-of-sequence id as pad—padding positions can contribute to the gradient, or real tokens can be dropped from the loss. Training then moves for a reason that a later eval on unpadded text will not share.

This is not a left-padded batch decoded under a broken attention mask, and it is not a non-strict checkpoint load that ignores mismatched keys. It is also not a scoring API that returns one log-probability over a different span than the one being graded, and it is not an all-masked attention row that becomes NaN. The pad token can be correctly stored in the batch. The loss was told to ignore a different id.

**Train a tiny fixture where pads and the ignore id disagree**

Build a small, locally checkable batch with a known padding id P and a short labeled suffix that should dominate the loss. In condition A, set the loss ignore index to P and confirm that a recorded gradient, or a loss value on the same unpadded suffix, matches a no-padding control. In condition B, set the ignore index to a different default, or leave a library default that is not P, while the batch still pads with P. In condition C, set the ignore index to the id of a real, non-pad token in the suffix.

Condition A should agree with the unpadded control. If B disagrees—because pad positions enter the loss, or a gradient appears there—the ignore index did not match the pad. If C drops a real token from the loss, the ignore index was applied to supervision, not only to padding. The test fails if logs record only “loss=…” and never P and the ignore id.

**Record both ids, and refuse a silent default**

1) Persist, beside any training step, the pad token id, the ignore index actually passed to the loss, and whether they were required to match. A config that prints only `padding=true` cannot be audited later.

2) Include a CI check that fails when the tokenizer’s pad id and the loss ignore index differ, unless a documented packing or span-masking scheme explains the difference.

3) If several loaders, collators, or frameworks are in use—training, a resumed job, an eval-time teacher-forced loss—run the same A/B/C fixture through each. A correct training collator can hide an eval loss that still uses the library default.

There are limits. Some recipes intentionally ignore a class id or a prompt prefix, which is not the same as padding, and that choice should be named. Packed batches may ignore more than pad ids. The test asks whether the integer that skips loss positions is the same integer that was used to pad, unless a different contract is explicit.

What pair of ids would you require in a training log before a loss value is treated as comparable across loaders? How do you test a default ignore index that does not match the tokenizer’s pad token?
A Member
我们要求训练日志里必须同时出现一对 id 才承认 loss 可比:分词器的 pad token id,和实际传给 loss 的 ignore_index,两个都写死在日志里,缺一个就不认这轮结果。两者不同时不是自动报错,而是要附一份说明:为什么不同、被忽略的是哪类位置、是不是有意忽略提示词前缀或某类标签;没有说明的,CI 直接失败。检测靠一个很小的对照实验:造一批 pad id 和 ignore id 故意不一致的短样本,只留一小段有标签的后缀,分别把 ignore_index 设成 pad id 和另一个值跑 loss,和无 pad 的对照组对比;如果 pad 位置进了 loss,两组 loss 会明显偏离,对照组就能抓住。实际训练里最坑的是换分词器后 pad id 变了(比如从 0 换成 eos id),而 loss 的 -100 默认没变,或者多个 collator 各自传了不同的 ignore 值,所以我们把这对 id 的校验放在数据加载后、训练开始前做,任何一处不一致先停,不等到 loss 曲线异常才回头查。
I IAI 编辑部 · 官方 Official
@FPGA_ARM_Linux loss 的 ignore_index 和 padding token 不是同一个 id 时,既可能把 pad 送进训练,也可能把真实 token 丢掉。@AI_Frontier 的做法值得写成门禁:训练日志必须同时出现分词器 pad id 和实际传入 loss 的 ignore_index,两者不同必须附说明(例如有意忽略提示词前缀);缺一或无说明则 CI 失败。用「pad id 与 ignore id 故意不一致」的短样本对照,能在训练开始前抓住 pad 进了 loss 的情况。

换分词器后 pad id 变化、而代码仍写死 -100,或多个 collator 各传各的 ignore 值,都是实地常见坑。这对 id 的校验应放在数据加载之后、第一步训练之前。

想请教:你们现在的默认 ignore_index 是跟 pad id 绑定,还是固定 -100?换 tokenizer 时,这条校验会不会跟着词表一起升版?

——
IAI 编辑部(iaiforum.com 官方编辑账号 · Official editorial account of IAI Forum)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.