|
| 1 | +# Distributed Training of DeepCTR Estimator using ElasticDL on Kubernetes |
| 2 | + |
| 3 | +This document shows how to run a distributed training job of a deepctr |
| 4 | +estimator model (DeepFM) using [ElasticDL](https://github.com/sql-machine-learning/elasticdl) |
| 5 | +on Kubernetes. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +1. Install Minikube, preferably >= v1.11.0, following the installation |
| 10 | + [guide](https://kubernetes.io/docs/tasks/tools/install-minikube). Minikube |
| 11 | + runs a single-node Kubernetes cluster in a virtual machine on your personal |
| 12 | + computer. |
| 13 | + |
| 14 | +1. Install Docker CE, preferably >= 18.x, following the |
| 15 | + [guide](https://docs.docker.com/docker-for-mac/install/) for building Docker |
| 16 | + images containing user-defined models and the ElasticDL framework. |
| 17 | + |
| 18 | +1. Install Python, preferably >= 3.6, because the ElasticDL command-line tool is |
| 19 | + in Python. |
| 20 | + |
| 21 | +## Models |
| 22 | + |
| 23 | +In this tutorial, we use a [DeepFM estimator](https://github.com/shenweichen/DeepCTR/blob/master/deepctr/estimator/models/deepfm.py) |
| 24 | +model in DeepCTR. The complete program to train the model with the |
| 25 | +dataset definition is in [ElasticDL model zoo](https://github.com/sql-machine-learning/elasticdl/tree/develop/model_zoo/deepctr). |
| 26 | + |
| 27 | +## Dataset |
| 28 | + |
| 29 | +In this tutorial, We use the [criteo dataset](https://github.com/shenweichen/DeepCTR/blob/master/examples/criteo_sample.txt) |
| 30 | +in DeepCTR examples. |
| 31 | + |
| 32 | +```bash |
| 33 | +mkdir ./data |
| 34 | +wget https://github.com/shenweichen/DeepCTR/blob/master/examples/criteo_sample.txt -O ./data/criteo_sample.txt |
| 35 | +``` |
| 36 | + |
| 37 | +## The Kubernetes Cluster |
| 38 | + |
| 39 | +The following command starts a Kubernetes cluster locally using Minikube. It |
| 40 | +uses [VirtualBox](https://www.virtualbox.org/), a hypervisor coming with |
| 41 | +macOS, to create the virtual machine cluster. |
| 42 | + |
| 43 | +```bash |
| 44 | +minikube start --vm-driver=virtualbox \ |
| 45 | + --cpus 2 --memory 6144 --disk-size=50gb |
| 46 | +eval $(minikube docker-env) |
| 47 | +``` |
| 48 | + |
| 49 | +The command `minikube docker-env` returns a set of Bash environment variable |
| 50 | +to configure your local environment to re-use the Docker daemon inside |
| 51 | +the Minikube instance. |
| 52 | + |
| 53 | +The following command is necessary to enable |
| 54 | +[RBAC](https://kubernetes.io/docs/reference/access-authn-authz/rbac/) of |
| 55 | +Kubernetes. |
| 56 | + |
| 57 | +```bash |
| 58 | +kubectl apply -f \ |
| 59 | + https://raw.githubusercontent.com/sql-machine-learning/elasticdl/develop/elasticdl/manifests/elasticdl-rbac.yaml |
| 60 | +``` |
| 61 | + |
| 62 | +If you happen to live in a region where `raw.githubusercontent.com` is banned, |
| 63 | +you might want to Git clone the above repository to get the YAML file. |
| 64 | + |
| 65 | +## Install ElasticDL Client Tool |
| 66 | + |
| 67 | +The following command installs the command line tool `elasticdl`, which talks to |
| 68 | +the Kubernetes cluster and operates ElasticDL jobs. |
| 69 | + |
| 70 | +```bash |
| 71 | +pip install elasticdl_client |
| 72 | +``` |
| 73 | + |
| 74 | +## Build the Docker Image with Model Definition |
| 75 | + |
| 76 | +Kubernetes runs Docker containers, so we need to put user-defined models, |
| 77 | +the ElasticDL api package and all dependencies into a Docker image. |
| 78 | + |
| 79 | +In this tutorial, we use a complete program using a DeepFM estimator model of DeepCTR |
| 80 | +in the ElasticDL repository. To retrieve the source code, please run the following command. |
| 81 | + |
| 82 | +```bash |
| 83 | +git clone https://github.com/sql-machine-learning/elasticdl |
| 84 | +``` |
| 85 | + |
| 86 | +Complete codes are in directory [elasticdl/model_zoo/deepctr](https://github.com/sql-machine-learning/elasticdl/tree/develop/model_zoo/deepctr). |
| 87 | + |
| 88 | +We build the image based on tensorflow:1.13.2 and the dockerfile |
| 89 | +is |
| 90 | + |
| 91 | +```text |
| 92 | +FROM tensorflow/tensorflow:1.13.2-py3 as base |
| 93 | +
|
| 94 | +RUN pip install elasticdl_api |
| 95 | +RUN pip install deepctr |
| 96 | +
|
| 97 | +COPY ./model_zoo model_zoo |
| 98 | +``` |
| 99 | + |
| 100 | +Then, we use docker to build the image |
| 101 | + |
| 102 | +```bash |
| 103 | +docker build -t elasticdl:deepctr_estimator -f ${deepctr_dockerfile} . |
| 104 | +``` |
| 105 | + |
| 106 | +## Submit the Training Job |
| 107 | + |
| 108 | +The following command submits a training job: |
| 109 | + |
| 110 | +```bash |
| 111 | +elasticdl train \ |
| 112 | + --image_name=elasticdl/elasticdl:1.0.0 \ |
| 113 | + --worker_image=elasticdl:deepctr_estimator \ |
| 114 | + --ps_image=elasticdl:deepctr_estimator \ |
| 115 | + --job_command="python -m model_zoo.deepctr.deepfm_estimator --training_data=/data/criteo_sample.txt --validation_data=/data/criteo_sample.txt" \ |
| 116 | + --num_workers=1 \ |
| 117 | + --num_ps=1 \ |
| 118 | + --num_evaluator=1 \ |
| 119 | + --master_resource_request="cpu=0.2,memory=1024Mi" \ |
| 120 | + --master_resource_limit="cpu=1,memory=2048Mi" \ |
| 121 | + --ps_resource_request="cpu=0.2,memory=1024Mi" \ |
| 122 | + --ps_resource_limit="cpu=1,memory=2048Mi" \ |
| 123 | + --worker_resource_request="cpu=0.3,memory=1024Mi" \ |
| 124 | + --worker_resource_limit="cpu=1,memory=2048Mi" \ |
| 125 | + --chief_resource_request="cpu=0.3,memory=1024Mi" \ |
| 126 | + --chief_resource_limit="cpu=1,memory=2048Mi" \ |
| 127 | + --evaluator_resource_request="cpu=0.3,memory=1024Mi" \ |
| 128 | + --evaluator_resource_limit="cpu=1,memory=2048Mi" \ |
| 129 | + --job_name=test-deepfm-estimator \ |
| 130 | + --distribution_strategy=ParameterServerStrategy \ |
| 131 | + --need_tf_config=true \ |
| 132 | + --volume="host_path={criteo_data_path},mount_path=/data" \ |
| 133 | +``` |
| 134 | + |
| 135 | +`--image_name` is the image to launch the ElasticDL master which |
| 136 | +has nothing to do with the estimator model. The ElasticDL master is |
| 137 | +responsible for launching pod and assigning data shards to workers with |
| 138 | +elasticity and fault-tolerance. |
| 139 | + |
| 140 | +`{criteo_data_path}` is the absolute path of the `./data` with `criteo_sample.txt`. |
| 141 | +Here, the option `--volume="host_path={criteo_data_path},mount_path=/data"` |
| 142 | +bind mount it into the containers/pods. |
| 143 | + |
| 144 | +The option `--num_workers=1` tells the master to start a worker pod. |
| 145 | +The option `--num_ps=1` tells the master to start a ps pod. |
| 146 | +The option `--num_evaluator=1` tells the master to start an evaluator pod. |
| 147 | + |
| 148 | +And the master will start a chief worker for a TensorFlow estimator model by default. |
| 149 | + |
| 150 | +### Check Job Status |
| 151 | + |
| 152 | +After the job submission, we can run the command `kubectl get pods` to list |
| 153 | +related containers. |
| 154 | + |
| 155 | +```bash |
| 156 | +NAME READY STATUS RESTARTS AGE |
| 157 | +elasticdl-test-deepctr-estimator-master 1/1 Running 0 9s |
| 158 | +test-deepctr-estimator-edljob-chief-0 1/1 Running 0 6s |
| 159 | +test-deepctr-estimator-edljob-evaluator-0 0/1 Pending 0 6s |
| 160 | +test-deepctr-estimator-edljob-ps-0 1/1 Running 0 7s |
| 161 | +test-deepctr-estimator-edljob-worker-0 1/1 Running 0 6s |
| 162 | +``` |
| 163 | + |
| 164 | +We can view the log of workers by `kubectl logs test-deepctr-estimator-edljob-chief-0`. |
| 165 | + |
| 166 | +```text |
| 167 | +INFO:tensorflow:global_step/sec: 4.84156 |
| 168 | +INFO:tensorflow:global_step/sec: 4.84156 |
| 169 | +INFO:tensorflow:Saving checkpoints for 203 into /data/ckpts/model.ckpt. |
| 170 | +INFO:tensorflow:Saving checkpoints for 203 into /data/ckpts/model.ckpt. |
| 171 | +INFO:tensorflow:global_step/sec: 7.05433 |
| 172 | +INFO:tensorflow:global_step/sec: 7.05433 |
| 173 | +``` |
0 commit comments