TensorFlow
Horovod
NCCL
MPI
Distributed Machine Learning

TensorFlow Horovod NCCL and MPI

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Horovod, NCCL, and MPI solve different parts of distributed TensorFlow training. Horovod is the training framework integration, NCCL is the high-performance GPU communication library for collective operations, and MPI is commonly used to launch and coordinate distributed processes.

The Role of Each Piece

The easiest way to keep the stack straight is to separate responsibilities:

  • Horovod integrates distributed training into TensorFlow code.
  • NCCL performs fast multi-GPU collectives such as all-reduce.
  • MPI often starts the processes and provides process-level coordination.

These technologies are complementary, not competing layers.

How Horovod Fits into TensorFlow

Horovod wraps the optimizer and coordinates gradient aggregation across workers. The code changes are intentionally small.

python
1import tensorflow as tf
2import horovod.tensorflow.keras as hvd
3
4hvd.init()
5
6gpus = tf.config.list_physical_devices("GPU")
7if gpus:
8    tf.config.set_visible_devices(gpus[hvd.local_rank()], "GPU")
9
10model = tf.keras.Sequential([
11    tf.keras.layers.Dense(128, activation="relu"),
12    tf.keras.layers.Dense(10, activation="softmax")
13])
14
15optimizer = tf.keras.optimizers.Adam(0.001 * hvd.size())
16optimizer = hvd.DistributedOptimizer(optimizer)
17
18model.compile(
19    optimizer=optimizer,
20    loss="sparse_categorical_crossentropy",
21    metrics=["accuracy"]
22)

In practice, each worker trains on its local batch, then Horovod synchronizes gradients across workers.

Where NCCL Comes In

On GPU systems, Horovod typically uses NCCL for collective GPU communication because it is optimized for bandwidth-heavy operations such as all-reduce.

Gradient synchronization is the expensive part of data-parallel training, so NCCL matters because it reduces the communication overhead between GPUs.

In simple terms:

  • TensorFlow computes gradients
  • Horovod orchestrates distributed reduction
  • NCCL moves those GPU tensors efficiently between workers and devices

That is why NCCL is usually the preferred backend for GPU-heavy Horovod jobs.

Where MPI Comes In

MPI is often the process launcher and coordination layer. A common launch pattern is:

bash
mpirun -np 4 python train.py

That starts four worker processes. Horovod reads MPI-provided rank information during hvd.init() so each worker knows its global rank and local rank.

Some environments use horovodrun, which can rely on MPI or another controller depending on configuration, but the underlying idea is the same: each process becomes one Horovod worker.

A Typical Multi-GPU Workflow

A standard Horovod workflow usually includes these steps:

  1. Initialize Horovod with hvd.init().
  2. Pin each process to a single GPU with hvd.local_rank().
  3. Scale or tune the learning rate based on worker count.
  4. Wrap the optimizer with hvd.DistributedOptimizer.
  5. Broadcast initial variable states from rank zero.

The broadcast step is easy to miss:

python
1callbacks = [
2    hvd.callbacks.BroadcastGlobalVariablesCallback(0)
3]
4
5model.fit(dataset, epochs=10, callbacks=callbacks)

Without it, workers may begin with inconsistent initial weights.

NCCL Versus MPI Is the Wrong Framing

A common source of confusion is treating NCCL and MPI as alternatives. In Horovod-based GPU training, they often work together.

MPI handles process launch and distributed process metadata. NCCL handles the high-performance GPU collectives. Horovod sits above both and exposes a TensorFlow-friendly programming model.

If NCCL is unavailable, Horovod may fall back to another communication path depending on how it was built, but GPU performance is usually best when NCCL is available and working.

Common Pitfalls

A common mistake is forgetting to pin each worker to a single GPU. That can cause multiple workers to fight over the same device.

Another pitfall is leaving the learning rate unchanged when scaling to many workers. Training behavior often changes with global batch size.

Version mismatches are also common. Horovod, TensorFlow, CUDA, NCCL, and MPI all need to be built and installed compatibly.

Finally, communication libraries do not fix poor input pipelines. If the dataset loader is slow, distributed training still stalls.

Summary

  • Horovod integrates distributed training into TensorFlow.
  • NCCL handles fast GPU collective communication, especially all-reduce.
  • MPI commonly launches and coordinates worker processes.
  • In GPU training, NCCL and MPI usually complement each other rather than replace each other.
  • Correct GPU pinning, optimizer wrapping, and variable broadcast are core parts of a working Horovod setup.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.