GuideOpen

Chapter 20: Logger, MPI Tools and Run Utils

The three utility modules are the only code shared between algorithms — logging (which is also model saving and config recording), MPI data-parallelism, and ExperimentGrid — and each has a small fixed usage pattern worth memorizing.

Chapter 20: Logger, MPI Tools and Run Utils

Core Idea

The three utility modules are the only code shared between algorithms — logging (which is also model saving and config recording), MPI data-parallelism, and ExperimentGrid — and each has a small fixed usage pattern worth memorizing.

Frameworks Introduced

  • Logger and EpochLogger. Logger carries the basic functionality: saving

diagnostics, hyperparameter configurations, the state of a training run, and the trained model. EpochLogger adds a thin layer that makes it easy to track the average, standard deviation, min and max of a diagnostic over each epoch and across MPI workers. All Spinning Up algorithm implementations use an EpochLogger.

  • The three-call pattern: store accumulates values into internal state,

log_tabular computes the statistics over everything stored, dump_tabular writes to file and stdout. The internal state is wiped clean after log_tabular, to prevent leakage into the next epoch's statistics.

  • The MPI + PyTorch pattern — three steps, in this order:
  1. At the beginning of the training script, call setup_pytorch_for_mpi(). This is the

fix for a real problem: each separate process's PyTorch instance tries to grab too many threads and they clobber each other.

  1. After constructing a PyTorch module, call sync_params(module).
  2. During gradient descent, call mpi_avg_grads after the backward pass and before the

optimizer step.

  • The two main ingredients are therefore syncing parameters and **averaging gradients

before they are used by the adaptive optimizer** — the order matters, because averaging after the optimizer step would give each worker a different adaptive state.

  • ExperimentGrid — a tool for hyperparameter ablations, based on but simpler than rllab's

VariantGenerator. eg.add(param_name, values, shorthand, in_name) then eg.run(thunk, **run_kwargs); ExperimentGrid.run uses call_experiment to launch the thunk. See ch4 for the full usage.

Code Examples

The EpochLogger statistic pattern:

from spinup.utils.logx import EpochLogger
epoch_logger = EpochLogger()
for i in range(10):
    epoch_logger.store(Test=i)
epoch_logger.log_tabular('Test', with_min_and_max=True)
epoch_logger.dump_tabular()
-------------------------------------
|     AverageTest |             4.5 |
|         StdTest |            2.87 |
|         MaxTest |               9 |
|         MinTest |               0 |
-------------------------------------

The MPI gradient-averaging step, in place:

optimizer.zero_grad()
loss = compute_loss(module)
loss.backward()
mpi_avg_grads(module)   # averages gradient buffers across MPI processes
optimizer.step()

Reference Tables

ModuleContents
spinup.utils.logxLogger, EpochLogger
spinup.utils.mpi_toolsCore MPI utilities
spinup.utils.mpi_pytorchsetup_pytorch_for_mpi, sync_params, mpi_avg_grads
spinup.utils.mpi_tfAdamOptimizer across MPI processes. Explicitly "a bit hacky" — for something more sophisticated and general-purpose, the docs point to horovod
spinup.utils.run_utilsExperimentGrid, call_experiment, setup_logger_kwargs
spinup.utils.plotThe plotter (documented in ch6)

Anti-patterns

  • Calling mpi_avg_grads after optimizer.step(). The averaging must happen before the

adaptive optimizer consumes the gradients, or workers diverge in optimizer state.

  • Skipping setup_pytorch_for_mpi(). The thread-clobbering problem it fixes is a real

performance failure, not a theoretical one.

  • Expecting log_tabular to be idempotent. It wipes the accumulated state; calling it

twice for the same key in one epoch gives you statistics over nothing.

  • Reaching for mpi_tf as a general distributed-training solution. The docs say to use

horovod instead if you need something serious.

Key Takeaways

  1. store / log_tabular / dump_tabular is the whole logger interface, and the state

resets on log_tabular.

  1. EpochLogger aggregates across MPI workers as well as across an epoch.
  2. MPI PyTorch is three calls in a fixed order: setup, sync params, average grads before step.
  3. The logger is also the model-saving and config-recording path (ch5).
  4. mpi_tf is acknowledged as hacky; horovod is the recommended alternative.

Connects To

  • Ch 4: ExperimentGrid usage and the CLI it backs.
  • Ch 5: the outputs the logger writes — progress.txt, config.json, the save directories.
  • Ch 6: the plotter, which reads what dump_tabular writes.
  • Ch 2: OpenMPI, the system dependency all of this rests on.

Source: https://github.com/alirezarezvani/claude-skills/blob/main/engineering/spinning-up-deep-rl/skills/spinning-up-deep-rl/chapters/ch20-logger-and-utilities.md

Author: alirezarezvani, Alireza Rezvani skills and agents. License: MIT.

Shown here with formatting for the web. Keep the author and license notice when you reuse it.

Related entries

There are more like this in the members library

The Codex library holds 7,009 entries. Members can search all of them by meaning and read every one in full.