.. meta::
   :description: Overview of the Neuron Graph Compiler (neuronx-cc), which compiles PyTorch (TorchNeuron) and JAX models into NEFF files for AWS Trainium (Trn2, Trn3) and Inferentia2, with a brief quickstart to its commands, files, and features.
   :keywords: neuronx-cc, Neuron Graph Compiler, Neuron compiler, TorchNeuron, torch.compile, JAX, NEFF, HLO, Trainium, Trn2, Trn3, Trn1, Inf2, auto-cast, mixed precision
   :date-modified: 2026-11-16

.. _neuronx-cc-overview:

===============================================
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 :doc:`Neuron Runtime </neuron-runtime/index>` loads and runs on NeuronCores.

``neuronx-cc`` targets the :ref:`NeuronCore-v2 through NeuronCore-v4 architectures <neuroncores-v4-arch>`
(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):** :ref:`TorchNeuron <about-pytorch-neuron>` compiles the
  operations that run in eager mode. With ``torch.compile(backend="neuron")``, it compiles
  the captured graph as one unit. See :ref:`about-torchneuron-torch-compile`.
- **JAX:** :ref:`JAX NeuronX <jax-neuron-main>` 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:

1. 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.
2. ``neuronx-cc`` compiles that graph for a target instance family.
3. The compiler writes a **NEFF** archive.
4. 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 :ref:`the Neuron Graph Compiler command reference <neuron-compiler-cli-reference-guide>`.

Files neuronx-cc works with
---------------------------

- **Model input (HLO)**: an XLA HLO file (``hlo.pb``). This is the graph the compiler
  reads. ``--framework XLA`` selects 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.neff`` by default and
  set with ``--output``. The Neuron Runtime loads this file to run the model.
- **Log file**: compiler messages, written to ``log-neuron-cc.txt`` by default and
  set with ``--logfile``. Use ``--verbose`` to 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``, or ``inf2``.
- **Model-type optimizations** (``--model-type``): ``generic`` (default),
  ``transformer``, or ``unet-inference``. These apply optimizations tuned to a
  model family.
- **Mixed precision** (``--auto-cast`` and ``--auto-cast-type``): trade accuracy
  for performance by casting FP32 operations to ``bf16``, ``fp16``, ``tf32``, or
  ``fp8_e4m3``. See :ref:`neuronx-cc-training-mixed-precision` 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), or
  ``3``, trading compile time against runtime performance.
- **Graph sharding** (``--logical-nc-config`` / ``--lnc``): on Trn2 and Trn3, shard the
  graph across ``1`` or ``2`` physical NeuronCores (default ``2``).
- **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
:ref:`Neuron Compiler CLI reference <neuron-compiler-cli-reference-guide>`.

Quickstart
----------

Compile an XLA HLO file into a NEFF for a Trainium instance, applying
transformer-specific optimizations:

.. code-block:: shell

   neuronx-cc compile bert-model.hlo \
     --framework XLA \
     --target trn2 \
     --model-type transformer \
     --output bert.neff

List the operators the compiler supports:

.. code-block:: shell

   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``.

Related concepts
----------------

- :doc:`Neuron Graph Compiler error codes </compiler/error-codes/index>` — the set of error codes that can be raised during compilation with ``neuronx-cc``.
- :ref:`neuronx-cc documentation <neuronx-cc-index>` — the full ``neuronx-cc``
  documentation set.
- :ref:`TorchNeuron <about-pytorch-neuron>` — the native PyTorch backend that calls the
  compiler for eager mode and ``torch.compile``.
- :ref:`Neuron Compiler artifacts <neuronx-cc-artifacts-overview>` — the files that
  TorchNeuron and the compiler write, and the compile caches.
- The :doc:`Neuron Runtime </neuron-runtime/index>` — loads and runs the NEFF files
  the compiler produces.

Further reading
---------------

- :ref:`How to generate a NEFF file <neuronx-cc-generate-neff-how-to>`
- :ref:`Neuron Compiler CLI reference <neuron-compiler-cli-reference-guide>`
- :ref:`Mixed precision and performance-accuracy tuning <neuronx-cc-training-mixed-precision>`
- :ref:`Neuron Compiler FAQ <neuronx_compiler_faq>`
