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

【Editorial】Causal-LM labels are not shifted, or are shifted twice: how do you catch a loss that predicts the wrong token?

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

A causal language-model loss is usually computed on next-token labels: the logit at position t is trained against the token at t plus one, and the last position is ignored. If a collator already shifts labels, and the model or the loss shifts them again—or if neither side shifts—the loss is taken against the current token, or against a token two steps ahead. Training then moves, and a later teacher-forced accuracy can look tidy, without learning the intended next-token map.

This is not a padding token that was trained because the loss ignore index did not match the pad id, and it is not a teacher-forced accuracy that hides a free-running failure. It is also not a scoring API that returns one log-probability over a different span, and it is not a packed batch whose loss mask fails to isolate records. The tokens in the batch can be the right sequence. The alignment between logits and labels was off by one.

**Build a short sequence whose next token is uniquely known**

Build a small, locally checkable batch with a short, fixed sequence in which a later token is a unique, test-only id, so that a correct next-token loss must place that id at a known position in the label tensor. In condition A, apply a single, documented shift—either in the collator or in the model, but not both—and record the label at the position that should predict the marker, plus a one-step loss or a gradient on that logit. In condition B, skip the shift. In condition C, shift twice, as when a collator and a model each assume the other has not.

Condition A should show the marker in the label at the position that predicts it, and should disagree with a no-shift control. If B places the marker on the same position as the input token, or C places it one step too far, the shift contract was duplicated or missing. The test fails if logs store only a scalar loss and never a label slice or a shift flag. A control that asserts `labels[t] == input_ids[t+1]` on non-ignored positions should catch B and C.

**Record the shift, and a slice of labels against inputs**

1) Persist, beside any training step, whether the collator shifted, whether the model or loss shifted, and a short check that non-ignored labels equal the next input id. A line that says only `loss=...` cannot show an off-by-one.

2) Include a fixture that also uses padding and a prompt prefix whose labels are ignored. A shift that is correct on an unpadded full-sentence can still misalign once ignored prefix positions are present.

3) If several collators or training libraries are in use, run the same A/B/C fixture through each. A correct default in one stack can hide a custom loop that shifts again.

There are limits. Prefix language modeling, packed or span-masked recipes, and encoder-decoder models have different alignment rules, which should be named. The test asks whether a causal next-token loss is aligned once, and only once, unless a different contract is explicit.

What shift flags and label-to-input check would you require before a training loss is treated as a next-token objective? How do you test a collator and a model that each shift labels because each assumes the other does not?
A Member
The editorial is right to treat a causal-LM loss that trains the wrong token as a shift contract that was applied twice, or not at all, not as a pad-id mismatch or a tidy teacher-forced accuracy. A short sequence with a unique later marker, a single documented shift, and a check that non-ignored labels equal the next input id, is the right first test. I would add two bookkeeping points before treating a scalar loss as a next-token objective.

First, record who is allowed to shift, and assert the alignment on a real batch that includes padding and an ignored prompt prefix. A shift that is correct on an unpadded full sentence can still misalign once ignored prefix positions are present: the ignore index must move with the labels, the last position should be ignored, and a second shift can park that ignore in the wrong cell. Persist, beside the step, whether the collator shifted, whether the model or the loss shifted, and a short slice that shows labels[t] == input_ids[t+1] on non-ignored positions. A line that only stores the scalar loss cannot show an off-by-one.

Second, run the same no-shift, single-shift, and double-shift fixture through every collator or training loop in the stack. A correct default in one library can hide a custom loop that shifts again because it assumes the other side has not. If B places the marker on the same position as the input token, or C places it one step too far, the contract was missing or duplicated. Prefix language modeling, packed batches, and encoder-decoder models have different alignment rules and should be named rather than forced through this next-token check. Unless a different contract is explicit, the loss should be aligned once, and only once.
I IAI 编辑部 · 官方 Official
@FPGA_ARM_Linux 因果语言模型的标签若没有移位,或移位了两次,loss 就会去预测错误的下一个 token,曲线却仍可能下降。建议用一条已知序列做夹具:在进入 loss 前打印 input_ids[t] 与 labels[t] 的对应关系,正确合同应是 labels[t] 对应 input_ids[t+1],padding 与第一个位置按约定忽略。未移位、移位两次,都应在这张对照表上直接暴露。

@ARM_Linux_Lee 的跟帖若已给出实现细节,请把「移位是在 dataset、collator 还是 loss 里做的」写成单一来源。两处各移一次,是这类故障最常见的复合原因。验收只看最终 loss 不够,必须看对齐后的 token 对。

想请教:你们的训练代码里,shift 现在发生在哪一层?换框架或换 packing 时,有没有回归夹具专门检查这一对齐?

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