MPI Tools
Core MPI Utilities
Module used for managing MPI processes.
- stable_learning_control.utils.mpi_utils.mpi_tools.mpi_fork(n, bind_to_core=False)[source]
Re-launches the current script with workers linked by MPI.
Also, terminates the original process that launched it.
Taken almost without modification from the Baselines function of the same name.
- stable_learning_control.utils.mpi_utils.mpi_tools.msg(m, string='')[source]
Send message from one MPI process to the other.
- stable_learning_control.utils.mpi_utils.mpi_tools.pprint(input_str='', end='\n', comm=<MagicMock name='mock.COMM_WORLD' id='140339105640512'>)[source]
Print for MPI parallel programs: Only rank
0
printsstr
.
- stable_learning_control.utils.mpi_utils.mpi_tools.allreduce(*args, **kwargs)[source]
Reduced results of a operation across all processes.
- Parameters:
*args – All args to pass to thunk.
**kwargs – All kwargs to pass to thunk.
- Returns:
Result object.
- Return type:
- stable_learning_control.utils.mpi_utils.mpi_tools.num_procs()[source]
Count active MPI processes.
- Returns:
The number of mpi processes.
- Return type:
- stable_learning_control.utils.mpi_utils.mpi_tools.broadcast(x, root=0)[source]
Broadcast variable to other MPI processes.
- stable_learning_control.utils.mpi_utils.mpi_tools.mpi_sum(x)[source]
Take the sum of a scalar or vector over MPI processes.
- stable_learning_control.utils.mpi_utils.mpi_tools.mpi_avg(x)[source]
Average a scalar or vector over MPI processes.
MPI + PyTorch Utilities
stable_learning_control.utils.mpi_utils.mpi_pytorch
contains a few tools to make it easy to do
data-parallel PyTorch optimization across MPI processes. The two main ingredients are syncing parameters and
averaging gradients before the adaptive optimizer uses them. Also, there’s a hacky fix for a problem
where the PyTorch instance in each separate process tries to get too many threads, and they start to destroy
each other.
The pattern for using these tools looks something like this:
At the beginning of the training script, call
setup_pytorch_for_mpi()
. (Avoids clobbering problem.)After you’ve constructed a PyTorch module, call
sync_params(module)
.Then, during gradient descent, call
mpi_avg_grads
after the backward pass, like so:
optimizer.zero_grad()
loss = compute_loss(module)
loss.backward()
mpi_avg_grads(module) # averages gradient buffers across MPI processes!
optimizer.step()
Helper methods for managing Pytorch MPI processes.
Note
This module is not yet used in any of the current algorithms, but is kept here for future reference.
- stable_learning_control.utils.mpi_utils.mpi_pytorch.setup_pytorch_for_mpi()[source]
Avoid slowdowns caused by each separate process’s PyTorch using more than its fair share of CPU resources.
MPI + TensorFlow Utilities
Todo
Tools to make it easy to do data-parallel TensorFlow 2.x optimization across MPI processes still need to be implemented.