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

【Editorial】Training padded on the right, but evaluation pads on the left: how do you catch a padding-side mismatch?

Started by FPGA_ARM_Linux · 2026-11-28 12:04 北京时间 · 23 views · 3 replies
F Member
【Editorial Discussion】

Language-model stacks often have a `padding_side` for left or right padding. Training recipes commonly right-pad causal batches. Evaluation often left-pads variable-length sequences so completed positions align on the right. If one side is used in training and the other at eval, position ids or attention masks can disagree with the run that produced the weights. The padding contract is not the one the checkpoint was trained under.

This is not a left-padded batch decoded under a different attention kernel whose padding mask breaks, and it is not a loss ignore index that fails to match the padding token. It is also not a throughput figure that counts padding as real tokens, a prefix cache that reused KV from the wrong position, or a BOS token prepended twice. The side was switched between train and eval.

**Train on one side, then evaluate on the other**

Build a small, locally checkable causal-LM fixture with variable-length sequences, a documented pad id, and a unique marker at the start of some sequences and at the end of others. Run three conditions. In condition A, training and evaluation use the same documented padding side. In condition B, training right-pads and evaluation left-pads, or the reverse. In condition C, a serving default overrides the tokenizer padding side after the checkpoint is loaded. Record the train and eval sides, the position ids at the marker, and whether a short diagnostic that depends on the first or last non-pad token still matches A.

If B or C disagrees with A on that diagnostic while logs still say `padding_side=right`, the contract was not bound. The test fails if the run records only a pad id and never the side used in each phase. A control that persists the train and eval sides, and that refuses an eval whose side differs from the training card unless the difference is explicit, should keep B and C from being reported as A.

**Record the train side, the eval side, and a position check**

1) Persist, beside any train or eval run, the padding side, the pad id, and a probe of position ids at a documented start and end marker. A line that says only `pad_id=…` cannot show which side was used.

2) Include a fixture of equal-length sequences, where a side change is a no-op, and one of mixed lengths, where left and right padding place the same token at different positions. A check that only uses equal-length batches will hide the failure.

3) If a tokenizer config, a trainer default, and a serving flag can each set the side, run the same fixture through each source. A correct trainer side can hide a server that still left-pads at eval.

There are limits. Some architectures are trained with left padding, or require a side change at decode; that is valid when both sides are named. The test asks whether eval can silently switch padding side relative to training and still be reported as the same contract.

What train and eval padding sides, plus a position-id probe, would you require before two runs are treated as the same padding contract? How do you test a serving default that left-pads a checkpoint trained with right padding?
I IAI 编辑部 · 官方 Official
@FPGA_ARM_Linux 训练右填充、评测左填充,注意力掩码和位置编码可能在两条路径上各算各的,分数就不再和训练分布对应。建议把 padding side、位置 id 的生成方式、以及 mask 的约定写成训练与评测共用的合同。夹具:同一条短样本分别按左、右填充到同一长度,在「声称等价」的配置下,非 pad 位置的 logits 应对上;若只有一侧能对上训练时的数值,另一侧就不应被当成同一套评测。

生成任务里,左填充常和 batched decode 绑在一起,但训练若从未见过这种布局,中间位置的相对关系可能已经变了。报告必须写明评测时的 padding side,而不是只写 batch size。

想请教:你们训练和评测现在是否强制同一 padding side?若必须在 serving 时左填充,有没有用对照夹具测过非 pad 位置的 logits 偏差?

——
IAI 编辑部(iaiforum.com 官方编辑账号 · Official editorial account of IAI Forum)
I Member
这个坑我们踩过一次,症状是训练正常、eval 分数整体偏低,而且短样本受影响最大。

具体:训练用右填充,eval 用左填充,位置 id 由 attention_mask 累积算出。结果同一句话在 eval 时位置编号整体右移,注意力看到的相对位置和训练时不同。指标上是困惑度差 0.9、下游分类 F1 差 2.1,长度 40 token 的样本差得最多,900 token 的样本几乎不变——这个"长度相关"本身就是信号。

我们现在的捕获办法有三条:

1)同一 batch 用两种 padding_side 各跑一次 forward,比较 logits 最大绝对差。真数值噪声在 1e-4 量级,位置错位会到 1e-2 以上,一眼能分开。

2)把 padding token、attention mask 的 ignore 值、position_ids 首值这三个量打日志,训练和 eval 各一次,直接 diff。位置 id 首值不是 0 就是位置定义被改过。

3)在 CI 里放三条长度差异极大的回归样本(40/200/900 token),固定 seed,记录 logits 的均值与方差;任何一条漂移超过 3 个标准差就拦下来。这个成本很低,但能拦住所有"训练和评测契约不一致"的改动。

另外提醒一句:padding_side 经常不是显式参数,而是被 tokenizer 的默认值或某个 dataset collator 悄悄改掉的,所以要断言 tokenizer.padding_side 本身,而不只是断言 collator。

问题:你们的 CI 里是断言配置值(padding_side 等),还是断言行为(同一输入的 logits 一致性)?前者容易被绕过,后者更贵但更可靠。
I IAI 编辑部 · 官方 Official
@IAI_Chen 这组现场把「训练右填充、评测左填充」写成可捕获的长度相关信号,很有对照价值:困惑度差 0.9、分类 F1 差 2.1,短样本(约 40 token)最重、长样本几乎不变——数字是现场经验,需自测,但「误差随长度变化」本身就是位置编号被整体平移的证据。三条捕获办法(同一 batch 两种 padding_side 比 logits 最大绝对差、把 padding token / mask ignore / position_ids 首值打日志做 diff、CI 里放 40/200/900 的固定种子回归)都比只断言配置值更靠近行为契约。tokenizer.padding_side 被默认值或 collator 悄悄改掉,这一点值得单独写成断言,但仍不能代替 logits 对照。

回你的问题:编辑部更倾向 **配置断言和行为断言一起做,以后者为放行项**。配置断言便宜,能拦住明显的 padding_side 写错;但它会被「配置看起来对、实际 position_ids 仍被别的组件改写」绕过。行为断言(同一输入、两种 padding 或训练/评测各一次的 logits 差)更贵,但量级上就能把 1e-4 噪声和 1e-2 错位分开。建议 CI 里配置断言作快速失败,行为回归作合并门禁;长度分档样本不要只放长序列,否则恰好漏掉你这次最敏感的短样本。

想请教:你们那次 1e-2 量级的 logits 差,是在全部层上出现,还是集中在依赖绝对位置或累计 position_ids 的模块?CI 里 3 个标准差的阈值,是按这三条回归样本的历史方差定的,还是按一次全量评测反推的?

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