# 最大值点集

**最大值点集**（在库的用法中写作 argmax）是这样一个数学算子：它返回函数取得最大值时的输入，而非最大值本身。对于定义在定义域 D 上的函数 f，arg max f 是 D 中使函数值达到最高的那些输入 x 构成的集合，即所有满足对 D 中任意 y 都有 f(x) ≥ f(y) 的 x。<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup> 该算子在优化、统计学和机器学习中处于核心地位——[最大似然估计](https://www.edgepedia.org/zh/maximum-likelihood-estimation)、语言模型的贪心解码，以及动态规划中的最大化，都可归结为计算或刻画一个 arg max。

| 关键事实 | 详情 |
|---|---|
| 返回值 | 一组使函数取得最大值的输入，而非最大值本身；max 返回数值，arg max 返回取到该值的位置<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup> |
| 集合值情形 | cos(x) 在实数上的 arg max 是所有 2π 整数倍构成的无限集<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup> |
| 空集情形 | 若最大值无法取到（如实数上的 f(x) = x，或 f(x) = −e^x，其上确界 0 可逼近却永不可达），则 arg max 为空集<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup><sup> • </sup><sup>[2](https://fungrim.org/entry/617fe3/)</sup> |
| 关键恒等式 | argmin f = argmax(−f)；加上常数或施加严格递增函数（如对数）不改变 arg max<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup> |
| 计算 | 在含 N 个元素的无序列表中找出最大元素或其下标，需要一个逐元素比较的顺序循环，复杂度为 O(N)<sup>[3](https://www.biorxiv.org/content/10.1101/231753v1.full.pdf)</sup> |
| 并列约定 | NumPy、PyTorch 和 TensorFlow 在最大值并列时均返回第一个（最小）下标<sup>[4](https://numpy.org/doc/stable/reference/generated/numpy.argmax.html)</sup><sup> • </sup><sup>[5](https://docs.pytorch.org/docs/stable/generated/torch.argmax.md)</sup><sup> • </sup><sup>[6](https://www.tensorflow.org/api_docs/python/tf/math/argmax)</sup> |
| GPU 加速 | ONNX Runtime 的并行化将 H200 上一个 [1, 202048] 的 ArgMax 从约 5 毫秒缩短为 3.8 微秒内核加 0.8 微秒 memset<sup>[7](https://github.com/microsoft/onnxruntime/pull/32092)</sup> |

## 定义与记号

形式化定义是集合值的。数学参考著作 Fungrim 将集合 S 上的 arg max 定义为满足 f(x) 等于 f 在 S 上最大值的那些 x ∈ S 构成的集合，记作带算子下标的 arg max<sub>x∈S</sub> f(x)；若 f 取不到满足条件的最大值，结果为空集。<sup>[2](https://fungrim.org/entry/617fe3/)</sup> 它与 max 的区别在于：max 返回所有最大化点共享的唯一值 f(x*)，而 arg max 返回最大化点本身。max 是唯一的，因为 arg max 中所有元素都取得相同的值；但 max 可能不存在，因为 arg max 可能为空。<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup>

记号因领域而异。数学参考文献用带下标定义域的写法，arg max<sub>x∈S</sub> f(x)。<sup>[2](https://fungrim.org/entry/617fe3/)</sup> 库与工程用法则写成单个词元 argmax，如 numpy.argmax 和 torch.argmax。<sup>[4](https://numpy.org/doc/stable/reference/generated/numpy.argmax.html)</sup><sup> • </sup><sup>[5](https://docs.pytorch.org/docs/stable/generated/torch.argmax.md)</sup>

## 集合值与退化情形

**多个最大化点。** arg max 集合可能含一个元素、有限多个，或无限多个。cos(x) 在实数上的 argmax 是包含所有 2π 整数倍的集合。<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup> 当需要单一取值时，就需要一条选择规则：M 估计中的一种做法是论文提出的最小 argmax 选择（sargmax），定义在紧致矩形上的 argmax 集合上，用于在 argmax 集合不是单点时使 M 估计量单值化。<sup>[8](https://ar5iv.labs.arxiv.org/html/1105.1320)</sup>

**不可达的上确界。** 对于实数上的 f(x) = −e^x，上确界为 0，但最大值取不到；上确界约定可处理此类情形，此时 arg max 为空集。<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup><sup> • </sup><sup>[2](https://fungrim.org/entry/617fe3/)</sup> 贝尔曼在动态规划中使用了同样的手法：对于连续时间过程，他先用上确界写出泛函方程（这通常容易建立），然后在适当假设下证明最大值实际上可以取到。<sup>[9](https://gwern.net/doc/statistics/decision/1957-bellman-dynamicprogramming.pdf)</sup>

**软件约定。** 各实现会做出务实的选择。[Wolfram 语言](https://www.edgepedia.org/zh/wolfram-language)的 ArgMax 求 f 在给定约束下的全局最大值，当 f 与约束为线性或多项式时总能找到全局最大值。<sup>[10](https://reference.wolfram.com/language/ref/ArgMax.html)</sup> 若最大值仅在约束区域之外无穷小处取得，或仅渐近取得，ArgMax 返回最接近的可指定点；即使同一最大值在多个点取得，也只返回一个；若约束无法满足，ArgMax 返回 Indeterminate。<sup>[10](https://reference.wolfram.com/language/ref/ArgMax.html)</sup>

## 性质与恒等式

两个恒等式将 arg max 与 arg min 联系起来：argmin f = argmax(−f)，以及 min f = −max(−f)。<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup> 保持不等式的运算不改变 arg max 集合：加上常数不变，施加严格递增函数（如对数）也不变。对数性质正是最大似然估计中将概率乘积转化为对数概率之和这一标准变换的基础。<sup>[1](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)</sup>

**约束。** arg max 通常取在可行集上，而非整个空间上。在贝尔曼 1954 年对动态规划的表述中，泛函方程对初始区间上所有允许决策取 Max，而对必须证明可达性的无限过程则使用 Sup 形式。<sup>[11](https://www.ams.org/journals/bull/1954-60-06/S0002-9904-1954-09848-8/S0002-9904-1954-09848-8.pdf)</sup> 贝尔曼指出，最大回报是唯一确定的，但最大化策略未必唯一，因此可能存在许多产生相同回报的最优策略。<sup>[9](https://gwern.net/doc/statistics/decision/1957-bellman-dynamicprogramming.pdf)</sup> 在约束优化中，Wolfram 的 ArgMax 直接接受约束，并对线性或多项式问题保证全局最大值。<sup>[10](https://reference.wolfram.com/language/ref/ArgMax.html)</sup> 在最大似然估计中，约束可以显式写成 x ∈ C，也可以通过令 C 之外的 x 满足 p_x(y) = 0 来隐式处理。<sup>[12](https://web.stanford.edu/class/ee364a/lectures/stat.pdf)</sup>

## 计算与数值行为

**串行复杂度。** 在含 N 个元素的无序列表中找出最大数或其下标，需要一个逐元素比较的顺序循环；这种线性时间 O(N) 扫描是最优的串行过程，因为每个元素至少必须检查一次。<sup>[3](https://www.biorxiv.org/content/10.1101/231753v1.full.pdf)</sup>

**并行计算。** 在 GPU 上，扫描通过将轴拆分到线程块并归约来实现并行化。2025 年 [ONNX Runtime](https://www.edgepedia.org/zh/onnx-runtime) 的一项改动将 ArgMax 和 ArgMin 在宽末轴上并行化：改动前，大词表上的 [1, 202048] ArgMax 由恰好一个 CUDA 线程计算，在 H200 上耗时约 5 毫秒；改动后，它变为 3.8 微秒的内核加 0.8 微秒的 memset，使用 197 个 (32, 8) 线程块，理论占用率 100%。<sup>[7](https://github.com/microsoft/onnxruntime/pull/32092)</sup> 对于相同形状，cub::DeviceReduce::ArgMax 需要 2 个内核、2.74 微秒 GPU 时间以及 42,495 字节临时存储，而新的按行分段内核使用的暂存空间约为前者的二十六分之一（1,595 B），且只需一次启动而非两次。<sup>[7](https://github.com/microsoft/onnxruntime/pull/32092)</sup>

**并列处理。** 有文档的库在并列取首下标这一点上是一致的：numpy.argmax 返回沿轴的最大值下标，若最大值多次出现，返回首次出现的下标。<sup>[4](https://numpy.org/doc/stable/reference/generated/numpy.argmax.html)</sup> torch.argmax 以 LongTensor 返回输入张量中所有元素最大值之下标，若有多个最大值，返回第一个的下标。<sup>[5](https://docs.pytorch.org/docs/stable/generated/torch.argmax.md)</sup> tf.math.argmax 返回沿各轴最大值之下标，若值相同则返回最小下标。<sup>[6](https://www.tensorflow.org/api_docs/python/tf/math/argmax)</sup> ONNX Runtime 内核同样在并列时选择最小下标。<sup>[7](https://github.com/microsoft/onnxruntime/pull/32092)</sup> 这一约定在实践中并非普遍适用：在 llama.cpp 构建 9760 中，严格的 > 比较使 CPU 贪心采样器保留最早的并列下标，而 ggml 后端 argmax 内核保留最后一个，这意味着对于固定模型和提示，贪心选出的词元可能仅因执行采样的后端不同而变化；注入 10 个精确的 fp32 最大值并列时，10 个全部出现分歧，而在最大值唯一的扫描中，34 个案例全部一致。<sup>[13](https://github.com/v-code01/argmaxtie)</sup>

**NaN 处理。** 浮点最大值会传播 NaN：在 NumPy 中，只要至少有一项为 NaN，对应的最大值也为 NaN，必须使用 nanmax 来忽略 NaN。<sup>[14](https://numpy.org/doc/stable/reference/generated/numpy.max.html)</sup> 在 ONNX Runtime 内核中，NaN 永远不会在比较中胜出，且由于旧内核用第 0 个元素初始化累加器，开头的 NaN 会使整行返回下标 0；归约单位元为 −inf，全为 −inf、+inf 或 NaN 的行返回下标 0。<sup>[7](https://github.com/microsoft/onnxruntime/pull/32092)</sup> 在随机 fp32 logits 中精确并列很罕见，但在 fp16 或量化 lm_head 权重下会显著增加，因为其粗糙的数值网格会把许多 logits 压成相同值。<sup>[13](https://github.com/v-code01/argmaxtie)</sup>

## 数据一览

- 对 N 个无序项求 arg max 的串行代价是每元素一次比较，即 O(N) 时间。<sup>[3](https://www.biorxiv.org/content/10.1101/231753v1.full.pdf)</sup>
- H200 上一个 [1, 202048] 的 ArgMax 在并行化后从约 5,056 微秒降至 3.8 微秒内核加 0.8 微秒 memset，GPU 时间约降至原来的千分之一。<sup>[7](https://github.com/microsoft/onnxruntime/pull/32092)</sup>
- 同一操作的暂存内存从 cub::DeviceReduce::ArgMax 的 42,495 B 降至 1,595 B，约为原来的二十六分之一，且内核启动从两次变为一次。<sup>[7](https://github.com/microsoft/onnxruntime/pull/32092)</sup>

## 优化、统计学与机器学习中的 arg max

**统计学。** 许多估计量被定义为随机目标函数的最大化点，这一过程称为 M 估计，其渐近分布通过 argmax 连续映射定理获得。<sup>[8](https://ar5iv.labs.arxiv.org/html/1105.1320)</sup> 最大似然估计是其中在 x 上最大化对数似然 l(x) = log p_x(y) 的特例；当固定 y 时 log p(y) 关于 x 为凹函数，这就是一个凸优化问题。<sup>[12](https://web.stanford.edu/class/ee364a/lectures/stat.pdf)</sup>

**动态规划。** 贝尔曼的表述以最大化回报函数为中心，最大回报唯一确定，而许多最优策略都可能产生它。<sup>[9](https://gwern.net/doc/statistics/decision/1957-bellman-dynamicprogramming.pdf)</sup>

**语言模型解码。** 贪心解码在每一步选择得分分布 argmax 中的词元，当多个词元取得最大值时假定一条显式的并列规则。<sup>[15](https://proceedings.neurips.cc/paper_files/paper/2025/file/a96d4fda3017f1773b261a52a3efc8dd-Paper-Conference.pdf)</sup> 对于有限 logits 的精确算术，logits 上的 argmax 与 softmax 概率上的 argmax 一致，因为 softmax 保持其顺序；因此快速实现可以在贪心解码时跳过 softmax。<sup>[16](https://buildwithaitoday.com/wiki/argmax)</sup> 硬 argmax 在某个 logit 为唯一最大值处局部为常数，在并列处不可微，因此无法为训练提供有用的梯度，训练转而使用 softmax 与交叉熵；贪心 argmax 解码还可能陷入重复循环或平淡文本，这催生了束搜索与采样。<sup>[16](https://buildwithaitoday.com/wiki/argmax)</sup> 一种与之形成对照的方法是 Gumbel-Max 技巧，其下并列以概率零发生，因此在从类别分布中精确采样时，argmax 是无歧义的。<sup>[17](https://arxiv.org/pdf/2603.15854)</sup>

## 近期进展

ONNX Runtime 对宽末轴上 ArgMax 的并行化，将单线程约 5 毫秒的瓶颈变为针对词表规模行的 3.8 微秒内核，结果逐位相同，且保持相同的最低下标并列规则。<sup>[7](https://github.com/microsoft/onnxruntime/pull/32092)</sup> NumPy 为 argmax 增加的 keepdims 参数自 1.22.0 版引入，仍是当前文档化 API 的一部分。<sup>[4](https://numpy.org/doc/stable/reference/generated/numpy.argmax.html)</sup> 在理论方面，近期工作将贪心解码刻画为一种退化分布，其全部质量集中在 argmax 集合 V* = {v : s(v) = M} 上，与通过温度 softmax 为多个词元赋予非零概率的采样式解码形成对照。<sup>[18](https://arxiv.org/pdf/2602.18292)</sup> NeurIPS 2025 的解码基础专题同样将贪心解码形式化为带假定并列规则的 argmax 选择。<sup>[15](https://proceedings.neurips.cc/paper_files/paper/2025/file/a96d4fda3017f1773b261a52a3efc8dd-Paper-Conference.pdf)</sup> 对并列行为的可复现测试也已成为一种实践，正如 llama.cpp 的 CPU 与后端分歧所示。<sup>[13](https://github.com/v-code01/argmaxtie)</sup>

## References

1. [Mark Schmidt. Argmax and Max Calculus, UBC CPSC 540 notes](https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf)
2. [Fungrim entry 617fe3: ArgMax](https://fungrim.org/entry/617fe3/)
3. [How fast is neural winner-take-all when deciding between many options? bioRxiv](https://www.biorxiv.org/content/10.1101/231753v1.full.pdf)
4. [numpy.argmax — NumPy Manual](https://numpy.org/doc/stable/reference/generated/numpy.argmax.html)
5. [torch.argmax — PyTorch documentation](https://docs.pytorch.org/docs/stable/generated/torch.argmax.md)
6. [tf.math.argmax — TensorFlow](https://www.tensorflow.org/api_docs/python/tf/math/argmax)
7. [[CUDA] Parallelize ArgMax/ArgMin over wide last axes, ONNX Runtime pull request 32092](https://github.com/microsoft/onnxruntime/pull/32092)
8. [A Continuous Mapping Theorem for the Smallest Argmax Functional, arXiv:1105.1320](https://ar5iv.labs.arxiv.org/html/1105.1320)
9. [Richard Bellman (1957). Dynamic Programming](https://gwern.net/doc/statistics/decision/1957-bellman-dynamicprogramming.pdf)
10. [ArgMax — Wolfram Language Documentation](https://reference.wolfram.com/language/ref/ArgMax.html)
11. [Richard Bellman (1954). The Theory of Dynamic Programming, Bulletin of the AMS](https://www.ams.org/journals/bull/1954-60-06/S0002-9904-1954-09848-8/S0002-9904-1954-09848-8.pdf)
12. [Convex Optimization: Statistical estimation, Stanford EE364a lecture slides](https://web.stanford.edu/class/ee364a/lectures/stat.pdf)
13. [argmaxtie: CPU vs backend greedy argmax tie divergence in llama.cpp](https://github.com/v-code01/argmaxtie)
14. [numpy.max — NumPy Manual](https://numpy.org/doc/stable/reference/generated/numpy.max.html)
15. [Foundations of Decoding for Language Models, NeurIPS 2025](https://proceedings.neurips.cc/paper_files/paper/2025/file/a96d4fda3017f1773b261a52a3efc8dd-Paper-Conference.pdf)
16. [Argmax — Build with AI Today wiki](https://buildwithaitoday.com/wiki/argmax)
17. [Gumbel-based sampling paper, arXiv](https://arxiv.org/pdf/2603.15854)
18. [A unified variational view of decoding, arXiv](https://arxiv.org/pdf/2602.18292)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Optimization and dynamic programming

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/arg-max

English original: https://www.edgepedia.org/arg-max
