This document is relevant for: Trn2, Trn3

nki.compiler.CompileConfig#

class nki.compiler.CompileConfig[source]#

Per-kernel compile configuration passed to @nki.jit(compile_config=...).

Options that change how the compiler allocates, schedules and optimizes the kernel. Every option is keyword-only. See Deep dive: NKI compile configuration for the current and new flows and for the options that need more than a few sentences.

Attributes

address_rotation

Which tensors the compiler can move to a different SBUF address or PSUM bank after allocation, so that a write no longer waits for a read of a different tensor that shares its memory (an AddressRotation).

allocation

How the compiler allocates the tiles of the kernel (an Allocation).

barrier_check

Compile with the LNC core barrier checker enabled.

dead_code_elim

Let the compiler remove instructions whose output is never read, and nisa.tensor_copy instructions whose readers can read the source directly.

dma_copy_elim

Remove a nisa.dma_copy that loads data that an earlier nisa.dma_copy already loaded into SBUF.

element_widening

Run a nisa.tensor_copy that does not convert the data type, or a nisa.memset to zero, of 1- or 2-byte elements on fewer 2- or 4-byte elements.

engine_balancing

Let the new scheduling choose the engine of each instruction that has no engine= and that more than one engine can run, such as nisa.tensor_scalar, nisa.tensor_tensor or nisa.memset, instead of its default engine.

experimental_allocation_mode

How the new allocation selects addresses in SBUF, HBM and PSUM (an AllocationMode).

experimental_hbm_allocation_mode

How the new allocation selects addresses in HBM only.

experimental_psum_allocation_mode

How the new allocation selects addresses in PSUM only.

experimental_sbuf_allocation_mode

How the new allocation selects addresses in SBUF only.

experimental_scheduling_heuristic

How the new scheduling selects the next instruction from the ready instructions (a SchedulingHeuristic).

nki_alloc_fallback

Compile the kernel again in the current flow when the new allocation runs out of SBUF, PSUM or HBM, instead of raising the out-of-memory error.

non_ssa_legalization

Let the compiler split a tensor that the kernel fills again with unrelated data into one copy per use.

opt_level

How many optimizations the compiler applies in the new flow (an OptLevel).

prefetch_auto

Keep DMA prefetch tensors apart from compute tensors in SBUF, so that a DMA that loads the next tile does not wait for compute that still reads the current one.

register_load_reuse

Let instructions that take the same SBUF tile as their scalar_offset share one register load of the index, instead of one load each.

remove_overwritten_writes

Remove an instruction whose output is fully overwritten before an instruction reads it, before the new allocation, so that the instruction takes no engine time.

remove_unused_instructions

Remove an instruction whose output no other instruction reads, before the new allocation, so that the instruction takes no engine time and its tile takes no memory.

sbuf_allocation_fallback

When the new allocation runs out of SBUF or HBM, retry with more compact strategies before failing.

scheduling

Whether the compiler schedules the instructions of the kernel in the new flow, before the rest of compilation.

split_reused_buffers

Split a single-buffered SBUF tile that the kernel fills again with unrelated data into one tile per use, so that the new allocation can put the tiles at different addresses and the load for the next use can start while the previous use is still read.

tensor_copy_elim

Remove a nisa.tensor_copy whose readers can read the source of the copy directly, before the new allocation, so that the copy takes no memory and no engine time.

address_rotation: AddressRotation = AddressRotation.AUTO#

Which tensors the compiler can move to a different SBUF address or PSUM bank after allocation, so that a write no longer waits for a read of a different tensor that shares its memory (an AddressRotation). AUTO (default) moves only the tensors that the current allocation places, so in the new flow every tensor stays where the new allocation put it. With allocation=Allocation.LINEAR, OptLevel.O2 changes AUTO to ENABLE. Keep AUTO unless a profile shows such waits. See address_rotation.

allocation: nki.compiler.Allocation | None = None#

How the compiler allocates the tiles of the kernel (an Allocation). None (default) compiles the kernel in the default flow, which is the current flow: the compiler allocates with graph coloring and schedules after it traces the kernel. Allocation.COLORING keeps the current flow also when the default changes. Allocation.LINEAR selects the new flow, which the options that need the new flow require. See Allocation and scheduling modes.

barrier_check: bool = False#

Compile with the LNC core barrier checker enabled. Off by default.

dead_code_elim: bool = True#

Let the compiler remove instructions whose output is never read, and nisa.tensor_copy instructions whose readers can read the source directly. On by default, in every flow. Set False to keep these instructions, for example a copy that moves data off PSUM early. Does not control the removal that the new flow does before allocation; for that, see remove_unused_instructions and remove_overwritten_writes. See dead_code_elim, remove_unused_instructions and remove_overwritten_writes.

dma_copy_elim: bool | None = None#

Remove a nisa.dma_copy that loads data that an earlier nisa.dma_copy already loaded into SBUF. The instructions that read the tile of the removed copy read the tile of the earlier copy instead, so the data is loaded into SBUF only once. None (default) follows opt_level: off at OptLevel.O0 and OptLevel.O1, on at OptLevel.O2. True turns it on. False keeps it off, also at OptLevel.O2. Enable it when a profile shows the same data loaded into SBUF more than once while the first copy is still there. The earlier tile stays in use until the last of these reads, so a later write to its address can wait longer. Has no effect unless allocation=Allocation.LINEAR. See dma_copy_elim.

element_widening: bool | None = None#

Run a nisa.tensor_copy that does not convert the data type, or a nisa.memset to zero, of 1- or 2-byte elements on fewer 2- or 4-byte elements. The instruction moves the same bytes, so the result is bit-exact, and it takes less time. None (default) follows opt_level: off at OptLevel.O0 and OptLevel.O1, on at OptLevel.O2. True turns it on. False keeps it off, also at OptLevel.O2. Enable it when a profile shows such copies or memsets of bfloat16, float16, float8 or int8 data that take a noticeable share of engine time. It does not change memory use. Has no effect unless allocation=Allocation.LINEAR. See element_widening.

engine_balancing: bool | None = None#

Let the new scheduling choose the engine of each instruction that has no engine= and that more than one engine can run, such as nisa.tensor_scalar, nisa.tensor_tensor or nisa.memset, instead of its default engine. The new scheduling already chooses the engine of nisa.tensor_copy without this option. None (default) follows opt_level: off at OptLevel.O0 and OptLevel.O1, on at OptLevel.O2. True turns it on. False keeps it off, also at OptLevel.O2. Try it when a profile shows one engine busy while another engine that can run the same work is idle. It adds compile time, and it chooses engines from an estimate, so compare the runtime with and without it. Acts only with the new scheduling (allocation=Allocation.LINEAR and scheduling=True). With neither allocation nor scheduling set, it has no effect; other combinations raise ValueError. See engine_balancing.

experimental_allocation_mode: nki.compiler.AllocationMode | None = None#

How the new allocation selects addresses in SBUF, HBM and PSUM (an AllocationMode). None (default) is RING. FAST and MAX_REUSE use less memory, with less overlap of loads and compute; try them when a kernel runs out of memory under RING. Acts only with allocation=Allocation.LINEAR. With no allocation set, it has no effect; with Allocation.COLORING, a value other than the default raises ValueError. See Allocation strategies.

Warning

This API is experimental and may change in future releases.

experimental_hbm_allocation_mode: nki.compiler.AllocationMode | None = None#

How the new allocation selects addresses in HBM only. None (default) uses experimental_allocation_mode. Under RING the new allocation reuses freed HBM only after it has used the DRAM of one core, so the HBM a kernel uses is about the sum of its HBM tensors. Such a kernel can fail to compile with an HBM out-of-memory error, or compile and then run out of device memory when it is loaded. Set FAST or MAX_REUSE for such a kernel. See HBM under RING.

Warning

This API is experimental and may change in future releases.

experimental_psum_allocation_mode: nki.compiler.AllocationMode | None = None#

How the new allocation selects addresses in PSUM only. None (default) uses experimental_allocation_mode. PSUM has 8 banks, so RING is costly there; FAST or MAX_REUSE keeps more banks free.

Warning

This API is experimental and may change in future releases.

experimental_sbuf_allocation_mode: nki.compiler.AllocationMode | None = None#

How the new allocation selects addresses in SBUF only. None (default) uses experimental_allocation_mode.

Warning

This API is experimental and may change in future releases.

experimental_scheduling_heuristic: nki.compiler.SchedulingHeuristic | None = None#

How the new scheduling selects the next instruction from the ready instructions (a SchedulingHeuristic). None (default) is ASAP, which suits most kernels. Another value can make a kernel slower, so compare the runtime with and without it. Acts only with the new scheduling (allocation=Allocation.LINEAR and scheduling=True). With neither allocation nor scheduling set, it has no effect; other combinations raise ValueError.

Warning

This API is experimental and may change in future releases.

nki_alloc_fallback: bool = False#

Compile the kernel again in the current flow when the new allocation runs out of SBUF, PSUM or HBM, instead of raising the out-of-memory error. Off by default. The compiler then reports the failure in a warning, and the new allocation, the new scheduling and the options that need the new flow have no effect for the kernel. Prefer changing the kernel to fit. An allocation failure that is not a memory shortage always raises. See nki_alloc_fallback.

non_ssa_legalization: bool = False#

Let the compiler split a tensor that the kernel fills again with unrelated data into one copy per use. The current allocation can then put the copies at different addresses, so the load for the next use can start while the previous use is still read. Off by default. Enable it in the current flow when a profile shows loads into one tile waiting for the compute that reads it. Each copy has the full size of the tensor. Has no benefit in the new flow, where each copy keeps the address of the original tensor; use split_reused_buffers there. See non_ssa_legalization.

opt_level: nki.compiler.OptLevel | None = None#

How many optimizations the compiler applies in the new flow (an OptLevel). None (default) is OptLevel.O1. OptLevel.O2 turns on most of the options that need the new flow. Has no effect unless allocation=Allocation.LINEAR; the current flow is the same at every level. See Optimization levels.

prefetch_auto: bool | None = None#

Keep DMA prefetch tensors apart from compute tensors in SBUF, so that a DMA that loads the next tile does not wait for compute that still reads the current one. Every SBUF destination of a DMA load from HBM is a prefetch tensor; mark other tensors with nl.ndarray(..., prefetch=True). None (default) follows opt_level: off at OptLevel.O0 and OptLevel.O1, on at OptLevel.O2. True turns it on. False keeps it off, also at OptLevel.O2. Enable it when a profile shows DMA loads that wait for compute on a different tensor at the same SBUF address. The stricter placement can raise peak SBUF usage, and can make a kernel slower when those waits are not the bottleneck. Has no effect unless allocation=Allocation.LINEAR. See prefetch_auto.

register_load_reuse: bool = True#

Let instructions that take the same SBUF tile as their scalar_offset share one register load of the index, instead of one load each. On by default. A reused register stays live until its last use, so turn this off if the kernel fails to compile because too many registers are live at the same time. Applies to instructions at one loop level that read the same element of the tile, with no write to the tile between them. Acts only with the new scheduling (allocation=Allocation.LINEAR and scheduling=True). With neither allocation nor scheduling set, it has no effect; register_load_reuse=False with other combinations raises ValueError. See register_load_reuse.

remove_overwritten_writes: bool | None = None#

Remove an instruction whose output is fully overwritten before an instruction reads it, before the new allocation, so that the instruction takes no engine time. Also removes a write that nothing reads before the kernel ends. An accumulating write, such as a matmul into PSUM that adds to the earlier value, does not overwrite. None (default) follows opt_level: off at OptLevel.O0 and OptLevel.O1, on at OptLevel.O2. True turns it on. False keeps it off, also at OptLevel.O2. Enable it when the kernel initializes a tile, for example with nisa.memset, that a later instruction fully overwrites. Has no effect on a kernel with a branch or with a loop that the compiler does not unroll. Acts only with allocation=Allocation.LINEAR. With no allocation set, it has no effect; with Allocation.COLORING, a value other than the default raises ValueError. See dead_code_elim, remove_unused_instructions and remove_overwritten_writes.

remove_unused_instructions: bool | None = None#

Remove an instruction whose output no other instruction reads, before the new allocation, so that the instruction takes no engine time and its tile takes no memory. Writes to kernel arguments and to nl.shared_hbm tensors are kept. None (default) follows opt_level: off at OptLevel.O0 and OptLevel.O1, on at OptLevel.O2. True turns it on. False keeps it off, also at OptLevel.O2. To keep such instructions, for example to time a section of the kernel whose output is unused, set this option, remove_overwritten_writes and dead_code_elim to False. Acts only with allocation=Allocation.LINEAR. With no allocation set, it has no effect; with Allocation.COLORING, a value other than the default raises ValueError. See dead_code_elim, remove_unused_instructions and remove_overwritten_writes.

sbuf_allocation_fallback: bool = False#

When the new allocation runs out of SBUF or HBM, retry with more compact strategies before failing. For SBUF, RING falls back to FAST and then MAX_REUSE, and FAST falls back to MAX_REUSE. For HBM, the allocation retries once with MAX_REUSE. PSUM is not retried. Off by default, so the kernel keeps the strategy that it selects, and the out-of-memory error lists the strategies to try. True gives the kernel the first strategy that fits, which can make it slower. If every attempt fails, nki_alloc_fallback selects what occurs next. Acts only with allocation=Allocation.LINEAR. With no allocation set, it has no effect; with Allocation.COLORING, a value other than the default raises ValueError. See sbuf_allocation_fallback.

scheduling: bool | None = None#

Whether the compiler schedules the instructions of the kernel in the new flow, before the rest of compilation. True needs allocation=Allocation.LINEAR. False or None (default) keeps the current scheduling. See Allocation and scheduling modes.

split_reused_buffers: bool | None = None#

Split a single-buffered SBUF tile that the kernel fills again with unrelated data into one tile per use, so that the new allocation can put the tiles at different addresses and the load for the next use can start while the previous use is still read. None (default) follows opt_level: off at OptLevel.O0 and OptLevel.O1, on at OptLevel.O2. True turns it on. False keeps it off, also at OptLevel.O2. Enable it when a profile shows a load that waits for the previous reader of the same tile. It does not raise the SBUF that is live at any point. Acts only with allocation=Allocation.LINEAR. With no allocation set, it has no effect; with Allocation.COLORING, a value other than the default raises ValueError. See split_reused_buffers.

tensor_copy_elim: bool | None = None#

Remove a nisa.tensor_copy whose readers can read the source of the copy directly, before the new allocation, so that the copy takes no memory and no engine time. None (default) follows opt_level: off at OptLevel.O0 and OptLevel.O1, on at OptLevel.O2. True turns it on. False keeps it off, also at OptLevel.O2. Enable it when a profile shows copies that only move data that the next instruction could read in place, such as a PSUM to SBUF copy of a matmul result. The source stays allocated until its last reader, so a PSUM tile holds its bank longer. Only a copy that moves a tile unchanged, with constant indices, is removed. Acts only with allocation=Allocation.LINEAR. With no allocation set, it has no effect; with Allocation.COLORING, a value other than the default raises ValueError. See tensor_copy_elim.

This document is relevant for: Trn2, Trn3