Skip to content

[v5] 复用 Twinkle metric 记录 GRPO 性能指标 - #10512

Merged
tastelikefeet merged 5 commits into
dev-swift-v5from
feat/add-log
Nov 6, 2026
Merged

tastelikefeet merged 5 commits into
dev-swift-v5from
feat/add-log

Conversation

@qychen2001

@qychen2001 qychen2001 commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

PR type

  • New Feature
  • Bug Fix
  • Document Updates
  • More Models or Datasets Support

PR information

Related issue: 暂无。依赖配套 Twinkle #368,建议先合入 Twinkle。

为 Swift v5 GRPO 保存可复算的性能观测,复用 Twinkle 的计时、Metric 和输出接口。Swift 负责训练流程的边界与计数,不维护独立 performance.py、JSONL writer 或 RPC 观察线程。

  • 阶段耗时使用既有 TimeRecorder / span,worker 方法使用 @timed;辅助 forward 保持并发提交。优化器副作用在完成并传播错误后才记录成功更新。
  • 每次消费的 rollout batch 明确记录起止、实际更新数、样本访问和去重数量;重复训练不重复累计生成 token,尾批不会伪装成完成的更新。
  • 长度、吞吐和内存归约复用 twinkle.metric.ThroughputMetric 与 memory_metrics;窗口吞吐使用总 token / 总秒数 / 物理设备数。
  • 保存统一走既有 RunTracker → MetricRecord → MetricsReporter。metrics_raw.jsonl 区分 train、rollout(scope=iteration)、run,summary.json 保存终态汇总;包含 run_id、时间戳、阶段和优化器步数。原有 WebUI metrics.jsonl 的平铺格式和频率保持兼容。
  • 原始流取代独立的 performance.jsonl、optimizer_metrics.jsonl 及其 writer;原始计时区间、prompt/response 长度和每卡内存读数放在 attributes,便于训练结束后核算。
  • GRPO 的优势流水线使用明确命名的 adv_pipeline,涵盖辅助前向和信号解析;worker 的原始方法耗时单独保留。不推测每条 RPC 的端到端时长,不把重叠阶段相加。
  • Colocate 记录权重同步完成后的整轮边界;异步路径使用 driver 的真实发布完成通知和资源快照。保留完整墙钟时间及扣除日志开销后的时间,重叠日志区间只扣一次。

本次整轮阶段观测覆盖 GRPO。只提交代码和测试,没有新增独立文档文件。使用 Codex 辅助实现、测试和提交,人工逐行 review 与提交者最终责任确认仍待完成。

Experiment results

2026-11-06 在最终实现上完成 3 轮真实 GRPO 训练。配置:8×PPU-ZW810(96 GiB)、Qwen3.5-4B、FSDP2、colocate、rollout TP=2×4、每轮 8 prompts×8 completions=64 条轨迹;每卡微批次 2、梯度累积 4、seed=42、thinking=false、回答上限 1024、context 上限 5120,不保存 checkpoint。回答上限 1024 用于缩短本次记录链路验证。

轮次 有效总 token 生成 s 策略训练 s 整轮逻辑耗时 s token/s/GPU
1 76019 46.416 15.052 81.659 116.366
2 73752 46.102 9.174 59.685 154.461
3 75208 46.640 9.938 61.091 153.885

检查结果:

  • 退出码 0,完成 3 次实际优化器更新、3 次权重发布、192 条轨迹;loss 与梯度范数有限,3 次梯度范数均非零。thinking 配置为 false,192 条结果均未解析出非空 reasoning。
  • metrics_raw.jsonl 共 7 条:3 条 train、3 条 rollout(scope=iteration)、1 条 run;同一 run_id,包含时间戳和明确的优化器步数。WebUI metrics.jsonl 保持原有平铺格式,共 3 条。
  • 每轮有效 prompt token 与输入 manifest 一致,response token 与 64 条实际 completion 长度之和一致;吞吐和每 token 耗时可独立复算。
  • 每轮保存 13 个原始计时区间、8 个 worker 的原始内存字节读数;独立核对阶段耗时、日志开销扣除、内存单位转换和跨卡归约均一致。内存是 worker allocator 生命周期峰值,CPU 项是 host 使用量。
  • summary.json 正确汇总 24 个生成组、192 条生成样本、192 次训练样本访问、3 次更新,无记录丢弃。全窗口吞吐为 138.920 token/s/GPU,按总 token / 总逻辑秒数 / 8 计算。完整进程耗时 398.561 秒,含环境和模型初始化。
  • 8 卡均释放;日志中无 traceback 或端口冲突。

这是一轮集成验证,不据此给出稳定提速或收敛结论;首轮含首次调用开销。阶段观测不相互独立,不能把并发或嵌套时间直接相加。

验证对应的提交:

  • Swift:4b0e012768ecb39d96ad759452f7834efa6f10d0
  • Twinkle:160c12be82162b6a72ba16dbf007cab453cced6e

训练时使用未提交工作区,提交后已逐文件核对两仓提交内容与被测源码的 SHA-256 一致。原始配置、源码变更快照、日志、逐步 CSV、验证结果和提交核验保存在本地 scripts/performance_a1/output/20261106-metric-pipeline-three-iterations/,实验产物不加入代码 diff。

回归与静态检查

在两仓共同父目录、swift-test 环境运行:

PYTHONPATH=ms-swift:twinkle/src python -m pytest -q \
  twinkle/tests/perf \
  twinkle/tests/metric/test_reporting.py \
  twinkle/tests/metric/test_throughput.py \
  /tmp/test_native_metric_existing_reporter.py \
  ms-swift/swift/dev/tests/component/recipe \
  ms-swift/swift/dev/tests/feature/rl/test_performance.py \
  ms-swift/swift/dev/tests/feature/rl/test_grpo_seams.py \
  ms-swift/swift/dev/tests/feature/rl/test_grpo_advanced.py \
  ms-swift/swift/dev/tests/component/config/test_tracking.py \
  twinkle/tests/twinkle_agentic/test_iteration_observers.py \
  --disable-warnings --maxfail=3

结果:236 passed, 4 warnings。覆盖 token/时间归一化、窗口吞吐采用总量之比、长度统计、内存转换、追加写入、并发记录、终态总数、日志重叠只扣一次、训练尾批/重复访问、辅助前向并发提交、远程优化器失败传播、发布边界和 WebUI 兼容。

其中 11 项来自既有 reporter 测试的隔离执行:原 test_async_rl_metrics.py 导入当前检出缺失的 twinkle_agentic.async_rl.metrics,无法整文件收集;临时文件仅去掉该无关导入及依赖它的 helper 测试,保留 reporter 测试行为。副本和运行日志保存在实验目录 checks/;不声明该原始测试模块或全仓测试全部通过。

两仓 git diff --check 通过。完整 pre-commit run --all-files 已在隔离源码目录执行,仍有仓库已有的 Ruff、格式和文件末尾换行问题;批量格式化未带入提交,不声明全仓 lint 通过。

Checklist

  • I have linked the related issue above, if there is one. (No related issue; paired PR linked.)
  • I have reviewed and understand every changed line, and I can explain how the change works and why it is correct.
  • I have run the relevant tests or reproductions locally, and included the commands and results above (if applicable). (Agent-run checks recorded above.)
  • I am responsible for the content of this PR, regardless of how it was produced.

@qychen2001 qychen2001 changed the title [v5] 保存可对标 veRL 的 GRPO 性能日志 [v5] 增加 GRPO 阶段计时和性能日志 Nov 4, 2026
@qychen2001 qychen2001 changed the title [v5] 增加 GRPO 阶段计时和性能日志 [v5] 接入 Twinkle perf 的 GRPO 性能日志 Nov 4, 2026
Use Twinkle timing primitives, throughput metrics and native metric reporting. Keep iteration boundaries and sample accounting in GRPO, preserve the flat WebUI stream, and replace dedicated performance writers and remote observation threads with typed records.

Co-authored-by: Codex
@qychen2001 qychen2001 changed the title [v5] 接入 Twinkle perf 的 GRPO 性能日志 [v5] 复用 Twinkle metric 记录 GRPO 性能指标 Nov 6, 2026
Extract stateless rollout timing and metric helpers into shared recipe utilities. Reuse Twinkle reporter normalization while preserving raw observations and legacy percentage values.

Validation: 226 focused tests passed; historical metric replay and before/after accounting parity passed. Full pre-commit retains baseline failures, with no new Ruff findings in changed files.

Co-authored-by: Codex
Move batch observation state, timing, training counters, memory snapshots and reporting into shared recipe utilities. Keep GRPO responsible for training and publication boundaries, support custom sample length extraction, and use a single recorder reference.

Validation: 244 focused regression tests passed. Nine batch records and three run summaries match the prior implementation. Full pre-commit retains baseline failures, with no new Ruff findings in changed files. Statistics overhead was not benchmarked.

Co-authored-by: Codex
Preserve rollout length balancing and generation-batch derivation alongside shared batch observations. Record balance metrics inside the timed optimizer update and count the reordered training samples. Add coverage for balancing and batch accounting in the same update.

Validation: 406 focused regression tests passed against the latest dev-swift-v5 Twinkle source. Two distillation budget assertions and one CLI import test also fail on the unmodified target branch. Full pre-commit retains existing lint and formatting failures.

Co-authored-by: Codex
@tastelikefeet
tastelikefeet merged commit f058b8e into dev-swift-v5 Nov 6, 2026
0 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants