You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vertex Pipeline可复用组件Metrics指标未写入工件问题排查

Vertex Pipeline 可复用组件Metrics指标不生效问题排查

我们尝试从Vertex Pipeline返回部分指标,使其可在Vertex UI的运行对比和元数据工具中正常展示。
此前了解到可以使用Output[Metrics]输出类型,搭配metrics.log_metric("metric_name", metric_val)方法添加指标,现有文档显示该配置已足够满足需求。
我们没有采用示例使用的Python函数式组件,而是选择可复用组件方案,具体实现及问题排查过程如下:

初始实现步骤

  • 第一步:在component.yaml中添加输出配置:
outputs:
    - name: metrics
      type: Metrics
      description: evaluation metrics path
  • 第二步:在实现的命令中添加该输出项:
command: [
    python3, main.py,
    --gcs-test-data-path,       {inputValue: gcs_test_data_path},
    --gcs-model-path,  {inputValue: gcs_model_path},
    --gcs-output-bucket-id,  {inputValue: gcs_output_bucket_id},
    --project-id, {inputValue: project_id},
    --timestamp, {inputValue: timestamp},
    --batch-size, {inputValue: batch_size},
    --img-height, {inputValue: img_height},
    --img-width,  {inputValue: img_width},
    --img-depth,  {inputValue: img_depth},
    --metrics,  {outputPath: metrics},
]
  • 第三步:在组件的主Python脚本中使用argparse解析该参数:
PARSER.add_argument('--metrics',
                    type=Metrics,
                    required=False,
                    help='evaluation metrics output')
  • 第四步:将其传入组件主函数:
if __name__ == '__main__':
    ARGS = PARSER.parse_args()
    evaluation(gcs_test_data_path=ARGS.gcs_test_data_path,
               gcs_model_path=ARGS.gcs_model_path,
               gcs_output_bucket_id=ARGS.gcs_output_bucket_id,
               project_id=ARGS.project_id,
               timestamp=ARGS.timestamp,
               batch_size=ARGS.batch_size,
               img_height=ARGS.img_height,
               img_width=ARGS.img_width,
               img_depth=ARGS.img_depth,
               metrics=ARGS.metrics,
               )
  • 第五步:在组件函数声明中,将metrics参数的类型指定为Output[Metrics]:
from kfp.v2.dsl import Output, Metrics

def evaluation(gcs_test_data_path: str,
               gcs_model_path: str,
               gcs_output_bucket_id: str,
               metrics: Output[Metrics],
               project_id: str,
               timestamp: str,
               batch_size: int,
               img_height: int,
               img_width: int,
               img_depth: int):
  • 第六步:在evaluation函数中调用log_metric方法写入指标:
metrics.log_metric('accuracy', acc)
    metrics.log_metric('precision', prec)
    metrics.log_metric('recall', recall)
    metrics.log_metric('f1-score', f_1)

问题现象

运行流水线后,可观测到正常现象:

  • DAG中可见生成的Metrics工件
  • Vertex元数据UI中也列出了Metrics Artifacts
    同时存在多处异常:
  • 点击查看工件的JSON内容时,没有显示任何元数据
  • 流水线UI的运行对比页面看不到任何元数据
  • 访问GCS中对应对象的URI时,提示「请求的实体未找到」,确认没有数据被写入GCS

最终解决方案

后续已经成功实现工件元数据和URI的更新。最终方案是使用kfp sdk基于@component装饰的Python函数生成yaml文件,再将该格式适配到可复用组件中。调整后的component.yaml示例如下:

name: predict
description: Prepare and create predictions request
implementation:
    container:
      args:
      - --executor_input
      - executorInput: null
      - --function_to_execute
      - predict
      command:
      - python3
      - -m
      - kfp.v2.components.executor_main
      - --component_module_path
      - predict.py
      image: gcr.io/PROJECT_ID/kfp/components/predict:latest
inputs: 
    - name: input_1
      type: String
    - name: intput_2
      type: String
outputs:
    - name: output_1
      type: Dataset
    - name: output_2
      type: Dataset

修改yaml后,即可通过artifact.path = '/path/to/file'成功更新工件的元数据字典和URI,更新内容可正常在Vertex UI中展示。
目前暂不清楚Kubeflow文档指定的component.yaml格式为何无法生效,推测可能是Vertex Pipelines存在Bug。

内容的提问来源于stack exchange,提问作者ML6-Liam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 01:06:01