使用缓存

了解在 Kubeflow Pipelines 中的缓存。

Kubeflow Pipelines 支持缓存以消除冗余执行并提高管道运行的效率。 本页提供了 KFP 中缓存的概述以及如何在您的管道中使用它。

概述

KFP中的缓存是一个功能,它允许您缓存组件执行的结果,并在后续运行中重用它们。当组件启用缓存时,KFP将在再次使用相同的输入和参数(并且输出仍然可用)的情况下重用组件的输出。

缓存特别有用,当您有执行时间较长的组件或当您有多个以相同输入和参数执行的组件时。

如果一个任务的结果是从缓存中检索的,它在用户界面中的表示将带有一个绿色的“来自云的箭头”图标。

如何使用缓存

默认情况下,KFP中的所有组件都启用缓存。您可以通过在任务对象上调用 .set_caching_options(enable_caching=False) 来禁用组件的缓存。

from kfp import dsl

@dsl.component
def say_hello(name: str) -> str:
    hello_text = f'Hello, {name}!'
    print(hello_text)
    return hello_text

@dsl.pipeline
def hello_pipeline(recipient: str = 'World!') -> str:
    hello_task = say_hello(name=recipient)
    hello_task.set_caching_options(False)
    return hello_task.output

您还可以通过在提交管道以执行时设置参数 caching 来启用或禁用管道中所有组件的缓存。这将覆盖管道中所有组件的缓存设置。

from kfp.client import Client

client = Client()
client.create_run_from_pipeline_func(
    hello_pipeline,
    enable_caching=True,  # overrides the above disabling of caching
)

默认情况下,--disable-execution-caching-by-default 标志禁用所有管道任务的缓存。

示例:

kfp dsl compile --py my_pipeline.py --output my_pipeline.yaml --disable-execution-caching-by-default

您还可以使用 KFP_DISABLE_EXECUTION_CACHING_BY_DEFAULT 环境变量设置默认的缓存行为。当设置为 true1 或其他真实值时,将默认禁用所有管道的执行缓存。当设置为 false 或缺失时,默认情况下缓存启用。

示例:

KFP_DISABLE_EXECUTION_CACHING_BY_DEFAULT=true \
kfp dsl compile --py my_pipeline.py --output my_pipeline.yaml

该环境变量也适用于 Compiler().compile()

给定以下管道文件:

@dsl.pipeline(name='my-pipeline')
def my_pipeline():
    task_1 = create_dataset()
    task_2 = create_dataset()
    task_1.set_caching_options(False)

Compiler().compile(
    pipeline_func=my_pipeline,
    package_path='my_pipeline.yaml',

)

正在执行以下内容:

KFP_DISABLE_EXECUTION_CACHING_BY_DEFAULT=true \
python my_pipeline.py

将导致 task_2 禁用缓存。

注意: 由于 Python 在导入过程中初始化配置,因此在导入管道组件后设置 KFP_DISABLE_EXECUTION_CACHING_BY_DEFAULT 环境变量将不会影响缓存行为。因此,始终在导入任何 Kubeflow Pipelines 组件之前设置它。

反馈

此页面有帮助吗?