This document is relevant for: Inf1, Inf2, Trn1, Trn2, Trn3
What is the Neuron Graph Compiler (neuronx-cc)?#
The Neuron Graph Compiler (neuronx-cc) compiles a machine learning model into
optimized code for AWS Trainium and Inferentia2. It analyzes the model graph, applies
hardware-specific optimizations, and writes a NEFF (Neuron Executable File Format) file
that the Neuron Runtime loads and runs on NeuronCores.
neuronx-cc targets the NeuronCore-v2 through NeuronCore-v4 architectures
(Trn2, Trn3, Trn1, Trn1n, and Inf2). It is the only current Neuron compiler. The
neuron-cc compiler for Inf1 is retired.
Applies to#
This concept is applicable to:
Training models on Trainium (Trn2, Trn3)
Running inference on Trainium and Inferentia2 (Trn2, Trn3, Inf2)
How neuronx-cc fits into the Neuron workflow#
Most users never call neuronx-cc directly. The framework calls it for you:
PyTorch (TorchNeuron): TorchNeuron compiles the operations that run in eager mode. With
torch.compile(backend="neuron"), it compiles the captured graph as one unit. See torch.compile on Neuron.JAX: JAX NeuronX gives the compiler an XLA HLO graph.
In both cases, the framework passes options to the compiler from the NEURON_CC_FLAGS
environment variable. The compilation flow is:
The framework lowers your model or operation to a graph: StableHLO from TorchNeuron, or XLA HLO from JAX. The framework writes it internally, so you do not see it.
neuronx-cccompiles that graph for a target instance family.The compiler writes a NEFF archive.
The Neuron Runtime loads the NEFF and runs it on one or more NeuronCores.
Call the compiler on the command line when you do not use a framework, when you customize a framework, or when you want to inspect or reproduce a build.
Commands#
neuronx-cc has the following commands. Run neuronx-cc <command> --help for full usage.
compile: Compile a model graph into a NEFF file for a target instance family.list-operators: Print the operators the compiler supports for a given framework.
For the full set of flags for these compiler commands, see the Neuron Graph Compiler command reference.
Files neuronx-cc works with#
Model input (HLO): an XLA HLO file (
hlo.pb). This is the graph the compiler reads.--framework XLAselects this input type. TorchNeuron and JAX write this file internally. You need it only when you call the compiler directly.NEFF output: the compiled artifact, written to
file.neffby default and set with--output. The Neuron Runtime loads this file to run the model.Log file: compiler messages, written to
log-neuron-cc.txtby default and set with--logfile. Use--verboseto control how much detail is logged.
Key features#
You control the compiler through compile options. The most common ones:
Target selection (
--target): the instance family to compile for:trn2,trn3,trn1,trn1n, orinf2.Model-type optimizations (
--model-type):generic(default),transformer, orunet-inference. These apply optimizations tuned to a model family.Mixed precision (
--auto-castand--auto-cast-type): trade accuracy for performance by casting FP32 operations tobf16,fp16,tf32, orfp8_e4m3. See Mixed Precision and Performance-accuracy Tuning (neuronx-cc) for guidance.Mixed precision accumulation (
--enable-mixed-precision-accumulation): enabled by default; accumulates operators such as softmax and layernorm in FP32 for better accuracy. Disable with--disable-mixed-precision-accumulation.Optimization level (
--optlevel/-O):1,2(default), or3, trading compile time against runtime performance.Graph sharding (
--logical-nc-config/--lnc): on Trn2 and Trn3, shard the graph across1or2physical NeuronCores (default2).Distributed training (
--distribution-strategy llm-training): enable optimizations for LLM training that shards parameters, gradients, and optimizer state across data-parallel workers.Runtime tuning:
--enable-fast-context-switch(faster model switching),--enable-fast-loading-neuron-binaries(larger but faster-loading NEFFs), and--enable-saturate-infinity(avoid NaNs on trn1).
For every option and its valid values, see the Neuron Compiler CLI reference.
Quickstart#
Compile an XLA HLO file into a NEFF for a Trainium instance, applying transformer-specific optimizations:
neuronx-cc compile bert-model.hlo \
--framework XLA \
--target trn2 \
--model-type transformer \
--output bert.neff
List the operators the compiler supports:
neuronx-cc list-operators --framework XLA
The compiler returns exit status 0 on success and a non-zero status on error.
Note
When you compile through a framework, you do not run these commands directly.
Pass the same options through NEURON_CC_FLAGS instead, and the framework
forwards them to neuronx-cc.
Further reading#
This document is relevant for: Inf1, Inf2, Trn1, Trn2, Trn3