Compile, Execute, and Profile NEFF Executables
A NEFF (Neuron Executable File Format) binary is the compiled executable run on AWS Trainium and Inferentia. This guide summarizes the full workflow: compiling NEFFs from PyTorch or NKI/NKIPy, executing them standalone with spike, and profiling traces with neuron-explorer.
flowchart LR
PT["PyTorch (torch.nn.Module)"] -->|"torch_neuronx.trace"| NEFF[".neff"]
NKI["NKI / NKIPy kernel"] -->|"extract HLO"| HLO[".hlo_module.pb"]
HLO -->|"neuronx-cc"| NEFF
NEFF --> Spike["spike (run)"]
NEFF --> Prof["neuron-explorer (profile)"]
1. Compile from PyTorch (torch_neuronx.trace)
torch_neuronx.trace compiles a PyTorch callable given concrete example inputs, with three key constraints:
- Inputs/outputs must be tensors or nested lists/dicts/tuples of tensors (bake scalars like
intorboolinto atorch.nn.Module). - Input shapes and dtypes must be concrete at trace time.
- The
.neffartifact is written intocompiler_workdir.
Example compiling a RoPE forward pass:
1 | |
--lnc=2 targets two NeuronCores; the compiled NEFF is written under ./compiler_workdir/, and traced can also be called directly in-process.
2. Compile from NKI or NKIPy Kernels
Extract HLO from an NKI kernel (matmul.py)
Write a kernel wrapped with PyTorchXLAKernel and trigger compilation with xm.mark_step():
1 | |
Dump model.hlo_module.pb without executing on hardware (NEURON_EXTRACT_GRAPHS_ONLY=1):
1 | |
The kernel module is typically the largest MODULE_*/model.hlo_module.pb in cache_dir.
Extract HLO with nkipy
For nkipy, trace and serialize directly:
1 | |
Compile HLO to NEFF (neuronx-cc)
1 | |
| Flag | Description |
|---|---|
--framework XLA |
Required. Input is an XLA HLO module |
--target trn1|trn2|trn3 |
Target Neuron instance family |
-o <file> |
Output NEFF path |
--optlevel 1|2|3 |
Optimization level (default 2) |
--enable-fast-loading-neuron-binaries |
Uncompressed NEFF for faster load time |
--auto-cast matmult|all |
Auto-cast FP32 to lower precision |
--auto-cast-type fp16|bf16|tf32 |
Auto-cast target dtype (default bf16) |
--logical-nc-config 1|2 |
NeuronCores per logical core (trn2 only) |
3. Execute a Pre-Compiled NEFF with spike
spike loads and executes .neff binaries on Neuron devices without invoking the compiler (regardless of whether the NEFF came from torch_neuronx.trace or neuronx-cc):
1 | |
SpikeTensor names must match the keys reported in model.input_tensors_info.
4. Profile with neuron-explorer
Capture an NTFF trace
1 | |
--profile-nth-exec=2: skips the first execution so cold warmup effects do not skew the trace.--enable-dge-notifs: captures DGE DMA events, but can overflow the status notification queue and time out if a NEFF issues many DGE instructions.
View the trace in the web UI
1 | |
This starts a web UI on port 3001 (http://localhost:3001/) and an API server on port 3002. On a headless remote host, either:
- Port forward ports 3001 and 3002 over SSH (with local copies of the NEFF and NTFF files), or
- Use a VNC remote desktop (Setting Up a VNC Server for Remote Desktop Access § Headless VNC Setup Guide) and open
http://localhost:3001/in a browser inside the session.