Skip to content

Commit

Permalink
Updated samples to V2
Browse files Browse the repository at this point in the history
* Updated output_a_directory.py to V2

* Update output_a_directory_test.py to V2

* Update parallel_join.py to V2

* Update multiple_outputs.ipynb to V2

* Update multiple_outputs_test.py to V2

* Updated kfp_env_validation to V2

* Updated loop_parallelism to V2
  • Loading branch information
PratyushaRavi committed Aug 21, 2023
1 parent b630d5c commit 2d170e5
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 187 deletions.
17 changes: 8 additions & 9 deletions samples/core/condition/nested_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from kfp.deprecated import components
from kfp.deprecated import dsl
from kfp import dsl, compiler


@components.create_component_from_func
@dsl.component()
def flip_coin_op() -> str:
"""Flip a coin and output heads or tails randomly."""
import random
result = 'heads' if random.randint(0, 1) == 0 else 'tails'
return result


@components.create_component_from_func
@dsl.component()
def print_op(msg: str):
"""Print a message."""
print(msg)
Expand All @@ -33,18 +32,18 @@ def print_op(msg: str):
@dsl.pipeline(name='nested-conditions-pipeline')
def my_pipeline():
flip1 = flip_coin_op()
print_op(flip1.output)
print_op(msg=flip1.output)
flip2 = flip_coin_op()
print_op(flip2.output)
print_op(msg=flip2.output)

with dsl.Condition(flip1.output != 'no-such-result'): # always true
flip3 = flip_coin_op()
print_op(flip3.output)
print_op(msg=flip3.output)

with dsl.Condition(flip2.output == flip3.output):
flip4 = flip_coin_op()
print_op(flip4.output)
print_op(msg=flip4.output)


if __name__ == '__main__':
kfp.compiler.Compiler().compile(my_pipeline, __file__ + '.yaml')
compiler.Compiler().compile(my_pipeline, __file__ + '.yaml')
4 changes: 2 additions & 2 deletions samples/core/condition/nested_condition_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import kfp.deprecated as kfp
import kfp as kfp
from .nested_condition import my_pipeline
from kfp.samples.test.utils import run_pipeline_func, TestCase

run_pipeline_func([
TestCase(
pipeline_func=my_pipeline,
mode=kfp.dsl.PipelineExecutionMode.V1_LEGACY,
mode=kfp.dsl.PipelineExecutionMode.V2_ENGINE,
),
])
63 changes: 23 additions & 40 deletions samples/core/kfp_env_validation/kfp_env_validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2020 The Kubeflow Authors. All Rights Reserved.\n",
"# Copyright 2020-2023 The Kubeflow Authors. All Rights Reserved.\n",
"#\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
Expand Down Expand Up @@ -45,6 +45,7 @@
"metadata": {},
"outputs": [],
"source": [
"@dsl.component(base_image='google/cloud-sdk:442.0.0')\n",
"def run_diagnose_me():\n",
" \"\"\" Prints a dump of gcp environment configurations.\n",
"\n",
Expand All @@ -60,7 +61,7 @@
" subprocess.run(['apt-get', 'install', 'python3-distutils', '--yes'],\n",
" capture_output=True)\n",
" subprocess.run(['python3', 'get-pip.py'], capture_output=True)\n",
" subprocess.run(['python3', '-m', 'pip', 'install', 'kfp>=0.1.31', '--quiet'],\n",
" subprocess.run(['python3', '-m', 'pip', 'install', 'kfp>=2.0.1', '--quiet'],\n",
" capture_output=True)\n",
"\n",
" subprocess.run(['kfp', 'diagnose_me'])"
Expand All @@ -79,6 +80,7 @@
"metadata": {},
"outputs": [],
"source": [
"@dsl.component(base_image='google/cloud-sdk:442.0.0')\n",
"def verify_gcp_credentials():\n",
" \"\"\" Verifies if gcp credentials are configured correctly.\n",
"\n",
Expand All @@ -94,13 +96,13 @@
" subprocess.run(['apt-get', 'install', 'python3-distutils', '--yes'],\n",
" capture_output=True)\n",
" subprocess.run(['python3', 'get-pip.py'], capture_output=True)\n",
" subprocess.run(['python3', '-m', 'pip', 'install', 'kfp>=0.1.31', '--quiet'],\n",
" subprocess.run(['python3', '-m', 'pip', 'install', 'kfp>=2.0.1', '--quiet'],\n",
" capture_output=True)\n",
"\n",
" import sys\n",
" from typing import List, Text\n",
" import os\n",
" from kfp.deprecated.cli.diagnose_me import gcp\n",
" from kfp.cli.diagnose_me import gcp\n",
"\n",
" # Get the project ID\n",
" project_config = gcp.get_gcp_configuration(\n",
Expand Down Expand Up @@ -134,6 +136,7 @@
"metadata": {},
"outputs": [],
"source": [
"@dsl.component(base_image='google/cloud-sdk:442.0.0')\n",
"def print_scopes():\n",
" \"\"\" Prints the scope settings for each instance and service account.\n",
"\n",
Expand All @@ -149,13 +152,13 @@
" subprocess.run(['apt-get', 'install', 'python3-distutils', '--yes'],\n",
" capture_output=True)\n",
" subprocess.run(['python3', 'get-pip.py'], capture_output=True)\n",
" subprocess.run(['python3', '-m', 'pip', 'install', 'kfp>=0.1.31', '--quiet'],\n",
" subprocess.run(['python3', '-m', 'pip', 'install', 'kfp>=2.0.1', '--quiet'],\n",
" capture_output=True)\n",
"\n",
" import sys\n",
" from typing import List, Text \n",
" import os\n",
" from kfp.deprecated.cli.diagnose_me import gcp\n",
" from kfp.cli.diagnose_me import gcp\n",
" import json\n",
" # Get the project ID\n",
" project_config = gcp.get_gcp_configuration(gcp.Commands.GET_GCLOUD_DEFAULT,human_readable=False)\n",
Expand Down Expand Up @@ -202,6 +205,7 @@
"metadata": {},
"outputs": [],
"source": [
"@dsl.component(base_image='google/cloud-sdk:442.0.0')\n",
"def verfiy_gcp_apis(target_apis:str):\n",
" \"\"\" Verifies if specified APIs are enabled under the gcp project.\n",
" \n",
Expand All @@ -219,13 +223,13 @@
" subprocess.run(['curl','https://bootstrap.pypa.io/get-pip.py','-o','get-pip.py'], capture_output=True)\n",
" subprocess.run(['apt-get', 'install', 'python3-distutils','--yes'], capture_output=True)\n",
" subprocess.run(['python3', 'get-pip.py'], capture_output=True)\n",
" subprocess.run(['python3', '-m','pip','install','kfp>=0.1.31', '--quiet'], capture_output=True)\n",
" subprocess.run(['python3', '-m','pip','install','kfp>=2.0.1', '--quiet'], capture_output=True)\n",
" \n",
" \n",
" import sys\n",
" from typing import List, Text \n",
" import os\n",
" from kfp.deprecated.cli.diagnose_me import gcp\n",
" from kfp.cli.diagnose_me import gcp\n",
" \n",
" # Get the project ID\n",
" project_config = gcp.get_gcp_configuration(gcp.Commands.GET_GCLOUD_DEFAULT,human_readable=False)\n",
Expand Down Expand Up @@ -271,30 +275,7 @@
"metadata": {},
"outputs": [],
"source": [
"import kfp.deprecated.components as comp\n",
"\n",
"run_diagnose_me_op = comp.func_to_container_op(\n",
" run_diagnose_me, base_image='google/cloud-sdk:279.0.0')\n",
"\n",
"verify_gcp_credentials_op = comp.func_to_container_op(\n",
" verify_gcp_credentials, base_image='google/cloud-sdk:279.0.0')\n",
"\n",
"print_scopes_op = comp.func_to_container_op(\n",
" print_scopes, base_image='google/cloud-sdk:279.0.0')\n",
"\n",
"\n",
"verify_gcp_apis_op = comp.func_to_container_op(\n",
" verfiy_gcp_apis, base_image='google/cloud-sdk:279.0.0')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from kfp.deprecated.gcp import use_gcp_secret\n",
"from kfp.deprecated import dsl\n",
"from kfp import dsl\n",
"\n",
"@dsl.pipeline(\n",
" name='verify-kfp-env',\n",
Expand All @@ -307,17 +288,17 @@
" available APIs go to https://pantheon.corp.google.com/apis/library/.\"\"\"\n",
")\n",
"def verify_gcp_kfp_env(\n",
" target_apis='stackdriver.googleapis.com, storage-api.googleapis.com, '\n",
" target_apis: str='stackdriver.googleapis.com, storage-api.googleapis.com, '\n",
" 'bigquery.googleapis.com, dataflow.googleapis.com'\n",
"):\n",
" \"\"\"A sample pipeline to help verifies KFP environment setup.\"\"\"\n",
" \n",
" # This pipeline assumes a user-gcp-sa is needed for execution, if no secret is needed,\n",
" # or a different secret is being used following should be updated accordingly. \n",
" task0 = run_diagnose_me_op().apply(use_gcp_secret('user-gcp-sa'))\n",
" task1 = verify_gcp_credentials_op().apply(use_gcp_secret('user-gcp-sa'))\n",
" task2 = print_scopes_op().apply(use_gcp_secret('user-gcp-sa'))\n",
" task3 = verify_gcp_apis_op(target_apis).apply(use_gcp_secret('user-gcp-sa'))"
" task0 = run_diagnose_me_op()\n",
" task1 = verify_gcp_credentials_op()\n",
" task2 = print_scopes_op()\n",
" task3 = verify_gcp_apis_op(target_apis=target_apis)"
]
},
{
Expand All @@ -326,8 +307,10 @@
"metadata": {},
"outputs": [],
"source": [
"from kfp.deprecated import Client\n",
"client = Client(host='<your-host-name>')"
"from kfp import client\n",
"\n",
"kfp_endpoint = None\n",
"kfp_client = client.Client(host=kfp_endpoint)"
]
},
{
Expand All @@ -336,7 +319,7 @@
"metadata": {},
"outputs": [],
"source": [
"client.create_run_from_pipeline_func(verify_gcp_kfp_env, arguments={})"
"run = kfp_client.create_run_from_pipeline_func(verify_gcp_kfp_env, arguments={})"
]
}
],
Expand Down
11 changes: 5 additions & 6 deletions samples/core/loop_parallelism/loop_parallelism.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from kfp.deprecated import dsl, components, compiler
from kfp import compiler, dsl

@components.create_component_from_func
@dsl.component()
def print_op(s: str):
print(s)

@dsl.pipeline(name='my-pipeline')
def pipeline():
loop_args = [{'A_a': 1, 'B_b': 2}, {'A_a': 10, 'B_b': 20}]
with dsl.ParallelFor(loop_args, parallelism=10) as item:
print_op(item)
print_op(item.A_a)
print_op(item.B_b)
with dsl.ParallelFor(items=loop_args, parallelism=10) as item:
print_op(s=item.A_a)
print_op(s=item.B_b)


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions samples/core/loop_parallelism/loop_parallelism_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import kfp.deprecated as kfp
import kfp
from .loop_parallelism import pipeline
from kfp.samples.test.utils import run_pipeline_func, TestCase

run_pipeline_func([
TestCase(
pipeline_func=pipeline,
mode=kfp.dsl.PipelineExecutionMode.V1_LEGACY,
mode=kfp.dsl.PipelineExecutionMode.V2_ENGINE,
),
])
23 changes: 12 additions & 11 deletions samples/core/multiple_outputs/multiple_outputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"outputs": [],
"source": [
"!python3 -m pip install 'kfp>=0.1.31' --quiet"
"!python3 -m pip install 'kfp>=2.0.0' --quiet"
]
},
{
Expand All @@ -48,9 +48,7 @@
"metadata": {},
"outputs": [],
"source": [
"import kfp.deprecated as kfp\n",
"import kfp.deprecated.components as components\n",
"import kfp.deprecated.dsl as dsl\n",
"from kfp import client, dsl\n",
"from typing import NamedTuple"
]
},
Expand All @@ -68,7 +66,7 @@
"metadata": {},
"outputs": [],
"source": [
"@components.create_component_from_func\n",
"@dsl.component()\n",
"def product_sum(a: float, b: float) -> NamedTuple(\n",
" 'output', [('product', float), ('sum', float)]):\n",
" '''Returns the product and sum of two numbers'''\n",
Expand Down Expand Up @@ -97,11 +95,11 @@
" name='multiple-outputs-pipeline',\n",
" description='Sample pipeline to showcase multiple outputs'\n",
")\n",
"def pipeline(a=2.0, b=2.5, c=3.0):\n",
" prod_sum_task = product_sum(a, b)\n",
" prod_sum_task2 = product_sum(b, c)\n",
" prod_sum_task3 = product_sum(prod_sum_task.outputs['product'],\n",
" prod_sum_task2.outputs['sum'])"
"def pipeline(a: float=2.0, b: float=2.5, c: float=3.0):\n",
" prod_sum_task = product_sum(a=a, b=b)\n",
" prod_sum_task2 = product_sum(a=b, b=c)\n",
" prod_sum_task3 = product_sum(a=prod_sum_task.outputs['product'],\n",
" b=prod_sum_task2.outputs['sum'])"
]
},
{
Expand All @@ -126,7 +124,10 @@
" 'b': 2.5,\n",
" 'c': 3.0,\n",
"}\n",
"run_result = kfp.Client().create_run_from_pipeline_func(pipeline, arguments=arguments)"
"\n",
"kfp_endpoint = None\n",
"kfp_client = client.Client(host=kfp_endpoint)\n",
"run = kfp_client.create_run_from_pipeline_func(pipeline, arguments={})"
]
}
],
Expand Down
6 changes: 3 additions & 3 deletions samples/core/multiple_outputs/multiple_outputs_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 The Kubeflow Authors
# Copyright 2021-2023 The Kubeflow Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import kfp.deprecated as kfp
import kfp as kfp
from kfp.samples.test.utils import TestCase, relative_path, run_pipeline_func

run_pipeline_func([
TestCase(
pipeline_file=relative_path(__file__, 'multiple_outputs.ipynb'),
mode=kfp.dsl.PipelineExecutionMode.V1_LEGACY,
mode=kfp.dsl.PipelineExecutionMode.V2_LEGACY,
),
])
Loading

0 comments on commit 2d170e5

Please sign in to comment.