.. meta::
   :description: How to select nki.compiler.CompileConfig options for a NKI kernel on AWS Neuron by profiling NKI allocation, opt_level and individual optimizations against a baseline.
   :keywords: NKI, CompileConfig, tune, profile, opt_level, OptLevel.O2, NKI allocation, NKI scheduling, prefetch_auto, split_reused_buffers, Neuron Explorer, AWS Neuron, Trainium
   :date-modified: 2026-11-13

.. _nki-compile-config-tuning-how-to:

=============================================
How to tune a NKI kernel with CompileConfig
=============================================

Task overview
=============

This topic shows how to select :class:`nki.compiler.CompileConfig` options for
a NKI kernel with the AWS Neuron SDK. You profile the kernel with neuronx-cc
allocation as a baseline, then with NKI allocation, and keep only the options
that make the kernel faster.

Prerequisites
=============

* A NKI kernel that compiles and gives correct results with the default
  ``CompileConfig``.
* Neuron Explorer, and a way to profile the kernel on a Trainium instance. See
  :doc:`/nki/guides/use-neuron-profile`.
* Familiarity with the allocation and scheduling modes. See
  :ref:`Deep dive: NKI compile configuration <nki-compile-config>`.

.. _nki-compile-config-choose-option:

Choose an option from the profile
=================================

Start from what you see in the kernel or in its profile. For a performance
problem, try ``opt_level=OptLevel.O2`` first, because it turns on most of the
options in the following table. If ``O2`` helps, you can turn on only the
options that the kernel needs instead.

.. list-table::
   :header-rows: 1
   :widths: 40 25 15 20

   * - What you see
     - What to try
     - Needs
     - Details
   * - A performance problem, and you do not know which option helps.
     - ``opt_level=OptLevel.O2``
     - ``LINEAR``
     - :ref:`nki-compile-config-opt-levels`
   * - A DMA load waits for compute that reads a tensor the load does *not*
       write.
     - ``prefetch_auto=True``
     - ``LINEAR``
     - :ref:`nki-compile-config-prefetch-auto`
   * - A DMA load waits for compute that reads the *same* tensor the load
       writes, as in a loop that reuses one tensor.
     - ``split_reused_buffers=True``
     - ``LINEAR``
     - :ref:`nki-compile-config-split-reused-buffers`
   * - A DMA load waits for compute that reads the *same* tensor the load
       writes, and the kernel uses neuronx-cc allocation.
     - ``non_ssa_legalization=True``
     - neuronx-cc allocation
     - :ref:`nki-compile-config-non-ssa-legalization`
   * - With ``allocation=Allocation.LINEAR``, a write waits for a read of a
       different tensor that shares its memory. With neuronx-cc allocation,
       the default ``AUTO`` already acts on this wait.
     - ``address_rotation=AddressRotation.ENABLE``
     - Any mode
     - :ref:`nki-compile-config-address-rotation`
   * - Copies only move data that the next instruction could read in place.
     - ``tensor_copy_elim=True``
     - ``LINEAR``
     - :ref:`nki-compile-config-tensor-copy-elim`
   * - The kernel loads the same data into SBUF more than once while the first
       copy is still there.
     - ``dma_copy_elim=True``
     - ``LINEAR``
     - :ref:`nki-compile-config-dma-copy-elim`
   * - Copies or zero memsets of 1-byte or 2-byte data take a noticeable part
       of engine time.
     - ``element_widening=True``
     - ``LINEAR``
     - :ref:`nki-compile-config-element-widening`
   * - One engine is busy while another engine that could run the same work is
       idle.
     - ``engine_balancing=True``
     - ``LINEAR`` and ``scheduling=True`` (experimental)
     - :ref:`nki-compile-config-engine-balancing`
   * - The kernel fails to compile because too many registers are live at
       the same time.
     - ``register_load_reuse=False``
     - ``LINEAR`` and ``scheduling=True`` (experimental)
     - :ref:`nki-compile-config-register-load-reuse`
   * - The compiler removes an instruction that you need, such as a section
       that you time on its own.
     - ``dead_code_elim=False``. With ``LINEAR``, also
       ``remove_unused_instructions=False`` and
       ``remove_overwritten_writes=False``.
     - Any mode
     - :ref:`nki-compile-config-dead-code-elim`
   * - NKI allocation fails with an out-of-memory error.
     - Change the kernel to fit, or ``sbuf_allocation_fallback=True``
     - ``LINEAR``
     - :ref:`nki-compile-config-out-of-memory`

In the **Needs** column, ``LINEAR`` is ``allocation=Allocation.LINEAR``. In
other modes, the option has no effect or ``CompileConfig`` raises
``ValueError``. For which of the two occurs, see
:ref:`nki-compile-config-nki-flow-options`.

.. _nki-compile-config-tuning:

Instructions
============

**1:** Profile the kernel with neuronx-cc allocation.

Compile the kernel without ``allocation``, profile it, and keep the profile as
the baseline.

**2:** Profile the kernel with NKI allocation.

Set ``allocation=Allocation.LINEAR`` at the default ``OptLevel.O1``, and
profile again. NKI allocation alone can make a kernel faster or slower.

**3:** Profile the kernel at ``OptLevel.O2``.

Add ``opt_level=OptLevel.O2``, profile again, and compare the runtime with the
two earlier profiles.

**4:** Find the options that make the difference.

Take the fastest of the three profiles. If it uses ``O2``, turn off one of the
options that ``O2`` turns on, profile, and compare the runtime. To turn off an
option, set it to ``False``, or set ``address_rotation`` to
``AddressRotation.DISABLE``. Repeat for each option. For the options that
``O2`` turns on, see :ref:`nki-compile-config-opt-levels`.

**5:** Keep only the options that make the kernel faster.

Set those options explicitly in the ``CompileConfig``. Profile again after a
large change to the kernel, because the best options can change.

The configuration from steps 2 to 5, with NKI allocation and without NKI
scheduling, is the recommended configuration.

**6:** *(Optional)* Try NKI scheduling.

NKI scheduling is experimental (see :ref:`nki-compile-config-availability`).
To try it, add ``scheduling=True`` to the fastest configuration from steps 2
to 5. Compare the runtime with that configuration, not only with the
baseline. ``engine_balancing``, ``register_load_reuse`` and
``experimental_scheduling_heuristic`` act only with NKI scheduling, so try
them in this step. Keep ``scheduling=True`` only if the kernel is faster and
its results are correct.

Confirm your work
=================

To confirm the configuration, do the following checks:

* The results of the kernel with the final configuration match the results
  with the default configuration.
* The runtime with the final configuration is lower than the baseline from
  step 1.
* Each option that you kept shows its effect in the profile. The "How to
  check it" section of each option in
  :ref:`nki-compile-config-ref` describes what to look for.

Common issues
=============

For configuration errors, out-of-memory errors, and options that have no
effect, see :ref:`Troubleshooting NKI compile configuration <nki-compile-config-tshoot>`.

Related information
===================

* :ref:`NKI CompileConfig options reference <nki-compile-config-ref>`
* :ref:`Deep dive: NKI compile configuration <nki-compile-config>`
* :doc:`Profile a NKI kernel with Neuron Explorer </nki/guides/use-neuron-profile>`
