Skip to content

Feat/define metric - #1733

Open
Nexisato wants to merge 1 commit into
mainfrom
feat/define-metric
Open

Feat/define metric#1733
Nexisato wants to merge 1 commit into
mainfrom
feat/define-metric

Conversation

@Nexisato

@Nexisato Nexisato commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

新增 swanlab.define_metric() API,在 log 之前定义指标的图表行为(自定义 X 轴、图表分组、隐藏)

API

swanlab.define_metric(
    key: str,                      # 精确 key 或末尾单个 '*' 的 glob(如 "train/*")
    x_axis: Optional[str] = None,  # 自定义 X 轴 key;None 为系统 step;兼容 step_metric 别名
    section_name: Optional[str] = None,  # 图表分组名;None 使用默认分组
    hidden: bool = False,          # True 时图表放入 HIDDEN 分组
    step_sync: Optional[bool] = None,    # X/Y 分开 log 时自动同步 X 值(当前强制开启)
    overwrite: bool = False,       # False 合并已有定义;True 重置未指定字段为默认值
) -> None

示例:

swanlab.define_metric("train/loss", x_axis="train/epoch")
swanlab.log({"train/epoch": 1})
swanlab.log({"train/loss": 0.8})   # 自动同步 train/epoch=1

swanlab.define_metric("val/*", section_name="Validation")  # glob 批量定义

语义要点:

  • 图表定义 first-writer-wins:同一 (class, key) 全项目共享一张图,仅首个引入该 key 的 run 的定义生效
  • 同一 X 值上仅保留首个 Y 点
  • x_axis 仅对 scalar 生效,media 一律回退 _step
  • 精确匹配优先度高于 glob
    ⚠️ 注意:
  • 有效作用域仅限定在 【当前视图】 和 【被复制的视图】
  • 【复制/移动实验】、【import runs】、【创建默认视图】时,分区关系可以保留,而自定义的 x 轴关联关系无法保留,key 会回退到 step 计数
  • 自定义的 X 轴不强制要求单调递增,但是看起来可能会比较奇怪

Changes

  • 新增 DefinitionResolver 组件(run/components/resolver/):exact/glob 规则注册、首次 log 时物化定义、custom X 缓存与 step 同步注入、X 值去重
  • 事件总线:ScalarDefineEventMetricDefineEvent,新增 UNSET 哨兵区分"未提供"与 None
  • proto:ColumnRecord 新增 x_axishidden 可选字段(含重新生成的 Python/Go 代码)
  • transport:encode_column 按 presence 编码,仅发送已设置的字段
  • 离线 sync 回放 ColumnRecord 时保留 x_axis/hidden,同 key first-writer-wins

@Nexisato Nexisato self-assigned this Aug 12, 2026
@Nexisato
Nexisato force-pushed the feat/define-metric branch 2 times, most recently from 1c086b6 to 7c8e63d Compare August 19, 2026 09:53
@Nexisato
Nexisato force-pushed the feat/define-metric branch from 7c8e63d to b05f4b3 Compare August 21, 2026 07:00
@Nexisato
Nexisato force-pushed the feat/define-metric branch from b05f4b3 to 218fa2c Compare August 21, 2026 07:08
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