This document is relevant for: Trn2, Trn3

nki.lnc.sendrecv_compute#

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

Perform point-to-point communication between NeuronCores and reduce the local and received tiles into a single result, using the DMA engine’s reduction capability.

Note

Available only on NeuronCore-v3 or newer, when running with LNC (Logical NeuronCore) degree 2.

This instruction combines a bidirectional data exchange (like nki.lnc.sendrecv) with a reduction. The current NeuronCore sends its src tile to the target NeuronCore specified by send_to_rank, simultaneously receives the peer tile from recv_from_rank, and writes reduce_op(src, peer) to dst.

The use case is fusing an inter-core exchange with an element-wise reduction, such as cross-core all-reduce-style accumulation, without a separate nki.isa.tensor_tensor step after nki.lnc.sendrecv.

The communication is between the physical NeuronCores that make up the same Logical NeuronCore (LNC) and share an HBM stack, so this instruction requires an LNC degree of 2, and send_to_rank / recv_from_rank must be a valid rank within the LNC (0 or 1).

The reduce_op parameter selects the reduction applied to the local src and the peer tile. Supported operations are nl.add, nl.maximum, and nl.minimum.

Constraints.

  • src and dst tiles must be in the same memory space: both in SBUF or both in private HBM (mixing SBUF and HBM, or using shared HBM, is not supported).

  • src and dst must have the same data type.

  • 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 and reduced

  • dst – the destination tile on the current NeuronCore where the reduced result is 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

  • reduce_op – the reduction operation to apply: nl.add, nl.maximum, or nl.minimum

  • 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.

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_compute_kernel(src, reduce_op=nl.add, priority=None):
    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])
    reduced = 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_compute(
        src=tile,
        dst=reduced,
        send_to_rank=1 - rank,
        recv_from_rank=1 - rank,
        reduce_op=reduce_op,
        priority=priority,
    )
    nisa.dma_copy(dst=out[rank], src=reduced)
    return out

This document is relevant for: Trn2, Trn3