{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Name\n",
    "\n",
    "Deploying a trained model to Cloud Machine Learning Engine \n",
    "\n",
    "\n",
    "# Label\n",
    "\n",
    "Cloud Storage, Cloud ML Engine, Kubeflow, Pipeline\n",
    "\n",
    "\n",
    "# Summary\n",
    "\n",
    "A Kubeflow Pipeline component to deploy a trained model from a Cloud Storage location to Cloud ML Engine.\n",
    "\n",
    "\n",
    "# Details\n",
    "\n",
    "\n",
    "## Intended use\n",
    "\n",
    "Use the component to deploy a trained model to Cloud ML Engine. The deployed model can serve online or batch predictions in a Kubeflow Pipeline.\n",
    "\n",
    "\n",
    "## Runtime arguments\n",
    "\n",
    "| Argument | Description | Optional | Data type | Accepted values | Default |\n",
    "|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|--------------|-----------------|---------|\n",
    "| model_uri | The URI of a Cloud Storage directory that contains a trained model file.<br/> Or <br/> An [Estimator export base directory](https://www.tensorflow.org/guide/saved_model#perform_the_export) that contains a list of subdirectories named by timestamp. The directory with the latest timestamp is used to load the trained model file. | No | GCSPath |  |  |\n",
    "| project_id | The ID of the Google Cloud Platform (GCP) project of the serving model. | No | GCPProjectID |  |  |\n",
    "| model_id | The name of the trained model. | Yes | String |  | None |\n",
    "| version_id | The name of the version of the model. If it is not provided, the operation uses a random name. | Yes | String |  | None |\n",
    "| runtime_version | The Cloud ML Engine runtime version to use for this deployment. If it is not provided, the default stable version, 1.0, is used. | Yes | String |  | None |\n",
    "| python_version | The version of Python used in the prediction. If it is not provided, version 2.7 is used. You can use Python 3.5 if runtime_version is set to 1.4 or above. Python 2.7 works with all supported runtime versions. | Yes | String |  | 2.7 |\n",
    "| model | The JSON payload of the new [model](https://cloud.google.com/ml-engine/reference/rest/v1/projects.models). | Yes | Dict |  | None |\n",
    "| version | The new [version](https://cloud.google.com/ml-engine/reference/rest/v1/projects.models.versions) of the trained model. | Yes | Dict |  | None |\n",
    "| replace_existing_version | Indicates whether to replace the existing version in case of a conflict (if the same version number is found.) | Yes | Boolean |  | FALSE |\n",
    "| set_default | Indicates whether to set the new version as the default version in the model. | Yes | Boolean |  | FALSE |\n",
    "| wait_interval | The number of seconds to wait in case the operation has a long run time. | Yes | Integer |  | 30 |\n",
    "\n",
    "\n",
    "\n",
    "## Input data schema\n",
    "\n",
    "The component looks for a trained model in the location specified by the  `model_uri` runtime argument. The accepted trained models are:\n",
    "\n",
    "\n",
    "*   [Tensorflow SavedModel](https://cloud.google.com/ml-engine/docs/tensorflow/exporting-for-prediction) \n",
    "*   [Scikit-learn & XGBoost model](https://cloud.google.com/ml-engine/docs/scikit/exporting-for-prediction)\n",
    "\n",
    "The accepted file formats are:\n",
    "\n",
    "*   *.pb\n",
    "*   *.pbtext\n",
    "*   model.bst\n",
    "*   model.joblib\n",
    "*   model.pkl\n",
    "\n",
    "`model_uri` can also be an [Estimator export base directory, ](https://www.tensorflow.org/guide/saved_model#perform_the_export)which contains a list of subdirectories named by timestamp. The directory with the latest timestamp is used to load the trained model file.\n",
    "\n",
    "## Output\n",
    "| Name    | Description                 | Type      |\n",
    "|:------- |:----                        | :---      |\n",
    "| job_id  | The ID of the created job.  |  String   |\n",
    "| job_dir | The Cloud Storage path that contains the trained model output files. |  GCSPath  |\n",
    "\n",
    "\n",
    "## Cautions & requirements\n",
    "\n",
    "To use the component, you must:\n",
    "\n",
    "*   [Set up the cloud environment](https://cloud.google.com/ml-engine/docs/tensorflow/getting-started-training-prediction#setup).\n",
    "*   The component can authenticate to GCP. Refer to [Authenticating Pipelines to GCP](https://www.kubeflow.org/docs/gke/authentication-pipelines/) for details.\n",
    "*   Grant read access to the Cloud Storage bucket that contains the trained model to the Kubeflow user service account.\n",
    "\n",
    "## Detailed description\n",
    "\n",
    "Use the component to: \n",
    "*   Locate the trained model at the Cloud Storage location you specify.\n",
    "*   Create a new model if a model provided by you doesn’t exist.\n",
    "*   Delete the existing model version if `replace_existing_version` is enabled.\n",
    "*   Create a new version of the model from the trained model.\n",
    "*   Set the new version as the default version of the model if `set_default` is enabled.\n",
    "\n",
    "Follow these steps to use the component in a pipeline:\n",
    "\n",
    "1.  Install the Kubeflow Pipeline SDK:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture --no-stderr\n",
    "\n",
    "!pip3 install kfp --upgrade"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Load the component using KFP SDK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import kfp.components as comp\n",
    "\n",
    "mlengine_deploy_op = comp.load_component_from_url(\n",
    "    'https://raw.githubusercontent.com/kubeflow/pipelines/1.7.0-rc.3/components/gcp/ml_engine/deploy/component.yaml')\n",
    "help(mlengine_deploy_op)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sample\n",
    "Note: The following sample code works in IPython notebook or directly in Python code.\n",
    "\n",
    "In this sample, you deploy a pre-built trained model from `gs://ml-pipeline-playground/samples/ml_engine/census/trained_model/` to Cloud ML Engine. The deployed model is `kfp_sample_model`. A new version is created every time the sample is run, and the latest version is set as the default version of the deployed model.\n",
    "\n",
    "#### Set sample parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "parameters"
    ]
   },
   "outputs": [],
   "source": [
    "# Required Parameters\n",
    "PROJECT_ID = '<Please put your project ID here>'\n",
    "\n",
    "# Optional Parameters\n",
    "EXPERIMENT_NAME = 'CLOUDML - Deploy'\n",
    "TRAINED_MODEL_PATH = 'gs://ml-pipeline-playground/samples/ml_engine/census/trained_model/'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Example pipeline that uses the component"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import kfp.dsl as dsl\n",
    "import json\n",
    "@dsl.pipeline(\n",
    "    name='CloudML deploy pipeline',\n",
    "    description='CloudML deploy pipeline'\n",
    ")\n",
    "def pipeline(\n",
    "    model_uri = 'gs://ml-pipeline-playground/samples/ml_engine/census/trained_model/',\n",
    "    project_id = PROJECT_ID,\n",
    "    model_id = 'kfp_sample_model',\n",
    "    version_id = '',\n",
    "    runtime_version = '1.10',\n",
    "    python_version = '',\n",
    "    version = {},\n",
    "    replace_existing_version = 'False',\n",
    "    set_default = 'True',\n",
    "    wait_interval = '30'):\n",
    "    task = mlengine_deploy_op(\n",
    "        model_uri=model_uri, \n",
    "        project_id=project_id, \n",
    "        model_id=model_id, \n",
    "        version_id=version_id, \n",
    "        runtime_version=runtime_version, \n",
    "        python_version=python_version,\n",
    "        version=version, \n",
    "        replace_existing_version=replace_existing_version, \n",
    "        set_default=set_default, \n",
    "        wait_interval=wait_interval)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Compile the pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipeline_func = pipeline\n",
    "pipeline_filename = pipeline_func.__name__ + '.zip'\n",
    "import kfp.compiler as compiler\n",
    "compiler.Compiler().compile(pipeline_func, pipeline_filename)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Submit the pipeline for execution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Specify pipeline argument values\n",
    "arguments = {}\n",
    "\n",
    "#Get or create an experiment and submit a pipeline run\n",
    "import kfp\n",
    "client = kfp.Client()\n",
    "experiment = client.create_experiment(EXPERIMENT_NAME)\n",
    "\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)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "* [Component python code](https://github.com/kubeflow/pipelines/blob/master/components/gcp/container/component_sdk/python/kfp_component/google/ml_engine/_deploy.py)\n",
    "* [Component docker file](https://github.com/kubeflow/pipelines/blob/master/components/gcp/container/Dockerfile)\n",
    "* [Sample notebook](https://github.com/kubeflow/pipelines/blob/master/components/gcp/ml_engine/deploy/sample.ipynb)\n",
    "* [Cloud Machine Learning Engine Model REST API](https://cloud.google.com/ml-engine/reference/rest/v1/projects.models)\n",
    "* [Cloud Machine Learning Engine Version REST API](https://cloud.google.com/ml-engine/reference/rest/v1/projects.versions)\n",
    "\n",
    "## License\n",
    "By deploying or using this software you agree to comply with the [AI Hub Terms of Service](https://aihub.cloud.google.com/u/0/aihub-tos) and the [Google APIs Terms of Service](https://developers.google.com/terms/). To the extent of a direct conflict of terms, the AI Hub Terms of Service will control."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
