【免费下载链接】Auto-claude-code-research-in-sleep
ARIS ⚔️ (Auto-Research-In-Sleep) — Lightweight Markdown-only skills for autonomous ML research: cross-model review loops, idea discovery, and experiment automation. No framework, no lock-in — works with Claude Code, Codex, OpenClaw, or any LLM agent.
本篇基于 ARIS 仓库中的长文教程 docs/tutorials/reasoning_models_tutorial.md,系统拆解 2024–2026 年 LLM 领域最大的范式转移——从 scale 训练算力转向 scale 推理算力。读完后,你将掌握推理模型的完整技术版图:CoT 演进路线、PRM 与 ORM 两条验证器路线、GRPO 相对 PPO 的算法级差异(含可直接运行的 from-scratch PyTorch 实现)、DeepSeek-R1 四阶段 pipeline、test-time compute 的 compute-optimal 分配策略,以及一套按 L1/L2/L3 分层组织的 25 道高频面试题。
1. 范式转移:从 train-time scaling 到 test-time scaling
2020–2023 年的 scaling law(Kaplan、Hoffmann/Chinchilla)刻画的是 训练时算力 与性能的关系:性能随参数、数据、训练 FLOPs 的对数增长。模型一旦训完,推理时的算力是固定的(一次 forward pass)。
2024 年出现两个打破该格局的现象:
- OpenAI o1(2024-09):花更多推理 token 思考,性能持续提升,且 accuracy 对 reasoning compute 呈 log-linear scaling(OpenAI 公开图表中是一条直线)。o1 用 RL 训练 hidden chain-of-thought,API 只返回
reasoning_tokens计数而非内容本身。 - Snell et al. 2024(arXiv 2408.03314):在固定 inference/test-time compute 预算下做 compute-optimal 分配(best-of-N + PRM beam search + sequential revision 的混合策略),FLOPs-matched 设置下,小模型 + 优化的 test-time compute 可以匹配甚至超过 14× 更大模型——前提是 base model 在该任务上已有 non-trivial success rate。相同推理 FLOPs 下,混合策略比单一 best-of-N 高 >4× 效率。
范式 mental model——把推理看成 search over reasoning paths:
- System 1(快):一次 greedy decode = 一条路径;
- System 2(慢):多条路径 + verifier + 回溯 = 树搜索(Tree of Thought)或长 CoT(o1 / R1)。
可以进一步把 LLM 看成 policy + value 的组合(类似 AlphaZero),推理即 MCTS-like 的搜索过程。
1.1 推理模型的核心组件
所有推理模型本质上都是下面这个三组件框架的变体:
问题 prompt
│
↓
┌───────────────────────────┐
│ Policy LLM (sampler) │ ← 生成 candidate reasoning traces
│ - greedy / temperature │
│ - long CoT (o1, R1) │
│ - tree expansion (ToT) │
└───────────────────────────┘
│ N traces
↓
┌───────────────────────────┐
│ Verifier / Reward Model │
│ - ORM (outcome only) │
│ - PRM (per-step) │
│ - rule-based (math/code) │
└───────────────────────────┘
│
↓
┌───────────────────────────┐
│ Aggregator │
│ - majority vote (SC) │
│ - best-of-N (verifier) │
│ - beam search (PRM) │
│ - MCTS (rStar) │
└───────────────────────────┘
│
↓
最终答案
三条主流路线的分野:
- o1 / R1:把 search 内化到 policy 里(单次长 CoT,policy 自己反思、回溯);
- ToT / rStar:外部显式 search(MCTS / BFS / beam);
- Best-of-N + PRM:sampling + verifier,最朴素的 test-time scaling。
2. CoT 演进:从 Wei 2022 到 o1
2.1 Chain-of-Thought(Wei et al., NeurIPS 2022, arXiv 2201.11903)
核心发现:few-shot prompt 展示 "step-by-step reasoning",性能在 >62B 的 PaLM 上出现 emergent jump,GSM8K 从 18% → 57%。
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls.
Each can has 3 tennis balls. How many tennis balls does he have now?
A: Roger started with 5 balls. 2 cans of 3 balls each is 6 balls. 5 + 6 = 11.
The answer is 11.
三个关键点:
- 不是 fine-tune——纯 prompting,能力来自 base model 本身;
- emergence:小模型用 CoT 反而变差(噪声大于信号);
- 后续 Kojima et al. 2022("Let's think step by step", arXiv 2205.11916)发现 zero-shot CoT 也能触发。
2.2 Self-Consistency(Wang et al. 2022, arXiv 2203.11171)
CoT decoding 是 stochastic 的(temperature > 0),同一题多次采样会得到不同推理路径,但正确答案往往出现得更频繁(模型能力足够时)。算法三步:
- 对同一 prompt 采样 $N$ 条 CoT;
- 提取每条的 final answer;
- 多数投票 选 majority answer:
$$\hat{y} = \arg\max_{y \in \mathcal{Y}} \sum_{i=1}^{N} \mathbb{1}[\text{extract}(\text{trace}_i) = y]$$
效果:GSM8K +17.9%(Wang et al. 报告的 PaLM-540B 数字)。
常见 bug:投票前必须做 answer extraction normalization(去单位、化简分数、整数化)。否则 "1/2" 和 "0.5" 会被算成不同答案,多数票被打散。
2.3 Tree of Thoughts(Yao et al., NeurIPS 2023, arXiv 2305.10601)
把 CoT 从 chain 升级成 tree:每个节点是一个 "thought"(一步推理),从根节点 BFS/DFS 展开多个候选 child,LLM 自己评分("is this step promising?"),保留 top-k 并支持剪枝与回溯。
root (problem)
│
┌───────┼───────┐
step1a step1b step1c ← sample k thoughts
│ ✗ ✗ ← LLM evaluator score
│ prune prune
├──────┼──────┐
step2a step2b step2c ← expand surviving node
Game of 24(用 4 个数字 + 四则运算得到 24):
- GPT-4 CoT:4% 成功率;
- GPT-4 ToT(b=5, depth=3):74%——巨大跳变。
ToT vs CoT 的本质区别:CoT 是 autoregressive decoding(一条路径,快但容易陷入早期错误);ToT 是 deliberate search(多条路径 + 显式回溯 + evaluator,慢但能跳出局部最优)。
2.4 从 ToT 到 long-CoT(o1 / R1 路线)
ToT 需要外部搜索框架(递归 prompt + state management)。o1 / R1 走另一条路:把 search 训练进 policy 里——单次长 CoT,但模型自己学会:
- "wait, let me reconsider..."
- "actually that's wrong, the correct way is..."
- "let me verify by trying a different approach"
这些回溯 / 反思 token 在 base model 里很罕见,靠 RL 反复采样 + reward 信号 才能放大成稳定行为。R1-Zero 报告的 "aha moment" 就是这种行为在 RL 训练中突然涌现的时刻。
3. PRM vs ORM:推理验证器的两条路线
3.1 定义对比
| 维度 | ORM(Outcome Reward Model) | PRM(Process Reward Model) |
|---|---|---|
| 监督粒度 | 整条 trace 一个 reward | 每个 step 一个 reward |
| Label 来源 | answer 对 → +1,错 → 0 | 人工标注(PRM800K)或 Monte Carlo rollout 估计(Math-Shepherd) |
| 训练目标 | $\max \mathbb{E}[r(\text{trace})]$ | $\max \sum_t \mathbb{E}[r_t(\text{step}_t)]$ |
| 优势 | 标注便宜(只要 ground-truth answer) | 信号密集;能定位错误步 |
| 劣势 | 稀疏 reward,credit assignment 难 | 标注昂贵;step boundary 难定义 |
3.2 PRM800K(Lightman et al. 2023, arXiv 2305.20050)
OpenAI 在 MATH 数据集上人工标注了 80 万个 step-level 标签,每个 step 为 positive / neutral / negative。训练目标是 per-step 分类:
$$\mathcal{L}\text{PRM} = -\sum{t=1}^{T} \log p_\phi(\ell_t \mid s_{\leq t})$$
其中 $\ell_t \in {+, 0, -}$,$s_{\leq t}$ 是前 $t$ 步推理。推理时把 PRM 当 verifier,对整条 trace 打分:
$$\text{score}(\text{trace}) = \prod_{t} p_\phi(\ell_t = +\mid s_{\leq t}) \quad \text{或} \quad \min_t p_\phi(\ell_t = +\mid s_{\leq t})$$
前者乘积形式更标准;min 形式更悲观但抓最弱步。
关键发现:Lightman 2023 报告,用 PRM800K 做 best-of-1024 verifier,在 MATH test 上 78%(vs ORM 72%,majority vote 70%)——PRM > ORM > self-consistency,但代价是 80 万人工标注。
3.3 Math-Shepherd(Wang et al. 2023, arXiv 2312.08935):自动标注 step label
人标贵,怎么 scale?Math-Shepherd 的思路:用 Monte Carlo completion rollouts 估计每个 step 的"潜在正确率"(注意:是从中间 step 出发采样完整 completion,不是 MCTS 树搜索):
对每个 step s_t(部分推理):
从 s_t 出发 rollout K 条 completion
count 多少条最终 answer 正确
reward_t = (正确数) / K
直觉:如果一个 step 是好的,从它出发往下走应该容易得到正确答案;如果是坏的,怎么走都错。用这些标签以 MSE 回归或 BCE 分类训练 PRM:Mistral-7B 在 GSM8K 上 77.9% → 84.1%,无需人标。
MCTS-label 的隐含假设:base model 在该任务上要有 non-trivial success rate(否则 rollout 全错,标签全 0)。所以 PRM 训练存在 bootstrap 问题——base model 至少要能解出部分题。
3.4 Generative PRM 与 Critic LM
2024 年后流行 "generative verifier":把 PRM 当 next-token-prediction("is this step correct? yes/no"),直接复用 LLM 架构,不用单独 reward head。代表作:Generative Verifiers(Zhang et al. 2024, arXiv 2408.15240)。优势:能利用 in-context reasoning 评分,比 scalar head PRM 更准。
4. Best-of-N + Verifier:最朴素的 test-time scaling
4.1 公式与饱和行为
给定 prompt $x$、policy $\pi$、verifier $V$、采样数 $N$:
$$i^{} = \arg\max_{i \in [N]} V(\text{trace}_i), \quad \text{trace}i \sim \pi(\cdot \mid x); \quad \hat{y} = \text{extract}(\text{trace}{i^{}})$$
理论极限("oracle BoN",假设有完美 verifier):
$$\text{pass}@N = 1 - (1 - \text{pass}@1)^N$$
实际中 verifier 不完美,BoN 的 saturation curve 远低于 oracle。Snell 2024 观察到:
- 简单题上 BoN 很快饱和(N=4 之后边际收益小);
- 难题上 BoN 持续上涨直到 N=64/128;
- compute-optimal 策略:题难 → 加 N;题易 → greedy。
4.2 Best-of-N + PRM 参考实现
import torch
from typing import Callable
def best_of_n_prm(
policy_sample: Callable, # prompt -> (trace, step_list)
prm_score: Callable, # step_list -> scalar in [0, 1]
prompt: str,
n: int = 16,
aggregation: str = "min", # "min" | "prod" | "mean"
) -> tuple[str, float]:
"""
Best-of-N with process reward model verifier.
Returns (best_trace, best_score). Note: in `prod` mode, `best_score`
is the cumulative log-product (negative); compare scores within the same
aggregation mode only.
"""
best_trace, best_score = None, -float("inf")
for _ in range(n):
trace, steps = policy_sample(prompt)
step_probs = [prm_score(steps[: t + 1]) for t in range(len(steps))]
if aggregation == "min":
score = min(step_probs)
elif aggregation == "prod":
# log-sum 避免数值下溢
score = sum(torch.log(torch.tensor(p) + 1e-9) for p in step_probs).item()
elif aggregation == "mean":
score = sum(step_probs) / len(step_probs)
else:
raise ValueError(aggregation)
if score > best_score:
best_score, best_trace = score, trace
return best_trace, best_score
PRM aggregation 选择:Lightman 2023 实验表明 min 和 prod 在 BoN 上接近,mean 显著更差(被高分 step 掩盖低分错步)。生产里常用 min——直觉是"trace 强度取决于最弱一环"。
4.3 Self-Consistency 参考实现(含 answer 解析器)
from collections import Counter
from typing import Callable
import re
def _extract_braced(s: str, open_idx: int) -> str | None:
"""从 `\\boxed{` 后第一个 `{` 起,按平衡括号提取内层(支持嵌套 LaTeX)。"""
if open_idx >= len(s) or s[open_idx] != "{":
return None
depth = 0
for i in range(open_idx, len(s)):
if s[i] == "{":
depth += 1
elif s[i] == "}":
depth -= 1
if depth == 0:
return s[open_idx + 1:i]
return None # 未闭合
def extract_answer(trace: str) -> str:
"""从 trace 提取 final answer。注意:生产里要做更全的规范化
(单位、分数→小数、LaTeX 化简等)。"""
# 用平衡括号匹配 `\boxed{...}`,避免 `\boxed{\frac{1}{2}}` 被截成 `\frac{1`
pos = trace.find(r"\boxed")
if pos != -1:
inner = _extract_braced(trace, pos + len(r"\boxed"))
if inner is not None:
return inner.replace(",", "").replace("$", "").strip()
m = re.search(r"answer is[:\s]+([^.\n]+)", trace, re.IGNORECASE)
if m:
return m.group(1).strip().replace(",", "").replace("$", "").strip()
return ""
def self_consistency(
policy_sample: Callable,
prompt: str,
n: int = 40,
temperature: float = 0.7,
) -> tuple[str, dict]:
"""Self-Consistency: sample N traces, majority vote on extracted answers."""
answers = []
for _ in range(n):
trace = policy_sample(prompt, temperature=temperature)
ans = extract_answer(trace)
if ans: # 跳过解析失败
answers.append(ans)
if not answers:
return "", {}
counts = Counter(answers)
return counts.most_common(1)[0][0], dict(counts)
两个工程要点值得注意:
_extract_braced的平衡括号解析是交叉评审阶段发现的真实 bug 修复——朴素正则会在\boxed{\frac{1}{2}}上被截断成\frac{1,导致答案解析错误、多数票被无谓打散。这正是 §2.2 中"投票前必须 normalization"论断的代码级体现;- 生产参数:Wang 2022 报告 GSM8K 上 N=40 收益开始饱和,N=64 接近极限;temperature 太高(>1.0)会引入 garbage 推理,太低(<0.3)多次采样几乎重复,sweet spot 通常 0.5–0.7。
5. RL 路线:PPO → GRPO
5.1 Vanilla PPO 回顾(Schulman et al. 2017)
对每个 token $t$ 做带 clipping 的 policy gradient:
$$\mathcal{L}_\text{PPO} = -\mathbb{E}_t \left[ \min!\left( \rho_t A_t,; \text{clip}(\rho_t, 1-\epsilon, 1+\epsilon) A_t \right) \right]$$
其中 $\rho_t = \pi_\theta(o_t \mid s_t) / \pi_{\theta_\text{old}}(o_t \mid s_t)$ 是 importance ratio;$A_t$ 常用 GAE-$\lambda$:$A_t = \sum_{l\geq 0} (\gamma\lambda)^l \delta_{t+l}$,$\delta_t = r_t + \gamma V_\psi(s_{t+1}) - V_\psi(s_t)$。
LLM 场景下 PPO 的三大痛点:
- 需要训练一个 value network $V_\psi$(critic),通常和 policy 同等大小——显存翻倍;
- Critic 在 long-CoT 上很难收敛(reward 极度稀疏,只有最终答案对错);
- GAE 需要 token-level value 估计;long-CoT(4K+ token)上每个 token 的 value 噪声很大。
5.2 GRPO(Shao et al. 2024, DeepSeekMath, arXiv 2402.03300)
核心想法:用 group statistics 替代 critic。算法:
- 对每个 prompt $x$,从 $\pi_{\theta_\text{old}}$ 采样 $G$ 个 completion ${o_1, \dots, o_G}$(通常 $G = 16$ 或 $64$);
- 用 reward model 给每个 completion 打分 ${r_1, \dots, r_G}$;
- Group-relative advantage(trace-level,不是 token-level):
$$\boxed{;A_i = \frac{r_i - \text{mean}(\mathbf{r})}{\text{std}(\mathbf{r}) + \epsilon};}$$
- 把 trace-level $A_i$ 广播给该 trace 的所有 token:$A_t = A_i ;\forall t \in o_i$;
- 用 PPO clipping objective 更新 policy,DeepSeekMath/R1 同时加 KL 正则 $\beta \cdot \mathrm{KL}(\pi_\theta ,|, \pi_\text{ref})$。实践中用 Schulman 的 unbiased k3 估计 $\widehat{\mathrm{KL}}t = e^{\log\pi\text{ref}(o_t|s_t) - \log\pi_\theta(o_t|s_t)} - (\log\pi_\text{ref}(o_t|s_t) - \log\pi_\theta(o_t|s_t)) - 1$ 对每个 token 取值(恒 $\geq 0$),再 mask + 平均。
GRPO 关键洞察——为什么 group-relative 比 critic 好用:
- 同 prompt 同源:$G$ 个 completion 共享 prompt 难度,差异完全来自 policy 输出;mean 自动减掉 prompt-specific baseline,等价于 control variate;
- 无需 value network:直接省一半显存 + 一倍计算;critic 在 long-CoT 上本来就难学(reward 稀疏 + episode 长);
- 稳定性来自 group size $G$:$G$ 越大,advantage 估计方差越小;DeepSeek-R1 用 $G \approx 16$。
仓库中姊妹教程 docs/tutorials/rlhf_dpo_grpo_ppo_tutorial.md 从 RLHF/DPO 视角给出了同一算法的交叉印证,并补充了 GRPO 与 RLOO/ReMax 的共性(都用多采样 baseline 替代 critic)、DAPO 等工业级进阶变体(clip higher、dynamic sampling、token-level loss),适合作为延伸阅读。
5.3 GRPO advantage 与 loss 的 from-scratch 实现
import torch
def grpo_advantage(rewards: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
"""
Group-relative advantage estimation.
Args:
rewards: [G] tensor of trace-level rewards (one prompt's group).
eps: numerical stability.
Returns:
advantages: [G] tensor, mean ≈ 0, std ≈ 1.
"""
mu = rewards.mean()
sigma = rewards.std(unbiased=False) # 用 biased std(除 G 而非 G-1)
return (rewards - mu) / (sigma + eps)
def grpo_loss(
log_probs_new: torch.Tensor, # [G, T] new policy log p
log_probs_old: torch.Tensor, # [G, T] old policy log p (detached)
log_probs_ref: torch.Tensor, # [G, T] reference policy log p
rewards: torch.Tensor, # [G] trace-level
mask: torch.Tensor, # [G, T] valid token mask
clip_eps: float = 0.2,
kl_beta: float = 0.04,
) -> torch.Tensor:
"""Single-prompt GRPO loss (group size = G traces).
要求 `log_probs_old` 和 `log_probs_ref` 都是 detached(无梯度);
若调用方传入带梯度 tensor,会把梯度错误地传进 old/reference policy。
"""
adv = grpo_advantage(rewards) # [G]
adv = adv.unsqueeze(-1) # [G, 1] broadcast to tokens
# 防御性 detach:即使调用方忘了,old / ref policy 也不会被反传
log_probs_old = log_probs_old.detach()
log_probs_ref = log_probs_ref.detach()
# PPO clipping
ratio = torch.exp(log_probs_new - log_probs_old) # [G, T]
surr1 = ratio * adv
surr2 = torch.clamp(ratio, 1 - clip_eps, 1 + clip_eps) * adv
pg_loss = -torch.min(surr1, surr2) # [G, T]
# KL 正则:约束 policy 不漂离 reference 太远
# DeepSeekMath/R1 用 Schulman 的 unbiased k3 估计:
# KL ≈ exp(log_ref - log_new) - (log_ref - log_new) - 1 ≥ 0
# 这是 KL(π_θ || π_ref) 的样本无偏估计,且总是非负,比 raw log-ratio 稳得多
log_diff = log_probs_ref - log_probs_new # [G, T]
kl = torch.exp(log_diff) - log_diff - 1.0 # [G, T], ≥ 0
loss = pg_loss + kl_beta * kl # [G, T]
# Masked mean over valid tokens
return (loss * mask).sum() / mask.sum().clamp_min(1.0)
GRPO 实现的常见坑(这些边界条件正是交叉评审逐条核验过的点):
log_probs_old必须 detach(不参与梯度),否则和ratio形成奇怪的梯度路径。上面代码里的防御性detach()就是评审阶段补上的加固;std用 biased 还是 unbiased 影响小但要一致(DeepSeek 公开版用 biased);- 当 $G$ 个 rewards 全相同(全对或全错):$\sigma = 0$ 且 $r_i - \mu \equiv 0$,加
eps后 advantage = 0 → policy-gradient 项归零;但 KL 正则项仍然存在,总 loss 退化成kl_beta * KL(仍会把 policy 拉回 reference)。生产里通常 skip 这种 prompt(data filtering)以避免无信号更新,或 clamp std 下限(如 0.1)。注意这里不是 $\pm\infty$(分子也是 0),不加 eps 时是 0/0 = NaN; - Mask 必须覆盖所有 padding token,否则 padded log-prob 会污染 loss。
5.4 GRPO 为什么 sample efficient
先看一个常被混淆的数字:R1 论文公开的 ~80 万样本(≈ 600k reasoning + 200k non-reasoning)是 Stage 3 rejection sampling + SFT 数据,不是 RL prompt 数。R1 Stage 2 reasoning RL 的 prompt 总量论文未严格公开,但整体训练计算量(~147K H800-hours)仍比同期 RLHF(InstructGPT 几百万人类偏好 pair)显著小。
根因(不止是"省 critic"):
- Trace-level reward 与 trace-level credit 自然对齐:GRPO 直接把 trace reward 当 advantage 给所有 token,不需要 critic 做 per-token credit assignment。在 reward 完全只看 final answer 的设定下,这就是 sequence-level Monte-Carlo return 的标准 policy-gradient 估计——对 trace-level reward 而言方差更低,比 critic 在 long-CoT 上的 noisy per-token $V$ 更稳;
- 同 prompt 内的 contrast 自动消除 prompt-level noise:advantage = $(r - \mu) / \sigma$ 让 advantage 分布稳定,等价于在每个 prompt 内做了 paired comparison;
- PPO clipping 限制单步更新幅度:与 $G$ 一起保证不会因某个 prompt 的 outlier reward 把 policy 推飞;
- Rule-based reward 难以被 RM-hacking:R1 用规则 reward(答案正则匹配 + 格式
<think></think>),没有 learned RM 可被 over-optimization;advantage 主要反映"是否答对"。规则 reward 并非完全不可 hack——policy 仍可能利用正则漏洞、格式 trick 或测试集泄露——但避开了 learned-RM 漂出训练分布的主要 failure mode。
6. DeepSeek-R1-Zero / R1 全流程
6.1 R1-Zero:纯 RL、不要 SFT 的极端实验
唯一假设:base model(DeepSeek-V3-Base, 671B MoE)已具备基础语言 + 数学能力。
训练设置:
- Reward =
accuracy_reward + format_reward(rule-based,无 RM):accuracy_reward看答案能否被自动 extract + match ground truth;format_reward看是否用think标签包围推理过程; - 算法:GRPO,$G = 16$,KL $\beta = 0.001$;
- 数据:数学(MATH, AIME 等)+ 代码(LeetCode-like 可执行评测)。
涌现行为("aha moment",DeepSeek-R1 paper Fig 3):
- 训练几 K steps 后,CoT 长度自发增长(200 token → 数千 token);
- 出现 "Wait, let me reconsider..."、"Actually that's wrong"、"Let me verify by..." 等 self-reflection 句式;
- 在 AIME 2024 上 pass@1 从 base 的 15.6% → 71.0%(远超 GPT-4o-0513 的 9.3%)。
R1-Zero 的历史意义:在它之前,社区普遍认为 reasoning 能力必须靠 SFT cold start 才能 RL bootstrap。R1-Zero 证明 base model + 纯规则 reward 也能做到——这是 LLM 训练历史上第一次大规模复现 "RL from scratch elicit reasoning"(相当于 AlphaGo Zero 在 LLM 上的等价物,虽然 base 已 pretrain)。
问题:R1-Zero 输出有可读性问题——推理过程语言混杂、有时不分段、人类读不懂。所以 R1 加了后续阶段。
6.2 R1 完整 4 阶段 pipeline
| 阶段 | 输入 | 方法 | 目标 |
|---|---|---|---|
| Stage 1: Cold-start SFT | DeepSeek-V3-Base | 数千条精选 long-CoT(部分来自 R1-Zero 输出 + 人工修正可读性) | 让模型学到 "human-readable reasoning format" |
| Stage 2: Reasoning-oriented RL | Stage 1 模型 | GRPO + rule-based reward + 语言一致性 reward(penalize 中英混杂) | 提升推理能力 |
| Stage 3: Rejection sampling + SFT | Stage 2 模型 | 用 Stage 2 模型大量 sample → 用 PRM/规则筛选 → 60 万 reasoning + 20 万通用数据再 SFT | 扩展到非数学/代码领域;保留通用能力 |
| Stage 4: All-scenario RL | Stage 3 模型 | GRPO + (rule reward for math/code) + (RM for helpfulness/harmlessness) | 全场景对齐 |
核心 insight:
- Stage 1 = readability injection,不是为了 reasoning(reasoning 来自 RL);
- Stage 3 = generalization injection,把数学/代码 RL 学到的推理能力 transfer 到非可验证领域(写作、对话、QA);
- Stage 4 = safety + helpfulness alignment,等价于 RLHF 收尾。
6.3 R1-Distill:把 R1 蒸到小模型
DeepSeek 用 Stage 3 数据(60 万 reasoning 样本)对 Qwen2.5-{1.5B, 7B, 14B, 32B} 和 Llama3-{8B, 70B} 做纯 SFT(无 RL),得到 R1-Distill 系列。关键发现(论文 Table 5):
- DeepSeek-R1-Distill-Qwen-32B 在 AIME 2024 上 72.6 vs o1-mini 63.6——SFT 蒸馏的小模型超过 o1-mini;
- 1.5B 模型在 MATH-500 上 83.9,远超原 Qwen2.5-Math-1.5B 的 51.0;
- 论文公开版 R1-Distill 只做了 SFT,没有再叠 RL;作者明确指出 "incorporating RL could substantially boost performance"——所以"distill 后能否 RL 出更多"是未结论的开放问题。
高频问题:"为什么 R1-Distill 远超原 model,但比直接对小模型做 RL 反而更好?"
- DeepSeek-R1-Distill-Qwen-32B (SFT only) > 直接对 Qwen-32B 从头做 RL 的效果;
- 原因:小模型 base 太弱,纯 RL 难涌现 reasoning(rollout 几乎全错,reward 信号过稀疏);用大模型 R1 生成的 reasoning trace 做 SFT 等价于 distillation through demonstrations,把"reasoning 行为模式"直接复制过来;
- Implication:reasoning 能力的涌现需要大 base + 强 RL;小模型上靠蒸馏复制是最经济路线。
7. Test-Time Scaling:Snell 2024 与 s1 budget forcing
7.1 Snell et al. 2024(arXiv 2408.03314)核心结论
题目:固定 inference FLOPs,怎么分配最优?
实验设置:同一 base model(PaLM-2 系列);对比不同 test-time 策略(BoN、majority vote、ToT-like beam search、PRM beam search、sequential revision);compute-optimal 指每个 prompt 根据难度动态选策略(简单题 greedy,难题 beam search + PRM)。
核心 finding:
- 在固定预算下,optimal test-time scaling 在某些 MATH 子集上 > 14× 模型 scaling;
- 简单题:majority vote / BoN 4–8 即可;
- 难题:PRM-guided sequential revision + beam search 收益最大。
Compute-optimal scaling 公式:
$$\text{Compute}\text{optimal}(x) = \arg\min{(\theta, N)} \mathbb{E}[L(\pi_\theta(x; N))] ;\text{s.t.}; \text{FLOPs}(\theta, N) \leq B$$
其中 $\theta$ = 模型规模,$N$ = test-time samples / beam width。论文给的实际经验:在 base model 能 pass@1 > 30% 的题上,加 N 比加参数更划算。
7.2 s1: Simple Test-Time Scaling(Muennighoff et al., 2025-02, arXiv 2501.19393)
最简单的 reasoning model:1000 条样本 SFT + budget forcing 推理控制。
- 数据(s1K,1000 条):按 difficulty、diversity、quality 三个 criteria 筛选;每条 = question + reasoning trace(用 Gemini Thinking 生成)+ answer;
- 训练:Qwen2.5-32B-Instruct 上做 26 分钟 SFT(16×H100,1 epoch)——堪称史上最便宜的 reasoning model。
推理 trick:budget forcing
模型生成:
think
[推理 token...]
[模型试图输出 /think,但当前 token 数 < target_budget]
[强行替换 /think 为 "Wait"]
[模型继续推理...]
...
[当 token 数 >= target_budget 或自然结束]
/think
答案:...
效果:AIME 2024 上 s1-32B(用 budget forcing)= 56.7%,超过 o1-preview 的 44.6%。
为什么 "Wait" 这么简单的 trick 有用?
- SFT 让 base model 学会了
think.../think格式,但 reasoning 长度有分布(短题短推理); - 强行注入 "Wait" 让模型停在
/think的 boundary,激活其 in-context 的"反思"能力; - 等价于 forcing 模型留在 "thinking mode" 多采样几条推理路径;
- 失败案例:base model 完全没见过反思 pattern 时,"Wait" 后面接的内容是 garbage——s1 的 1K SFT 数据已隐含了反思 pattern,这是必要前提。
7.3 Sequential vs Parallel test-time compute
| 维度 | Parallel(BoN, Self-Consistency) | Sequential(long CoT, s1 budget forcing, o1) |
|---|---|---|
| 实现 | Sample N 次,verifier/vote 选 | 单条 trace 拉长,模型自己反思 |
| 延迟 | $N$ 倍延迟(并行可降低) | 单条但很长(不可并行降低) |
| 显存 | KV cache × N 或顺序复用 | 单 KV cache 但 sequence 长 |
| Plateau | 早期饱和(N=8-16) | 持续扩展(10K-100K token) |
| 适合 | 浅推理(GSM8K、commonsense) | 深推理(AIME、Codeforces) |
Snell 2024 报告:对简单题 parallel 更划算,对难题 sequential 显著更好。
8. MCTS for Reasoning:rStar 系列
8.1 PUCT 公式(AlphaGo / AlphaZero 起源)
在节点 $s$,对每个 action $a$,PUCT score:
$$\boxed{;U(s, a) = Q(s, a) + c_\text{puct} \cdot \pi(a \mid s) \cdot \frac{\sqrt{N(s)}}{1 + N(s, a)};}$$
- $Q(s, a)$ = 当前 $(s, a)$ 的平均 value(exploitation);
- $\pi(a \mid s)$ = policy prior(来自 policy network);
- $N(s)$ = 节点 $s$ 的总访问次数(分子是 parent visits);
- $N(s, a)$ = action $a$ 被选过几次(分母是 child visits,这是易错点);
- $c_\text{puct}$ = exploration constant(典型 1.0–2.0;AlphaZero 用 ≈1.0,MCTS-for-LLM 常用 1.5–2.0,因为 LLM policy prior 比围棋更准)。
直觉:访问次数少的 + policy 看好的 + 当前 value 高的 action 被偏好。$c_\text{puct}$ 大偏向 exploration,小偏向 exploitation。
8.2 rStar(Microsoft Research, arXiv 2408.06195)关键 idea
把 LLM 当 policy + value 套进 MCTS:
- State:当前部分推理 $s_{<t}$;
- Action:下一步要做什么(rStar 定义了 5 种 reasoning action:propose one-step, propose sub-question, generate full CoT, decompose, rephrase);
- Reward:终止节点用 mutual consistency check(另一个 LLM verify)。
rStar-Math(arXiv 2501.04519) 进一步:用 MCTS rollout 自动标 process label(类似 Math-Shepherd 思路),训出 process preference model(PPM),policy + PPM 反复 self-evolve 四轮。Qwen2.5-Math-7B 在 MATH 上从 58.8 → 90.0,逼近 o1-preview。
8.3 简化 MCTS for reasoning 参考实现
import math
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class MCTSNode:
state: str # 部分推理 trace
parent: Optional["MCTSNode"] = None
prior: float = 0.0 # policy network prob
visit_count: int = 0
value_sum: float = 0.0
children: dict[str, "MCTSNode"] = field(default_factory=dict)
is_terminal: bool = False
@property
def q_value(self) -> float:
return self.value_sum / self.visit_count if self.visit_count > 0 else 0.0
def puct_score(node: MCTSNode, parent_visits: int, c_puct: float = 1.5) -> float:
"""PUCT: Q + c * P * sqrt(N_parent) / (1 + N)"""
return node.q_value + c_puct * node.prior * math.sqrt(parent_visits) / (1 + node.visit_count)
def select(root: MCTSNode) -> MCTSNode:
"""Traverse tree by PUCT until reaching a leaf."""
node = root
while node.children and not node.is_terminal:
node = max(node.children.values(),
key=lambda c: puct_score(c, node.visit_count))
return node
def expand(node: MCTSNode, policy_lm, k: int = 4):
"""Sample k next-step candidates from policy LM."""
if node.is_terminal:
return
candidates = policy_lm.sample_next_steps(node.state, k=k) # list of (step_text, prior)
for step_text, prior in candidates:
new_state = node.state + "\n" + step_text
is_term = step_text.startswith("Final answer:")
node.children[step_text] = MCTSNode(
state=new_state, parent=node, prior=prior, is_terminal=is_term,
)
def rollout(node: MCTSNode, policy_lm, verifier) -> float:
"""Simulate from node to terminal; return reward."""
if node.is_terminal:
return verifier(node.state)
state = node.state
while len(state) < 4096:
step = policy_lm.sample_next_step(state, temperature=1.0)
state += "\n" + step
if step.startswith("Final answer:"):
break
return verifier(state) # ORM or PRM final score
def backup(node: MCTSNode, reward: float):
"""Propagate reward up the path."""
while node is not None:
node.visit_count += 1
node.value_sum += reward
node = node.parent
def mcts_search(prompt: str, policy_lm, verifier, n_simulations: int = 100, k: int = 4) -> str:
"""Standard MCTS for reasoning."""
root = MCTSNode(state=prompt)
# 首轮先 expand 根节点,保证 root.children 非空
expand(root, policy_lm, k=k)
for _ in range(n_simulations):
leaf = select(root)
if leaf.visit_count > 0 and not leaf.is_terminal:
expand(leaf, policy_lm, k=k)
if leaf.children:
leaf = max(leaf.children.values(),
key=lambda c: c.prior) # 先选 prior 最高
reward = rollout(leaf, policy_lm, verifier)
backup(leaf, reward)
if not root.children:
return root.state # safety guard
# 沿 visit_count 最高路径下行,直到 terminal 或 leaf(叶子未 expand)
# 这样返回的是完整 trace 而非仅第一层 child;如果中间层最强路径未到 terminal,
# 也至少返回该路径上当前已 expand 的最深 state
node = root
while node.children and not node.is_terminal:
node = max(node.children.values(), key=lambda c: c.visit_count)
return node.state
MCTS for LLM 的真实开销:上面伪代码假设 policy_lm.sample_next_steps 是廉价 op,实际上每个 expansion 是一次完整 LLM forward pass(生成几十 token + 评分)。100 simulations × 4 children = 400 次 LLM call,比单次 greedy decode 慢 100–400×。这正是实际部署中 long-CoT(o1/R1)比 MCTS 更主流的原因——把 search 内化到 policy。
8.4 PRM-Guided Beam Search(更轻量替代)
不做完整 MCTS,只保留 top-$b$ 个 partial trace,每步用 PRM 评分:
import math
def prm_beam_search(
policy_lm,
prm,
prompt: str,
beam_width: int = 4,
expansion: int = 4,
max_steps: int = 16,
) -> str:
"""PRM-guided step-level beam search."""
# (cumulative_log_score, partial_trace, is_done)
beams = [(0.0, prompt, False)]
for _ in range(max_steps):
new_beams = []
for score, trace, done in beams:
if done:
new_beams.append((score, trace, True))
continue
# 从当前 trace expand expansion 个候选 step
candidates = policy_lm.sample_next_steps(trace, k=expansion, temperature=0.8)
for step, _ in candidates:
new_trace = trace + "\n" + step
step_prob = prm.score_step(new_trace) # in [0, 1]
# 累加 log 概率(数值稳定)
new_score = score + math.log(step_prob + 1e-6)
is_terminal = step.startswith("Final answer:")
new_beams.append((new_score, new_trace, is_terminal))
# 防御性 edge case:若所有 beam 都 expand 出空候选,避免 max(empty)
if not new_beams:
break
# 按累积 log-score 选 top-b
beams = sorted(new_beams, key=lambda x: x[0], reverse=True)[:beam_width]
if all(b[2] for b in beams):
break
# 返回最高分的 trace(空 beam 时回退到初始 prompt)
return max(beams, key=lambda x: x[0])[1] if beams else prompt
beam search vs MCTS:beam search 是 deterministic + breadth-bounded(每层固定 b),不回溯;MCTS 是 stochastic + adaptive(visit count 决定深度),可回溯。前者快 5–10×,后者在搜索深树时质量更高。Snell 2024 的经验:题难度中等 → PRM beam search;超难 → MCTS。注意代码中
if not new_beams: break这个守卫同样是交叉评审抓到的 edge case(否则max(empty)会抛 ValueError)。
9. 推理模型全景对比与选型
9.1 主流推理模型对比表(截至 2026)
| 模型 | 厂商 | 发布 | 训练范式 | 推理控制 | 公开性 |
|---|---|---|---|---|---|
| o1-preview / o1 | OpenAI | 2024-09 | RL on hidden CoT(细节闭源) | reasoning_effort: low/med/high | 闭源 |
| o3 | OpenAI | 2024-12 / 2025 | o1 后继;ARC-AGI 75.7%/87.5% | 推理 budget 可调;high-compute 172× | 闭源 |
| DeepSeek-R1-Zero | DeepSeek | 2025-01 | 纯 RL(GRPO + 规则 reward) | 自然停止 | 完全开源(权重 + 论文) |
| DeepSeek-R1 | DeepSeek | 2025-01 | 4 阶段:SFT cold-start + RL + rejection SFT + RL | 自然停止 | 完全开源 |
| R1-Distill (1.5B-70B) | DeepSeek | 2025-01 | SFT only on R1 reasoning data | 自然停止 | 完全开源 |
| Claude 3.7 Sonnet | Anthropic | 2025-02 | hybrid: standard + extended thinking | budget tokens 用户可设(up to 128K) | 闭源,但 thinking 内容可见 |
| Gemini 2.0 Flash Thinking | Google DeepMind | 2024-12 | 推理优化(具体方法未公布) | 显式展示 thinking | 闭源 |
| s1-32B | Stanford/Allen AI | 2025-02 | 1K-sample SFT + "Wait" budget forcing | budget forcing 控制 thinking 长度 | 完全开源(含 s1K 数据) |
| rStar-Math | Microsoft | 2025-01 | MCTS + PPM self-evolution | 推理时显式 MCTS | 部分开源 |
| DeepSeek-Prover-V2 | DeepSeek | 2025-04 | subgoal decomposition + Lean 4 + RL | Lean 形式化验证 | 完全开源 |
9.2 选型决策树(面试常问)
任务领域?
├── 数学/竞赛(AIME / IMO) ─→ R1 / R1-Distill-32B / o1 / s1-32B
├── 形式化定理证明(Lean / Coq) ─→ DeepSeek-Prover-V2
├── 代码(Codeforces / SWE-bench) ─→ o1-pro / R1 / Claude 3.7 + thinking
├── 通用 reasoning(agent / chain task) ─→ Claude 3.7 / Gemini 2 Thinking / R1
├── 小模型部署(边缘 / 移动) ─→ R1-Distill-1.5B-7B
└── ARC-AGI / 抽象推理 ─→ o3(高算力档)
部署预算?
├── 高(CoT 几千 token / 题)─→ o1, o3, R1, Claude 3.7-extended
├── 中(CoT 1-2K token)─→ s1-32B, R1-Distill-32B
└── 低(greedy 或 BoN=8)─→ R1-Distill-1.5B + best-of-N with verifier
9.3 CoT 是否真的反映推理?
经典争议:Attention is not Explanation(Jain & Wallace 2019)在 CoT 上的 echo——模型展示的 reasoning 是否真实反映其内部计算?
- 支持真实:Anthropic Sleeper Agents 实验(Hubinger et al. 2024)显示 CoT 内容会影响下游 action(不是纯 post-hoc rationalization);
- 支持非真实:Turpin et al. 2023 "Language Models Don't Always Say What They Think" 发现给 biased exemplar 后,模型 CoT 给出 plausible 但错误的 reasoning(biased 但 CoT 没提 bias);
- R1-Zero 的 "aha moment" 是有限证据:行为变化(更长 + 更多反思)确实和性能上涨同步,但仍可能是 surface pattern。
面试时应给 balanced view:CoT 是有用 + 部分可信的,但不能当 100% 的 explanation。
10. 25 道高频面试题(L1 必会 / L2 进阶 / L3 顶级 lab)
L1 必会题(任何 LLM 岗都会问)
Q1. Chain-of-Thought 是什么?什么时候用? few-shot prompt 展示 "step-by-step" 推理 demonstration;在 >62B 大模型上 emergent 出推理能力(GSM8K +30+%);小模型上 CoT 反而变差;Kojima 2022 发现 zero-shot 也行。误区:把 CoT 当"魔法 prompt"——它只是触发,能力来自 base model 自身。
Q2. Self-Consistency 怎么工作?为什么比 greedy 好? temperature > 0 采样 N 条 CoT;提取 final answer 并 normalize(去单位、化简分数);多数票。直觉:正确答案是"吸引子",多条采样路径会收敛于此;Wang 2022 报告 GSM8K +17.9%。误区:不做 answer normalization;以为温度越高越好(实际 0.5–0.7 最佳)。
Q3. Tree-of-Thought 与 CoT 的区别? CoT 是 autoregressive 单链路径;ToT 是显式树搜索:每节点一个 thought,sample k 个 child,LLM 自评分,可回溯剪枝;但需要外部搜索框架 + 多次 LLM call,比 CoT 慢 5–50×。Game of 24: CoT 4% → ToT 74%(GPT-4)。误区:只说 ToT 是"多次 CoT"——它的核心是显式 evaluator + backtrack。
Q4. ORM 和 PRM 区别? ORM:整条 trace 一个 reward(基于 final answer 对错);PRM:每个 reasoning step 一个 reward,信号密集但标注贵(人标 PRM800K 80 万 step)。Math-Shepherd 从中间 step 采样 Monte Carlo completion rollouts(不是 MCTS 树搜索),用 rollout 正确率自动标 step label。Lightman 2023: best-of-1024 上 PRM 78% > ORM 72% > majority vote 70%。误区:把 PRM 当"训练 reward"——它主要用于推理时 verify,不一定参与 RL。
Q5. Best-of-N 怎么工作?什么时候饱和? 同 prompt 采样 N 条 trace,用 verifier 选最高分。简单题 N=4–8 饱和;难题可涨到 N=64–128。理论上限 oracle pass@N = $1-(1-p_1)^N$,但 verifier 不完美时远低于此。误区:以为 N 越大越好——verifier 误差会让 BoN 在某点之后开始下降("verifier overfit to surface features")。
Q6. o1 / R1 的 reasoning_tokens 是什么? 模型生成的 hidden chain-of-thought tokens;API 只返回 token 计数(OpenAI o1 返回 reasoning_tokens 字段),内容不公开;用户按 reasoning_tokens 付费(这就是 o1 贵的原因);R1 把 reasoning 包在 think 标签里全文可见。推理算力 ≈ reasoning_tokens × model FLOPs/token。误区:把 reasoning_tokens 当 "no-cost 优化"——它显著影响延迟和成本。
Q7. GRPO 和 PPO 主要区别? PPO 需要 critic 估计每个 token 的 baseline;GRPO 用 group statistics 替代 critic:同 prompt 采样 G 个 trace,advantage = $(r_i - \mu)/\sigma$,并把 trace-level advantage 广播给所有 token;显存降一半,long-CoT 上 critic 本来就难学,GRPO 反而稳;共享 PPO clipping 与 KL 正则。误区:以为 GRPO 是"小改动"——它在 long-CoT 上的稳定性优势是质变。
Q8. R1-Zero 的 "aha moment" 是什么? DeepSeek-R1 paper Fig 3 报告:纯 RL 训练几 K steps 后,CoT 长度自发增长(数百 → 数千 token);自发出现 "Wait, let me reconsider..." 等反思 pattern;性能跳变(AIME pass@1 15% → 70%)。直觉:rule-based reward + GRPO 让"思考更久 + 自验证"成为高 reward strategy。误区:把 "aha moment" 当玄学——它是 reward shaping + 长 episode RL 的可预期涌现。
Q9. R1-Distill 为什么用 SFT 不用 RL? R1 的 reasoning 能力靠大 base + 强 RL 涌现;直接对小模型 RL 难涌现(base 太弱,rollout 几乎全错,reward 信号过稀疏);用 R1 生成的 60 万 reasoning trace 做 SFT 等价 demonstration learning;论文报告 32B SFT-distill 在 AIME 上 72.6 vs 直接对 Qwen-32B RL 的 47.0。Implication:小模型上蒸馏 reasoning > 直接 RL(在当前算法下)。误区:以为 RL 总是比 SFT 好——前提是 base 够强。
Q10. Snell 2024 的 "test-time compute > parameter scaling" 是什么意思? 固定 inference 算力预算,让小模型多 sample + verify;在某些 MATH 子集上可超过 14× 大模型的 greedy 性能;前提是 base model 在该任务有 non-trivial pass@1(>30%),不是普适——完全不会的任务再多 test-time compute 也救不回来;实际工业部署常用 R1-Distill + BoN=8 + PRM 替代直接调 R1。误区:把它当 "scaling law 终结"——它只是"另一个维度的 scaling law",不取代训练 scaling。
L2 进阶题(reasoning 方向 / research 岗)
Q11. 手推 GRPO advantage 公式,以及 std=0 的情况如何处理? 对每个 prompt 采样 G 条 trace,得 rewards ${r_1, \dots, r_G}$;$\mu = \frac{1}{G}\sum r_i$,$\sigma = \sqrt{\frac{1}{G}\sum(r_i - \mu)^2}$(biased std);$A_i = (r_i - \mu)/(\sigma + \epsilon)$。当 G 个 rewards 全相同:加 $\epsilon$ 时 advantage = $0/\epsilon = 0$,policy-gradient 项归零,但 KL 正则项仍在(把 policy 拉回 reference);不加 $\epsilon$ 是 0/0 = NaN(注意不是 $\pm\infty$,分子也是 0)。实践:skip 该 prompt 或 clamp $\sigma$ 到 floor(如 0.1)——这类 prompt 过易或过难,data filtering 应剔除。误区:只写公式不说 std=0 的边界。
Q12. R1 的 4 阶段 pipeline 每阶段目标是什么? Stage 1 Cold-start SFT:学会 human-readable reasoning format(不为 reasoning 能力本身);Stage 2 Reasoning RL:GRPO + 规则 reward 提升数学/代码推理;Stage 3 Rejection sampling + SFT:扩展到非可验证领域 + 保留通用能力;Stage 4 All-scenario RL:safety + helpfulness 收尾。关键:reasoning 来自 Stage 2 RL;Stage 1+3 是 readability/generalization 注入;Stage 4 是对齐。误区:把 4 阶段当"做菜步骤"——每阶段功能正交。
Q13. 为什么 R1 的 reward 是 rule-based 而非学的 RM? 数学/代码可以程序化验证(答案正则匹配、单元测试);学的 RM 容易被 hack(reward model overoptimization);规则 reward 提供接近 ground-truth 的信号,避开了 learned-RM 漂出训练分布的主要 failure mode(仍可能被正则漏洞、格式 trick、test-set 污染 hack,但远比 RM hacking 容易堵);代价是只能用于可验证任务;Stage 4 又加 RM 处理 helpfulness/harmlessness——可验证任务用 rule,开放任务用 RM。误区:把 rule-based 当"简单"——它的关键是难以被 RM-hacking,而非简单。
Q14. Budget forcing("Wait" trick)为什么有效? s1 在 1K reasoning trace 上 SFT,模型学会 think 格式 + 反思 pattern;推理时如果模型试图输出 /think 但 token 数没到 target budget,强行替换为 "Wait",模型自然续接反思 token;等价于 forcing 模型留在 thinking mode,多采样几条 internal reasoning path。失败模式:base 完全没见过反思 pattern → "Wait" 后接 garbage(1K SFT 是必要前提)。误区:以为 "Wait" 是 prompting trick——它依赖 SFT 注入的反思 pattern。
Q15. PRM 怎么用在 best-of-N?aggregation 怎么选? 对 N 条 trace,每条用 PRM 给每个 step 打分 $p_1, \dots, p_T$;三种 aggregation:min、prod(log-sum)、mean。Lightman 2023:min ≈ prod >> mean(mean 被高分 step 掩盖弱 step);min 直觉:trace 强度取决于最弱一环。代码细节:prod 用 log-sum 避免数值下溢。误区:只用 mean——常见错误。
Q16. PUCT 公式是什么?c_puct 怎么调? $U(s, a) = Q(s, a) + c_\text{puct} \cdot \pi(a \mid s) \cdot \sqrt{N(s)} / (1 + N(s, a))$;$Q$ = exploit,后一项 = explore;$c_\text{puct}$ 大偏向 exploration,小偏向 exploitation;AlphaZero 用 ≈1.0,MCTS-for-LLM 常用 1.5–2.0。注意:分子是 $\sqrt{N(s)}$(parent visits),分母是 $1 + N(s, a)$(child visits)。误区:把 $\sqrt{N}$ 错记为 child visits。
Q17. Math-Shepherd 怎么自动标 step label?关键假设是什么? 对每个 step $s_t$,从 $s_t$ 出发 rollout K 条 completion;数多少条最终 answer 正确,得 $\hat{q}_t = (\text{正确数})/K$;用 $\hat{q}_t$ 当 BCE/MSE 标签训 PRM。关键假设:base model 在该任务有 non-trivial success rate(否则 rollout 全错,$\hat{q}_t$ 全 0);bootstrap 问题:弱 base 没法用 MCTS-label,强 base 不需要 PRM;实际用中等强度 base(Mistral-7B post-SFT)作 rollout source。误区:以为 MCTS-label 是免费的——它需要 base 已有部分能力。
Q18. CoT 是真的反映推理吗?怎么验证? 部分真实、部分 post-hoc rationalization(共识)。Turpin 2023:biased exemplar 下 CoT 给出 plausible 但错误的解释;Sleeper Agents(Hubinger 2024):CoT 内容影响下游 action,不纯 post-hoc。验证方法:causal intervention(改 CoT 看 output 是否变)、faithfulness benchmark。误区:只说"CoT 是 explanation"或"CoT 全是 post-hoc"——两个极端都错。
Q19. 怎么判断哪个 reasoning model 适合你的任务? 可验证任务(math/code)→ rule-based RL 路线(R1 / R1-Distill);开放任务(writing/dialogue)→ hybrid RM 路线(Claude 3.7-extended / o1);抽象推理(ARC-AGI)→ o3 高算力档;形式化证明 → DeepSeek-Prover-V2(Lean 4);部署预算严 → R1-Distill-7B/14B + best-of-N + PRM verifier(比直接 R1 便宜 10×+)。误区:不看任务就推荐 o1——错配会贵且效果差。
Q20. KL 正则在 GRPO 里的作用是什么?β 怎么调? $\mathcal{L}\text{total} = \mathcal{L}\text{PG} + \beta \cdot \mathrm{KL}(\pi_\theta | \pi_\text{ref})$,$\pi_\text{ref}$ 是训练开始前的 policy;$\beta$ 大 → policy 不漂离 reference 但学不动新能力;$\beta$ 小 → 自由探索但可能 collapse。DeepSeek-R1 用 $\beta = 0.001$(很小,鼓励探索);标准 RLHF 用 0.01–0.1。对 long-CoT,KL 在 token-level 累加、总量很大,所以 $\beta$ 要远小于短 CoT。误区:照搬 RLHF 的 $\beta$ 到 long-CoT——会过度抑制探索。
L3 高级题(顶级 lab / 研究方向)
Q21. GRPO 比 PPO sample efficient 的 root cause 是什么?(不止"省 critic")
- Trace-level reward 与 trace-level credit 对齐:reward 只来自 final answer 时,PPO 用 critic 做 per-token credit 反而引入噪声;GRPO 直接 broadcast trace-level advantage——估计本身带有 group baseline 的有限偏差(mean/std 都是有偏样本统计),但比 critic 在长 episode 上的 high-variance 估计更稳;
- 同 prompt 内 contrast 消除 prompt-level baseline noise:advantage = $(r-\mu)/\sigma$ 等价 paired comparison,比 critic 的全局 baseline 准;
- Long-CoT 上 critic 难学:reward 极度稀疏(episode 4K–32K token),$V_\psi$ 在中间 token 上几乎随机;GRPO 跳过这个学习问题;
- Rule-based reward 难被 RM-hacking:policy 优化方向接近 ground-truth(仍可能被正则/格式漏洞 hack,但避开 RM-distribution-shift 主路);
- Group size G 控制 variance:variance ∝ $1/G$,$G=16$ 给出足够低 variance 且不爆显存。
结论:GRPO 不是"小改动",是 long-CoT + rule reward 设定下的 algorithmically right answer。
Q22. R1-Zero 的纯 RL 涌现 vs 历史 PPO RLHF (InstructGPT) 差别在哪?
- Reward 来源:R1-Zero 用 rule-based;InstructGPT 用学的 RM(preference model);
- Reward 粒度:两者其实都是 response-level scalar reward(InstructGPT 的 learned RM 输出的是 response-level scalar preference reward,不是逐 token 打分;token-level advantage 由 critic + GAE + KL penalty 共同构造),但 RM 在 RLHF 全流程里仍易遭遇 reward overoptimization(policy 漂出 RM 训练分布 → 拿到不真实高分);
- 算法:R1-Zero 用 GRPO;InstructGPT 用 PPO + critic + RM;
- Reward scope:R1-Zero 训 reasoning(可验证,有 oracle reward);InstructGPT 训 alignment(开放,无 oracle reward);
- Base model:R1-Zero 用 V3-Base(671B MoE),已有强 pretrained reasoning prior;InstructGPT 是 GPT-3(175B dense)。
含义:reasoning RL 突破 = rule reward + GRPO + 强 base + long-CoT 联合作用,不是单点技术胜利。(此处关于 InstructGPT RM 语义的修正,正是本教程交叉评审抓到的事实性错误之一。)
Q23. 为什么 "Wait" 这么简单的 trick 能超 o1-preview?这告诉我们什么?
- 第一层:1K SFT 已让模型学会
think格式 + 反思 pattern 的 "shape"; - 第二层:模型实际上已经"知道怎么想"(pretrain 中见过大量人类推理),SFT 只是激活 + 格式化;
- 第三层:"Wait" 强制模型在 thinking boundary 停下重新采样——相当于强行做了一次 in-context self-revision;
- 推论:reasoning 能力的核心是激活而非注入;
- Implication:不要假设 reasoning 必须靠大规模 RL 才能出;SFT data quality > quantity(s1K 1000 条 > 大量低质数据);推理控制(budget forcing)是 vastly underexplored 维度;
- 反思:s1 不否定 R1 路线——R1-Distill 也是一种 distill SFT,s1 是这条思路的极端版本。
Q24. Sequential 与 parallel test-time compute 的本质差异?什么时候选哪个?
- Sequential(o1, R1, s1):单条 trace 拉长,模型自反思自验证。优势:单 KV cache(显存友好)、trace 内信息连续传递;劣势:早期错误传播到末端(无 backtrack)、难任务需要 10K–100K token 超长 trace;
- Parallel(BoN, ToT, MCTS):多条独立 trace,外部 aggregator/verifier 选。优势:可并行、错误不传播;劣势:trace 间无信息交换、verifier 必须精准;
- 选型:sequential dependency 强(数学竞赛、定理证明)→ long CoT;多解任务(code、creative writing)→ BoN;有 well-defined intermediate verifier(math step)→ MCTS / PRM beam search;延迟敏感 → parallel;单 GPU 内存敏感 → sequential;
- 未来方向:sequential + parallel 混合——单条 long CoT 内嵌入 multi-path 探索。
Q25. 如果让你设计下一代 reasoning model,应该往哪几个方向走?(open-ended 顶级 lab 题) 可信回答框架(挑 2–3 个深入展开即可):
- 方向 1 训练算法:GRPO 现在 trace-level,如何 token-level 又不引入 critic(如 PRM-as-critic)?rule reward 太稀疏,能否 dense 化但保持 hard-to-hack(形式化验证 intermediate steps)?能否 online RL during deployment?
- 方向 2 Test-time compute scaling:adaptive budget(按题目难度动态分配 reasoning tokens);sequential + parallel 混合;multi-agent debate;
- 方向 3 Verifier:Generative PRM 替代 scalar PRM;self-verifier(DeepSeek-Prover-V2 在 Lean 上是雏形);math PRM 能否 cross-domain transfer 到 code PRM?
- 方向 4 评测:AIME/MATH 接近饱和,下一代 evaluation 标准?adversarial prompt 下的 robustness?
- 方向 5 推理可解释性:CoT faithfulness;mechanistic 可解释性能否定位"反思"具体负责的 attention head?
- 方向 6 Reasoning + agent:agentic setting 中 reasoning 如何跨 turn 保持?tool use 与 reasoning 如何 jointly optimize?
顶级 lab 面试开放题的关键是展现 research taste:每个方向给一个 concrete proposal + 一个 expected failure mode,而不是"我会做 reasoning model"这种空话。
11. 附录:核心论文时间线与精读建议
按时间顺序:
| 日期 | Paper | arXiv | 一句话贡献 |
|---|---|---|---|
| 2022-01 | Chain-of-Thought (Wei) | 2201.11903 | few-shot step-by-step demonstration,CoT 在大模型上 emergent |
| 2022-03 | Self-Consistency | 2203.11171 | sample N + 多数票,GSM8K +17.9% |
| 2022-05 | Zero-shot CoT (Kojima) | 2205.11916 | "Let's think step by step",zero-shot 触发 CoT |
| 2023-05 | Let's Verify Step by Step | 2305.20050 | PRM > ORM > majority vote;PRM800K 数据集 |
| 2023-05 | Tree of Thoughts | 2305.10601 | 显式 tree search + LLM evaluator |
| 2023-12 | Math-Shepherd | 2312.08935 | Monte Carlo rollout 自动标 PRM label |
| 2024-02 | DeepSeekMath / GRPO | 2402.03300 | GRPO 算法首次提出,去除 critic |
| 2024-08 | rStar | 2408.06195 | MCTS + mutual reasoning,小 LM 大幅提升 |
| 2024-08 | Snell et al. Test-Time Compute | 2408.03314 | 优化 test-time compute > 14× 模型 scaling |
| 2024-09 | o1 (OpenAI) | (no arXiv) | 首个商用 reasoning model,hidden CoT + RL |
| 2024-12 | Gemini 2.0 Flash Thinking | (no arXiv) | Google 首个推理模型,thinking 显式可见 |
| 2024-12 | o3 (OpenAI) | (no arXiv) | ARC-AGI 75.7%–87.5%,首次抽象推理逼近人类 |
| 2025-01 | DeepSeek-R1 / R1-Zero | 2501.12948 | 纯 RL(GRPO + rule reward)涌现推理;R1 = 4 阶段 pipeline |
| 2025-01 | rStar-Math | 2501.04519 | MCTS + PPM self-evolution,7B 接近 o1-preview |
| 2025-02 | s1: Simple Test-Time Scaling | 2501.19393 | 1K SFT + "Wait" budget forcing 超 o1-preview |
| 2025-02 | Claude 3.7 Sonnet | (no arXiv) | hybrid 模型,extended thinking budget 用户可控 |
| 2025-04 | DeepSeek-Prover-V2 | 2504.21801 | subgoal decomposition + Lean 4 RL,MiniF2F 88.9% |
建议精读 4 篇(按优先级):
- DeepSeek-R1(2501.12948)——涵盖 GRPO + 4 阶段 + R1-Zero "aha moment";
- DeepSeekMath(2402.03300)——GRPO 算法原始论文;
- Let's Verify Step by Step(2305.20050)——PRM 基础;
- Snell et al.(2408.03314)——test-time compute scaling 范式。
12. 本篇在 ARIS 仓库中的位置:质量流水线如何保障这些公式与代码
最后补充一个常被忽略但对本篇可信度很重要的工程事实:这篇教程并非手写一次定稿,而是 ARIS 仓库中 /interview-cheatsheet 技能流水线产物。按 docs/tutorials/README.md 的描述,该技能会规划 12–14 节结构、按既定 style guide 起草 Markdown、然后执行跨模型交叉评审(新起线程的 codex 模型做数学/代码/面试题答案/引用正确性检查)、fix-and-loop 迭代,最后经 render-html 渲染为单文件 HTML 并写入审计日志。
该教程对应的审计文件 docs/tutorials/reasoning_models_tutorial.review.json 记录了评审过程:最终 verdict 为 PASS (with length WARN),render 检查 13/13 通过(45 个标题、11 个代码块、5 张表格、25 个 details 折叠块)。其中一轮评审抓到并修复的 9 处问题,恰好对应前文几处"看起来理所当然、实则容易写错"的细节:
| 评审抓到的问题 | 对应正文位置 |
|---|---|
extract_answer 对嵌套 \boxed{\frac{1}{2}} 解析截断 | §4.3 的平衡括号 _extract_braced 实现 |
grpo_loss 未对 log_probs_old/ref 强制 detach | §5.3 的防御性 detach + docstring |
| "std=0 时 loss=0" 的错误表述(实际 PG 项归零但 KL 项仍在) | §5.3 常见坑与 Q11 |
prm_beam_search 空 new_beams 导致 max() 抛错 | §8.4 的空 beam 守卫 |
| Math-Shepherd 被误标为 "MCTS rollout"(实为 Monte Carlo completion rollouts) | §3.3 与 Q4 的明确纠正 |
| Q22 中 InstructGPT RM 被误描述为 "dense token-level reward"(实为 response-level scalar) | Q22 的修正表述 |
| TL;DR 中 Snell 2024 "4×" 的表述不精确(应为 compute-optimal 混合策略 vs 单一 BoN 的效率对比) | §1 的 >4× 与 14× 表述 |
这套"写作 Agent 起草 + 异构模型评审 + 审计留痕"的流程,正是 ARIS 项目"Markdown-only skills for autonomous research"理念的一个缩影:内容可信度不只来自作者,也来自可复查的评审轨迹。若想深入 GRPO 在 RLHF 体系中的坐标,可继续阅读姊妹篇 docs/tutorials/rlhf_dpo_grpo_ppo_tutorial.md;渲染后的单文件 HTML 版本为 docs/tutorials/reasoning_models_tutorial.html(含 MathJax 公式与可折叠面试题)。
小结:本篇以 docs/tutorials/reasoning_models_tutorial.md 为骨架,完整继承了原文的八句 TL;DR 要点、三组件架构图、CoT→ToT→long-CoT 演进、PRM/ORM 对比表、BoN/SC/GRPO/MCTS/beam search 五段可直接运行的参考代码、R1 四阶段 pipeline、s1 budget forcing、选型决策树、25 道分层面试题与论文时间线;并结合仓库中的评审审计日志与技能定义,说明了每一处"反直觉边界条件"(detach、std=0、空 beam、嵌套 LaTeX 解析)背后的工程来源。读完并复现这些代码块,你已具备对 o1/R1 类推理模型做面试级乃至研究级讨论的完整知识栈。
【免费下载链接】Auto-claude-code-research-in-sleep
ARIS ⚔️ (Auto-Research-In-Sleep) — Lightweight Markdown-only skills for autonomous ML research: cross-model review loops, idea discovery, and experiment automation. No framework, no lock-in — works with Claude Code, Codex, OpenClaw, or any LLM agent.
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



