附加功能

关于编写 KFP 组件的更多信息

组件 docstring 格式

KFP 允许你使用 Python docstring 来记录你的组件和流水线。当你编译组件和流水线时,KFP SDK 会自动解析你的 docstring 并将某些字段包含在 IR YAML 中。

对于组件,KFP 可以提取你的组件输入描述输出描述

对于流水线,KFP 可以提取你的流水线输入描述输出描述,以及你的完整流水线描述

为了让 KFP SDK 正确解析你的 docstring,你应该按照 KFP docstring 风格来编写你的 docstring。KFP docstring 风格是 Google docstring 风格 的一个特定变体,并有以下变化

  • Returns: 部分的结构与 Args: 部分相同,Returns: 部分中的每个返回值应采用 <name>: <description> 的形式。这与典型的 Google docstring Returns: 部分不同,后者采用 <type>: <description> 的形式,不包含返回值的名称。
  • 组件输出应包含在 Returns: 部分中,即使它们是通过组件函数输入参数声明的。这适用于使用 dsl.OutputPath 注释的函数参数,以及用于声明输出制品Output[<Artifact>] 类型标记。
  • 建议: 类型信息,包括哪些输入是可选/必需的,应从输入/输出描述中省略。此信息与注释是重复的。

例如,KFP SDK 可以从以下使用 KFP docstring 风格的组件 docstring 中提取输入和输出描述

@dsl.component
def join_datasets(
    dataset_a: Input[Dataset],
    dataset_b: Input[Dataset],
    out_dataset: Output[Dataset],
) -> str:
    """Concatenates two datasets.

    Args:
        dataset_a: First dataset.
        dataset_b: Second dataset.

    Returns:
        out_dataset: The concatenated dataset.
        Output: The concatenated string.
    """
    ...

类似地,KFP 可以从以下流水线 docstring 中提取组件输入描述、组件输出描述和流水线描述

@dsl.pipeline(display_name='Concatenation pipeline')
def dataset_concatenator(
    string: str,
    in_dataset: Input[Dataset],
) -> Dataset:
    """Pipeline to convert string to a Dataset, then concatenate with
    in_dataset.

    Args:
        string: String to concatenate to in_artifact.
        in_dataset: Dataset to which to concatenate string.

    Returns:
        Output: The final concatenated dataset.
    """
    ...

请注意,如果你为 @dsl.pipeline 装饰器提供了 description 参数,KFP 将使用此描述而不是 docstring 描述。

反馈

此页面是否有帮助?


上次修改时间 2024 年 7 月 5 日: Fixed broken fragments in links (#3790) (36c2ce1)