From c4f1c462986b3a6ea59d76b82b21315dc90c88b0 Mon Sep 17 00:00:00 2001 From: Amy Date: Tue, 21 Jan 2020 17:21:24 -0800 Subject: [PATCH] 'simple pipeline' notebook update to reflect SDK changes and KF notebook image changes (#705) * 'simple pipeline' notebook update to reflect SDK changes and KF notebook images changes * added example of 'create_run_from_pipeline_func' --- pipelines/simple-notebook-pipeline/README.md | 3 - .../Simple Notebook Pipeline.ipynb | 149 +++++++++--------- 2 files changed, 75 insertions(+), 77 deletions(-) diff --git a/pipelines/simple-notebook-pipeline/README.md b/pipelines/simple-notebook-pipeline/README.md index 186f4535..0c28f3b3 100644 --- a/pipelines/simple-notebook-pipeline/README.md +++ b/pipelines/simple-notebook-pipeline/README.md @@ -6,8 +6,5 @@ This notebook shows how to compile and run a simple Kubeflow pipeline using Jupy ### Setup notebook server This pipeline requires you to [setup a notebook server](https://www.kubeflow.org/docs/notebooks/setup/) in the Kubeflow UI. After you are setup, upload this notebook and then run it in the notebook server. -### Create a GCS bucket -This pipeline requires a GCS bucket. If you haven't already, [create a GCS bucket](https://cloud.google.com/storage/docs/creating-buckets) to run the notebook. Make sure to create the storage bucket in the same project that you are running Kubeflow on to have the proper permissions by default. You can also create a GCS bucket by running `gsutil mb -p gs://`. - ### Upload the notebook to the Kubeflow UI In order to run this pipeline, make sure to upload the notebook to your notebook server in the Kubeflow UI. You can clone this repo in the Jupyter notebook server by connecting to the notebook server and then selecting New > Terminal. In the terminal type `git clone https://github.com/kubeflow/examples.git`. \ No newline at end of file diff --git a/pipelines/simple-notebook-pipeline/Simple Notebook Pipeline.ipynb b/pipelines/simple-notebook-pipeline/Simple Notebook Pipeline.ipynb index 739901af..3480b9d0 100644 --- a/pipelines/simple-notebook-pipeline/Simple Notebook Pipeline.ipynb +++ b/pipelines/simple-notebook-pipeline/Simple Notebook Pipeline.ipynb @@ -1,26 +1,5 @@ { "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Copyright 2019 Google Inc. 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", - "# You may obtain a copy of the License at\n", - "#\n", - "# http://www.apache.org/licenses/LICENSE-2.0\n", - "#\n", - "# Unless required by applicable law or agreed to in writing, software\n", - "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", - "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", - "# See the License for the specific language governing permissions and\n", - "# limitations under the License." - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -41,43 +20,28 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Prerequisites: Install the pipelines SDK\n", - "If you followed the [notebook setup instructions](https://www.kubeflow.org/docs/notebooks/setup/) then you don't have to run the next cell. You only need to install the KFP package once in your notebook server. You can find the latest KFP package [here](https://github.com/kubeflow/pipelines/releases)." + "### Prerequisites: Install or update the pipelines SDK\n", + "\n", + "You may need to **restart your notebook kernel** after updating the KFP sdk.\n", + "\n", + "This notebook is intended to be run from a Kubeflow notebook server. (From other environments, you would need to pass different arguments to the `kfp.Client` constructor.)" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": true - }, + "metadata": {}, "outputs": [], "source": [ - "# If you've already done this during setup you don't need to do it again\n", - "!pip3 install kfp --upgrade" + "# You may need to restart your notebook kernel after updating the kfp sdk\n", + "!python3 -m pip install kfp --upgrade --user" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Setup\n", - " Important: Make sure to use your GCP project name and a GCS bucket for saving the pipelines. If you haven't already, [setup a GCS bucket](https://cloud.google.com/storage/docs/creating-buckets)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [ - "parameters" - ] - }, - "outputs": [], - "source": [ - "# Set your output directory and project name. \n", - "PROJECT_NAME = '[YOUR-GCP-PROJECT-NAME]' # 'Your-Gcp-Project-Name'\n", - "OUTPUT_DIR = 'gs://[YOUR-GCS-BUCKET]/assets' # A path for asset outputs" + "### Setup\n" ] }, { @@ -86,7 +50,7 @@ "metadata": {}, "outputs": [], "source": [ - "EXPERIMENT_NAME = 'Simple notebook pipeline' # Name of the experiment in the UI\n", + "EXPERIMENT_NAME = 'Simple notebook pipeline' # Name of the experiment in the UI\n", "BASE_IMAGE = 'tensorflow/tensorflow:2.0.0b0-py3' # Base image used for components in the pipeline" ] }, @@ -98,7 +62,8 @@ "source": [ "import kfp\n", "import kfp.dsl as dsl\n", - "from kfp import compiler" + "from kfp import compiler\n", + "from kfp import components" ] }, { @@ -112,7 +77,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Create python function" + "#### Create a python function" ] }, { @@ -136,26 +101,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Build pipeline component from the function" + "#### Build a pipeline component from the function" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": true - }, + "metadata": {}, "outputs": [], "source": [ - "# Docker image for the pipeline component\n", - "TARGET_IMAGE = 'gcr.io/%s/add-op:latest' % PROJECT_NAME \n", - "\n", - "# The return value \"DeployerOp\" represents a step that is used in a pipeline\n", - "add_op = compiler.build_python_component(\n", - " component_func=add,\n", - " staging_gcs_path=OUTPUT_DIR,\n", - " base_image=BASE_IMAGE,\n", - " target_image=TARGET_IMAGE)" + "# Convert the function to a pipeline operation.\n", + "add_op = components.func_to_container_op(\n", + " add,\n", + " base_image=BASE_IMAGE, \n", + ")" ] }, { @@ -196,24 +155,35 @@ "### Compile and run the pipeline" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Kubeflow Pipelines lets you group pipeline runs by *Experiments*. You can create a new experiment, or call `kfp.Client().list_experiments()` to see existing ones.\n", + "If you don't specify the experiment name, the `Default` experiment will be used.\n", + "\n", + "You can directly run a pipeline given its function definition:" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "pipeline_func = calc_pipeline\n", - "pipeline_filename = pipeline_func.__name__ + '.pipeline.zip'\n", - "compiler.Compiler().compile(pipeline_func, pipeline_filename)" + "# Specify pipeline argument values\n", + "arguments = {'a': '7', 'b': '8'}\n", + "# Launch a pipeline run given the pipeline function definition\n", + "kfp.Client().create_run_from_pipeline_func(calc_pipeline, arguments=arguments, \n", + " experiment_name=EXPERIMENT_NAME)\n", + "# The generated links below lead to the Experiment page and the pipeline run details page, respectively" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Create an Experiment in the Pipeline System\n", - "\n", - "Pipeline system requires an \"Experiment\" to group pipeline runs. You can create a new experiment, or call client.list_experiments() to get existing ones." + "Alternately, you can separately compile the pipeline and then upload and run it as follows:" ] }, { @@ -222,7 +192,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Get or create an experiment and submit a pipeline run\n", + "# Compile the pipeline\n", + "pipeline_func = calc_pipeline\n", + "pipeline_filename = pipeline_func.__name__ + '.pipeline.zip'\n", + "compiler.Compiler().compile(pipeline_func, pipeline_filename)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Get or create an experiment\n", "client = kfp.Client()\n", "experiment = client.create_experiment(EXPERIMENT_NAME)" ] @@ -231,7 +213,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Submit the pipeline for execution" + "Submit the compiled pipeline for execution:" ] }, { @@ -240,15 +222,14 @@ "metadata": {}, "outputs": [], "source": [ - "#Specify pipeline argument values\n", + "# Specify pipeline argument values\n", "arguments = {'a': '7', 'b': '8'}\n", "\n", - "#Submit a pipeline run\n", + "# Submit a pipeline run\n", "run_name = pipeline_func.__name__ + ' run'\n", "run_result = client.run_pipeline(experiment.id, run_name, pipeline_filename, arguments)\n", "\n", - "#This link leads to the run information page. \n", - "#Note: There is a bug in JupyterLab that modifies the URL and makes the link stop working" + "# The generated link below leads to the pipeline run information page." ] }, { @@ -258,6 +239,26 @@ "### That's it!\n", "You just created and deployed your first pipeline in Kubeflow! You can put more complex python code within the functions, and you can import any libraries that are included in the base image (you can use [VersionedDependencies](https://kubeflow-pipelines.readthedocs.io/en/latest/source/kfp.compiler.html#kfp.compiler.VersionedDependency) to import libraries not included in the base image). " ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "----\n", + "Copyright 2019 Google Inc. 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", + "You may obtain a copy of the License at\n", + "\n", + " http://www.apache.org/licenses/LICENSE-2.0\n", + "\n", + "Unless required by applicable law or agreed to in writing, software\n", + "distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "See the License for the specific language governing permissions and\n", + "limitations under the License." + ] } ], "metadata": { @@ -276,7 +277,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.6.8" } }, "nbformat": 4,