Temperature 0 Is Not Determinism: Four Sampling Myths and a Repeat-Run Probe

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Temperature 0 Is Not Determinism: Four Sampling Myths and a Repeat-Run Probe

我的持续集成(CI)在一个昨天还能通过的测试上报错了。同样的提示词,同样的温度参数:0,却得到了不同的输出。于是我问了一个无聊的问题:温度 0 是一个承诺,还是仅仅一个请求?答案是请求。端点决定了它在多大程度上严格执行该请求。这篇文章是一个破除迷思的常见问题解答。我整理了四个我经常听到的说法、反驳它们的证据,以及一个你可以在十分钟内运行的小型探测脚本。

Myth 1: temperature=0 gives the same output every time

This is the claim that cost me a morning. It sounds mathematical. It is not. What temperature actually does: Temperature scales logits before sampling. At 0, most stacks take the argmax instead of sampling. That part is real. But argmax over which floats? That is the part nobody pins down.

迷思 1:temperature=0 每次都会给出相同的输出

这个说法让我浪费了一个上午。它听起来很符合数学逻辑,但事实并非如此。温度参数实际上做了什么:温度在采样前对逻辑值(logits)进行缩放。在 0 时,大多数技术栈会选择取最大值(argmax)而不是采样。这部分是真的。但究竟是对哪些浮点数取最大值?这才是没人能确定的部分。

Why identical inputs still drift

Floating-point addition is not associative. Batch order changes the sum. Serving stacks batch your request with strangers. Batch composition changes. Kernel and library versions change between deploys. A provider can swap the model behind the same name. Ask yourself: did you pin the weights, or did you pin a string? You pinned a string. Corrected mental model: temperature=0 means “as greedy as this endpoint allows”. It is not a seed. It is not a cache key. Treat it as a hint toward stability, not a guarantee of identity.

为什么相同的输入仍然会产生偏差

浮点数加法不满足结合律。批处理顺序会改变求和结果。服务栈会将你的请求与他人的请求进行批处理,而批处理的构成是会变化的。内核和库版本在部署之间也会更新。提供商可以在同一个名称下更换模型。问问你自己:你锁定的是权重,还是一个字符串?你锁定的是字符串。修正后的思维模型:temperature=0 意味着“在该端点允许的范围内尽可能贪婪”。它不是随机种子,也不是缓存键。将其视为对稳定性的提示,而不是身份一致性的保证。

Myth 2: max_tokens caps the length of my answer

It caps tokens, not characters. Those are different units, and the mapping is model-specific. Two things bite people here. finish_reason tells you why generation stopped. If it is “length”, you were truncated. Check it. usage.completion_tokens may not match the visible text. Depending on the provider, it can include tokens you never see, or omit tokens it still counted. If your parser assumes “the answer ended because the model finished”, you will eventually parse a half sentence. Ask for the finish reason. Store it next to the text.

迷思 2:max_tokens 限制了我回答的长度

它限制的是 Token,而不是字符。这是不同的单位,且映射关系取决于具体模型。这里有两点容易让人踩坑。finish_reason 会告诉你生成停止的原因。如果是 “length”,说明你被截断了,请检查它。usage.completion_tokens 可能与可见文本不符。根据提供商的不同,它可能包含你从未见过的 Token,或者忽略了它仍计算在内的 Token。如果你的解析器假设“回答结束是因为模型完成了”,你最终会解析到半句话。请获取 finish_reason 并将其与文本一起存储。

Myth 3: A stop sequence cuts cleanly

Stop handling is not standardized. Two endpoints, two behaviors. Some return the text up to the stop string. Some return it including the stop string. With streaming, the stop string can straddle two chunks. Naive clients leak it. Some endpoints drop trailing whitespace when they trim. Your diff notices. So do not assume. Probe it with one request and read the raw repr.

迷思 3:停止序列(stop sequence)会干净利落地截断

停止处理并没有标准化。两个端点,两种行为。有些返回到停止字符串为止的文本,有些则包含停止字符串。在使用流式传输时,停止字符串可能跨越两个数据块。简单的客户端会泄露它。有些端点在修剪时会丢弃末尾的空格,你的差异对比工具会发现这一点。所以不要想当然。用一个请求进行探测,并读取原始的 repr。

Myth 4: The same model name behaves the same everywhere

A model name is a label, not a contract. The endpoint is the contract. Free access and paid access can differ in version, quantization, default parameters, or limits. Even two endpoints with the same label on the same day can differ. That is not a scandal. It is just two deployments. Why does this matter in practice? Because your retry logic, your eval baseline, and your prompt tuning are all calibrated against one endpoint. Move the base URL and you moved the ground.

迷思 4:同一个模型名称在任何地方表现都一样

模型名称只是一个标签,而不是合同。端点才是合同。免费访问和付费访问在版本、量化、默认参数或限制上可能有所不同。即使是同一天、同一标签的两个端点也可能存在差异。这并不是什么丑闻,仅仅是两次不同的部署。为什么这在实践中很重要?因为你的重试逻辑、评估基准和提示词调优都是针对一个端点进行校准的。更换了基础 URL,你就改变了立足点。

The probe: repeat the same request eight times

Here is the artifact. It is a probe, not a benchmark. It measures one endpoint, one prompt, one parameter set. [Code omitted for brevity]. Run it like this: export PROBE_BASE_URL='https://your-endpoint.example/v1' ... python3 probe_sampling.py. The output shape looks like this: {"model": "<model-id>", "runs": 8, "distinct": <n>, "top_share": <0..1>}. distinct above 1 at temperature 0 means the endpoint is not locked. That is your finding. Now you can design around it.

探测:重复同一个请求八次

这是探测工具,不是基准测试。它测量的是一个端点、一个提示词、一组参数。[代码省略]。运行方式如下:export PROBE_BASE_URL='https://your-endpoint.example/v1' ... python3 probe_sampling.py。输出格式如下:{"model": "<model-id>", "runs": 8, "distinct": <n>, "top_share": <0..1>}。在温度为 0 时,如果 distinct 大于 1,意味着该端点并未锁定。这就是你的发现。现在你可以围绕这个事实进行设计了。

Probe the cap and the stop string too

Two more checks, thirty seconds each: [Code omitted for brevity]. That repr is the whole point. It removes ambiguity about trimming and inclusion.

同时探测上限和停止字符串

再做两个检查,每个只需三十秒:[代码省略]。那个 repr 是关键所在。它消除了关于修剪和包含内容的歧义。

The decision table

Read the probe output, then pick a row.

ObservationWhat it meansWhat to change
distinct: 1Stable for this prompt, todayExact-match tests may hold; re-run after any deploy
distinct > 1 at temperature 0Sampling is

决策表

读取探测输出,然后选择一行。

观察结果含义调整建议
distinct: 1对该提示词,今天表现稳定精确匹配测试可能有效;每次部署后重新运行
distinct > 1 at temperature 0采样正在