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
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).How the compiler allocates the tiles of the kernel (an
Allocation).Compile with the LNC core barrier checker enabled.
Let the compiler remove instructions whose output is never read, and
nisa.tensor_copyinstructions whose readers can read the source directly.Remove a
nisa.dma_copythat loads data that an earliernisa.dma_copyalready loaded into SBUF.Run a
nisa.tensor_copythat does not convert the data type, or anisa.memsetto zero, of 1- or 2-byte elements on fewer 2- or 4-byte elements.Let the new scheduling choose the engine of each instruction that has no
engine=and that more than one engine can run, such asnisa.tensor_scalar,nisa.tensor_tensorornisa.memset, instead of its default engine.How the new allocation selects addresses in SBUF, HBM and PSUM (an
AllocationMode).How the new allocation selects addresses in HBM only.
How the new allocation selects addresses in PSUM only.
How the new allocation selects addresses in SBUF only.
How the new scheduling selects the next instruction from the ready instructions (a
SchedulingHeuristic).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.
Let the compiler split a tensor that the kernel fills again with unrelated data into one copy per use.
How many optimizations the compiler applies in the new flow (an
OptLevel).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.
Let instructions that take the same SBUF tile as their
scalar_offsetshare one register load of the index, instead of one load each.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 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.
When the new allocation runs out of SBUF or HBM, retry with more compact strategies before failing.
Whether the compiler schedules the instructions of the kernel in the new flow, before the rest of compilation.
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.
Remove a
nisa.tensor_copywhose 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. Withallocation=Allocation.LINEAR,OptLevel.O2changesAUTOtoENABLE. KeepAUTOunless 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.COLORINGkeeps the current flow also when the default changes.Allocation.LINEARselects the new flow, which the options that need the new flow require. See Allocation and scheduling modes.
- dead_code_elim: bool = True#
Let the compiler remove instructions whose output is never read, and
nisa.tensor_copyinstructions whose readers can read the source directly. On by default, in every flow. SetFalseto 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, seeremove_unused_instructionsandremove_overwritten_writes. See dead_code_elim, remove_unused_instructions and remove_overwritten_writes.
- dma_copy_elim: bool | None = None#
Remove a
nisa.dma_copythat loads data that an earliernisa.dma_copyalready 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) followsopt_level: off atOptLevel.O0andOptLevel.O1, on atOptLevel.O2.Trueturns it on.Falsekeeps it off, also atOptLevel.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 unlessallocation=Allocation.LINEAR. See dma_copy_elim.
- element_widening: bool | None = None#
Run a
nisa.tensor_copythat does not convert the data type, or anisa.memsetto 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) followsopt_level: off atOptLevel.O0andOptLevel.O1, on atOptLevel.O2.Trueturns it on.Falsekeeps it off, also atOptLevel.O2. Enable it when a profile shows such copies or memsets ofbfloat16,float16,float8orint8data that take a noticeable share of engine time. It does not change memory use. Has no effect unlessallocation=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 asnisa.tensor_scalar,nisa.tensor_tensorornisa.memset, instead of its default engine. The new scheduling already chooses the engine ofnisa.tensor_copywithout this option.None(default) followsopt_level: off atOptLevel.O0andOptLevel.O1, on atOptLevel.O2.Trueturns it on.Falsekeeps it off, also atOptLevel.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.LINEARandscheduling=True). With neitherallocationnorschedulingset, it has no effect; other combinations raiseValueError. 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) isRING.FASTandMAX_REUSEuse less memory, with less overlap of loads and compute; try them when a kernel runs out of memory underRING. Acts only withallocation=Allocation.LINEAR. With noallocationset, it has no effect; withAllocation.COLORING, a value other than the default raisesValueError. 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) usesexperimental_allocation_mode. UnderRINGthe 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. SetFASTorMAX_REUSEfor 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) usesexperimental_allocation_mode. PSUM has 8 banks, soRINGis costly there;FASTorMAX_REUSEkeeps 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) usesexperimental_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) isASAP, 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.LINEARandscheduling=True). With neitherallocationnorschedulingset, it has no effect; other combinations raiseValueError.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_buffersthere. 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) isOptLevel.O1.OptLevel.O2turns on most of the options that need the new flow. Has no effect unlessallocation=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) followsopt_level: off atOptLevel.O0andOptLevel.O1, on atOptLevel.O2.Trueturns it on.Falsekeeps it off, also atOptLevel.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 unlessallocation=Allocation.LINEAR. See prefetch_auto.
- register_load_reuse: bool = True#
Let instructions that take the same SBUF tile as their
scalar_offsetshare 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.LINEARandscheduling=True). With neitherallocationnorschedulingset, it has no effect;register_load_reuse=Falsewith other combinations raisesValueError. 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) followsopt_level: off atOptLevel.O0andOptLevel.O1, on atOptLevel.O2.Trueturns it on.Falsekeeps it off, also atOptLevel.O2. Enable it when the kernel initializes a tile, for example withnisa.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 withallocation=Allocation.LINEAR. With noallocationset, it has no effect; withAllocation.COLORING, a value other than the default raisesValueError. 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_hbmtensors are kept.None(default) followsopt_level: off atOptLevel.O0andOptLevel.O1, on atOptLevel.O2.Trueturns it on.Falsekeeps it off, also atOptLevel.O2. To keep such instructions, for example to time a section of the kernel whose output is unused, set this option,remove_overwritten_writesanddead_code_elimtoFalse. Acts only withallocation=Allocation.LINEAR. With noallocationset, it has no effect; withAllocation.COLORING, a value other than the default raisesValueError. 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,
RINGfalls back toFASTand thenMAX_REUSE, andFASTfalls back toMAX_REUSE. For HBM, the allocation retries once withMAX_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.Truegives the kernel the first strategy that fits, which can make it slower. If every attempt fails,nki_alloc_fallbackselects what occurs next. Acts only withallocation=Allocation.LINEAR. With noallocationset, it has no effect; withAllocation.COLORING, a value other than the default raisesValueError. 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.
Trueneedsallocation=Allocation.LINEAR.FalseorNone(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) followsopt_level: off atOptLevel.O0andOptLevel.O1, on atOptLevel.O2.Trueturns it on.Falsekeeps it off, also atOptLevel.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 withallocation=Allocation.LINEAR. With noallocationset, it has no effect; withAllocation.COLORING, a value other than the default raisesValueError. See split_reused_buffers.
- tensor_copy_elim: bool | None = None#
Remove a
nisa.tensor_copywhose 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) followsopt_level: off atOptLevel.O0andOptLevel.O1, on atOptLevel.O2.Trueturns it on.Falsekeeps it off, also atOptLevel.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 withallocation=Allocation.LINEAR. With noallocationset, it has no effect; withAllocation.COLORING, a value other than the default raisesValueError. See tensor_copy_elim.
This document is relevant for: Trn2, Trn3