This document is relevant for: Trn2, Trn3

nki.lnc.sendrecv#

nki.lnc.sendrecv(src: NkiTensor, dst: NkiTensor, send_to_rank, recv_from_rank, dma_engine=dma_engine.dma, name=None, priority=None)[source]#

Perform point-to-point communication between NeuronCores by sending and receiving data simultaneously using DMA engines.

Note

Available only on NeuronCore-v3 or newer.

This instruction enables bidirectional data exchange between two NeuronCores within a Logical NeuronCore (LNC) configuration. The current NeuronCore sends its src tile to the dst location of the target NeuronCore specified by send_to_rank, while simultaneously receiving data from recv_from_rank into its own dst tile.

The use case is when NeuronCores need to exchange data for distributed computation patterns, such as all-gather communication or other collective operations where cores need to coordinate their computations by exchanging tiles.

This instruction is only allowed in NeuronCore-v3 or newer when LNC (Logical NeuronCore) degree 2 is enabled. The communication occurs between NeuronCores that share the same HBM stack within the LNC configuration. Therefore, send_to_rank and recv_from_rank must be either 0 or 1.

Sendrecv messages are paired across NeuronCore ranks positionally by program order: the Nth sendrecv on one rank rendezvous with the Nth sendrecv on the peer rank. The compiler can reorder sendrecv ops, but applies the same order on every rank, so the pairing is unchanged.

The dma_engine parameter specifies which DMA transfer mechanism to use:

  • nki.isa.dma_engine.dma (default): Uses the standard DMA engine with CoreBarrier synchronization. Can be triggered from any engine.

  • nki.isa.dma_engine.gpsimd_dma: Uses the GpSimd Engine’s internal DMA engine for low-latency SBUF-to-SBUF swaps in LNC=2. Implies GpSimd as the trigger engine. This mode restricts the data size per partition to not exceed:

    • 1024 bytes for 32-bit types

    • 512 bytes for 16-bit types

    • 256 bytes for 8-bit types

Constraints.

  • src and dst tiles must both be in SBUF.

  • src and dst must have the same data type, but they can be any supported data types in NKI.

  • src and dst must have the same shape and layout.

  • src and dst must have the same partition dimension size and the same number of elements per partition.

Parameters:
  • src – the source tile on the current NeuronCore to be sent to the target NeuronCore

  • dst – the destination tile on the current NeuronCore where received data will be stored

  • send_to_rank – rank ID of the target NeuronCore to send data to

  • recv_from_rank – rank ID of the source NeuronCore to receive data from

  • dma_engine – the DMA transfer mode; defaults to nki.isa.dma_engine.dma

  • name – (optional) name for the instruction.

  • priority – DMA quality-of-service priority from 0 (highest) to 3 (lowest). Available on NeuronCore-v4 or newer. None uses the compiler default. Must be None when dma_engine=nki.isa.dma_engine.gpsimd_dma.

Example:

Run the kernel with LNC degree 2 and input shape (2, 128, 64). On NeuronCore-v4 or newer, pass priority=0 to select the highest priority.

import nki
import nki.isa as nisa
import nki.language as nl
import nki.lnc as lnc

@nki.jit
def sendrecv_kernel(src, priority=None, dma_engine=nisa.dma_engine.dma):
    rank = nl.program_id(0)
    tile = nl.ndarray(src.shape[1:], dtype=src.dtype, buffer=nl.sbuf)
    nisa.dma_copy(dst=tile, src=src[rank])
    received = nl.ndarray(src.shape[1:], dtype=src.dtype, buffer=nl.sbuf)
    out = nl.ndarray(src.shape, dtype=src.dtype, buffer=nl.shared_hbm)
    lnc.sendrecv(
        src=tile,
        dst=received,
        send_to_rank=1 - rank,
        recv_from_rank=1 - rank,
        dma_engine=dma_engine,
        priority=priority,
    )
    nisa.dma_copy(dst=out[rank], src=received)
    return out

This document is relevant for: Trn2, Trn3