---
title: "【AI 核心深度 M1-031】为什么 softmax 必须先减最大值？（Explain Why Softmax Must Subtract the Maximum Value and How It Prevents Numerical Overflow）深度数理推导与工程落地解析"
canonical_url: "https://airsota.com/2026/10/01/science-depth-m1-031-numerical-stability/"
date_published: "2026-10-01T08:02:30-07:00"
date_modified: "2026-10-01T08:02:30-07:00"
language: "zh-CN"
categories: ["AI 核心深度 856 题 (Science Depth)", "TalentMe (AI 学习与算法架构)", "大厂算法与面试备考 (Interview Guides &amp; Math)"]
tags: ["applied-scientist", "machine-learning", "module-m1", "numerical-stability", "science-depth"]
summary: "exp 对大正数溢出为 inf；减去最大值后指数最大为 0，结果不变但数值安全。"
publisher: "AirSOTA (https://airsota.com)"
---

# 【AI 核心深度 M1-031】为什么 softmax 必须先减最大值？（Explain Why Softmax Must Subtract the Maximum Value and How It Prevents Numerical Overflow）深度数理推导与工程落地解析

> **Executive Summary**: exp 对大正数溢出为 inf；减去最大值后指数最大为 0，结果不变但数值安全。

> 
**所属模块**：`M1 · 数学与统计基础 (Mathematics & Statistics Fundamentals)` | **专题分类**：`数值稳定性 (Numerical Stability)` | **难度等级**：`Easy`

### 一、核心一句话结论 (One-Sentence Summary)

**exp 对大正数溢出为 inf；减去最大值后指数最大为 0，结果不变但数值安全。**

*Subtracting $x_{max}$ prevents $e^{x_i}$ from exceeding floating-point representation limits ($+infty$), exploiting shift invariance: $text{Softmax}(x) = text{Softmax}(x - c)$.*

### 二、核心考点要义 (Key Insights)

- 📌 数学上等价，数值上必需

- 📌 同理 log-sum-exp 用于交叉熵

**English Insights:**

- *Floating-point limits: In FP32, maximum representable value is $approx 3.4 times 10^{38}$ ($e^{88.7}$ overflows); in FP16, maximum is $65504$ ($e^{11.1}$ overflows).*

- *Shift invariance: $frac{e^{x_i - c}}{sum_j e^{x_j - c}} = frac{e^{-c} e^{x_i}}{e^{-c}sum_j e^{x_j}} = frac{e^{x_i}}{sum_j e^{x_j}}$ for any constant $c$.*

- *Safe implementation: Choosing $c = max_j x_j$ forces all exponents $le 0$, ensuring $e^{x_i - x_{max}} in (0, 1]$ and eliminating overflow completely.*

### 三、核心数学原理与机理推导 (Mathematical Principles & Derivation)

$$mathrm{softmax}(x)_i=frac{e^{x_i-max_j x_j}}{sum_k e^{x_k-max_j x_j}}$$

数学等价性来自 softmax 的**平移不变性**：softmax(x+c)=softmax(x)，因为分子分母同乘 e^c 后约掉。因此减 max 不改变输出，只改变中间计算的数值范围。未减 max 时，若 x 中有元素 ≥ 89（FP32 下 exp(88.7)≈3.4×10³⁸ 接近 float32 上限 3.4×10³⁸），e^{xᵢ} 直接溢出为 inf，随后 inf/inf=NaN，梯度传播中断；在 FP16 下阈值更低（exp(11.1)≈65504 即 FP16 上限），所以混合精度训练中这个问题更频繁。减去 max 后，最大的指数为 e⁰=1，其余 ≤1，求和结果落在 [1, n] 区间，完全安全。

  📖 查看英文严格数学推导 (English Mathematical Derivation)

Let $z_i = x_i - max_k x_k$. Then $z_i le 0$ for all $i$, with at least one element $z_{text{argmax}} = 0$. Consequently, $0 < e^{z_i} le e^0 = 1$. The sum of exponentials satisfies $1 le sum_{j=1}^K e^{z_j} le K$. Because the denominator is bounded below by 1, division by zero is mathematically impossible. In contrast, if $x = [1000, 1001, 1002]$, naive computation of $e^{1000}$ triggers floating-point overflow to `inf`, yielding `[inf/inf, inf/inf, inf/inf] = [NaN, NaN, NaN]`.

  

### 四、工业级落地权衡与工程考量 (Industrial Trade-offs)

两个进阶要点：① **减 max 不是唯一选择**——减任何常数都数学等价，减 max 只是保证不溢出；某些实现减 max 是为了配合'在线 softmax'（FlashAttention）的分块计算，此时维护一个**运行最大值** m 并随时修正历史累加和 l。② **log 域的配套**——计算 log(softmax(x)) 时应直接用 `x - logsumexp(x)`，而非 `log(softmax(x))`（后者在概率极小时 log(0)=−inf）。框架提供的 `log_softmax`、`cross_entropy`、`bce_with_logits` 都内置了这些技巧，这也是为什么**绝不应该**先 sigmoid/softmax 再取 log。③ FP16 下还需注意：减 max 后的和可能仍很小（如全部为 −100 时 e^{−100}≈3.7×10⁻⁴⁴ 在 FP16 下下溢为 0），此时应全程在 FP32 累加。

  ⚙️ 查看英文落地权衡分析 (English Systems & Trade-offs)

In FlashAttention and Transformer inference kernels, finding $x_{max}$ requires an additional pass over tokens. Online Softmax (Milakov & Gimelshein, 2018; Dao et al., 2022) maintains running maximums $m_{text{new}} = max(m_{text{old}}, x_{text{new}})$ and dynamically rescales accumulated sums $l_{text{new}} = l_{text{old}} e^{m_{text{old}} - m_{text{new}}} + e^{x_{text{new}} - m_{text{new}}}$, fusing softmax computation into a single IO-efficient SRAM pass.

  

### 五、常见面试避坑陷阱 (Common Pitfalls & Traps)

- ⚠️ 先 softmax 再取 log（数值灾难）

- ⚠️ 在 FP16 下用 FP16 累加 softmax 分母

**English Pitfalls:**

- *Implementing Softmax naively in CUDA/Triton without subtracting the maximum, producing NaN loss spikes during mixed precision training.*

- *Forgetting that underflow ($e^{z_i} to 0$ for very negative inputs) is harmless in softmax sums because the maximum element guarantees a denominator $ge 1$.*

### 六、高频深度面试追问与预测 (Follow-Up Questions)

- **log-sum-exp 技巧的写法？**

- *How does Online Softmax enable FlashAttention to compute attention without writing intermediate $Ntimes N$ matrices to HBM?*

- **FP16 下还有哪些常见溢出点？**

- *Why does Softmax temperature $T$ affect numerical stability when $T to 0$?*

### 七、知识图谱对齐 (Knowledge Graph Anchor)

- 🔗 **关联底层卡片**：`浮点运算、下溢/上溢、Log-Sum-Exp 稳定算子` (`Floating-Point, Underflow/Overflow & LogSumExp`)

- 🗺️ **知识图谱模块**：`数理基础思维导图`

  

#### 🔬 算法科学家与机器学习深度考察全量题库 (Science Depth)

  本题收录于 **TalentMe 算法科学家深度考察真题库 (Science Depth)**。全库共 856 道硬核考点，深度覆盖数学统计、经典ML、深度学习、Transformer、大语言模型、多模态、推荐系统与 MLOps。支持 Jev 面经智能匹配、一键离线单文件 HTML 手册导出并直连 Obsidian 本地记忆。

  [👉 前往 TalentMe 交互式研读本题 (M1-031) →](https://talentme.airsota.com/tools/science-depth/M1-031)

---
*Source: [AirSOTA Knowledge Matrix](https://airsota.com/2026/10/01/science-depth-m1-031-numerical-stability/) · Complete Index: [llms.txt](https://airsota.com/llms.txt)*
