<p>distributed training is model and framework specific. Not all models are easy to distribute, and from ML framework to ML framework things are not equally easy. <strong>It is rarely automatic, even less so with TensorFlow and Keras.</strong></p>

<p>Neural nets are conceptually easy to distribute under the data-parallel paradigm, whereby the gradient computation of a given mini-batch is split among workers, which could be multiple devices in the same host (multi-device) or multiple hosts with each multiple devices (multi-device multi-host). The D2L.ai course provides an in-depth view of how neural nets are distributed <a href="http://d2l.ai/chapter_computational-performance/multiple-gpus.html" rel="nofollow noreferrer">here</a> and <a href="http://d2l.ai/chapter_computational-performance/parameterserver.html" rel="nofollow noreferrer">here</a>.</p>

<p>Keras used to be trivial to distribute in multi-device, single host fashion with the <a href="https://www.tensorflow.org/api_docs/python/tf/keras/utils/multi_gpu_model" rel="nofollow noreferrer"><code>multi_gpu_model</code>, which will sadly get deprecated in 4 months</a>. In your case, you seem to refer to multi-host model (more than one machine), and that requires writing ad-hoc synchronization code such as the one seen in <a href="https://www.tensorflow.org/tutorials/distribute/multi_worker_with_keras" rel="nofollow noreferrer">this official tutorial</a>. </p>

<p>Now let's look at how does this relate to SageMaker.</p>

<p>SageMaker comes with 3 options for algorithm development. Using distributed training may require a varying amount of custom work depending on the option you choose:</p>

<ol>
<li><p>The <strong>built-in algorithms</strong> is a library of 18 pre-written algorithms. Many of them are written to be distributed in single-host multi-GPU or multi-GPU multi-host. With that first option, you don't have anything to do apart from setting <code>train_instance_count</code> > 1 to distribute over multiple instances</p></li>
<li><p>The <strong>Framework containers</strong> (the option you are using) are containers developed for popular frameworks (TensorFlow, PyTorch, Sklearn, MXNet) and provide pre-written docker environment in which you can write arbitrary code. In this options, some container will support one-click creation of ephemeral training clusters to do distributed training, <strong>however using <code>train_instance_count</code> greater than one is not enough to distribute the training of your model. It will just run your script on multiple machines. In order to distribute your training, you must write appropriate distribution and synchronization code in your <code>mnist_keras_tf.py</code> script.</strong> For some frameworks such code modification will be very simple, for example for TensorFlow and Keras, SageMaker comes with Horovod pre-installed. Horovod is a peer-to-peer ring-style communication mechanism that requires very little code modification and is highly scalable (<a href="https://eng.uber.com/horovod/" rel="nofollow noreferrer">initial annoucement from Uber</a>, <a href="https://sagemaker.readthedocs.io/en/stable/using_tf.html#training-with-horovod" rel="nofollow noreferrer">SageMaker doc</a>, <a href="https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/tensorflow_script_mode_horovod/tensorflow_script_mode_horovod.ipynb" rel="nofollow noreferrer">SageMaker example</a>, <a href="https://aws.amazon.com/fr/blogs/machine-learning/launching-tensorflow-distributed-training-easily-with-horovod-or-parameter-servers-in-amazon-sagemaker/" rel="nofollow noreferrer">SageMaker blog post</a>). My recommendation would be to try using Horovod to distribute your code. Similarly, in Apache MXNet you can easily create Parameter Stores to host model parameters in a distributed fashion and sync with them from multiple nodes. MXNet scalability and ease of distribution is one of the reason Amazon loves it.</p></li>
<li><p>The <strong>Bring-Your-Own Container</strong> requires you to write both docker container and algorithm code. In this situation, you can of course distribute your training over multiple machines but you also have to write machine-to-machine communication code</p></li>
</ol>

<p>For your specific situation my recommendation would be to scale horizontally first in a single node with multiple GPUs over bigger and bigger machine types, because latency and complexity increase drastically as you switch from single-host to multi-host context. If truly necessary, use multi-node context and things maybe easier if that's done with Horovod.
In any case, things are still much easier to do with SageMaker since it manages creation of ephemeral, billed-per-second clusters with built-in, logging and metadata and artifacts persistence and also handles fast training data loading from s3, sharded over training nodes. </p>

<p><strong>Note on the relevancy of distributed training</strong>: Keep in mind that when you distribute over N devices a model that was running fine on one device, you usually grow the batch size by N so that the per-device batch size stays constant and each device keeps being busy. This will disturb your model convergence, because bigger batches means a less noisy SGD. A common heuristic is to grow the learning rate by N (more info in <a href="https://arxiv.org/abs/1706.02677" rel="nofollow noreferrer">this great paper from Priya Goyal et al</a>), but this on the other hand induces instability at the first couple epochs, so it is sometimes associated with a learning rate warmup. Scaling SGD to work well with very large batches is still an active research problem, with new ideas coming up frequently. Reaching good model performance with very large batches sometimes require ad-hoc research and a fair amount of parameter tuning, occasionally to the extent where the extra money spent on finding how to distribute well overcome the benefits of the faster training you eventually manage to run. A situation where distributed training makes sense is when an individual record represent too much compute to form a big enough physical batch on a device, a situation seen on big input sizes (eg vision over HD pictures) or big parameter counts (eg BERT). That being said, for those models requiring very big logical batch you don't necessarily have to distribute things physically: you can run sequentially N batches through your single GPU and wait N per-device batches before doing the gradient averaging and parameter update to simulate having an N times bigger GPU. (a clever hack sometimes called <em>gradient accumulation</em>)</p>
sagemaker horizontally scaling tensorflow (keras) model
<p>I am roughly following this script <a href="https://gitlab.com/juliensimon/dlnotebooks/blob/master/keras/05-keras-blog-post/Fashion%20MNIST-SageMaker.ipynb" rel="nofollow noreferrer">fashion-MNIST-sagemaker</a>.</p>

<p>I see that in the notebook </p>

<pre><code>from sagemaker.tensorflow import TensorFlow

tf_estimator = TensorFlow(entry_point='mnist_keras_tf.py', 
                          role=role,
                          train_instance_count=1, 
                          train_instance_type='local',
                          framework_version='1.12', 
                          py_version='py3',
                          script_mode=True,
                          hyperparameters={'epochs': 1}
                         )
</code></pre>

<p>I am wondering to what extent I can and should use the <code>train_instance_count</code> parameter. Will it distribute training along some dimension automatically, if yes - what is the dimension?</p>

<p>Further, does it generally make sense to distribute training horizontally in a keras (with tensorflow) based setting?</p>