This document is relevant for: Inf1, Inf2, Trn1, Trn2, Trn3
What are Neuron Compiler artifacts?#
When TorchNeuron compiles a PyTorch model, it writes files to disk. These files are the
compiler artifacts. They include the intermediate representations (IR) of each graph, the
working directory of each neuronx-cc call, the compiled NEFF (Neuron Executable File
Format) files, and the caches that let later runs skip compilation.
You do not need the artifacts to run a model. Use them to find out what was compiled, to confirm the compiler options, to debug a compilation failure, and to reuse compiled programs across runs and instances.
Applies to#
This concept is applicable to:
TorchNeuron (
torch-neuronx) in eager mode and withtorch.compile(backend="neuron")Trn2 and Trn3 instances
How TorchNeuron produces the artifacts#
Eager mode and torch.compile use the same job-based artifact hierarchy. For each graph,
TorchNeuron does these steps:
It captures the graph as a PyTorch FX graph, and then transforms the FX graph for Neuron.
It lowers the graph through torch-mlir to StableHLO.
It calls
neuronx-ccin a working directory of its own. The compiler writes a NEFF file.It stores the NEFF in the persistent NEFF cache. The Neuron Runtime loads the NEFF and runs it on the NeuronCores.
Each step writes files. TorchNeuron writes the files of steps 1 to 3 under the debug
directory, TORCH_NEURONX_DEBUG_DIR (default /tmp/neuron_backend). By default,
TorchNeuron removes these files after execution. To keep them, set
TORCH_NEURONX_PRESERVE_COMPILATION_ARTIFACTS=1.
Graph artifacts#
TorchNeuron writes the IR of each graph under
{TORCH_NEURONX_DEBUG_DIR}/torch_neuronx/rank{rank}/{job_id}/. There is one directory for
each rank and job:
Directory |
Contents |
|---|---|
|
The FX graph that TorchNeuron captured ( |
|
The FX graph after the Neuron transformations ( |
|
The raw torch-mlir output ( |
|
The StableHLO module ( |
|
The module that torch-mlir could not lower. TorchNeuron writes this directory only when torch-mlir fails. |
|
The inputs and outputs of each compiled graph, as pickled NumPy arrays. TorchNeuron writes
these directories only when |
Compiler artifacts#
Each neuronx-cc call has its own working directory,
{TORCH_NEURONX_DEBUG_DIR}/neuronx-cc/{hash}/:
File |
Contents |
|---|---|
|
The module that TorchNeuron gave to the compiler. |
|
The compiled NEFF file. |
|
The arguments of the compiler call. Use this file to confirm the options that you set in
|
|
The compiler log. If the compilation fails, look here first. |
|
Metadata about the compiler call. |
The number of neuronx-cc directories tells you how many graphs TorchNeuron compiled. If
there are more than you expect, look for graph breaks or recompilations. See
How to optimize a model with torch.compile.
The NEFF file#
The NEFF is the executable that the Neuron Runtime loads. It contains the program that runs on
the NeuronCores. A NEFF is built for one target instance family and one Logical NeuronCore
(LNC) configuration, so a NEFF for trn2 does not run on trn3.
To compile NEFFs on a host with no Neuron device, set TORCH_NEURONX_COMPILE_ONLY=1 and set
the target with NEURON_PLATFORM_TARGET_OVERRIDE (for example, trn2 or trn3).
Compile caches#
TorchNeuron keeps compiled results in caches on disk, so that a later run does not compile the same graph again:
Cache |
Location (default) |
Contents |
|---|---|---|
NEFF cache |
|
Compiled NEFF files. Point this directory at a shared mount, such as Amazon FSx, to reuse NEFFs across runs and instances. |
Lock and scratch directory |
|
File locks and scratch files only. Keep this directory on a local POSIX file system, because file locks are not reliable on NFS or Lustre. |
HLO cache |
|
The lowered HLO of eager operations and |
NKI trace cache |
|
The compiled NKI kernels. |
The NEFF cache key includes the torch-neuronx version, the compiler version, the target
platform, the LNC configuration, and the compiler flags in NEURON_CC_FLAGS. A change to any of these values makes a new key,
and TorchNeuron compiles the graph again. To get cache hits in production, pin the versions of
torch-neuronx and neuronx-cc.
To turn off the NEFF cache, set TORCH_NEURONX_NEFF_DISABLE_CACHE=1. Every compilation then
runs from the start.
Profile files#
When you capture a profile of a NEFF, the Neuron Runtime writes profile files. Neuron Explorer reads these files. For the file types, see Neuron Explorer profile files.
Further reading#
This document is relevant for: Inf1, Inf2, Trn1, Trn2, Trn3