Prometheus + Grafana 并非传统意义上的关系型数据库应用,而是一个专门针对 时序数据(Time Series Data) 的监控与可视化组合。它们非常适合存储和分析您描述的 AI训练性能指标(时间、脚本、算子名称、输入参数规模、软件版本、吞吐量等)。以下是具体方案:
1. 技术栈定位
组件 |
角色 |
适用性说明 |
Prometheus |
时序数据库 + 监控系统 |
存储时间序列数据,支持多维标签查询 |
Grafana |
数据可视化平台 |
创建交互式仪表盘,支持 Prometheus 数据源 |
AI训练代码 |
数据生产者 |
通过埋点推送性能指标到 Prometheus |
2. 数据模型设计
在 Prometheus 中,您的性能指标应设计为 带标签的时间序列,例如:
1 2 3 4 5 6 7 8 9
| # 指标名称: operator_throughput # 标签: script, operator_name, input_scale, can_version, mm_version operator_throughput{ script="train_resnet.py", operator_name="conv2d", input_scale="[batch=32, in_ch=256, H=56, W=56]", can_version="6.3.RC2", mm_version="1.8.0" } 1.2e9 # 值=1.2 GFLOPs/s
|
标签设计原则:
- 高基数警告:避免标签值过多(如
input_scale
的每个具体数值都不同),建议对参数规模做分桶(如 input_scale="small/medium/large"
)。
- 版本控制:
can_version
和 mm_version
作为标签,方便对比不同软件版本性能。
3. 代码接入方案
(1) 在训练代码中埋点
使用 prometheus-client
库推送指标:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| from prometheus_client import Gauge, push_to_gateway
OP_THROUGHPUT = Gauge( 'operator_throughput', 'Throughput of AI operators (FLOPs/s)', ['script', 'operator_name', 'input_scale', 'can_version', 'mm_version'] )
def log_operator_performance( script_name: str, op_name: str, input_shape: tuple, can_ver: str, mm_ver: str, flops: float, duration: float ): throughput = flops / duration OP_THROUGHPUT.labels( script=script_name, operator_name=op_name, input_scale=str(input_shape), can_version=can_ver, mm_version=mm_ver ).set(throughput)
push_to_gateway('prometheus:9091', job='ai_training', registry=REGISTRY)
|
(2) Prometheus 配置
在 prometheus.yml
中添加 Pushgateway 目标:
1 2 3 4 5
| scrape_configs: - job_name: 'pushgateway' honor_labels: true static_configs: - targets: ['pushgateway:9091']
|
4. 数据存储与查询
存储特性:
- 时间序列数据库:数据按时间索引,自动处理时间窗口聚合。
- 保留策略:默认保留 15 天,可通过
--storage.tsdb.retention.time=365d
调整。
PromQL 查询示例:
1 2 3 4 5
| # 查询最新版本 CANN 的卷积算子平均吞吐量 avg(operator_throughput{operator_name="conv2d", can_version=~"6.3.*"}) by (input_scale)
# 对比不同软件版本的性能 operator_throughput{operator_name="matmul"} / ignoring(version) group_left operator_throughput{operator_name="matmul", can_version="6.2.0"}
|
5. Grafana 可视化
仪表盘设计建议:
- 全局筛选器:脚本名称、算子类型、软件版本。
- 核心图表:
- 吞吐量随时间变化曲线
- 不同输入规模的性能热力图
- 版本对比柱状图
- 统计面板:当前峰值、历史均值、性能变化率。

6. 报警机制
(1) Prometheus Alertmanager
配置规则检测性能劣化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| groups: - name: ai_performance rules: - alert: OperatorThroughputDrop expr: | operator_throughput / ignoring(version) group_left operator_throughput offset 1h < 0.8 # 吞吐量下降超过20% for: 10m labels: severity: critical annotations: summary: "算子 {{ $labels.operator_name }} 性能下降 (当前 {{ $value }}x)"
|
(2) Grafana 内置报警
直接在仪表盘中设置阈值报警:
1
| WHEN last() OF query(A, 5m, now) IS BELOW 1e9
|
7. 扩展方案
长期存储:
- Thanos/Cortex:扩展 Prometheus 的存储容量,支持无限保留时间。
- InfluxDB:替代 Prometheus,适合更高写入吞吐场景。
元数据关联:
- 将脚本版本、训练超参数等存储到 PostgreSQL,通过
script_name
关联时序数据。
- 多维分析:通过标签组合快速定位瓶颈(如特定算子+特定输入规模)。
- 实时性:秒级监控延迟,适合训练任务实时调优。
- 历史回溯:对比不同版本/硬件下的性能表现。
建议先以 Pushgateway 实现最小原型,再逐步扩展为完整监控体系。