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-py36:<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.py
Copy and paste the following code inside
condition.py
:condition.py1 # Copyright © 2021 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(): 15 gen = random.sample(range(0, 20), 2) 16 return sum(gen) 17 18 19 @step(name="printer") 20 def printer(value): 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 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(): 15 gen = random.sample(range(0, 20), 2) 16 return sum(gen) 17 18 19 @step(name="printer") 20 def printer(value): 21 print(value) 22 23 24 @pipeline(name="conditional", experiment="kale-tutorial") 25 - def conditional_pipeline(): 26 + def conditional_pipeline(threshold=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 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(): 15 gen = random.sample(range(0, 20), 2) 16 return sum(gen) 17 18 19 @step(name="printer") 20 def printer(value): 21 print(value) 22 23 24 @pipeline(name="conditional", experiment="kale-tutorial") 25 - def conditional_pipeline(threshold=12): 26 + def conditional_pipeline(upper=13, lower=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)
What's Next¶
Check out how you can set Kubernetes configurations for your steps.