Understanding PyTorch Distributed Checkpointing: The Role of FileSystemReader


Distributed Checkpointing in PyTorch

DCP is a technique that allows you to efficiently save and load large models across multiple processes (ranks) in a distributed training environment. It addresses the memory limitations of single machines when dealing with massive models.

torch.distributed.checkpoint

This function is the core of DCP in PyTorch. It facilitates saving and loading models in a distributed manner, handling the complexities of splitting and coordinating the checkpoint across multiple ranks.

torch.distributed.checkpoint.FileSystemReader

  • Collaboration with load_state_dict
    It works in conjunction with torch.load (or torch.nn.Module.load_state_dict) to reconstruct the model state from the distributed checkpoint files.
  • Reading from Storage
    It handles the low-level details of reading checkpoint data from the filesystem, ensuring efficient and reliable data access.
  • Coordinator and Follower
    A single instance of FileSystemReader can function as both a coordinator and a follower.
    • As a coordinator, it gathers information about the available checkpoint files from all participating ranks.
    • As a follower, it waits for instructions from the coordinator and reads specific checkpoint files as directed.

Key Points

  • Its role is transparent to the user, as torch.distributed.checkpoint handles the low-level details.
  • It's crucial for ensuring correct and efficient loading of distributed checkpoints.
  • FileSystemReader is an internal component of DCP, not typically used directly in your code.


import torch
import torch.distributed as dist
import torch.distributed.checkpoint as dcp

def load_checkpoint(model, checkpoint_path):
  """Loads a distributed checkpoint into the model.

  Args:
    model: The PyTorch model to load the checkpoint into.
    checkpoint_path: The path to the directory containing the distributed checkpoint files.
  """

  # Initialize a FileSystemReader instance
  reader = dcp.FileSystemReader(checkpoint_path)

  # Load the state dictionary from the distributed checkpoint files
  state_dict = dcp.load_state_dict(model, reader=reader)

  # Load the state dictionary into the model
  model.load_state_dict(state_dict)

# ... (rest of your training code)
  1. load_checkpoint Function
    This function takes a PyTorch model and the path to the checkpoint directory as arguments.
  2. FileSystemReader Instance
    We create an instance of FileSystemReader using the checkpoint_path. This tells it where to find the distributed checkpoint files on the filesystem.
  3. load_state_dict with reader
    The dcp.load_state_dict function is called. It internally uses the provided reader to coordinate reading the state dictionary fragments from the distributed checkpoint files.
  4. Load State Dict into Model
    The loaded state dictionary is then used to call model.load_state_dict(), which actually loads the parameters and optimizer states back into the model.

Key Points

  • The FileSystemReader handles the distributed coordination and file access for efficient loading.
  • User code primarily interacts with dcp.load_state_dict, which manages the FileSystemReader internally.

Remember, this is a simplified example for demonstration purposes. For complete distributed training setups, you'll likely use additional functionalities from torch.distributed and torch.distributed.checkpoint to manage things like process initialization, communication, and saving checkpoints.



  • Integration with DCP
    It's tightly integrated with torch.distributed.checkpoint and uses low-level communication mechanisms specific to that API. A replacement would need to understand the underlying DCP communication protocols.
  • Distributed Coordination
    FileSystemReader handles the distributed aspects of reading checkpoint files. It ensures all ranks collaborate to gather information about available files and efficiently load the correct pieces. Replacing it would require implementing your own distributed coordination logic.

However, if you're looking for more control over reading data from the filesystem in a distributed setting, you might consider these options:

  1. Custom Reader with Communication Library
    You could write your own reader class that interacts with a distributed file system (DFS) or a cloud storage service like Amazon S3. This reader would need to use a distributed communication library like MPI or ZeroMQ to coordinate file access among ranks. This approach offers flexibility but requires more manual effort to implement the distributed logic.

  2. DCP with Custom Storage Backend
    While FileSystemReader is the default, PyTorch DCP allows you to provide a custom storage backend. This could be an abstraction layer that wraps access to a DFS or cloud storage instead of the local filesystem. This approach requires some understanding of DCP's internal workings but leverages the existing distributed coordination mechanisms. (Note: This functionality might not be available in all PyTorch versions.)

Recommendation