Source code for nki.compiler

# AUTO GENERATED FILE - DO NOT MANUALLY MODIFY.

from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
from typing import *

[docs]class AddressRotation(Enum): r"""Which tensors the compiler can move to a different SBUF address or PSUM bank after allocation (see :attr:`CompileConfig.address_rotation`). - ``AUTO`` (default): the compiler can move the tensors that the current allocation places. A tensor whose address is fixed keeps it: a tensor allocated with ``nl.ndarray(..., address=...)``, and, in the new flow, every tensor that the new allocation places. - ``ENABLE``: the compiler can move every tensor, also a tensor whose address is fixed. - ``DISABLE``: the compiler moves no tensor.""" AUTO = 'auto' ENABLE = 'enable' DISABLE = 'disable' ...
[docs]@dataclass(frozen=True, kw_only=True) class CompileConfig(): r"""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 :ref:`nki-compile-config` for the current and new flows and for the options that need more than a few sentences.""" allocation: Allocation | None = None r"""How the compiler allocates the tiles of the kernel (an :class:`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 :ref:`nki-compile-config-compile-flow`.""" scheduling: bool | None = None r"""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 :ref:`nki-compile-config-compile-flow`.""" opt_level: OptLevel | None = None r"""How many optimizations the compiler applies in the new flow (an :class:`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 :ref:`nki-compile-config-opt-levels`.""" address_rotation: AddressRotation = AddressRotation.AUTO r"""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 :class:`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 :ref:`nki-compile-config-address-rotation`.""" non_ssa_legalization: bool = False r"""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 :ref:`nki-compile-config-non-ssa-legalization`.""" dead_code_elim: bool = True r"""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 :ref:`nki-compile-config-dead-code-elim`.""" barrier_check: bool = False r"""Compile with the LNC core barrier checker enabled. Off by default.""" prefetch_auto: bool | None = None r"""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 :ref:`nki-compile-config-prefetch-auto`.""" nki_alloc_fallback: bool = False r"""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 :ref:`nki-compile-config-nki-alloc-fallback`.""" dma_copy_elim: bool | None = None r"""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 :ref:`nki-compile-config-dma-copy-elim`.""" element_widening: bool | None = None r"""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 :ref:`nki-compile-config-element-widening`.""" tensor_copy_elim: bool | None = None r"""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 :ref:`nki-compile-config-tensor-copy-elim`.""" remove_unused_instructions: bool | None = None r"""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 :ref:`nki-compile-config-dead-code-elim`.""" remove_overwritten_writes: bool | None = None r"""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 :ref:`nki-compile-config-dead-code-elim`.""" split_reused_buffers: bool | None = None r"""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 :ref:`nki-compile-config-split-reused-buffers`.""" experimental_allocation_mode: AllocationMode | None = None r"""How the new allocation selects addresses in SBUF, HBM and PSUM (an :class:`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 :ref:`nki-compile-config-sbuf-strategies`. .. warning:: This API is experimental and may change in future releases.""" experimental_sbuf_allocation_mode: AllocationMode | None = None r"""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_hbm_allocation_mode: AllocationMode | None = None r"""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 :ref:`nki-compile-config-hbm-ring`. .. warning:: This API is experimental and may change in future releases.""" experimental_psum_allocation_mode: AllocationMode | None = None r"""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.""" sbuf_allocation_fallback: bool = False r"""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 :ref:`nki-compile-config-sbuf-allocation-fallback`.""" engine_balancing: bool | None = None r"""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 :ref:`nki-compile-config-engine-balancing`.""" experimental_scheduling_heuristic: SchedulingHeuristic | None = None r"""How the new scheduling selects the next instruction from the ready instructions (a :class:`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.""" register_load_reuse: bool = True r"""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 :ref:`nki-compile-config-register-load-reuse`.""" ...
[docs]@dataclass(frozen=True) class SpecializationConfig(): r"""Per-kernel specialization-time config passed to ``@nki.jit(specialization_config=...)``. Options that tune the specialization phase (turning the kernel into IR).""" allow_nested_config_override: bool = False r"""When this kernel is the top-level compiled kernel, warn (instead of raising) if a nested @nki.jit sub-kernel's config disagrees, and let it inline under this kernel's config.""" ...
[docs]class AllocationMode(Enum): r"""How the new allocation selects an address for each tile (``CompileConfig.experimental_allocation_mode``). - ``RING``: give each tile a fresh address, and reuse freed memory only after the allocation wraps around, so a DMA into a new tile seldom waits for compute that reads an earlier tile. Fragments SBUF the most. In PSUM, rotates through the 8 banks. - ``FAST``: stack tiles whose lifetimes nest, and put every other tile in the freed memory that fits it best. Compact, but a tile can take the address of a tile freed shortly before, so a DMA into it waits for the compute that reads the earlier tile. - ``MAX_REUSE``: always put a tile in the freed memory that fits it best. Uses the least memory, with the least overlap of loads and compute.""" RING = 'ring' FAST = 'fast' MAX_REUSE = 'max-reuse' ...
[docs]class SchedulingHeuristic(Enum): r"""How the new scheduling selects the next instruction from the ready instructions (``CompileConfig.experimental_scheduling_heuristic``). - ``ASAP`` (default): select the instruction that can start first. - ``CRITICAL_PATH``: prefer the instruction on the longest path to the kernel's outputs. - ``DMA_PREFETCH``: prefer a DMA to an instruction that is not a DMA. - ``TRIVIAL``: keep the program order of the kernel.""" ASAP = 'asap' CRITICAL_PATH = 'critical-path' DMA_PREFETCH = 'dma-prefetch' TRIVIAL = 'trivial' ...
[docs]class Allocation(Enum): r"""How the compiler assigns SBUF, PSUM and HBM addresses to the tiles of the kernel (``CompileConfig.allocation``). - ``COLORING``: the current flow. The compiler allocates the tiles with graph coloring after it traces the kernel. - ``LINEAR``: the new flow. The compiler allocates the tiles with linear scan before the rest of compilation. It assigns an address to a tile at its first write and releases the address after its last read. See :ref:`nki-compile-config-compile-flow`.""" COLORING = 'coloring' LINEAR = 'linear' ...
[docs]class OptLevel(Enum): r"""How many optimizations the compiler applies in the new flow (``CompileConfig.opt_level``). - ``O0``: a predictable baseline. Each instruction that can run on more than one engine and has no ``engine=`` runs on its default engine, also ``nisa.tensor_copy``. - ``O1`` (default): the compiler chooses the engine of each ``nisa.tensor_copy`` that has no ``engine=``. Other instructions that have no ``engine=`` run on their default engine, unless ``engine_balancing`` is on. - ``O2``: ``O1``, plus the optimizations that the flow can turn on, for example ``prefetch_auto``, ``dma_copy_elim`` and ``element_widening``. The current flow is the same at every level. See :ref:`nki-compile-config-opt-levels`.""" O0 = 'O0' O1 = 'O1' O2 = 'O2' ...