Skip to content

feat: add temporary log backfill API - #1767

Draft
ShaohonChen wants to merge 1 commit into
mainfrom
temp/add_log_backfill
Draft

feat: add temporary log backfill API#1767
ShaohonChen wants to merge 1 commit into
mainfrom
temp/add_log_backfill

Conversation

@ShaohonChen

Copy link
Copy Markdown
Contributor

Summary

  • Add swanlab.log_backfill(data, step) for temporary historical scalar backfill in Resume runs.
  • Query cloud metrics first: existing points are warned and skipped; missing points are uploaded through the normal log path.
  • Document the temporary 0.10.1.dev0-only API, its 50,000-point performance limit, and playground examples.

Validation

  • uv run pytest tests/unit/sdk/cmd/test_temporary.py -q
  • uv run ruff check swanlab/init.py swanlab/sdk/init.py swanlab/sdk/cmd/temporary.py tests/unit/sdk/cmd/test_temporary.py

@ShaohonChen

Copy link
Copy Markdown
Contributor Author

swanlab.log_backfill 临时使用说明

swanlab.log_backfill(data, step) 用于在 Resume 训练时补打历史标量指标点。

警告:这是仅在临时版本 SwanLab 0.10.1.dev0 中提供的兼容接口,不保证后续版本仍可用,也不保证 API 稳定性或性能。

使用限制

  • 只能在云端实验中使用,且必须通过 swanlab.init(resume=True, id="...") 初始化。该实验可以是已有实验,也可以是刚创建的新实验。
  • 仅支持标量指标。
  • 调用时会从云端读取目标指标的历史数据,以确认目标 step 是否已经存在。
  • 云端实验的单个指标点数应少于 50,000。该接口需要读取完整历史数据再筛选目标 step;点数更多时补打可能较慢。
  • 服务端已有相同 (metric key, step) 时,接口会输出 warning 并跳过该点;只上传缺失的指标。

边界行为

  • 补打的指标从未存在:云端查询会返回该指标没有历史点,接口随后会像 swanlab.log 一样自动创建这个标量指标,并上传目标 step 的数据。
  • 补打的 step 已有该指标数据:接口会输出 warning 并跳过该指标,不会覆盖已有值,也不会再次上传。因此在同一次调用中,已存在的 key 会跳过,其他缺失 key 仍会正常补打。
  • 刚创建的新实验:只要使用了 resume=True 和显式 idlog_backfill 可以直接使用。新实验中没有历史点,因而它的行为与 swanlab.log 一致:自动创建指标并上传数据。

示例

下面的代码先故意漏掉 loss 在 step 2 的记录,再 Resume 后补打该点:

import uuid

import swanlab

project = "test_log_backfill"
experiment_id = str(uuid.uuid4())[:21]

# 第一次训练:故意漏掉 step=2。
swanlab.init(project=project, resume=True, id=experiment_id)
swanlab.log({"loss": 1.0}, step=1)
swanlab.log({"loss": 0.25}, step=3)
swanlab.finish()

# Resume 后补打。接口先查云端;step=2 不存在时才上传。
swanlab.init(project=project, resume=True, id=experiment_id)
swanlab.log_backfill({"loss": 0.5}, step=2)
swanlab.finish()

完整可运行脚本见 test_log_backfill.py;新实验直接调用的场景见 test_log_backfill_new_run.py

@ShaohonChen
ShaohonChen force-pushed the temp/add_log_backfill branch from fcf4d20 to e77c514 Compare September 2, 2026 01:36
@ShaohonChen
ShaohonChen marked this pull request as draft September 2, 2026 03:37
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.

1 participant