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 itssrctile to the target NeuronCore specified bysend_to_rank, simultaneously receives the peer tile fromrecv_from_rank, and writesreduce_op(src, peer)todst.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_tensorstep afternki.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_rankmust be a valid rank within the LNC (0 or 1).The
reduce_opparameter selects the reduction applied to the localsrcand the peer tile. Supported operations arenl.add,nl.maximum, andnl.minimum.Constraints.
srcanddsttiles 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).srcanddstmust have the same data type.srcanddstmust have the same shape and layout.srcanddstmust 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, ornl.minimumname – (optional) name for the instruction.
priority – DMA quality-of-service priority from 0 (highest) to 3 (lowest). Available on NeuronCore-v4 or newer.
Noneuses the compiler default.
Example:
Run the kernel with LNC degree 2 and input shape
(2, 128, 64). On NeuronCore-v4 or newer, passpriority=0to 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