This document is relevant for: Trn2, Trn3
nki.multi_rank_execute#
- nki.multi_rank_execute(kernel: Kernel | Callable, *, world_size: int, per_rank_inputs: Mapping[int, tuple[tuple[Any, ...], dict[str, Any]]] | Sequence[tuple[tuple[Any, ...], dict[str, Any]]]) list[Any][source]#
Specializes a kernel into a NEFF (Neuron Executable File Format) and executes it on hardware across multiple ranks.
kernelmay be anki.jit()kernel or a plain Python function.per_rank_inputsgives each rank an(args, kwargs)pair, whereargsis a tuple andkwargsis a dict. It is either a sequence in rank order or a mapping with one key for each rank from0toworld_size - 1. Rankrrunskernel(*args, **kwargs)with its pair.Every rank runs the same NEFF, so each rank must pass arguments for the same parameters. Tensor arguments are dynamic: they are NumPy arrays (
numpy.ndarray) whose contents can differ across ranks, but they must have the same shape and dtype on every rank. All other arguments are static and must be equal on every rank.Example
import nki import nki.collectives as nccl import nki.isa as nisa import nki.language as nl import numpy as np @nki.jit def all_reduce_kernel(x, replica_group): src = nl.ndarray(x.shape, dtype=x.dtype, buffer=nl.private_hbm) out = nl.ndarray(x.shape, dtype=x.dtype, buffer=nl.shared_hbm) nisa.dma_copy(dst=src, src=x) nccl.all_reduce(srcs=[src], dsts=[out], replica_group=replica_group, op=nl.add) return out
Run it on two ranks:
# Rank r's input is filled with r. inputs = [np.full((128, 512), rank, dtype=np.float32) for rank in range(2)] replica_group = nccl.ReplicaGroup([[0, 1]]) results = nki.multi_rank_execute( all_reduce_kernel, world_size=2, per_rank_inputs=[((x,), {"replica_group": replica_group}) for x in inputs], ) # Every rank returns the sum of all ranks' inputs. for out in results: np.testing.assert_allclose(out, inputs[0] + inputs[1])
- Parameters:
kernel – The kernel to execute, either a
nki.jit()kernel or a plain Python function.world_size – Number of ranks to execute on.
per_rank_inputs – Each rank’s
(args, kwargs)pair, as a sequence in rank order or a mapping keyed by rank.
- Returns:
A list with one entry per rank, where entry
iis ranki’s return value.
This document is relevant for: Trn2, Trn3