This document is relevant for: Trn2, Trn3

nki.execute#

nki.execute(kernel: Kernel | Callable, *args: Any, **kwargs: Any) → Any[source]#

Specializes a kernel into a NEFF (Neuron Executable File Format) and executes it on hardware. kernel may be a nki.jit() kernel or a plain Python function.

Example

import nki
import nki.isa as nisa
import nki.language as nl
import numpy as np

@nki.jit
def matmul_kernel(X: nl.NkiTensor, y: nl.NkiTensor) -> nl.NkiTensor:
    X_sbuf = nl.ndarray(shape=X.shape, dtype=X.dtype, buffer=nl.sbuf)
    y_sbuf = nl.ndarray(shape=y.shape, dtype=y.dtype, buffer=nl.sbuf)
    nisa.dma_copy(dst=X_sbuf, src=X)
    nisa.dma_copy(dst=y_sbuf, src=y)

    z_psum = nl.matmul(X_sbuf, y_sbuf)  # X @ y
    z_sbuf = nl.ndarray(shape=z_psum.shape, dtype=X.dtype, buffer=nl.sbuf)
    nisa.tensor_copy(z_sbuf, z_psum)

    z = nl.ndarray(shape=z_sbuf.shape, dtype=X.dtype, buffer=nl.shared_hbm)
    nisa.dma_copy(dst=z, src=z_sbuf)
    return z

X = np.random.rand(2, 2).astype(np.float32)
y = np.random.rand(2, 1).astype(np.float32)

# Specialization and execution.
results = nki.execute(matmul_kernel, X, y)
Parameters:
  • kernel – The kernel to execute, either a nki.jit() kernel or a plain Python function.

  • args – Positional arguments to pass to kernel.

  • kwargs – Keyword arguments to pass to kernel.

Returns:

The kernel’s return value.

This document is relevant for: Trn2, Trn3