Monarch Documentation

repository·main·Indexed 21 days ago

https://github.com/meta-pytorch/monarch

A distributed programming framework for PyTorch utilizing an actor-based messaging model. Monarch is designed for high-performance distributed training and includes features such as scalable actor meshes, supervision trees for fault tolerance, RDMA for fast memory transfers, and distributed tensors. The framework supports deployment on Kubernetes via MonarchMesh CRDs, with integration options for Volcano and Kueue schedulers.

Tokens
180.6K
Snippets
485
Records
786
Agent score
77%

What's inside Monarch

  1. Overview of the Monarch Python API packages

    main

    Monarch's public APIs are organized into several specialized packages depending on your task:

    • monarch.actor: Used for defining and managing actors (the units of computation).
    • monarch: Contains APIs for computing with distributed tensors.
    • monarch.job: Provides a declarative interface for managing distributed job resources.
    • monarch.rdma: Provides RDMA (Remote Direct Memory Access) support for high-performance networking.
    • monarch.spmd: Provides primitives for running torchrun-style SPMD (Single Program, Multiple Data) scripts over Monarch meshes.
  2. What is Monarch?

    main

    Monarch is a distributed programming framework for PyTorch built on scalable actor messaging. It is designed to simplify distributed training and computation through several core capabilities:

    • Remote actors with scalable messaging: Actors are organized into collections called meshes, allowing for efficient message broadcasting to all members.
    • Fault tolerance via supervision trees: Actors and processes are structured in a tree. Failures propagate up the tree, providing standard error behavior and enabling fine-grained fault recovery.
    • Point-to-point RDMA transfers: Supports cheap registration of GPU or CPU memory for one-sided transfers based on libibverbs.
    • Distributed tensors: Enables actors to operate on tensor objects that are sharded across multiple processes.

    Platform Support:

    • Linux: Supports both CPU-only and GPU-accelerated features.
    • macOS: Supports CPU-only tensor engine operations.
    • GPU Requirements: GPU features require Linux with a supported GPU toolchain.
  3. Overview of Monarch

    main

    Monarch is a distributed programming framework for PyTorch built on scalable actor messaging. It provides remote actors with scalable messaging, fault tolerance via supervision trees, point-to-point RDMA transfers, and distributed tensors.

    Key Components:

    • Rust Core: Implements the core actor system, messaging, RDMA, and tensor operations.
    • Python API: Provides Python bindings to expose functionality through a simple API.
    • Hyperactor System: The underlying actor mesh implementation.
    • Tensor Engine: Optional component providing GPU/RDMA support for distributed tensors (can be disabled for CPU-only builds).

    Note for Python users: If you are writing code using the Monarch Python API, refer to docs/DOCS_INDEX.md for tutorials, API documentation, and examples.

  4. Overview of the Monarch Simulator

    main

    The Simulator is a tool designed to simulate execution time and memory usage based on input messages. It can be used in two ways:

    1. As a backend: Functioning similarly to ProcessBackend.
    2. As a standalone object: Receiving messages from a pre-stored file.

    Its primary purpose is to provide insights into how a workload would perform in terms of resource consumption without requiring actual hardware execution.

  5. Run torchtitan on MAST via monarch

    main

    This example demonstrates how to run torchtitan (specifically the llama3_debugmodel configuration) across multiple H100 nodes (e.g., 4 nodes with 32 GPUs total) using the monarch apply and monarch exec workflow.

    Key characteristics:

    • H100 x86 only: Designed for this specific hardware.
    • Remote Mount Workflow: The trainer code (torchtitan + torch) is shipped to workers via a remote_mount of a uv-managed .venv, rather than being packaged into the worker's fbpkg. This keeps the worker bootstrap slim (~530 MB vs ~5 GB).
    • Vanilla Scripts: You write standard Python training scripts (train.py) that run locally with a standard venv and can be executed remotely via monarch exec without requiring import monarch in your code.
  6. Hyperactor Channel Benchmark Details

    main

    The benchmark suite evaluates the performance of Hyperactor's channel communication system using the following parameters:

    Transport Types

    • Local: In-memory transport for same-process communication.
    • TCP: Network transport using TCP sockets.
    • MetaTLS: Secure transport using Meta's TLS implementation.
    • Unix: Unix domain socket transport for inter-process communication.

    Message Sizes

    Tests cover a range from 10 bytes to 1 GB ($10^1$ to $10^9$ bytes), categorized as:

    • Small: 10B, 100B, 1KB
    • Medium: 10KB, 100KB, 1MB
    • Large: 10MB, 100MB, 1GB

    Each message includes a u64 ID field and a zero-filled payload of the specified size.

    Metrics and Implementation

    • Metric: Throughput measured in bytes per second.
    • Framework: Uses the Criterion framework for statistical analysis and Tokio's async runtime for asynchronous channel operations.
  7. Hyperactor Core Concepts and API Reference

    main

    The Hyperactor book provides a comprehensive guide to the Hyperactor framework, covering its core abstractions for distributed actor-based programming. Key areas of study include:

    • References: Understanding how to identify and address entities using ProcId, ActorId, PortId, and Addr, including the use of Typed References.
    • Mailboxes and Routers: Managing communication via Ports, MailboxSender, MailboxServer, MailboxClient, and Mailbox. This includes understanding Delivery Semantics, Multiplexers, and Routers.
    • Channels: Implementing communication through Transmits and Receives using various Transports such as Local, TCP, Unix, and MetaTLS. It also covers Channel Addresses and Frames.
    • Procs: Working with the execution hierarchy including Proc, Gateway, and Host.
    • Actors: Defining and managing Actor lifecycles, using Handlers, RemoteSpawn, Remote Registry, Referable traits, Binds, RemoteHandles, and ActorHandle.
    • Remote Supervision and Rendezvous: Managing distributed actor lifecycles and synchronization.
    • Macros: Using procedural macros to simplify actor implementation, such as #[derive(Handler)], #[derive(HandleClient)], #[derive(RefClient)], #[derive(Named)], #[export], #[handle], and #[behavior].
  8. Use the monarch distributed tensor computation API

    main

    The monarch module provides the core API for distributed tensor computation. It allows users to manage tensors across distributed meshes, handle asynchronous execution via streams, and perform collective operations like reductions and remote calls.

    Key components include:

    • Tensor: The primary abstraction for distributed data.
    • Stream: Manages execution flow and synchronization.
    • Mesh Operations: Functions like to_mesh, slice_mesh, and world_mesh allow you to control how tensors are distributed across hardware topologies.
    • Collective Operations: Functions such as reduce, reduce_, coalescing, and fetch_shard enable communication between distributed processes.
  9. Use hyperactor macros for actor and message integration

    main

    The hyperactor crate provides a suite of macros designed to support a complete message-passing workflow. These macros facilitate defining message enums, generating client APIs, routing messages, and exporting actors for dynamic or remote use.

    Key capabilities include:

    • Generating message handling and client traits.
    • Implementing client traits for ActorHandle<T> and ActorRef<T>.
    • Assigning globally unique names and ports for routing and reflection.
    • Making actors remotely spawnable and routable.
    • Routing messages to user-defined handler trait implementations.
    • Defining façade actor types to expose restricted or stable APIs.
  10. Navigate the Monarch Python Documentation

    main

    The Monarch Python documentation is organized into four main categories to help you build, run, and observe distributed workloads:

    • Tutorials & Guides: Includes the Getting Started tutorial, a complete guide to Actors, a task-oriented Cookbook, a Debugging Guide, and Installation instructions.
    • API Reference: Detailed documentation for core modules including monarch.actor, monarch.rdma, monarch.config, monarch.job, and monarch.spmd.
    • Tools & Observability: Covers observability concepts, distributed telemetry, the Mesh Admin TUI, the Monarch Dashboard, and OpenTelemetry/Grafana integration.
    • Examples: Practical implementations such as GRPO actors, distributed tensors, ping-pong communication, crawlers, and various DDP (Distributed Data Parallel) patterns for Kubernetes or SPMD.
  11. Use monarch.rdma for high-performance networking

    main
    The monarch.rdma module enables Remote Direct Memory Access (RDMA) support, facilitating high-performance networking and zero-copy data transfers between processes. This is typically used in distributed training or high-throughput computing environments where minimizing CPU overhead and latency during data movement is critical. For a high-level overview of how to implement these patterns, refer to the Point-to-Point RDMA guide.
  12. What is the Referable trait?

    main

    The Referable trait is a marker trait used to indicate that a type is eligible to serve as a reference to a remote actor. This is particularly useful in distributed systems where an actor may reside on a different process than the one attempting to reference it.

    To implement Referable, a type must also implement the Named trait, which ensures the type provides a static name used for identification across process boundaries.

    Note on Thread Safety: Referable does not impose Send or Sync bounds by default. If you are spawning actors across threads or storing them in shared data structures, you must add these bounds explicitly at the call site.

    pub trait Referable: Named {}