This document is relevant for: Trn2, Trn3

How to set up your environment for NKI development#

The Neuron Kernel Interface (NKI) lets you write kernels that directly use hardware resources in the Trn2 / Trn3 family of Neuron ML accelerators. NKI kernels use low-level operators that match instructions on Neuron devices. You can use kernels with PyTorch or JAX to speed up critical sections of your model. This topic shows you how to set up your environment for NKI development using the AWS Neuron SDK. After you set up your environment, you can access the NKI and Neuron Graph compilers.

Task overview#

This tutorial walks you through launching a Trn2 / Trn3 instance with an Amazon Machine Image (AMI).

Prerequisites#

  • You need an AWS login to launch a Trn2 / Trn3 EC2 instance.

Instructions#

You can set up an environment to use NKI in several ways. The easiest method uses the Neuron Multi-framework Deep Learning AMI (DLAMI). The DLAMI provides Python virtual environments (using venv) for frameworks like PyTorch and JAX. AWS updates the DLAMI with each new Neuron SDK release. If you prefer to manage the environment directly, you can start with a standard Amazon Linux 2023 (AL2023) AMI and install the Neuron SDK and NKI library directly. If you already have a configured environment, follow the upgrade tab instructions to upgrade to the latest SDK.

  1. Launch the instance using the Neuron Deep Learning AMI.

    ../../_images/nki-setup-1.png

    Select the desired region from the EC2 Console and choose “Launch Instance”. In the “Quick Start” tab, select “Amazon Linux”, then in the AMI dropdown search for “neuron”. The “Deep Learning AMI Neuron (Amazon Linux 2023)” should be the only option. Select an Trn2 / Trn3 instance type. For more details see the Trn2 or Trn3 EC2 pages.

    Once the instance is launched, an environment can be activated with the NKI library and Neuron SDK already installed.

    • Note: If you are looking to use the Neuron DLAMI in your cloud automation flows, Neuron also supports SSM parameters to easily retrieve the latest DLAMI id.

Confirm your work#

To test the NKI environment is set up and ready to use, a venv that contains the nki library must be activated. Select the tab below that corresponds to how you installed the Neuron SDK above.

The Deep Learning AMI provides a number of environments for PyTorch, JAX, and other supported ML frameworks. Any of the PyTorch or JAX venvs supplied as a part of the Deep Learning AMI will include the nki library. See the Neuron DLAMI overview for the full list of environments. For simplicity, the JAX and PyTorch tabs below each choose the plain JAX and PyTorch venv respectively.

source /opt/aws_neuronx_venv_pytorch_2_9/bin/activate

Once the venv is activated, confirm that NKI is available.

python -c 'import nki'

If the environment is setup correctly, Python should return without reporting any errors.

(Optional) Run compiled kernels with nrtpy#

This step is optional. You only need it if you want to use the nrtpy libraries as part of your NKI kernel development. Your NKI environment above is already complete for writing and compiling kernels, so skip this section if you don’t plan to use nrtpy.

After you compile NKI kernels to Neuron Executable File Format (NEFF) files, you can load and run them directly from Python with nrtpy, the Neuron Runtime Python layer. This is useful for benchmarking a compiled kernel or building test harnesses without going through PyTorch or JAX.

Important

nrtpy and nki currently (as of Neuron release 2.32.0) cannot be installed in the same Python environment because of a namespace conflict. Keep using the NKI environment above to compile kernels to NEFFs, and create a separate nrtpy environment to load and run them.

Create a dedicated nrtpy environment, separate from the NKI venv above:

# Create and activate a separate venv for nrtpy
python3 -m venv ~/nrtpy_venv
source ~/nrtpy_venv/bin/activate

# Let nrtpy find the Neuron Runtime library (libnrt)
export LD_LIBRARY_PATH=/opt/aws/neuron/lib:$LD_LIBRARY_PATH

# Install nrtpy from the Neuron pip repository
pip install nrtpy --extra-index-url=https://pip.repos.neuron.amazonaws.com

For the full walkthrough, including how to verify the install, see Get started with nrtpy.

Common issues#

Uh oh! Did you encounter an error or other issue while working through this task? Here are some commonly encountered issues and how to address them.

  • Python reports an error trying to import NKI when using a Deep Learning AMI:

    • Make sure a PyTorch or JAX venv (provided as part of the Deep Learning AMI) is activated. Your shell prompt should reflect this by starting with (aws_neuronx_venv_<framework+version>) ...

  • Python reports an error trying to import NKI in the venv created as part of the Standard AMI install:

    • Make sure the venv you created is activated. Your shell prompt should reflect this by starting with (<venv-name>) ...

    • Make sure that the NKI library installation (with pip) from the previous instructions succeeded.