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 Inf2 / Trn1 / Trn1n / 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 Python package and the Neuron compiler.

Task overview#

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

Next, you’ll install the Neuron SDK (if not included in the AMI), and you will validate that the nki library works.

Prerequisites#

  • You need an AWS login to launch an Inf2 / Trn1 / 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 Inf2 / Trn1 / Trn1n / Trn2 instance type. For more details see the Inf2, Trn1, or Trn2 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_8/bin/activate

Once the venv is activated, Python can be used to test that the library is available.

python -c 'import nki'

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

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.