ThomasDelteil commented on a change in pull request #15158: [TUTORIAL] Add 
multiple GPUs training tutorial
URL: https://github.com/apache/incubator-mxnet/pull/15158#discussion_r293580240
 
 

 ##########
 File path: docs/tutorials/gluon/multi_gpu.md
 ##########
 @@ -0,0 +1,190 @@
+<!--- Licensed to the Apache Software Foundation (ASF) under one -->
+<!--- or more contributor license agreements.  See the NOTICE file -->
+<!--- distributed with this work for additional information -->
+<!--- regarding copyright ownership.  The ASF licenses this file -->
+<!--- to you under the Apache License, Version 2.0 (the -->
+<!--- "License"); you may not use this file except in compliance -->
+<!--- with the License.  You may obtain a copy of the License at -->
+
+<!---   http://www.apache.org/licenses/LICENSE-2.0 -->
+
+<!--- Unless required by applicable law or agreed to in writing, -->
+<!--- software distributed under the License is distributed on an -->
+<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
+<!--- KIND, either express or implied.  See the License for the -->
+<!--- specific language governing permissions and limitations -->
+<!--- under the License. -->
+
+# Multiple GPUs training with Gluon API
+
+In this tutorial we will walk through how one can train deep learning neural 
networks on multiple GPUs within a single machine. This tutorial focuses on 
data parallelism as opposed to model parallelism. Data parallelism approach 
assumes, that you can fit whole your model in a GPU and only training data 
needs to be partitioned. This is different from model parallelism, where the 
model is so big, that it doesn't fit into a single GPU, so it needs to be 
partitioned as well. Model parallelism is not supported by Apache MXNet out of 
the box, and one has to manually route the data among different devices to 
achieve model parallelism. Check out [model parallelism 
tutorial](https://mxnet.incubator.apache.org/versions/master/faq/model_parallel_lstm.html)
 to learn more about it.
+Here we will focus on implementing data parallel training for a convolutional 
neural network called LeNet.
+
+## Prerequisites
+
+- Two or more GPUs 
+- CUDA 9 or higher
+- cuDNN v7 or higher
+- Knowledge of how to train a model using Gluon API
+
+## Storing data on GPU
+
+The basic primitive in Apache MXNet to specify a tensor is 
[NDArray](https://mxnet.incubator.apache.org/api/python/ndarray/sparse.html#module-mxnet.ndarray).
 When you create NDArray you have to provide the context - the device where 
this tensor is going to be stored. The context can be either CPU or GPU and 
both can be indexed: if your machine has multiple GPUs, you can provide an 
index to specify which GPU to use. By default, CPU context is used, and that 
means that the tensor will live in main RAM. Below is an example how to create 
two tensors where one is stored on the first GPU and the second is stored on 
the second GPU.
+
+```python
+import mxnet as mx
+
+a = mx.nd.array([1, 2, 3], ctx=mx.gpu(0))
+b = mx.nd.array([5, 6, 7], ctx=mx.gpu(1))
+```
+
+The next step would be to do operations on these 2 NDArrays. But, 
unfortunately, if we try to do any operation involved both these arrays, Apache 
MXNet will return an error: `Check failed: e == cudaSuccess CUDA: an illegal 
memory access was encountered`. This error is returned because we tried to use 
arrays that are stored on different contexts: Apache MXNet wants users to 
explicitly control memory allocation and doesn't automatically copy data 
between GPUs. If we want to do an operation on these arrays we have to have 
them in the same GPU. The result of the operation is going to be also stored on 
that GPU as well.
+
+We can manually copy data between GPUs using [as_in_context 
method](https://mxnet.incubator.apache.org/api/python/ndarray/ndarray.html?#mxnet.ndarray.NDArray.as_in_context).
 We can get the current context of an NDArray via [context 
property](https://mxnet.incubator.apache.org/api/python/ndarray/ndarray.html?#mxnet.ndarray.NDArray.context).
+
+```python
+c = a + b.as_in_context(a.context)
+```
+
+Using this example, we have learnt that we can perform operations with 
NDArrays only if they are stored on the same GPU. So, how can we split the data 
between GPUs, but use the same model for training? We will answer this question 
in the next section.
+
+## Storing the network on multiple GPUs
+
+When you create a network using 
[Blocks](https://mxnet.incubator.apache.org/api/python/gluon/gluon.html#mxnet.gluon.Block)
 the parameters of blocks are also stored in NDArrays. When you initialize your 
network, you have to specify which context you are going to use for the 
underlying NDArrays. The feature of the [initialize 
method](https://mxnet.incubator.apache.org/api/python/gluon/gluon.html#mxnet.gluon.Block.initialize)
 is that it can accept the list of contexts, meaning that you can provide more 
than one context to store underlying parameters. In the example below, we 
create the LeNet network and initialize it to be stored on GPU(0) and GPU(1) 
simultaneously. Each GPU will receive its own copy of the parameters:
+
+```python
+from mxnet import init
+from mxnet.gluon import nn
+
+context = [mx.gpu(0), mx.gpu(1)]
 
 Review comment:
   can you make this a 
   ```
   n_gpu = mx.context.num_gpus()
   context = [mx.gpu(0), mx.gpu(1)] if n_gpu >= 2 else [mx.gpu(), mx.gpu()] if 
n_gpu == 1 else [mx.cpu(), mx.cpu()]
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to