|
14 | 14 | "metadata": {}, |
15 | 15 | "source": [ |
16 | 16 | "# Distributed PyTorch with Horovod\n", |
17 | | - "In this tutorial, you will train a PyTorch model on the [MNIST](http://yann.lecun.com/exdb/mnist/) dataset using distributed training via [Horovod](https://github.com/uber/horovod)." |
| 17 | + "In this tutorial, you will train a PyTorch model on the [MNIST](http://yann.lecun.com/exdb/mnist/) dataset using distributed training via [Horovod](https://github.com/uber/horovod) across a GPU cluster." |
18 | 18 | ] |
19 | 19 | }, |
20 | 20 | { |
21 | 21 | "cell_type": "markdown", |
22 | 22 | "metadata": {}, |
23 | 23 | "source": [ |
24 | 24 | "## Prerequisites\n", |
25 | | - "* Understand the [architecture and terms](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture) introduced by Azure Machine Learning (AML)\n", |
26 | | - "* Go through the [00.configuration.ipynb](https://github.com/Azure/MachineLearningNotebooks/blob/master/00.configuration.ipynb) notebook to:\n", |
27 | | - " * install the AML SDK\n", |
28 | | - " * create a workspace and its configuration file (`config.json`)\n", |
29 | | - "* Review the [tutorial](https://aka.ms/aml-notebook-pytorch) on single-node PyTorch training using the SDK" |
| 25 | + "* Go through the [Configuration](https://github.com/Azure/MachineLearningNotebooks/blob/master/configuration.ipynb) notebook to install the Azure Machine Learning Python SDK and create an Azure ML `Workspace`\n", |
| 26 | + "* Review the [tutorial](https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/training-with-deep-learning/train-hyperparameter-tune-deploy-with-pytorch/train-hyperparameter-tune-deploy-with-pytorch.ipynb) on single-node PyTorch training using Azure Machine Learning" |
30 | 27 | ] |
31 | 28 | }, |
32 | 29 | { |
|
92 | 89 | "cell_type": "markdown", |
93 | 90 | "metadata": {}, |
94 | 91 | "source": [ |
95 | | - "## Create a remote compute target\n", |
96 | | - "You will need to create a [compute target](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture#compute-target) to execute your training script on. In this tutorial, you create an `AmlCompute` cluster as your training compute resource. This code creates a cluster for you if it does not already exist in your workspace.\n", |
| 92 | + "## Create or attach existing AmlCompute\n", |
| 93 | + "You will need to create a [compute target](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture#compute-target) for training your model. In this tutorial, we use Azure ML managed compute ([AmlCompute](https://docs.microsoft.com/azure/machine-learning/service/how-to-set-up-training-targets#amlcompute)) for our remote training compute resource. Specifically, the below code creates an `STANDARD_NC6` GPU cluster that autoscales from `0` to `4` nodes.\n", |
97 | 94 | "\n", |
98 | | - "**Creation of the cluster takes approximately 5 minutes.** If the cluster is already in your workspace this code will skip the cluster creation process." |
| 95 | + "**Creation of AmlCompute takes approximately 5 minutes.** If the AmlCompute with that name is already in your workspace, this code will skip the creation process.\n", |
| 96 | + "\n", |
| 97 | + "As with other Azure services, there are limits on certain resources (e.g. AmlCompute) associated with the Azure Machine Learning service. Please read [this article](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-manage-quotas) on the default limits and how to request more quota." |
99 | 98 | ] |
100 | 99 | }, |
101 | 100 | { |
|
115 | 114 | " print('Found existing compute target.')\n", |
116 | 115 | "except ComputeTargetException:\n", |
117 | 116 | " print('Creating a new compute target...')\n", |
118 | | - " compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_NC6', \n", |
| 117 | + " compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_NC6',\n", |
119 | 118 | " max_nodes=4)\n", |
120 | 119 | "\n", |
121 | 120 | " # create the cluster\n", |
122 | 121 | " compute_target = ComputeTarget.create(ws, cluster_name, compute_config)\n", |
123 | 122 | "\n", |
124 | 123 | " compute_target.wait_for_completion(show_output=True)\n", |
125 | 124 | "\n", |
126 | | - "# Use the 'status' property to get a detailed status for the current cluster. \n", |
| 125 | + "# Use the 'status' property to get a detailed status for the current AmlCompute. \n", |
127 | 126 | "print(compute_target.status.serialize())" |
128 | 127 | ] |
129 | 128 | }, |
130 | 129 | { |
131 | 130 | "cell_type": "markdown", |
132 | 131 | "metadata": {}, |
133 | 132 | "source": [ |
134 | | - "The above code creates a GPU cluster. If you instead want to create a CPU cluster, provide a different VM size to the `vm_size` parameter, such as `STANDARD_D2_V2`." |
| 133 | + "The above code creates GPU compute. If you instead want to create CPU compute, provide a different VM size to the `vm_size` parameter, such as `STANDARD_D2_V2`." |
135 | 134 | ] |
136 | 135 | }, |
137 | 136 | { |
138 | 137 | "cell_type": "markdown", |
139 | 138 | "metadata": {}, |
140 | 139 | "source": [ |
141 | 140 | "## Train model on the remote compute\n", |
142 | | - "Now that we have the cluster ready to go, let's run our distributed training job." |
| 141 | + "Now that we have the AmlCompute ready to go, let's run our distributed training job." |
143 | 142 | ] |
144 | 143 | }, |
145 | 144 | { |
|
166 | 165 | "cell_type": "markdown", |
167 | 166 | "metadata": {}, |
168 | 167 | "source": [ |
169 | | - "Copy the training script `pytorch_horovod_mnist.py` into this project directory." |
| 168 | + "### Prepare training script\n", |
| 169 | + "Now you will need to create your training script. In this tutorial, the script for distributed training of MNIST is already provided for you at `pytorch_horovod_mnist.py`. In practice, you should be able to take any custom PyTorch training script as is and run it with Azure ML without having to modify your code.\n", |
| 170 | + "\n", |
| 171 | + "However, if you would like to use Azure ML's [metric logging](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture#logging) capabilities, you will have to add a small amount of Azure ML logic inside your training script. In this example, at each logging interval, we will log the loss for that minibatch to our Azure ML run.\n", |
| 172 | + "\n", |
| 173 | + "To do so, in `pytorch_horovod_mnist.py`, we will first access the Azure ML `Run` object within the script:\n", |
| 174 | + "```Python\n", |
| 175 | + "from azureml.core.run import Run\n", |
| 176 | + "run = Run.get_context()\n", |
| 177 | + "```\n", |
| 178 | + "Later within the script, we log the loss metric to our run:\n", |
| 179 | + "```Python\n", |
| 180 | + "run.log('loss', loss.item())\n", |
| 181 | + "```" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "markdown", |
| 186 | + "metadata": {}, |
| 187 | + "source": [ |
| 188 | + "Once your script is ready, copy the training script `pytorch_horovod_mnist.py` into the project directory." |
170 | 189 | ] |
171 | 190 | }, |
172 | 191 | { |
|
205 | 224 | "metadata": {}, |
206 | 225 | "source": [ |
207 | 226 | "### Create a PyTorch estimator\n", |
208 | | - "The AML SDK's PyTorch estimator enables you to easily submit PyTorch training jobs for both single-node and distributed runs. For more information on the PyTorch estimator, refer [here](https://docs.microsoft.com/azure/machine-learning/service/how-to-train-pytorch)." |
| 227 | + "The Azure ML SDK's PyTorch estimator enables you to easily submit PyTorch training jobs for both single-node and distributed runs. For more information on the PyTorch estimator, refer [here](https://docs.microsoft.com/azure/machine-learning/service/how-to-train-pytorch)." |
209 | 228 | ] |
210 | 229 | }, |
211 | 230 | { |
|
232 | 251 | "The above code specifies that we will run our training script on `2` nodes, with one worker per node. In order to execute a distributed run using MPI/Horovod, you must provide the argument `distributed_backend='mpi'`. Using this estimator with these settings, PyTorch, Horovod and their dependencies will be installed for you. However, if your script also uses other packages, make sure to install them via the `PyTorch` constructor's `pip_packages` or `conda_packages` parameters." |
233 | 252 | ] |
234 | 253 | }, |
| 254 | + { |
| 255 | + "cell_type": "markdown", |
| 256 | + "metadata": {}, |
| 257 | + "source": [ |
| 258 | + "To use the latest version of PyTorch 1.0, run the following cell:" |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": null, |
| 264 | + "metadata": {}, |
| 265 | + "outputs": [], |
| 266 | + "source": [ |
| 267 | + "estimator.conda_dependencies.remove_conda_package('pytorch=0.4.0')\n", |
| 268 | + "estimator.conda_dependencies.remove_pip_package('horovod==0.13.11')\n", |
| 269 | + "estimator.conda_dependencies.add_conda_package('pytorch-nightly')\n", |
| 270 | + "estimator.conda_dependencies.add_channel('pytorch')\n", |
| 271 | + "estimator.conda_dependencies.add_pip_package('horovod==0.15.2')" |
| 272 | + ] |
| 273 | + }, |
235 | 274 | { |
236 | 275 | "cell_type": "markdown", |
237 | 276 | "metadata": {}, |
|
255 | 294 | "metadata": {}, |
256 | 295 | "source": [ |
257 | 296 | "### Monitor your run\n", |
258 | | - "You can monitor the progress of the run with a Jupyter widget. Like the run submission, the widget is asynchronous and provides live updates every 10-15 seconds until the job completes." |
| 297 | + "You can monitor the progress of the run with a Jupyter widget. Like the run submission, the widget is asynchronous and provides live updates every 10-15 seconds until the job completes. You can see that the widget automatically plots and visualizes the loss metric that we logged to the Azure ML run." |
259 | 298 | ] |
260 | 299 | }, |
261 | 300 | { |
|
0 commit comments