NVIDIA Collective Communications Library (NCCL)

repository·master·Indexed 26 days ago

https://github.com/nvidia/nccl

A high-performance communication library for NVIDIA GPUs providing optimized primitives like all-reduce and broadcast for distributed training. Includes nccl4py Python bindings and a device API (nccl_device.h) for building custom CUDA kernels using NVLink primitives, such as multicast AllReduce and AllToAll algorithms for Hopper and Blackwell architectures (sm ≥ 9.0).

Tokens
108K
Snippets
183
Records
601
Agent score
89%

What's inside NCCL

  1. Overview of NCCL collective communication primitives

    master

    NCCL (NVIDIA Collective Communications Library) provides topology-aware inter-GPU communication primitives designed to accelerate communication in multi-GPU and multi-node environments. It implements both collective communication and point-to-point primitives.

    Supported collective communication primitives include:

    • AllReduce
    • Broadcast
    • Reduce
    • AllGather
    • ReduceScatter
    • AlltoAll
    • Gather
    • Scatter

    Additionally, NCCL supports point-to-point send/receive communication, which can be used to implement scatter, gather, or all-to-all operations.

  2. Overview of the NCCL Device API

    master

    The NCCL Device API enables communication to be initiated and performed directly from device (GPU) code. It is categorized into four primary functional areas:

    • Host-Side Setup: Functions for creating and configuring device communicators, querying device properties, and managing host-accessible device pointers.
    • Memory and LSA: Provides Load/Store Accessible (LSA) memory management, barriers, pointer accessors, and multimem capabilities.
    • GIN (GPU-Initiated Networking): Supports one-sided transfers, signals, counters, and network barriers.
    • Reduce, Broadcast, and Fused Building Blocks: Provides fundamental building blocks for computation-fused kernels, including reduce, copy (broadcast), and reduce-then-copy. These are used to implement collective algorithms like AllReduce, AllGather, and ReduceScatter.
  3. Overview of the nccl.core Communicator class

    master

    The nccl.core.Communicator class is the primary interface for performing collective and point-to-point communication in nccl4py. Its functionality is categorized into the following operational areas:

    • Class Identity: Constructor and properties for identity and device-API capability.
    • Lifecycle Management: Creating, splitting, growing, and tearing down communicators.
    • Collective Operations: Methods for collective communication such as allreduce, broadcast, and gather.
    • Point-to-Point (P2P) & Signaling: Methods for send, recv, signal, wait_signal, and put_signal.
    • Buffer & Window Registration: Registration for zero-copy and Remote Memory Access (RMA).
    • Device Setup: Host-side bootstrapping of device communicators.
    • Status & Cleanup: Error queries and resource management.
  4. Overview of NCCL Device API Modules

    master

    Starting with version 2.28, NCCL provides a device-side communication API for use directly within user CUDA kernels. The API is organized into several specialized modules:

    • LSA (Load/Store Accessible): Enables communication between devices via memory load/store operations using CUDA P2P (e.g., NVLink or P2P-capable PCIe).
    • Multimem: Uses hardware multicast via NVLink SHARP (available on certain Hopper-generation datacenter GPUs).
    • GIN (GPU-Initiated Networking): Enables communication over the network (available since version 2.28.7).
    • Reduce, Broadcast, and Fused Building Blocks: Optimized building blocks for computation-fused kernels (e.g., reduce, copy, and reduce-then-copy).
  5. Overview of NCCL

    master

    NCCL (pronounced "Nickel") is a library of optimized communication primitives for GPUs. It implements standard routines including all-reduce, all-gather, reduce, broadcast, and reduce-scatter, as well as arbitrary send/receive patterns.

    It is optimized for high bandwidth across:

    • PCIe
    • NVLink
    • NVSwitch
    • Networking via InfiniBand Verbs or TCP/IP sockets.

    NCCL supports an arbitrary number of GPUs in single-node or multi-node configurations and is compatible with single-process or multi-process (e.g., MPI) applications.

  6. Use NCCL Checkpoint Shim for collective checkpointing

    master

    The NCCL Checkpoint Shim enables collective checkpointing of all processes sharing NCCL communicators across multiple hosts. It intercepts NCCL calls to capture resource initialization, allowing applications to resume execution without reconfiguring communicators.

    To use the shim, you must:

    1. Launch your application with LD_PRELOAD pointing to libnccl-checkpoint-shim.so.
    2. Set NCCL_CHECKPOINT_KVS_PATH to a file containing the <address>:<port> of a Redis server used for process rendezvous during restore.
    3. Call the prepare/restore APIs at the appropriate lifecycle stages in your code.
  7. Understand NCCL Symmetric Memory implementation

    master

    NCCL uses a symmetric memory design to achieve barrier-free, low-latency asynchronous All-to-All (A2A) communication. It relies on 16-byte atomic writes (uint4) and a dual-epoch validation scheme to ensure data integrity without explicit synchronization.

    Key characteristics:

    • Atomic Writes: Data is written in 16 B chunks to prevent tearing.
    • Dual-Epoch Validation: Uses epoch tags to validate that data is fresh and not stale.
    • Performance Trade-off: This design provides extremely low latency for small messages but incurs a 2× bandwidth cost.
  8. Understand the NCCL Profiler Event Hierarchy

    master

    NCCL core events are organized in a hierarchical structure. This allows profilers to understand the relationship between high-level API calls and low-level execution details.

    Hierarchy Structure:

    • Group API event
      • Collective API event
        • Collective event
          • ProxyOp event
            • ProxyStep event
              • NetPlugin event
          • KernelCh event
      • Point-to-point API event
        • Point-to-point event
          • ProxyOp event
            • ProxyStep event
              • NetPlugin event
          • KernelCh event
      • Kernel Launch event
    • ProxyCtrl event
  9. Understand High Throughput (HT) vs Low Latency (LL) algorithms

    master

    NCCL EP supports two primary algorithms for MoE operations:

    High Throughput (HT)

    • Layout: Uses NCCL_EP_LAYOUT_FLAT.
    • Output: Dispatch output tokens are a contiguous flat sequence: [N(r) x hidden], where N(r) is the total tokens targeting the rank.
    • Dynamic Tokens: Supports max_dispatch_tokens_per_rank = NCCL_EP_AUTO. The actual received count is written to ncclEpLayoutInfo_t.recv_total_counter.
    • Workflow: Use topk_idx and topk_weights from dispatch outputs to route tokens to local experts and perform weighted reduction before calling ncclEpCombine.

    Low Latency (LL)

    • Layouts: Supports NCCL_EP_LAYOUT_EXPERT_MAJOR ([num_local_experts, N(e), hidden]) and NCCL_EP_LAYOUT_RANK_MAJOR ([num_ranks, max_dispatch_tokens_per_rank, hidden]).
    • Overlapping: Supports send_only in ncclEpDispatchConfig_t and ncclEpCombineConfig_t to enable computation/communication overlapping.
    • Constraint: Does not support dynamic max_dispatch_tokens_per_rank detection.
  10. Understand NCCL GIN and Symmetric Memory concepts

    master

    NCCL GIN (GPU-Initiated Networking) and Symmetric Memory are used to optimize communication in distributed systems, particularly for fusing computation and communication.

    • Symmetric Memory: Provides a unified abstraction that maps peer-memory access across a distributed system into a single, flat virtual-address space. This simplifies how parallel-computing kernels manage addresses.
    • GIN (GPU-Initiated Networking): Required for RDMA scale-out networks. While NVLink or PCIe peer-to-peer can use plain memory semantics, RDMA requires GIN to issue message-semantic network transfers directly from the device side.
  11. Understand the purpose and scope of the contrib/ directory

    master

    The contrib/ directory contains community and partner contributions that extend NCCL with new capabilities, algorithms, or higher-level APIs.

    Key characteristics of contrib/ content:

    • It builds on top of NCCL's public APIs (nccl.h, nccl_device.h).
    • It does not modify NCCL core (no changes to the src/ directory).
    • It is developed and maintained by its respective contributors, not the NCCL core team.
    • It falls outside the standard NCCL release quality standards.

    Available contributions:

    • custom_algos/: Reference custom collective kernels built on the NCCL Device API.
    • nccl_checkpoint/: NCCL Checkpoint library for multi-node, communicator-aware checkpoint and restore.
    • nccl_ep/: NCCL Expert Parallelism (EP) API for MoE communication (dispatch/combine primitives).
    • nccl_ubx/: UB-X (Ultra Bandwidth — eXperimental): low-latency NVLink collectives with compute fusion (residual + RMSNorm, mxfp8 dispatch) on a symmetric allocator.
    • nccl_m2n/: NCCL Cross-group Transfer (M2N) API for RL communication (reshard primitives).
  12. Manage GIN connections and contexts

    master

    GIN uses a hierarchy of connections and contexts to increase network parallelism:

    • Connection: A (src, dst) network device pair. A collComm manages connections to all peers. The number of connections is typically low (max ~4), usually corresponding to the number of network devices a rank has affinity to.
    • Context: A sub-resource of a connection. Multiple contexts allow parallel communication channels that are independent in terms of ordering. GIN ordering guarantees apply per-context (e.g., a signal completion guarantees the completion of all previous puts on that same context).