# PR #1890 完整报告

- 仓库：`THUDM/slime`
- 标题：Add missing metrics to log
- 合并时间：2026-05-06 13:52
- 原文链接：http://prhub.com.cn/THUDM/slime/pull/1890

---

# 执行摘要

- 一句话：在 rollout 日志中新增 spec 和 prefix cache 指标
- 推荐动作：建议合入，但建议作者或 reviewer 确认 `_compute_spec_metrics` 和 `_compute_prefix_cache_metrics` 的返回值对日志系统的兼容性（如是否可能返回非 dict 类型）。

# 功能与动机

补齐 rollout 过程中缺少的关键性能指标，便于监控和调试投机采样与前缀缓存的运行状态。

# 实现拆解

在 `slime/ray/rollout.py` 的 `compute_metrics_from_samples` 函数中，在调用 `_compute_reward_cat_metrics` 之前，新增两条调用：`log_dict |= _compute_spec_metrics(args, samples)` 和 `log_dict |= _compute_prefix_cache_metrics(args, samples)`。这两个函数已经在同一文件中定义，但之前未被集成到指标收集主流程中，导致相关指标遗漏。

关键文件：
- `slime/ray/rollout.py`（模块 rollout；类别 source；类型 core-logic；符号 compute_metrics_from_samples, _compute_spec_metrics, _compute_prefix_cache_metrics）: 核心变更文件，在 compute_metrics_from_samples 中新增了两个指标收集调用

关键符号：compute_metrics_from_samples, _compute_spec_metrics, _compute_prefix_cache_metrics

## 关键源码片段

### `slime/ray/rollout.py`

核心变更文件，在 compute_metrics_from_samples 中新增了两个指标收集调用

```python
def compute_metrics_from_samples(args, samples):
    response_lengths = [sample.effective_response_length for sample in samples]

    log_dict = {}
    log_dict |= dict_add_prefix(compute_statistics(response_lengths), "response_len/")
    log_dict |= _compute_zero_std_metrics(args, samples)
    # 新增：添加投机采样（speculative decoding）指标
    log_dict |= _compute_spec_metrics(args, samples)
    # 新增：添加前缀缓存（prefix cache）指标
    log_dict |= _compute_prefix_cache_metrics(args, samples)
    log_dict |= _compute_reward_cat_metrics(args, samples)
    log_dict["repetition_frac"] = np.mean([int(has_repetition(s.response)) for s in samples]).item()
    log_dict["truncated_ratio"] = np.mean([int(s.status == Sample.Status.TRUNCATED) for s in samples]).item()
    return log_dict

```

# 评论区精华

无 review 讨论。

- 暂无高价值评论线程

# 风险与影响

- 风险：低风险。变更仅添加两行函数调用，被调用的函数已存在且仅计算并返回字典，不会影响原有逻辑。但若 `_compute_spec_metrics` 或 `_compute_prefix_cache_metrics` 内部存在未预期的异常或性能开销，可能影响日志记录流程。建议确认这两个函数在无 spec/prefix cache 时的行为（应返回空字典或默认值）。
- 影响：对用户：启用 spec/prefix cache 的任务将获得更丰富的指标，方便分析性能；未启用用户不受影响。对系统：无性能影响，因为函数仅从现有样本数据中计算统计量。对团队：监控面板可补充新指标，但需注意指标命名和前缀以避免冲突。
- 风险标记：暂无

# 关联脉络

- PR #1856 refactor/ppo: 此 PR 重构了 PPO 训练架构，其中的指标收集逻辑可能与本 PR 的指标扩展相关
- PR #1884 [docker] update megatron-bridge and add qwen3.6 tests: 该 PR 修改了 rollout.py 文件，涉及相同的函数上下文件