Conditional Statements

You can enhance your pipelines and further parameterize your Kubeflow Pipeline by introducing conditionals into your @step or @pipeline decorators.

A conditional in the Kale SDK is an if statement that is not followed by *else *or elif and is composed of only two operators compared with

  • ==
  • !=
  • >
  • >=
  • <=

For the purposes of defining a Kale SDK conditional statement an operator is either:

  • a pipeline parameter
  • a step result
  • a constant (both numbers and string are supported).

Throughout this unit, you will explore conditional pipelines in the Kale SDK.

Please follow along in your own copy of our notebook as we complete the steps below.
1. Download conditional_pipeline_code.py

For this unit, we will look at a new code example focused specifically on conditional statements.

First, download the new code file by clicking here and add to your Notebook Server.

[2. Review Python Code]

Review the new code and observe that there is now a conditional statement within the pipeline definition.

[3. Add conditional parameter to pipeline declaration. ]

In addition to a static conditional as demonstrated in the original code you can use a parameter in the pipeline declaration to control the conditional.  Using pipeline parameters in your conditional statements, instead of constants, is useful for quick iteration and experimentation because you can quickly clone your pipeline and rerun with a new parameter value. Update the conditional_pipeline function definition to include:

threshold=12

Now parameterize the conditional by using the parameter from the conditional_pipeline function definition:

if res > threshold

When complete your pipeline function will look like this:

4. Open a new terminal

Select Terminal from the launcher to open a new terminal.

5. Deploy Code as Kubeflow Pipeline

Make sure you are in the directory with the Python code, kale-sdk-datavol-1 and execute the following to deploy the code as a Kubeflow Pipeline.

python3 -m kale conditional_pipeline_code.py --kfp

6. Review Pipeline Runs

Navigate to the Kubeflow UI and open the Runs UI. The number of runs will continue to increase throughout the course.

7. Access the pipeline run

Select the conditional-*** run to see the Kubeflow Pipeline execute successfully with the conditional statement

NOTE: if the value of res, which is set by generate(), is less than or equal to the threshold then your output will be as follows since the final step is not executed due to the conditional.

NOTE: if this is your result execute the pipeline until you achieve the successful printer() output.

8. Review Printer Output

Select the printer step, select Logs and scroll down to find the value that triggered the conditional and the pipeline to end.

[9. Add Nested Conditional ]

The Kale SDK supports nested conditionals with multiple pipeline function parameters.

Navigate back to your code and update conditional_pipeline function declaration with:

upper=13, lower=10

Then update the conditional statement in the pipeline function:

if res < upper:

        if res > lower:

            printer(res)

When complete your pipeline function will look like this:

10. Open a new terminal

Select Terminal from the launcher to open a new terminal.

11. Deploy Code as Kubeflow Pipeline

Make sure you are in the directory with the Python code, kale-sdk-datavol-1 and execute the following to deploy the code as a Kubeflow Pipeline.

python3 -m kale conditional_pipeline_code.py --kfp

12. Review Pipeline Runs

Navigate to the Kubeflow UI and open the Runs UI. The number of runs will continue to increase throughout the course.

13. Access the pipeline run

Select the second conditional-*** run to see the Kubeflow Pipeline execute successfully with the conditional statement. The pipeline will end in one of the following ways:

  1. res will be greater than 13 and will error out
  2. *res *will be less than 10 and error out
  3. res will be 11 or 12 and will this value will be printed.

The Kubeflow UI will show that the pipeline skipped steps by graying out the step and showing a message in the Step UI.

14. Recurring Runs UI

Now that you have pipeline with a parameterized conditional statement you can choose to set a recurring run with different parameter values. Navigate to the Recurring Runs UI:

[15. Create New Recurring Run]{style=“font-weight: 600;”}

Select +Create Run and then select the conditional-*** pipeline:

Scroll down and observe that the conditional parameters are available to you to modify for your recurring run.

With this approach you can set up a conditional pipeline and test using the Kale SDK and then set up a recurring run from within the Kubeflow UI.

For the complete code please click here.