kubeflow / pipelines

Machine Learning Pipelines for Kubeflow
https://www.kubeflow.org/docs/components/pipelines/
Apache License 2.0
3.5k stars 1.57k forks source link

Question about how to use `dsl.PipelineTaskFinalStatus` with `dsl.container_component` #10362

Open ysk24ok opened 5 months ago

ysk24ok commented 5 months ago

macOS Sonoma 14.2.1 Python 3.11 kfp 2.4.0

I tried implementing a container component which does exit handling.

from kfp import dsl

@dsl.container_component
def SampleExitOp(status: dsl.PipelineTaskFinalStatus):
    return dsl.ContainerSpec(
        image="gcr.io/...",
        command=["python3", "main.py"],
        args=["--state", status.state],
    )

When I compiled a pipeline which has the SampleExitOp component, the following error was shown.

Traceback (most recent call last):
  File "/Users/y-nishioka/repos/examples/kfp_v2/pipeline_with_reusable_components.py", line 7, in <module>
    import components
  File "/Users/y-nishioka/repos/examples/kfp_v2/components.py", line 6, in <module>
    @dsl.container_component
     ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/dsl/container_component_decorator.py", line 53, in container_component
    return component_factory.create_container_component_from_func(func)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/dsl/component_factory.py", line 643, in create_container_component_from_func
    container_spec = func(*arg_list)
                     ^^^^^^^^^^^^^^^
  File "/Users/y-nishioka/repos/examples/kfp_v2/components.py", line 16, in SampleExitOp
    status.state,
    ^^^^^^^^^^^^
AttributeError: 'InputValuePlaceholder' object has no attribute 'state'

Does it mean we can't use dsl.PipelineTaskFinalStatus with dsl.container_component?

github-actions[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 2 months ago

This issue has been automatically closed because it has not had recent activity. Please comment "/reopen" to reopen it.

HeGaoYuan commented 2 months ago

Can anybody ask this questionns? Thanks!

"Does it mean we can't use dsl.PipelineTaskFinalStatus with dsl.container_component?"

@connor-mccarthy @chensun

HeGaoYuan commented 2 months ago

@ysk24ok

Hi, have you known how to fix this problem?

connor-mccarthy commented 2 months ago

dsl.PipelineTaskFinalStatus is a placeholder, not a type annotation. Usage is as follows:

@dsl.container_component
def SampleExitOp(status: str = dsl.PipelineTaskFinalStatus):
ysk24ok commented 2 months ago

@connor-mccarthy Thank you for your response.

I tried with the following pipeline,

pipeline ```py from kfp import dsl PIPELINE_NAME = "exit-handler-pipeline" LOCAL_PIPELINE_PATH = f"outputs/{PIPELINE_NAME}.yaml" @dsl.container_component def exit_op(status: str = dsl.PipelineTaskFinalStatus): return dsl.ContainerSpec(image='alpine', command=['echo'], args=[status]) @dsl.component(base_image="python:3.11") def fail_op(): import sys sys.exit(1) @dsl.pipeline(name=PIPELINE_NAME) def pipeline(): print_status_task = exit_op() with dsl.ExitHandler(exit_task=print_status_task): fail_op() compiler.Compiler().compile(pipeline_func=pipeline, package_path=LOCAL_PIPELINE_PATH) ```

but got the following error.

  File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/compiler/pipeline_spec_builder.py", line 667, in _fill_in_component_input_default_value
    input_name].default_value.string_value = default_value
                              ^^^^^^^^^^^^
entire traceback ``` Traceback (most recent call last): File "/Users/y-nishioka/repos/examples/kfp_v2/exit_handler_pipeline.py", line 29, in @dsl.pipeline(name=PIPELINE_NAME) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/dsl/pipeline_context.py", line 65, in pipeline return component_factory.create_graph_component_from_func( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/dsl/component_factory.py", line 673, in create_graph_component_from_func return graph_component.GraphComponent( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/dsl/graph_component.py", line 68, in __init__ pipeline_spec, platform_spec = builder.create_pipeline_spec( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/compiler/pipeline_spec_builder.py", line 1934, in create_pipeline_spec build_exit_handler_groups_recursively( File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/compiler/pipeline_spec_builder.py", line 1583, in build_exit_handler_groups_recursively exit_task_component_spec = build_component_spec_for_exit_task( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/compiler/pipeline_spec_builder.py", line 330, in build_component_spec_for_exit_task return build_component_spec_for_task( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/compiler/pipeline_spec_builder.py", line 356, in build_component_spec_for_task component_spec = _build_component_spec_from_component_spec_structure( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/compiler/pipeline_spec_builder.py", line 387, in _build_component_spec_from_component_spec_structure _fill_in_component_input_default_value( File "/Users/y-nishioka/repos/examples/kfp_v2/venv/lib/python3.11/site-packages/kfp/compiler/pipeline_spec_builder.py", line 667, in _fill_in_component_input_default_value input_name].default_value.string_value = default_value ^^^^^^^^^^^^ ```

I'm using kfp 2.7.0. Am I missing something?

It seems like dsl.PipelineTaskFinalStatus is a dataclass, not a placeholder like dsl.PIPELINE_JOB_NAME_PLACEHOLDER.

connor-mccarthy commented 2 months ago

@ysk24ok, you are correct. My previous comment was a mistake.

Can you please share how you are using your component in your pipeline?

hhenke-mle commented 1 month ago

Have same problem more or less and yes it seems that PipelineTaskFinalStatus doesnt work in container_components as it does in components - fails with AttributeError: 'InputValuePlaceholder' object has no attribute 'state'

Yilinana commented 1 month ago

Is there a way to get the final status of pipeline in kfp v2? I tried to use PipelineTaskFinalStatus in a container component, but I encountered the same issue and obtained the same result as mentioned above.