# AUTO GENERATED FILE - DO NOT MANUALLY MODIFY.
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
from typing import *
from nki.isa import dma_engine
from nki.isa import engine
if TYPE_CHECKING:
from nki.language import NkiTensor
[docs]def core_barrier(data: NkiTensor, cores, engine=engine.gpsimd, name=None):
r"""Synchronize execution across physical NeuronCores sharing an HBM stack.
.. note::
Available only on NeuronCore-v3 or newer.
This instruction creates a synchronization point where all specified NeuronCores
must reach before any can proceed. The barrier is implemented using a
semaphore-based protocol where each NeuronCore writes a semaphore to each other
core (remote semaphore update) and then waits for the other cores' semaphores
before continuing execution (local semaphore wait).
The use case is when two NeuronCores both need to write to disjoint portions of a
shared HBM tensor (``data``) and they both need to consume the tensor after both
cores have finished writing into the tensor. In this case, both cores can
perform the write to ``data`` in HBM using ``nki.isa.dma_copy``, and then signal
to each other when the write operation is complete using ``nki.lnc.core_barrier``.
Currently only ``cores=(0, 1)`` is supported, synchronizing exactly two physical
NeuronCores that share the same HBM stack. This applies at LNC degree 1 and 2.
At LNC degree 1, each physical core forms a separate Logical NeuronCore, but
the pair can still access shared HBM and use the barrier to wait for each
other's writes.
The ``data`` parameter represents the shared data that all cores need to synchronize
on. This must be data in shared HBM that multiple cores are accessing.
The ``engine`` parameter allows specifying which engine inside the NeuronCores
should execute the barrier instruction (that is, the remote semaphore update and
local semaphore wait). The barrier will block execution on this engine, other
engines will not be blocked. The DMA Engine
(``nki.isa.engine.dma``) cannot execute the barrier instruction and is rejected.
:param data: the shared data that all cores need to synchronize on; must be data in
shared HBM
:param cores: a tuple of core indices to synchronize; only ``(0, 1)`` is supported
:param engine: the engine to execute the barrier instruction on; defaults to GpSimd
Engine
:param name: (optional) name for the instruction.
Example:
Run the kernel with LNC degree 2 and input shape ``(2, 128, 64)``.
.. code-block:: python
import nki
import nki.isa as nisa
import nki.language as nl
import nki.lnc as lnc
@nki.jit
def core_barrier_kernel(src):
rank = nl.program_id(0)
tile = nl.ndarray(src.shape[1:], dtype=src.dtype, buffer=nl.sbuf)
shared = nl.ndarray(src.shape, dtype=src.dtype, buffer=nl.shared_hbm)
out = nl.ndarray(src.shape, dtype=src.dtype, buffer=nl.shared_hbm)
nisa.dma_copy(dst=tile, src=src[rank])
nisa.dma_copy(dst=shared[rank], src=tile)
lnc.core_barrier(shared, (0, 1))
nisa.dma_copy(dst=tile, src=shared[1 - rank])
nisa.dma_copy(dst=out[rank], src=tile)
return out"""
...
[docs]def sendrecv(src: NkiTensor, dst: NkiTensor, send_to_rank, recv_from_rank, dma_engine=dma_engine.dma, name=None, priority=None):
r"""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) <https://awsdocs-neuron.readthedocs-hosted.com/en/latest/about-neuron/arch/neuron-features/logical-neuroncore-config.html>`_
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.
:param src: the source tile on the current NeuronCore to be sent to the target
NeuronCore
:param dst: the destination tile on the current NeuronCore where received data
will be stored
:param send_to_rank: rank ID of the target NeuronCore to send data to
:param recv_from_rank: rank ID of the source NeuronCore to receive data from
:param dma_engine: the DMA transfer mode; defaults to ``nki.isa.dma_engine.dma``
:param name: (optional) name for the instruction.
:param 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.
.. code-block:: python
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"""
...
[docs]def sendrecv_compute(src: NkiTensor, dst: NkiTensor, send_to_rank, recv_from_rank, reduce_op, name=None, priority=None):
r"""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) <https://awsdocs-neuron.readthedocs-hosted.com/en/latest/about-neuron/arch/neuron-features/logical-neuroncore-config.html>`_
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.
:param src: the source tile on the current NeuronCore to be sent and reduced
:param dst: the destination tile on the current NeuronCore where the reduced
result is stored
:param send_to_rank: rank ID of the target NeuronCore to send data to
:param recv_from_rank: rank ID of the source NeuronCore to receive data from
:param reduce_op: the reduction operation to apply: ``nl.add``, ``nl.maximum``, or
``nl.minimum``
:param name: (optional) name for the instruction.
:param 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.
.. code-block:: python
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"""
...