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
srctile to thedstlocation of the target NeuronCore specified bysend_to_rank, while simultaneously receiving data fromrecv_from_rankinto its owndsttile.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_rankandrecv_from_rankmust be either 0 or 1.Sendrecv messages are paired across NeuronCore ranks positionally by program order: the Nth
sendrecvon one rank rendezvous with the Nthsendrecvon the peer rank. The compiler can reordersendrecvops, but applies the same order on every rank, so the pairing is unchanged.The
dma_engineparameter 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.
srcanddsttiles must both be in SBUF.srcanddstmust have the same data type, but they can be any supported data types in NKI.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 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.dmaname – (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. Must beNonewhendma_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, 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_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