Create Conditional Statements¶
This guide will walk you through creating Kale pipelines with conditional statements.
Overview
What You’ll Need¶
- An EKF or MiniKF deployment with the default Kale Docker image.
- An understanding of how the Kale SDK works.
Procedure¶
Create a new Notebook server using the default Kale Docker image. The image will have the following naming scheme:
gcr.io/arrikto/jupyter-kale-py38:<IMAGE_TAG>Note
The
<IMAGE_TAG>
varies based on the MiniKF or EKF release.Create a new python file and name it
condition.py
:$ touch condition.pyCopy and paste the following code inside
condition.py
:condition.py1 # Copyright © 2021-2022 Arrikto Inc. All Rights Reserved. 2 3 """Kale SDK. 4 5 This script defines a pipeline with a conditional statement. 6 """ 7 8 import random 9 10 from kale.sdk import step, pipeline 11 12 13 @step(name="generate") 14 def generate() -> int: 15 gen = random.sample(range(0, 20), 2) 16 return sum(gen) 17 18 19 @step(name="printer") 20 def printer(value: int): 21 print(value) 22 23 24 @pipeline(name="conditional", experiment="kale-tutorial") 25 def conditional_pipeline(): 26 res = generate() 27 if res > 12: 28 printer(res) 29 30 31 if __name__ == "__main__": 32 conditional_pipeline() The pipeline above shows how you can influence the runtime of the pipeline by verifying the return value of a step.
You can also use pipeline parameters in your conditional statements. The following snippet summarizes the changes in code:
condition_param.py1 # Copyright © 2021-2022 Arrikto Inc. All Rights Reserved. 2 3 """Kale SDK. 4-21 4 5 This script defines a pipeline with a conditional statement. 6 """ 7 8 import random 9 10 from kale.sdk import step, pipeline 11 12 13 @step(name="generate") 14 def generate() -> int: 15 gen = random.sample(range(0, 20), 2) 16 return sum(gen) 17 18 19 @step(name="printer") 20 def printer(value: int): 21 print(value) 22 23 24 @pipeline(name="conditional", experiment="kale-tutorial") 25 - def conditional_pipeline(): 26 + def conditional_pipeline(threshold: int = 12): 27 res = generate() 28 - if res > 12: 29 + if res > threshold: 30 printer(res) 31 32 33 if __name__ == "__main__": 34 conditional_pipeline() Using pipeline parameters in your conditional statements, instead of constant values, is particularly useful for quick iteration and experimentation. You can quickly clone your pipeline run (look for the :guilabel:’Clone run’ button on the top-right of the KFP run page) and start a new one with a different pipeline parameter value.
Kale also supports nested conditionals. The following snippet summarizes the changes in code:
condition_param_nested.py1 # Copyright © 2021-2022 Arrikto Inc. All Rights Reserved. 2 3 """Kale SDK. 4-21 4 5 This script defines a pipeline with a conditional statement. 6 """ 7 8 import random 9 10 from kale.sdk import step, pipeline 11 12 13 @step(name="generate") 14 def generate() -> int: 15 gen = random.sample(range(0, 20), 2) 16 return sum(gen) 17 18 19 @step(name="printer") 20 def printer(value: int): 21 print(value) 22 23 24 @pipeline(name="conditional", experiment="kale-tutorial") 25 - def conditional_pipeline(threshold: int = 12): 26 + def conditional_pipeline(upper: int = 13, lower: int = 10): 27 res = generate() 28 - if res > threshold: 29 - printer(res) 30 + if res < upper: 31 + if res > lower: 32 + printer(res) 33 34 35 if __name__ == "__main__": 36 conditional_pipeline()
Note
A “conditional” is an if
statement that is not followed by else
or elif
statements. Also, a conditional should be composed of two
operators compared with ==
, !=
, >
, >=
, <
, or <=
.
An operator is either
- a pipeline parameter,
- a step result, or
- a constant (both numbers and strings are supported)
Important
For the local execution of pipelines with conditional statements you should declare Input and Output Data Types (Declare Input and Output Data Types), otherwise Kale treats everything as Strings. For example, the following fails since Kale tries to compare a String (pipeline parameter without type annotation) with an Integer constant value:
What’s Next¶
Check out how you can create parallel steps using loop statements.