ZenML 0.39.1到0.41.0版本迁移指南:步骤与管道的语法升级

ZenML 0.39.1到0.41.0版本迁移指南:步骤与管道的语法升级

zenml ZenML 🙏: Build portable, production-ready MLOps pipelines. https://zenml.io. zenml 项目地址: https://gitcode.com/gh_mirrors/ze/zenml

前言

在ZenML 0.40.0到0.41.0版本中,项目团队对步骤(Step)和管道(Pipeline)的定义语法进行了重大改进。这些改动使代码更加直观和灵活,同时也更符合Python的惯用写法。本文将详细介绍如何将现有代码从旧语法迁移到新语法。

核心改动概览

新版本主要带来了以下重要变化:

  1. 简化的参数传递:不再需要继承BaseParameters类来定义步骤参数
  2. 更自然的调用方式:步骤和管道可以直接像普通函数一样调用
  3. 改进的配置方法:使用with_options()方法替代旧的配置方式
  4. 增强的上下文访问:通过get_step_context()函数获取运行上下文
  5. 更清晰的输出定义:使用标准类型注解替代特殊的Output

详细迁移说明

1. 步骤参数定义的变化

旧语法需要创建一个继承自BaseParameters的参数类:

from zenml.steps import BaseParameters, step

class MyStepParameters(BaseParameters):
    param_1: int
    param_2: Optional[float] = None

@step
def my_step(params: MyStepParameters) -> None:
    ...

新语法可以直接将参数定义为步骤函数的参数:

from zenml import step

@step
def my_step(param_1: int, param_2: Optional[float] = None) -> None:
    ...

这种改变使代码更加简洁,减少了不必要的类定义。

2. 独立运行步骤

旧语法需要使用特殊的entrypoint()方法:

my_step.entrypoint()

新语法可以直接调用步骤函数:

my_step()

3. 管道定义与调用

旧语法需要将步骤作为管道函数的参数:

@pipeline
def my_pipeline(my_step):
    my_step()

新语法直接在管道函数中调用步骤:

@pipeline
def my_pipeline():
    my_step()

4. 管道配置与运行

旧语法需要先创建管道实例再配置:

pipeline_instance = my_pipeline(my_step=my_step())
pipeline_instance.configure(enable_cache=False)
pipeline_instance.run()

新语法使用with_options()方法:

my_pipeline = my_pipeline.with_options(enable_cache=False)
my_pipeline()

5. 调度管道执行

旧语法

schedule = Schedule(...)
pipeline_instance.run(schedule=schedule)

新语法

my_pipeline = my_pipeline.with_options(schedule=schedule)
my_pipeline()

6. 获取运行结果

旧语法使用post_execution模块:

last_run = pipeline_instance.get_runs()[0]
output = last_run.get_step["my_step"].outputs["output"].read()

新语法通过客户端直接访问:

last_run = my_pipeline.last_run
output = last_run.steps["my_step"].outputs["output"].load()

7. 控制步骤执行顺序

旧语法使用after()方法:

step_3.after(step_1)
step_3.after(step_2)

新语法在调用步骤时指定:

step_3(after=["step_1", "step_2"])

8. 定义多输出步骤

旧语法使用Output类:

@step
def my_step() -> Output(int_output=int, str_output=str):
    ...

新语法使用标准类型注解:

from typing import Tuple, Annotated

@step
def my_step() -> Tuple[
    Annotated[int, "int_output"],
    Annotated[str, "str_output"],
]:
    ...

9. 访问运行上下文

旧语法需要将StepContext作为参数:

@step
def my_step(context: StepContext) -> Any:
    output_uri = context.get_output_artifact_uri()

新语法使用get_step_context()函数:

from zenml import get_step_context

@step
def my_step() -> Any:
    context = get_step_context()
    output_uri = context.get_output_artifact_uri()

迁移建议

  1. 逐步迁移:可以先将部分管道迁移到新语法,旧语法仍然兼容
  2. 利用IDE功能:现代IDE的类型提示和重构功能可以大大简化迁移过程
  3. 测试验证:迁移后务必运行测试确保功能正常
  4. 文档参考:ZenML官方文档提供了更多新语法的使用示例

结语

ZenML 0.40.0+的新语法使代码更加Pythonic,减少了样板代码,提高了可读性。虽然旧语法仍然可用,但建议尽早迁移以获得更好的开发体验和未来兼容性。本文涵盖了最常见的迁移场景,如需了解更多细节,可以参考ZenML的官方文档。

zenml ZenML 🙏: Build portable, production-ready MLOps pipelines. https://zenml.io. zenml 项目地址: https://gitcode.com/gh_mirrors/ze/zenml

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幸俭卉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值