指标
TrainMetric
TrainMetric 报告训练进度:学习率、迭代数、已用时长和速度。它不读 inputs / outputs,device_mesh 与 process_group 也可以不传(单进程统计,不跨 rank 聚合),模型默认自带。
from twinkle.metric import TrainMetric
metric = TrainMetric()
metric.accumulate(None, None, lr=0.0001, step=10, gradient_accumulation_steps=16)
...
_metric = metric.calculate()accumulate 读的关键字
| 键 | 含义 |
|---|---|
lr | 当前学习率;优化器有多个参数组时是列表 |
step | microbatch 级的步数,迭代数 = step // gradient_accumulation_steps |
gradient_accumulation_steps | 梯度累积步数,默认 1 |
这些都由训练循环透传(见构建指标),recipe 不需要自己传。
输出
| 键 | 含义 |
|---|---|
learning rate | 单参数组时的学习率,六位有效数字的科学计数法 |
learning rate(param group N) | 多参数组时每组一个键 |
iters | 迭代数 |
total time elapse | 从构造指标开始的时长,一分钟内按秒、之后按分钟 |
speed | 两次 calculate 之间的迭代速度,iters/s |
reset 记下当前 step 与时间作为下一个窗口的起点;窗口时长为 0 时速度记 0。
ThroughputMetric
ThroughputMetric 统计 RL 训练中 rollout 与更新的 token 吞吐,由训练循环显式喂入计数,不在模型默认指标里。
metric = ThroughputMetric()
metric.accumulate(prompt_lengths=[10, 12], response_lengths=[20, 0],
timings={'step': 30.0, 'gen': 12.0}, num_devices=8)
results = metric.calculate(prompt_width=16, response_width=32)accumulate:prompt_lengths/response_lengths每条样本的 token 数(必须等长、非负整数);timings各阶段秒数,step必须大于 0;num_devices物理设备数,变化后须先reset。calculate(prompt_width=None, response_width=None):prompt_width/response_width是实际 padding 的宽度(用于算截断率),不传取观测到的最大长度,小于实际最大长度则报错。
| 键 | 含义 |
|---|---|
perf/total_num_tokens、perf/num_prompt_tokens、perf/num_response_tokens、perf/num_samples | 窗口内累计的 token 数与样本数 |
perf/num_gpus、perf/time_per_step | 设备数与窗口内 step 阶段累计秒数 |
perf/throughput / perf/throughput_total | 每设备 / 总 tokens/s |
perf/response_tokens_per_second、perf/samples_per_second | 生成 token 与样本的速率 |
timing_s/<阶段> | 各阶段累计秒数;timing_per_token_ms/<阶段> 按对应 token 数折算 |
prompt_length/*、response_length/* | mean、min、max、clip_ratio(长度等于 padding 宽度的样本占比) |
response/aborted_ratio | 响应长度为 0 的样本占比;response_length_non_aborted/* 只统计非空响应 |