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

如何在Vertex AI中基于管道参数动态设置ParallelFor的并行度?

动态设置Vertex AI管道ParallelFor的并行度

核心结论

可以基于country参数或上游任务输出的dr keys长度动态设置ParallelFor的并行度,Vertex AI(基于KFP)支持通过运行时参数或上游任务输出值配置并行度,解决编译时参数不可用的问题。

方案一:基于country参数动态设置并行度

通过将country作为管道运行时参数,映射不同国家对应的并行度,KFP会在运行时解析该值,无需编译时确定。

代码示例

from kfp import dsl
from kfp.dsl import component

# 获取对应国家的dr keys组件
@component
def get_country_dr_keys(country: str) -> list:
    # 替换为实际业务逻辑:根据country返回dr keys列表
    country_key_map = {
        "uk": ["uk_key1", "uk_key2", "uk_key3"],
        "de": ["de_key1", "de_key2"],
        "fr": ["fr_key1", "fr_key2", "fr_key3", "fr_key4"]
    }
    return country_key_map.get(country, [])

# 定义国家到并行度的映射函数
def map_country_to_parallelism(country: str) -> int:
    parallelism_config = {
        "uk": 3,
        "de": 2,
        "fr": 4
    }
    return parallelism_config.get(country, 1)  # 默认并行度1

# 定义管道
@dsl.pipeline(name="country-based-parallelism-pipeline")
def pipeline(country: str):
    # 执行获取dr keys的任务
    get_keys_task = get_country_dr_keys(country=country)
    # 根据country动态获取并行度
    dynamic_parallelism = map_country_to_parallelism(country)
    # 并行处理每个dr key
    with dsl.ParallelFor(
        items=get_keys_task.output,
        parallelism=dynamic_parallelism
    ) as dr_key:
        # 替换为实际的dr key处理任务
        dsl.ContainerOp(
            name="process-dr-key",
            image="gcr.io/your-project/your-processing-image:latest",
            arguments=["--dr-key", dr_key]
        )

说明

  • map_country_to_parallelism函数接收运行时传入的country参数,返回对应并行度,KFP会在运行时解析该值,不会触发编译错误。
  • 可根据业务需求扩展parallelism_config的国家映射规则。

方案二:基于dr keys列表长度动态设置并行度

直接以上游任务返回的dr keys列表长度作为并行度,让并行数与待处理的key数量完全匹配,避免资源浪费。

代码示例

from kfp import dsl
from kfp.dsl import component

@component
def get_country_dr_keys(country: str) -> list:
    # 实际业务逻辑:返回对应国家的dr keys
    country_key_map = {
        "uk": ["uk_key1", "uk_key2", "uk_key3"],
        "de": ["de_key1", "de_key2"]
    }
    return country_key_map.get(country, [])

@dsl.pipeline(name="list-length-based-parallelism-pipeline")
def pipeline(country: str):
    get_keys_task = get_country_dr_keys(country=country)
    # 直接用dr keys列表的长度作为并行度
    dynamic_parallelism = get_keys_task.output.length
    with dsl.ParallelFor(
        items=get_keys_task.output,
        parallelism=dynamic_parallelism
    ) as dr_key:
        # 替换为实际处理任务
        dsl.ContainerOp(
            name="process-dr-key",
            image="gcr.io/your-project/your-processing-image:latest",
            arguments=["--dr-key", dr_key]
        )

说明

  • get_keys_task.output.length会在运行时计算上游任务返回的列表长度,自动设置为ParallelFor的并行度。
  • 若dr keys列表为空,并行度会被设为0,此时ParallelFor不会执行任何分支任务。

注意事项

  1. KFP版本要求:确保使用KFP 1.8及以上版本,该版本开始支持用运行时参数或上游任务输出配置ParallelFor并行度。
  2. 配额限制:Vertex AI管道有项目级的并行任务配额,设置的并行度不能超过配额上限,否则任务会排队或失败。
  3. 默认值处理:无论哪种方案,都要设置合理的默认并行度,避免因参数无效或空列表导致异常。

内容的提问来源于stack exchange,提问作者Ferrard

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 18:45:31