This document is relevant for: Trn2, Trn3

How to tune a NKI kernel with CompileConfig#

Task overview#

This topic shows how to select 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 Profile a NKI Kernel.

  • Familiarity with the allocation and scheduling modes. See Deep dive: NKI compile configuration.

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.

What you see

What to try

Needs

Details

A performance problem, and you do not know which option helps.

opt_level=OptLevel.O2

LINEAR

Optimization levels

A DMA load waits for compute that reads a tensor the load does not write.

prefetch_auto=True

LINEAR

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

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

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

address_rotation

Copies only move data that the next instruction could read in place.

tensor_copy_elim=True

LINEAR

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

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

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)

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)

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

dead_code_elim, remove_unused_instructions and remove_overwritten_writes

NKI allocation fails with an out-of-memory error.

Change the kernel to fit, or sbuf_allocation_fallback=True

LINEAR

Out-of-memory errors

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 Options by mode.

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 Optimization 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 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 Reference: NKI CompileConfig options describes what to look for.

Common issues#

For configuration errors, out-of-memory errors, and options that have no effect, see Troubleshooting NKI compile configuration.