Skip to content

Bluefog-Lib/bluefog

Repository files navigation

BlueFog

License https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat

Logo

BlueFog is a high-performance distributed training framework built with decentralized optimization algorithms. The goal of Bluefog is to make decentralized algorithms easy to use, fault-tolerant, friendly to heterogeneous environment, and even faster than training frameworks built with parameter server, or ring-allreduce.

Performance

Below are the charts representing the performance of BlueFog that was done on ResNet50 benchmark. Each machine has 8 V100 GPUs (64GB memory) with NVLink-enabled and the inter-connected communication speed is 25Gbps. This is the same hardware setup you can get on AWS. We test the scaling efficiency with a batch size of 64 for a computationally intensive scenario, and a batch size of 32 for a communicationally intensive scenario.

Benchmark 1Benchmark 2

In the figures, the black box represents the ideal linear scaling. It is observed that Bluefog can achieve over 95% scaling efficiency while Horovod reaches around 66% sacling efficiency with batch size 64 on 128 GPUs. For the communicationally intensive scenario with batch size 32, the scaling efficiency gap between Bluefog and Horovod becomes even larger. To understand more details about the BlueFog benchmark, checkout our performance page.

Overview

BlueFog is built with decentralized optimization algorithms. This is fundamentally different from other popular distributed training frameworks, such as DistributedDataParallel provided by PyTorch, Horovod, BytePS, etc.

In each communication stage, neither the typical star-shaped parameter-server toplogy, nor the pipelined ring-allreduce topology is used. Instead, BlueFog will exploit a virtual and probably dynamic network topology (that can be in any shape) to achieve most communication efficiency.

Main Idea: Replace expensive allreduce averaging over gradients by cheap neighbor averaging over parameters

For each training iteration, one process (or agent) will update its model with information received from its direct neighbors defined by the virtual topology. It is observed all communications only occur over the predefied virtual topolgy and no global communication is required. This is why the algorithms is named decentralized. Decentralized training algorithms are proved in literature that it can converge to the same solution as their standard centralized counterparts.

The topology decides the communication efficiency. BlueFog supports both static topology and dynamic topology usages. After tremendous trials, the dynamic Exponential-2 graph is observed to achieve the best performance if the number of agents is the power of 2, such as 4, 32, 128 agents. In Exponential-2 graph, each agent will communicates with the neighbors that are 2 0, 2 1, ..., 2 t hops away. Dynamic toplogy means all agents select one neighbor only in one iteration and select next neighbor in next iteration as illustrated in the following figure:

one-peer-exp2

In this scenario, the communcation cost for each iteration is only one unit delay, one standard parameter size to transmit and no communication conflict happens, which is better than what parameter server or ring-allreduce promises. As for loss and accuracy guarantees, please check out our theoratical paper and our slides preseneted on MLA'20. [A full tutorial will be added in future].

Quick Start

First, make sure your environment is with python>=3.7 and openmpi >= 4.0. Then, install Bluefog with: pip install --no-cache-dir bluefog or BLUEFOG_WITH_NCCL=1 pip install bluefog if NCCL is supported (NCCL>=2.7). Check the install_bluefog page if you need more information or other install options.

Using BlueFog With Jupyter Notebook

BlueFog is able to run interactively with Jupyte Notebook. Please check out our hello world notebook or other notebooks in the example folder to start with. Interacitve BlueFog is great for research and algorithm experiment. For the large-scale machine learning problem, we recommand to use BlueFog with script.

Using BlueFog With Script

We provide high-level wrapper for torch optimizer. You just need to modify the existing script to distributed implementation is wrapping the optimizer with our DistributedNeighborAllreduceOptimizer, then run it through bfrun. That is it!

# Execute Python functions in parallel through
# bfrun -np 4 python file.py

import torch
import bluefog.torch as bf
...
bf.init()
optimizer = optim.SGD(model.parameters(), lr=lr * bf.size())
optimizer = bf.DistributedNeighborAllreduceOptimizer(
   optimizer, model=model
)
...

Previous example is for static topology usage. For dynamic topology case, you need a little bit more code:

from bluefog.common import topology_util
...
# Same setup code as previous snippets
dynamic_neighbors_gen = topology_util.GetInnerOuterExpo2DynamicSendRecvRanks(
          bf.size(), local_size=bf.local_size(), self_rank=bf.rank())
def dynamic_topology_update(epoch, batch_idx):
  send_neighbors, recv_neighbors = next(dynamic_neighbors_gen)
  avg_weight = 1/(len(recv_neighbors) + 1)
  optimizer.send_neighbors = to_neighbors
  optimizer.neighbor_weights = {r: avg_weight for r in recv_neighbors}
  optimizer.self_weight = avg_weight

# Torch training code
for epoch in range(epochs):
  for batch_idx, (data, target) in enumerate(train_loader):
      dynamic_topology_update(epoch, batch_idx)
      ...
      loss.backward()
      optimizer.step()

Check our BlueFog dynamic topology neighbor averaging page to see more on how to control and use topology. See BlueFog examples folder for full code.

We also provide lots of low-level functions, which you can use those as building blocks to construct your own distributed training algorithm. The following example illustrates how to run a simple consensus algorithm through bluefog.

import torch
import bluefog.torch as bf

bf.init()
x = torch.Tensor([bf.rank()])
for _ in range(100):
   x = bf.neighbor_allreduce(x)
print(f"{bf.rank()}: Average value of all ranks is {x}")

Checkout our API explanation page to see all supported synchronous and asynchronous features.

The Bluefog source code was based off Horovod repository. Hence, BlueFog shared lots of common features from Horovod such as timeline, tensor-fusion, etc. Here, we want to express our gratitude to the Horovod team.

Materials

Bluefog: Make decentralized algorithms practical for optimization and deep learning. B. Ying, K. Yuan, H. Hu, Y. Chen, and W. Yin. arXiv preprint arXiv:2111.04287, 2021. [link]

Faster Learning over Networks and BlueFog, BlueFog Team, invited talk at MLA, 2020 [slides]

Cite

Bluefog is uploaded to Zenodo. An equivalent BibTex format reference is below for all the versions:

% System paper
@article{bluefog,
  author       = {Ying, Bicheng and Yuan, Kun and Hu, Hanbin and Chen, Yiming and Yin, Wotao},
  title        = {BlueFog: Make Decentralized Algorithms Practical for Optimization and Deep Learning},
  journal.     = {arXiv preprint arXiv:2111.04287},
  year         = {2021},
}

% Theoratical Papers
@article{ying2021exponential,
  title={Exponential Graph is Provably Efficient for Decentralized Deep Training},
  author={Ying, Bicheng and Yuan, Kun and Chen, Yiming and Hu, Hanbin and Pan, Pan and Yin, Wotao},
  journal={Advances in Neural Information Processing Systems (NeurIPS), 34.
           Also available at arXiv:2110.13363},
  year={2021}
}

@inproceedings{yuan2021decentlam,
   title={DecentLaM: Decentralized Momentum SGD for Large-Batch Deep Training},
   author={Yuan, Kun and Chen, Yiming and Huang, Xinmeng and Zhang, Yingya and Pan, Pan and Xu, Yinghui and Yin, Wotao},
   booktitle={Proceedings of the IEEE/CVF International Conference on Computer Vision},
   pages={3029--3039},
   year={2021}
}

@article{yuan2020influence,
   title={On the influence of bias-correction on distributed stochastic optimization},
   author={Yuan, Kun and Alghunaim, Sulaiman A and Ying, Bicheng and Sayed, Ali H},
   journal={IEEE Transactions on Signal Processing},
   volume={68},
   pages={4352--4367},
   year={2020},
   publisher={IEEE}
}

Troubleshooting

Import bluefog.torch failed

If you see the error message below, it means that bluefog is not installed properly. Please install bluefog using github source and recompile bluefog (e.g. make clean && make -j $(nproc) && BLUEFOG_WITH_NCCL=1 pip install .)

import bluefog.torch as bf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/dist-packages/bluefog/torch/__init__.py", line 34, in <module>
    from bluefog.torch.mpi_ops import init, shutdown
File "/usr/local/lib/python3.7/dist-packages/bluefog/torch/mpi_ops.py", line 23, in <module>
    from bluefog.torch import mpi_lib  # C library
ImportError: /usr/local/lib/python3.7/dist-packages/bluefog/torch/mpi_lib.cpython-37m-x86_64-linux-gnu.so: undefined symbol: _ZN7bluefog6common14NCCLController9AllreduceERNS0_16TensorTableEntryE