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

【Editorial】The attention backend switches when the sequence crosses a length threshold: how do you catch a silent kernel change?

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

Inference stacks often have more than one attention implementation—flash, SDPA, eager, or a vendor kernel—and may switch automatically when the sequence length or head dimension crosses a threshold. If a long-context eval, or a mixed-length batch, silently changes kernel mid-run, numerics can change while the weights and the mask stay the same. The backend that produced a token was not the one the recipe pinned.

This is not a left-padded batch decoded under a different attention kernel whose padding mask breaks, and it is not an all-masked attention row that becomes NaN from 0 times -inf. It is also not a CUDA graph captured on one shape and replayed on a later padded batch, a model trained with bidirectional prompt attention and served as strictly causal, or an optional GPU extension that failed to import and fell back to CPU. The implementation switched at a length threshold.

**Pin one backend, then cross the switch threshold**

Build a small, locally checkable model and a documented length threshold at which the stack’s automatic selector changes implementation. Run three conditions. In condition A, the backend is pinned and does not change when length crosses the threshold. In condition B, `auto` is left on, and the same items are run just below and just above the threshold. In condition C, a mixed-length batch contains both sides of the threshold. Record the backend used for each length and a probe of the attention output on a fixed item.

If B or C disagrees with A when length crosses the threshold, or if logs store only `attn=auto` and never the implementation that ran, the backend was not bound. The test fails if the stack records only a latency number and never the kernel name. A control that pins the backend, writes the implementation beside each length, and refuses an `auto` switch in a reported eval, should keep B and C from being cited as A.

**Record the implementation, the length, and an output probe**

1) Persist, beside any train or eval forward, the attention implementation actually used, the sequence length, and a probe of the attention output on a documented item. A line that says only `attn=auto` cannot show which kernel ran.

2) Include a fixture of lengths that all sit on one side of the threshold, where a switch is a no-op, and one that crosses it, including a mixed-length batch. A check that only uses one short length will hide the failure.

3) If a library default, an environment flag, and a serving config can each select the backend, run the same fixture through each source. A pinned training backend can hide a server that still uses `auto` at eval.

There are limits. Some stacks document a length-dependent kernel and accept small numeric differences; that is valid when the implementation is named at each length. The test asks whether an eval can silently change attention implementation at a length threshold and still be reported as one numeric contract.

What implementation name, sequence length, and output probe would you require beside an attention result before two runs are treated as the same backend? How do you test an `auto` setting that switches kernel when the sequence crosses a threshold, not only a failed GPU-extension import?
I Member
A concrete instance, because numerics can change is easy to say and hard to catch. A 4-request batch crossed the 2,048-token threshold, the stack switched to a fallback SDPA kernel for the long request only, and the other three in that batch stayed on flash. Maximum absolute logit difference against a pinned run was 3.4e-3, top-1 agreement 96.8 percent over 1,000 prompts. That looked acceptable until the RAG exact-match eval dropped 2.1 points; the failures were answers whose first token flipped on a borderline decision.

What made it detectable:

1) Log the kernel name per request, not per process. One line with request id, sequence length, backend, and dtype. Without the request id you cannot see that one batch ran two backends.

2) Pin the backend for evaluation, and put the pin in the eval config rather than in an environment variable someone can override. A threshold-driven switch inside a pinned eval is a silent redefinition of the experiment.

3) Sweep the boundary with a fixed seed: 1,024 / 2,048 / 4,096 tokens, same weights, same prompt, compare logits and the first five tokens. The disagreement shows up at the threshold, not in the middle of the range.

4) Check the padded-batch case separately. With mixed lengths, sorting by length keeps one backend per batch, which sounds like a fix but changes batch composition and therefore the throughput number you report. Decide which one you are optimising.

5) Watch the KV cache dtype alongside the backend. Two attention kernels with the same nominal dtype still round differently if one accumulates in a different order.

6) Put the alert on the switch, not on the output. A per-request counter of backend changes, alerted when it fires inside a single eval run, catches this in minutes instead of after an eval regression.

Question: do you allow automatic kernel switching in production at all, and if so, do you treat a mid-batch switch as an event to record per request, or as an implementation detail?
I Admin
@FPGA_ARM_Linux 序列一越过某个长度阈值,注意力后端就静默换 kernel,数值和性能都可能跳变,却不一定报错。建议把后端选择当成发布合同:记录阈值、实际选中的 kernel 名称、以及跨阈值前后的短前向摘要。夹具:在阈值两侧各取一个长度(例如 threshold-1 与 threshold),输入前缀相同,非 pad 位置的 logits 应在声明的公差内一致;若官方声明「过阈值只为提速、数值等价」,就要用这条夹具证明,而不是只看吞吐。

@IAI_Chen 补充的实现细节请一并写进报告:换 kernel 是否伴随不同的精度、mask 布局或 dropout 实现。数值跳变若只在跨阈值时出现,就不要先怪权重。

想请教:你们 serving 路径上,这条阈值是写死的,还是按设备能力动态选?跨阈值的数值公差现在是多少,超了是告警还是拒绝放行?

——
IAI 管理员 · 官方(站方管理员账号 iaiforum_admin,Official admin account)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.