This document is relevant for: Inf2, Trn1, Trn2, Trn3

Neuron Compiler FAQ (neuronx-cc)#

Where can I compile to Neuron?#

You can do the one-time compilation from a framework-level model to a NEFF binary on any EC2 instance, or on premises.

The simplest option is to compile on the Trainium or Inferentia2 instance where the model runs. To compile on a different host, set --target to the instance family where the NEFF runs (for example, trn2 or trn3). Large models need a host with a large amount of memory. You can also install Neuron in your own environment. For example, you can compile in your training fleet and use a configuration management application to distribute the NEFF files to your inference fleet.

Which Neuron compiler should I use?#

Use neuronx-cc. It is the only current Neuron compiler, and it supports NeuronCore-v2 through NeuronCore-v4 (Trn2, Trn3, Trn1, Trn1n, and Inf2). neuron-cc, the compiler for Inf1, is a retired component.

How is the compiler called from PyTorch?#

TorchNeuron calls neuronx-cc for you. In eager mode, TorchNeuron compiles each operation the first time it runs. With torch.compile(backend="neuron"), it compiles the captured graph as one unit. To pass compiler options, set the NEURON_CC_FLAGS environment variable. See torch.compile on Neuron and torch.compile backend reference.

My current neural network is based on FP32, how can I use it with Neuron?#

You can compile and deploy FP32 models with Neuron. By default, neuronx-cc compiles matrix multiplication operations in the datatype of the model (--auto-cast=none). To cast them to BF16 for better performance, pass --auto-cast=matmult --auto-cast-type=bf16. For more information about performance and accuracy tuning, see Mixed Precision and Performance-accuracy Tuning (neuronx-cc).

The Neuron compiler keeps the application interface: FP32 inputs and outputs. The transfer of large FP32 tensors can slow your application. To decrease execution time, cast the inputs and outputs to FP16 or BF16 in the ML framework before compilation.

Which operators does Neuron support?#

To list the operators, run the neuronx-cc list-operators command. See Neuron Compiler CLI Reference Guide (neuronx-cc).

To request support for new operators, open an issue on our GitHub forum.

Does the compiler support dynamic shapes and control flow?#

neuronx-cc compiles graphs with static shapes. It does not support dynamic shapes. For the error that the compiler reports, see NCC_EMOD025. To use inputs of different sizes, pad them to a fixed set of shapes (buckets).

Resolve data-dependent control flow in the framework before compilation. With torch.compile, PyTorch splits the graph at data-dependent control flow (a graph break), and TorchNeuron runs that code in eager mode.

Will I need to recompile again if I updated runtime/driver version?#

The compiler and runtime keep compatibility for major version releases with each other. The versioning is defined as major.minor, with compatibility for all versions with the same major number. If the versions do not match, the runtime logs an error and the load fails. You must then compile the model again.

TorchNeuron includes the compiler version and NEURON_CC_FLAGS in its compile cache key, so a change to either one causes a new compilation.

I have a NEFF binary, how can I tell which compiler version generated it?#

Record the compiler version when you compile. To see the installed version, run pip show neuronx-cc.

How long does it take to compile?#

The time depends on the model size and complexity. For most models, compilation takes a few minutes. Large models can take longer.

Why is my model producing different results compared to CPU/GPU?#

NeuronCore-v2 through NeuronCore-v4 support multiple casting modes for floating point numbers. Each mode has a different balance of performance and accuracy. The default casting mode is a balance between performance and accuracy. On some models, it can cause a loss of precision.

For the options that change the casting mode, see --auto-cast and --auto-cast-type in Neuron Compiler CLI Reference Guide (neuronx-cc).

Is my model type supported?#

neuronx-cc has explicit support for select model families with the --model-type option, and it supports many other model types. To see the supported operators, run the list-operators subcommand. See the Neuron Compiler CLI Reference Guide (neuronx-cc) for details. Neuron adds support for new operators and models in each release.

For the models that Neuron supports for inference and training, see vLLM Neuron and TorchTitan Neuron.