horovod使用学习之一--hvd.init()
Posted lixiaolun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了horovod使用学习之一--hvd.init()相关的知识,希望对你有一定的参考价值。
horovod使用学习方式参考:https://github.com/uber/horovod#usage
To use Horovod, make the following additions to your program:
-
Run
hvd.init()
. -
Pin a server GPU to be used by this process using
config.gpu_options.visible_device_list
. With the typical setup of one GPU per process, this can be set to local rank. In that case, the first process on the server will be allocated the first GPU, second process will be allocated the second GPU and so forth. -
Scale the learning rate by number of workers. Effective batch size in synchronous distributed training is scaled by the number of workers. An increase in learning rate compensates for the increased batch size.
-
Wrap optimizer in
hvd.DistributedOptimizer
. The distributed optimizer delegates gradient computation to the original optimizer, averages gradients using allreduce or allgather, and then applies those averaged gradients. -
Add
hvd.BroadcastGlobalVariablesHook(0)
to broadcast initial variable states from rank 0 to all other processes. This is necessary to ensure consistent initialization of all workers when training is started with random weights or restored from a checkpoint. Alternatively, if you‘re not usingMonitoredTrainingSession
, you can simply execute thehvd.broadcast_global_variables
op after global variables have been initialized. -
Modify your code to save checkpoints only on worker 0 to prevent other workers from corrupting them. This can be accomplished by passing
checkpoint_dir=None
totf.train.MonitoredTrainingSession
ifhvd.rank() != 0
.
Example (see the examples directory for full training examples):
import tensorflow as tf import horovod.tensorflow as hvd # Initialize Horovod hvd.init() # Pin GPU to be used to process local rank (one GPU per process) config = tf.ConfigProto() config.gpu_options.visible_device_list = str(hvd.local_rank()) # Build model... loss = ... opt = tf.train.AdagradOptimizer(0.01 * hvd.size()) # Add Horovod Distributed Optimizer opt = hvd.DistributedOptimizer(opt) # Add hook to broadcast variables from rank 0 to all other processes during # initialization. hooks = [hvd.BroadcastGlobalVariablesHook(0)] # Make training operation train_op = opt.minimize(loss) # Save checkpoints only on worker 0 to prevent other workers from corrupting them. checkpoint_dir = ‘/tmp/train_logs‘ if hvd.rank() == 0 else None # The MonitoredTrainingSession takes care of session initialization, # restoring from a checkpoint, saving to a checkpoint, and closing when done # or an error occurs. with tf.train.MonitoredTrainingSession(checkpoint_dir=checkpoint_dir, config=config, hooks=hooks) as mon_sess: while not mon_sess.should_stop(): # Perform synchronous training. mon_sess.run(train_op)
第一步就是horovod的init操作,本节就重点学习hvd.init()过程。
1. hvd.init()调用horovod.common.__init__.py(代码)中的
def init(): """A function that initializes Horovod. """ return MPI_COMMON_LIB_CTYPES.horovod_init()
2. MPI_COMMON_LIB_CTYPES.horovod_init()调用horovod.common.operations.cc(代码)中的
void horovod_init() { InitializeHorovodOnce(); }
3.InitializeHorovodOnce()调用horovod.common.operations.cc(代码)中的
// Start Horovod background thread. Ensure that this is // only done once no matter how many times this function is called. void InitializeHorovodOnce() { // Ensure background thread is only started once. // initialize_flag是atomic_flag类型的,如果已经被设置为true,test_and_set()返回true,否则返回false if (!horovod_global.initialize_flag.test_and_set()) { // 启动后台线程 // 后台线程调用BackgroundThreadLoop()方法,参数为horovod_global的引用 horovod_global.background_thread = std::thread(BackgroundThreadLoop, std::ref(horovod_global)); } // Wait to ensure that the background thread has finished initializing MPI. // 初始化完MPI之后,initialization_done会在BackgroundThreadLoop()方法中被设置为true while (!horovod_global.initialization_done) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } }
以上是关于horovod使用学习之一--hvd.init()的主要内容,如果未能解决你的问题,请参考以下文章
[源码解析] 深度学习分布式训练框架 horovod --- 后台线程架构
LF DL的Horovod项目增加了对PySpark和Apache MXNet的支持以及其他功能,以加快培训速度
如何修复:horovod.run.common.util.network.NoValidAddressesFound
CoRR 2018 | Horovod: Fast and Easy Distributed Deep Learning in Tensorflow