"""Python API declarations for the PyPTO Pro DSL (``pl.xxx``).
These declarations exist so that:
- IDE "Go to Definition" works for every ``pl.xxx`` call
- Python catches typos at import time (``pl.tensr`` raises ``AttributeError``)
- Type checkers can validate argument types
- Docstrings document the user-facing calling convention
None of these functions are meant to be called at runtime. Inside a PyPTO
kernel the AST parser intercepts every ``pl.xxx`` call before Python executes
it. Outside a kernel, calling a declaration raises ``RuntimeError``.
When adding a new op to the parser registry / block-default handler, add a
matching declaration here and re-export it from the package ``__init__.py``.
"""
from __future__ import annotations
import builtins
from contextlib import contextmanager
import functools
import inspect
from typing import Any, List, Optional, Sequence, Union
from pypto.ir import (
AccPhase,
AccToVecMode,
AtomicType,
QuantMode,
ReluPreMode,
RoundMode,
SaturationFlagMode,
STPhase,
)
from pypto_pro.ir.op.block_ops import FillPadMode
from .._errors import InvalidOperation
from . import MemorySpace, TensorLayout, TilePad
from .typing import Ptr
Tile = Any
TileGroup = Any
Tensor = Any
Scalar = Any
DType = Any
Offset = Sequence[int]
Shape = Sequence[int]
Struct = Any
_API_MSG = "This function is a DSL API declaration and must be used inside a PyPTO kernel"
def _api_decl(func):
sig = inspect.signature(func)
@functools.wraps(func)
def wrapper(*args, **kwargs):
sig.bind(*args, **kwargs)
raise InvalidOperation(_API_MSG)
wrapper.__wrapped__ = func
wrapper.bind_declared = sig.bind
return wrapper
@_api_decl
def load(
dst_tile: Tile,
src_tensor: Tensor,
/,
offsets: Offset,
*,
order: Optional[List[int]] = None,
) -> None:
"""Load data from GM Tensor into on-chip Tile by absolute element coordinates.
Args:
dst_tile: Destination Tile (L1 or UB only)
src_tensor: Source Tensor (global memory, from kernel parameter)
offsets: Element-level offset per axis, e.g. ``[row, col]`` or ``[b, n, sq, sk]``
order: Optional, mapping of Tile dimensions to Tensor axes.
Each element is an absolute axis index of the Tensor.
Ascending order (e.g. ``[0, 1]``) loads without transposition;
descending order (e.g. ``[1, 0]``) loads with transposition (DN layout).
Default: last N axes ascending (N = Tile ndim), i.e. no transposition.
**Merged rows (GM -> L1 only).** Naming 3 axes for a rank-2 Tile flattens the
first two into the Tile's row axis, ``row = i_outer * inner + i_inner``, so a
Tensor ``[B, S, N, G, D]`` can feed a matmul that wants (S, G) as one axis::
pl.load(l1, x, [b, s0, h, 0, k0], order=[1, 3, 4]) # NZ [S_tile*G, D_tile]
pl.load(l1, x, [b, s0, h, 0, k0], order=[4, 3, 1]) # ZN [D_tile, S_tile*G]
Multi-ND nd2nz issues either form as matrices of ``inner`` rows, where ``inner``
is the middle axis's extent. Static and dynamic inner extents support the same
partial-matrix transfers; the merged Tile extent need not be a multiple of it.
The middle axis offset must be a compile-time zero. Source and destination
dtypes must match and use 8, 16 or 32-bit elements; the destination uses a
512-byte fractal. The source column stride must be 1.
Requires PTO-ISA multi-ND2NZ and multi-DN2ZN TLOAD with partial-matrix support.
TLOAD uses a 3D descriptor with a bounded matrix extent. Source stride and static
Tile shape limits follow the selected PTO-ISA implementation.
Runtime valid shapes limit the transfer; a partial final matrix uses a separate DMA.
The caller must keep runtime valid extents positive and within the declared Tile
shape, with a valid source covering the requested transfer and a positive inner extent.
The last axis must be the Tensor's innermost -- nd2nz reads each row as
contiguous elements and has no column stride. The reversed spelling is the same
transfer under the other label: an NZ ``[M, K]`` tile and a ZN ``[K, M]`` tile at
one address are the same bytes, so it costs nothing and the Mat->L0 move delivers
the transposed operand. Any other permutation of the three axes is rejected.
Merged orders are currently unsupported for Vec (UB), load_tile and store.
A UB reinterpret can reshape already packed data, but cannot gather separated GM regions.
"""
@_api_decl
def load_tile(
dst_tile: Tile, src_tensor: Tensor, /, tile_offsets: Offset, *, order: Optional[List[int]] = None
) -> None:
"""Load data from GM Tensor into on-chip Tile by tile-block index.
Offsets are in tile-block units, internally multiplied by tile shape.
E.g. with tile shape ``[64, 128]``, ``tile_offsets=[2, 2]`` equals
``load`` with absolute offsets ``[128, 256]``.
Args:
dst_tile: Destination Tile (L1 or UB only)
src_tensor: Source Tensor (global memory)
tile_offsets: Tile-block index, e.g. ``[tile_row, tile_col]``
order: Optional, mapping of Tile dimensions to Tensor axes.
Each element is an absolute axis index of the Tensor.
Ascending order (e.g. ``[0, 1]``) loads without transposition;
descending order (e.g. ``[1, 0]``) loads with transposition (DN layout).
Default: last N axes ascending (N = Tile ndim), i.e. no transposition.
"""
@_api_decl
def store(
dst_tensor: Tensor,
src_tile: Tile,
/,
offsets: Offset,
*,
relu_pre_mode: Optional[ReluPreMode] = None,
scale: Optional[Union[int, float, Tile]] = None,
order: Optional[List[int]] = None,
atomic: AtomicType = AtomicType.AtomicNone,
phase: Optional[STPhase] = None,
) -> None:
"""Store on-chip Tile back to GM Tensor by absolute element coordinates.
Args:
dst_tensor: Destination Tensor (global memory)
src_tile: Source Tile (on-chip buffer)
offsets: Element-level offset per axis, e.g. ``[row, col]`` or ``[b, n, sq, sk]``
relu_pre_mode: Optional ReLU fusion — ``pl.ReluPreMode.NormalRelu``
scale: Optional fixpipe quantization scale.
``float`` or runtime scalar → per-tensor quantization (deqScalar path);
a runtime **FP32** scalar is auto-reinterpreted as its IEEE-754 bit
pattern (codegen bitcast), so pass the raw float value; a runtime
**INT32/INT64** scalar must carry the pre-encoded float32 bit pattern
(``struct.pack("!f", scale)``). Other runtime scalar dtypes
(FP16/BF16/unsigned/narrower ints) are rejected at parse time.
``Tile`` (INT64, MemorySpace.Scaling, shape ``[1, N]``) → per-channel quantization with a
**user-prepared deqTensor tile**; the framework reuses it directly (no auto-allocation,
no sync insertion) — the user owns the data flow and must ensure the tile is ready
(load → move → sync MTE1→FIX) before the store.
Hardware requires ``[1, N]`` (row == 1, per-column), ``N % 16 == 0`` and ``N <= 512`` —
``[N, 1]`` per-row scaling is NOT supported.
``None`` → no fixpipe quantization.
Quantization direction (quantize vs dequantize) is determined by ``dst_tensor`` dtype:
INT8 → quantize; FP16 → dequantize (from INT32 L0C). Unsupported scale combos —
UINT8 output, and FP32/INT32 → BF16 output — are rejected at parse time
(hardware fixpipe has no unsigned requantization and only dequantizes INT32→FP16).
order: Optional, which axes of the Tensor the Tile dimensions map to.
When the Tensor has more dimensions than the Tile, this specifies the mapping.
E.g. ``order=[0, 2]`` means Tile dim 0 → Tensor axis 0, Tile dim 1 → Tensor axis 2.
Default: last N axes of the Tensor (N = Tile ndim)
atomic: Atomic write mode — ``pl.AtomicType.AtomicNone`` (overwrite) or
``pl.AtomicType.AtomicAdd`` (atomic accumulate)
phase: Fixpipe drain phase — ``pl.STPhase.Partial`` or ``pl.STPhase.Final``
"""
@_api_decl
def store_tile(
dst_tensor: Tensor,
src_tile: Tile,
/,
tile_offsets: Offset,
*,
relu_pre_mode: Optional[ReluPreMode] = None,
scale: Optional[Union[int, float, Tile]] = None,
order: Optional[List[int]] = None,
atomic: AtomicType = AtomicType.AtomicNone,
phase: Optional[STPhase] = None,
) -> None:
"""Store on-chip Tile back to GM Tensor by tile-block index.
Args:
dst_tensor: Destination Tensor (global memory)
src_tile: Source Tile (on-chip buffer)
tile_offsets: Tile-block index, e.g. ``[tile_row, tile_col]``;
internally multiplied by tile shape to get element offsets
relu_pre_mode: Optional ReLU fusion — ``pl.ReluPreMode.NormalRelu``
scale: Optional fixpipe quantization scale.
``float`` or runtime scalar → per-tensor quantization (deqScalar path);
a runtime **FP32** scalar is auto-reinterpreted as its IEEE-754 bit
pattern (codegen bitcast), so pass the raw float value; a runtime
**INT32/INT64** scalar must carry the pre-encoded float32 bit pattern
(``struct.pack("!f", scale)``). Other runtime scalar dtypes
(FP16/BF16/unsigned/narrower ints) are rejected at parse time.
``Tile`` (INT64, MemorySpace.Scaling, shape ``[1, N]``) → per-channel quantization with a
**user-prepared deqTensor tile**; the framework reuses it directly (no auto-allocation,
no sync insertion) — the user owns the data flow and must ensure the tile is ready
(load → move → sync MTE1→FIX) before the store.
Hardware requires ``[1, N]`` (row == 1, per-column), ``N % 16 == 0`` and ``N <= 512`` —
``[N, 1]`` per-row scaling is NOT supported.
``None`` → no fixpipe quantization.
order: Optional, which axes of the Tensor the Tile dimensions map to.
When the Tensor has more dimensions than the Tile, this specifies the mapping.
E.g. ``order=[0, 2]`` means Tile dim 0 → Tensor axis 0, Tile dim 1 → Tensor axis 2.
Default: last N axes of the Tensor (N = Tile ndim)
atomic: Atomic write mode — ``pl.AtomicType.AtomicNone`` (overwrite) or
``pl.AtomicType.AtomicAdd`` (atomic accumulate)
phase: Fixpipe drain phase — ``pl.STPhase.Partial`` or ``pl.STPhase.Final``
"""
@_api_decl
def init_output(
tensor: Tensor,
*,
offset: int = 0,
size: int,
value: Union[int, float] = 0,
) -> None:
"""Initialize a region of a GM Tensor with a scalar value.
The framework allocates a temporary UB buffer, fills it with *value*
via the V pipeline (Duplicate), then copies it to GM via the MTE3
pipeline (DataCopyPad). V→MTE3 synchronization is inserted
automatically. Must be called before Tile buffer allocation
(``make_tile_group`` etc.).
This op never emits a cross-core barrier (``pipe_barrier`` would only
drain the calling core's pipes). When the initialized GM data is
consumed by other cores or the cube sub-core (e.g. a subsequent
``pl.matmul``), the caller must explicitly call
``pl.system.sync_all(core_type=pl.SyncCoreType.MIX)`` on both sides
(or an equivalent cross-core sync primitive) after this call.
Args:
tensor: Destination GM Tensor to initialize.
offset: Element-level offset from the start of *tensor* (default 0).
size: Number of elements to initialize.
value: Fill value — int/float literal or ``pl.const()`` scalar;
must be type-compatible with *tensor*'s dtype (default 0).
"""
@_api_decl
def move(
dst_tile: Tile,
src_tile: Tile,
/,
offset: Optional[Offset] = None,
*,
acc_to_vec_mode: Optional[AccToVecMode] = None,
relu_pre_mode: Optional[ReluPreMode] = None,
scale: Optional[Union[int, float, Tile]] = None,
phase: Optional[STPhase] = None,
) -> None:
"""Move data between on-chip Tiles (tile↔tile, no GM access).
Supported memory-space paths:
============ ============ ========
src dst pipe
============ ============ ========
Acc (L0C) Vec (UB) fix
Acc (L0C) Mat (L1) fix
Mat (L1) Left (L0A) mte1
Mat (L1) Right (L0B) mte1
Mat (L1) Vec (UB) v
Vec (UB) Mat (L1) mte3
others — v
============ ============ ========
Supported fusion (side operations):
- ``acc_to_vec_mode``: Acc→Vec conversion mode (single/dual split M/N)
- ``relu_pre_mode``: ReLU activation before destination
- ``scale``: fixpipe quantization scale (per-tensor or per-channel)
- ``phase``: FixPipe unit-flag phase for Acc→Vec or Acc→Mat moves
Args:
dst_tile: Destination Tile
src_tile: Source Tile
offset: Optional ``[offset_m, offset_k]`` to extract a sub-block from a wider source Tile
acc_to_vec_mode: Acc→Vec mode — ``pl.AccToVecMode.SingleModeVec0``, ``pl.AccToVecMode.SingleModeVec1``,
``pl.AccToVecMode.DualModeSplitM``, ``pl.AccToVecMode.DualModeSplitN``;
only meaningful when src is Acc and dst is Vec
relu_pre_mode: Optional ReLU fusion — ``pl.ReluPreMode.NormalRelu``
scale: Optional fixpipe quantization scale.
``float`` or runtime scalar → per-tensor quantization (deqScalar path);
a runtime **FP32** scalar is auto-reinterpreted as its IEEE-754 bit
pattern (codegen bitcast), so pass the raw float value; a runtime
**INT32/INT64** scalar must carry the pre-encoded float32 bit pattern
(``struct.pack("!f", scale)``). Other runtime scalar dtypes
(FP16/BF16/unsigned/narrower ints) are rejected at parse time.
``Tile`` (INT64, MemorySpace.Scaling, shape ``[1, N]``) → per-channel quantization with a
**user-prepared deqTensor tile**; the framework reuses it directly (no auto-allocation,
no sync insertion) — the user owns the data flow and must ensure the tile is ready
(load → move → sync MTE1→FIX) before the move.
Hardware requires ``[1, N]`` (row == 1, per-column), ``N % 16 == 0`` and ``N <= 512`` —
``[N, 1]`` per-row scaling is NOT supported.
``None`` → no fixpipe quantization.
phase: Optional FixPipe unit-flag phase for Acc→Vec or Acc→Mat —
``pl.STPhase.Partial`` or ``pl.STPhase.Final``. It must be paired
correctly with the producer's ``AccPhase`` sequence. Acc→Mat
supports TMOV, TEXTRACT, and TINSERT; Acc→Vec does not support an
``offset`` together with phase.
"""
@_api_decl
def insert(
dst_tile: Tile,
src_tile: Tile,
/,
offset: Offset,
*,
relu_pre_mode: Optional[ReluPreMode] = None,
scale: Optional[Union[int, float, Tile]] = None,
phase: Optional[STPhase] = None,
) -> None:
"""Insert a small Tile into a larger Tile at the given 2-D offset (TINSERT).
Args:
dst_tile: Destination (larger) Tile
src_tile: Source (smaller) Tile
offset: Insertion coordinates ``[row, col]`` in the destination
relu_pre_mode: Optional ReLU fusion for Acc→Mat
scale: Optional scalar or Scaling Tile for Acc→Mat FixPipe quantization
phase: Optional FixPipe unit-flag phase for Acc→Mat —
``pl.STPhase.Partial`` or ``pl.STPhase.Final``. It must be paired
correctly with the producer's ``AccPhase`` sequence.
"""
@_api_decl
def ssbuf_load(struct_var: Struct, offset: int, /) -> None:
"""Load data from SuperScalar Buffer (SSBUF) into a struct variable.
Args:
struct_var: Struct variable (created via ``pl.struct``)
offset: SSBUF offset
"""
@_api_decl
def ssbuf_store(struct_var: Struct, offset: int, /) -> None:
"""Write a struct variable to SuperScalar Buffer (SSBUF).
Args:
struct_var: Struct variable (created via ``pl.struct``)
offset: SSBUF offset
"""
@_api_decl
def add(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise addition: ``out = lhs + rhs``
Supports both tile-tile and tile-scalar operations:
- ``add(out, tile_a, tile_b)`` -> tile-tile
- ``add(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def sub(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise subtraction: ``out = lhs - rhs``
Supports both tile-tile and tile-scalar operations:
- ``sub(out, tile_a, tile_b)`` -> tile-tile
- ``sub(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def mul(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise multiplication: ``out = lhs * rhs``
Supports both tile-tile and tile-scalar operations:
- ``mul(out, tile_a, tile_b)`` -> tile-tile
- ``mul(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def div(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise division: ``out = lhs / rhs``
Supports both tile-tile and tile-scalar operations:
- ``div(out, tile_a, tile_b)`` -> tile-tile
- ``div(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def rem(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise remainder (truncation semantics): ``out = lhs % rhs``
Supports both tile-tile and tile-scalar operations:
- ``rem(out, tile_a, tile_b)`` -> tile-tile
- ``rem(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def and_(out: Tile, lhs: Tile, rhs: Union[Tile, int]) -> None:
"""Element-wise bitwise AND: ``out = lhs & rhs``
Supports both tile-tile and tile-scalar operations:
- ``and_(out, tile_a, tile_b)`` -> tile-tile
- ``and_(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def or_(out: Tile, lhs: Tile, rhs: Union[Tile, int]) -> None:
"""Element-wise bitwise OR: ``out = lhs | rhs``
Supports both tile-tile and tile-scalar operations:
- ``or_(out, tile_a, tile_b)`` -> tile-tile
- ``or_(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def shl(out: Tile, lhs: Tile, rhs: Union[Tile, int]) -> None:
"""Element-wise left shift: ``out = lhs << rhs``
Supports both tile-tile and tile-scalar operations:
- ``shl(out, tile_a, tile_b)`` -> tile-tile
- ``shl(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def shr(out: Tile, lhs: Tile, rhs: Union[Tile, int]) -> None:
"""Element-wise right shift: ``out = lhs >> rhs``
Supports both tile-tile and tile-scalar operations:
- ``shr(out, tile_a, tile_b)`` -> tile-tile
- ``shr(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def xor(out: Tile, lhs: Tile, rhs: Union[Tile, int], tmp: Tile) -> None:
"""Element-wise bitwise XOR: ``out = lhs ^ rhs``
Args:
out: Destination Tile
lhs: Left-hand Tile
rhs: Right-hand Tile or Scalar (tile-scalar path lowers to ``block.xors``)
tmp: Workspace Tile
"""
@_api_decl
def expands(out: Tile, scalar: Union[int, float]) -> None:
"""Fill Tile with a scalar (splat): ``out[i] = scalar``"""
@_api_decl
def neg(out: Tile, src: Tile) -> None:
"""Element-wise negate: ``out = -src``"""
@_api_decl
def not_(out: Tile, src: Tile) -> None:
"""Element-wise bitwise NOT: ``out = ~src``"""
@_api_decl
def abs(out: Tile, src: Tile) -> None:
"""Element-wise absolute value: ``out = |src|``"""
@_api_decl
def exp(out: Tile, src: Tile) -> None:
"""Element-wise exponential: ``out = exp(src)``"""
@_api_decl
def log(out: Tile, src: Tile) -> None:
"""Element-wise natural log: ``out = log(src)``"""
@_api_decl
def sqrt(out: Tile, src: Tile) -> None:
"""Element-wise square root: ``out = sqrt(src)``"""
@_api_decl
def rsqrt(out: Tile, src: Tile) -> None:
"""Element-wise reciprocal square root: ``out = 1/sqrt(src)``"""
@_api_decl
def recip(out: Tile, src: Tile) -> None:
"""Element-wise reciprocal: ``out = 1/src``"""
@_api_decl
def relu(out: Tile, src: Tile) -> None:
"""Element-wise ReLU: ``out = max(0, src)``"""
@_api_decl
def fillpad(out: Tile, src: Tile, *, mode: FillPadMode = FillPadMode.NORMAL) -> None:
"""Fill padding region of a Tile.
Args:
out: Destination Tile.
src: Source Tile.
mode: Fill mode — ``pl.FillPadMode.NORMAL`` (default, dst and src same shape),
``pl.FillPadMode.EXPAND`` (dst larger than src, expand fill),
``pl.FillPadMode.INPLACE`` (dst and src share the same address).
"""
@_api_decl
def cast(out: Tile, src: Tile, *, mode: RoundMode = RoundMode.CAST_ROUND) -> None:
"""Cast Tile to a different data type.
The target dtype is inferred from ``out`` tile's dtype.
Args:
out: Destination Tile (determines target dtype)
src: Source Tile
mode: Rounding mode — ``pl.RoundMode.CAST_NONE``, ``pl.RoundMode.CAST_RINT``, ``pl.RoundMode.CAST_ROUND``,
``pl.RoundMode.CAST_FLOOR``, ``pl.RoundMode.CAST_CEIL``,
``pl.RoundMode.CAST_TRUNC``, ``pl.RoundMode.CAST_ODD``
"""
@_api_decl
def add_relu_cast(
out: Tile, lhs: Tile, rhs: Tile, *, target_type: DType, mode: RoundMode = RoundMode.CAST_ROUND
) -> None:
"""Fused add + ReLU + cast: ``out = cast(relu(lhs + rhs), target_type)``"""
@_api_decl
def sub_relu_cast(
out: Tile, lhs: Tile, rhs: Tile, *, target_type: DType, mode: RoundMode = RoundMode.CAST_ROUND
) -> None:
"""Fused sub + ReLU + cast: ``out = cast(relu(lhs - rhs), target_type)``"""
@_api_decl
def mul_cast(out: Tile, lhs: Tile, rhs: Tile, *, target_type: DType, mode: RoundMode = RoundMode.CAST_ROUND) -> None:
"""Fused mul + cast: ``out = cast(lhs * rhs, target_type)``"""
@_api_decl
def eq(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise equal: ``out = (lhs == rhs)``
Supports both tile-tile and tile-scalar operations:
- ``eq(out, tile_a, tile_b)`` -> tile-tile
- ``eq(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def ne(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise not equal: ``out = (lhs != rhs)``
Supports both tile-tile and tile-scalar operations:
- ``ne(out, tile_a, tile_b)`` -> tile-tile
- ``ne(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def lt(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise less than: ``out = (lhs < rhs)``
Supports both tile-tile and tile-scalar operations:
- ``lt(out, tile_a, tile_b)`` -> tile-tile
- ``lt(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def le(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise less or equal: ``out = (lhs <= rhs)``
Supports both tile-tile and tile-scalar operations:
- ``le(out, tile_a, tile_b)`` -> tile-tile
- ``le(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def gt(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise greater than: ``out = (lhs > rhs)``
Supports both tile-tile and tile-scalar operations:
- ``gt(out, tile_a, tile_b)`` -> tile-tile
- ``gt(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def ge(out: Tile, lhs: Tile, rhs: Union[Tile, int, float]) -> None:
"""Element-wise greater or equal: ``out = (lhs >= rhs)``
Supports both tile-tile and tile-scalar operations:
- ``ge(out, tile_a, tile_b)`` -> tile-tile
- ``ge(out, tile_a, scalar)`` -> tile-scalar
"""
@_api_decl
def select(out: Tile, mask: Tile, lhs: Tile, rhs: Union[Tile, int, float], tmp: Tile) -> None:
"""Select by mask: ``out[i] = lhs[i] if mask[i] else rhs[i]``
Supports both tile-tile and tile-scalar operations:
- ``select(out, mask, tile_a, tile_b, tmp)`` -> tile-tile
- ``select(out, mask, tile_a, scalar, tmp)`` -> tile-scalar
Args:
out: Destination Tile
mask: Mask Tile (from ``eq``/``gt``/...)
lhs: Tile selected when mask is true
rhs: Tile or Scalar selected when mask is false
tmp: Workspace Tile
"""
@_api_decl
def add_relu(out: Tile, lhs: Tile, rhs: Tile) -> None:
"""Fused add + ReLU: ``out = relu(lhs + rhs)``"""
@_api_decl
def sub_relu(out: Tile, lhs: Tile, rhs: Tile) -> None:
"""Fused sub + ReLU: ``out = relu(lhs - rhs)``"""
@_api_decl
def mul_add_dst(out: Tile, lhs: Tile, rhs: Tile) -> None:
"""Fused multiply-add into out: ``out = lhs * rhs + out``"""
@_api_decl
def fused_mul_add(out: Tile, lhs: Tile, rhs: Tile) -> None:
"""Fused multiply-add in-place: ``out = lhs * out + rhs``"""
@_api_decl
def fused_mul_add_relu(out: Tile, a: Tile, b: Tile) -> None:
"""Fused multiply-add + ReLU in-place: ``out = relu(out * a + b)``"""
@_api_decl
def axpy(out: Tile, src: Tile, alpha: Union[int, float], /) -> None:
"""AXPY: ``out[i] = alpha * src[i] + out[i]``
Args:
out: Destination Tile (also accumulates)
src: Source Tile
alpha: Scalar multiplier
"""
@_api_decl
def partadd(out: Tile, src0: Tile, src1: Tile) -> None:
"""Partial addition: ``out = src0 + src1`` (only src1's valid region)."""
@_api_decl
def partmax(out: Tile, src0: Tile, src1: Tile) -> None:
"""Partial maximum over the valid regions of two Tiles."""
@_api_decl
def partmin(out: Tile, src0: Tile, src1: Tile) -> None:
"""Partial minimum over the valid regions of two Tiles."""
@_api_decl
def partmul(out: Tile, src0: Tile, src1: Tile) -> None:
"""Partial multiplication over the valid regions of two Tiles."""
@_api_decl
def matmul(
dst_tile: Tile, lhs_tile: Tile, rhs_tile: Tile, bias_tile: Optional[Tile] = None, /,
*, phase: Optional[AccPhase] = None
) -> None:
"""Matrix multiply: ``dst = lhs @ rhs`` (L0A × L0B → L0C).
When ``bias_tile`` is provided, performs fused matmul + bias:
``dst = lhs @ rhs + bias`` (bias row-broadcast [1,N], added in fixpipe).
Args:
dst_tile: Accumulator Tile (L0C, output)
lhs_tile: Left matrix Tile (L0A)
rhs_tile: Right matrix Tile (L0B)
bias_tile: Optional positional Bias Tile (L0B Bias area, shape [1, N], row-broadcast)
phase: Optional — ``pl.AccPhase.Partial`` or ``pl.AccPhase.Final``
"""
@_api_decl
def matmul_acc(
dst_tile: Tile, acc_tile: Tile, lhs_tile: Tile, rhs_tile: Tile, /, *, phase: Optional[AccPhase] = None
) -> None:
"""In-place accumulating matrix multiply: ``dst += lhs @ rhs``.
``acc_tile`` must be the same Tile as ``dst_tile``. Initialize it with
``matmul`` before the first accumulating operation; this operation does not zero it.
Args:
dst_tile: Accumulator Tile (L0C, input/output)
acc_tile: Must be the same Tile as dst_tile
lhs_tile: Left matrix Tile (L0A)
rhs_tile: Right matrix Tile (L0B)
phase: Optional — ``pl.AccPhase.Partial`` or ``pl.AccPhase.Final``
"""
@_api_decl
def matmul_mx(
dst_tile: Tile, lhs_tile: Tile, rhs_tile: Tile, /, scale_a: Tile, scale_b: Tile,
*, phase: Optional[AccPhase] = None
) -> None:
"""MX matmul with per-group E8M0 scale: ``dst = lhs @ rhs``
Left/Right tiles use FP8/FP4 dtype; scale tiles use E8M0 in ScaleLeft/ScaleRight.
Hardware mad_mx reads scale implicitly via SFractal layout.
Args:
dst_tile: Accumulator Tile (L0C, output, FP32)
lhs_tile: Left matrix Tile (L0A, FP8/FP4)
rhs_tile: Right matrix Tile (L0B, FP8/FP4)
scale_a: Left scale Tile (ScaleLeft, E8M0)
scale_b: Right scale Tile (ScaleRight, E8M0)
phase: Optional — ``pl.AccPhase.Partial`` or ``pl.AccPhase.Final``
"""
@_api_decl
def matmul_mx_acc(
dst_tile: Tile, acc_tile: Tile, lhs_tile: Tile, rhs_tile: Tile, /, scale_a: Tile, scale_b: Tile,
*, phase: Optional[AccPhase] = None
) -> None:
"""In-place MX matmul with accumulation: ``dst += scaled_lhs @ scaled_rhs``.
``acc_tile`` must be the same Tile as ``dst_tile``. Initialize it with
``matmul_mx`` before the first accumulating operation; this operation does not zero it.
Args:
dst_tile: Accumulator Tile (L0C, input/output, FP32)
acc_tile: Must be the same Tile as dst_tile
lhs_tile: Left matrix Tile (L0A, FP8/FP4)
rhs_tile: Right matrix Tile (L0B, FP8/FP4)
scale_a: Left scale Tile (ScaleLeft, E8M0)
scale_b: Right scale Tile (ScaleRight, E8M0)
phase: Optional — ``pl.AccPhase.Partial`` or ``pl.AccPhase.Final``
"""
@_api_decl
def transpose(out: Tile, src: Tile) -> None:
"""Transpose Tile by swapping the last two dimensions.
Args:
out: Destination Tile
src: Source Tile
"""
@_api_decl
def sum(out: Tile, src: Tile, tmp: Tile, *, dim: int = 0) -> None:
"""Sum reduction along the specified dimension.
Args:
out: Destination Tile
src: Source Tile
tmp: Workspace Tile (required by hardware)
dim: Reduction dimension — 0=row (last axis), 1=column (first axis)
Example::
tmp_type = pl.TileType(shape=src.type.shape, dtype=pl.DT_FP32,
target_memory=pl.MemorySpace.Vec)
tmp = pl.make_tile(tmp_type, addr=0x1000)
pl.sum(out, src, tmp, dim=0) # row-wise sum, out shape = [src.shape[0], 1]
"""
@_api_decl
def argmax(out: Tile, src: Tile, tmp: Tile, *, dim: int = 0) -> None:
"""Argmax reduction along the specified dimension.
Args:
out: Destination Tile
src: Source Tile
tmp: Workspace Tile (required by hardware)
dim: Reduction dimension — 0=row (last axis), 1=column (first axis)
"""
@_api_decl
def argmin(out: Tile, src: Tile, tmp: Tile, *, dim: int = 0) -> None:
"""Argmin reduction along the specified dimension.
Args:
out: Destination Tile
src: Source Tile
tmp: Workspace Tile (required by hardware)
dim: Reduction dimension — 0=row (last axis), 1=column (first axis)
"""
@_api_decl
def expand_max(out: Tile, src: Tile, scalar: Tile, *, dim: int = 0) -> None:
"""Max expand (broadcast reduction result back to full shape).
Args:
out: Destination Tile
src: Source Tile (reduction result)
scalar: Scalar Tile for the expand operation
dim: Expand dimension — 0=row, 1=column
"""
@_api_decl
def expand_min(out: Tile, src: Tile, scalar: Tile, *, dim: int = 0) -> None:
"""Min expand.
Args:
out: Destination Tile
src: Source Tile (reduction result)
scalar: Scalar Tile for the expand operation
dim: Expand dimension — 0=row, 1=column
"""
@_api_decl
def expand_mul(out: Tile, src: Tile, scalar: Tile, *, dim: int = 0) -> None:
"""Multiply expand.
Args:
out: Destination Tile
src: Source Tile (reduction result)
scalar: Scalar Tile for the expand operation
dim: Expand dimension — 0=row, 1=column
"""
@_api_decl
def expand_sub(out: Tile, src: Tile, scalar: Tile, *, dim: int = 0) -> None:
"""Subtract expand.
Args:
out: Destination Tile
src: Source Tile (reduction result)
scalar: Scalar Tile for the expand operation
dim: Expand dimension — 0=row, 1=column
"""
@_api_decl
def expand_div(out: Tile, src: Tile, scalar: Tile, *, dim: int = 0) -> None:
"""Divide expand.
Args:
out: Destination Tile
src: Source Tile (reduction result)
scalar: Scalar Tile for the expand operation
dim: Expand dimension — 0=row, 1=column
"""
@_api_decl
def expand_add(out: Tile, src: Tile, scalar: Tile, *, dim: int = 0) -> None:
"""Add expand (broadcast the reduction result back to full shape).
Args:
out: Destination Tile
src: Source Tile (reduction result)
scalar: Scalar Tile for the expand operation
dim: Expand dimension — 0=row, 1=column
"""
@_api_decl
def expand_expdif(out: Tile, src: Tile, scalar: Tile, *, dim: int = 0) -> None:
"""Fused expand-expdif (softmax-style update: exp of the broadcast difference).
Args:
out: Destination Tile
src: Source Tile (reduction result)
scalar: Scalar Tile for the expand operation
dim: Expand dimension — 0=row, 1=column
"""
@_api_decl
def prod(out: Tile, src: Tile, tmp: Tile, *, dim: int = 0) -> None:
"""Product reduction along the specified dimension.
Args:
out: Destination Tile
src: Source Tile
tmp: Workspace Tile (required by hardware)
dim: Reduction dimension — 0=row (last axis), 1=column (first axis)
"""
@_api_decl
def gather(
out: Tile,
src: Tile,
indices_or_k_value: Tile,
tmp_or_cdst: Optional[Tile] = None,
tmp: Optional[Tile] = None,
*,
cmp_mode: int = 0,
offset: int = 0,
) -> None:
"""Gather elements by index, or by comparison against a k-value.
Two forms share this signature; the trailing positional parameters mean
different things in each:
- index form ``gather(out, src, indices[, tmp])``: ``out[i] = src[indices[i]]``
- compare form ``gather(out, src, k_value, cdst, tmp, cmp_mode=...)``: indices
where ``src`` compares against the k-value per ``cmp_mode``
Args:
out: Destination Tile
src: Source Tile
indices_or_k_value: Index Tile (index form) or k-value Tile (compare form)
tmp_or_cdst: Workspace Tile (index form) or cdst Tile (compare form)
tmp: Workspace Tile, compare form only
cmp_mode: Comparison mode (default 0)
offset: Index offset
"""
@_api_decl
def gatherb(out: Tile, src: Tile, offsets: Tile) -> None:
"""Gather elements by 32-byte block byte offset.
Args:
out: Destination Tile
src: Source Tile
offsets: Byte offset Tile
"""
@_api_decl
def gathermask(out: Tile, src: Tile, *, pattern_mode: int) -> None:
"""Extract columns by bit-pattern mask.
Args:
out: Destination Tile
src: Source Tile
pattern_mode: Bit-pattern extraction mode
"""
@_api_decl
def scatter(out: Tile, src: Tile, idx: Tile) -> None:
"""Scatter elements by index.
Args:
out: Destination Tile
src: Source Tile
idx: Index Tile
"""
@_api_decl
def mrgsort(dst: Tile, src: Tile, *, block_len: int) -> None:
"""Merge sort.
Args:
dst: Sorted output Tile
src: Source Tile
block_len: Sort block length
"""
@_api_decl
def mrgsort2(src0: Tile, src1: Tile, dst: Tile, tmp: Tile, *args, exhausted: bool = False) -> None:
"""Two-way (or multi-way) merge sort.
Args:
src0: First source Tile (val-idx pairs)
src1: Second source Tile (val-idx pairs)
dst: Destination Tile
tmp: Workspace Tile
*args: Optional additional source Tiles for multi-way merge
exhausted: Whether any source is already exhausted
"""
@_api_decl
def sort32(dst: Tile, src: Tile, idx: Tile, tmp: Optional[Tile] = None) -> None:
"""Sort 32 elements with index tracking.
Args:
dst: Destination Tile for sorted values
src: Source Tile (32 elements)
idx: Index Tile for tracking original positions
tmp: Optional workspace Tile (for tail-block handling)
"""
@_api_decl
def histogram(dst: Tile, src: Tile, idx: Tile, *, is_msb: bool) -> None:
"""Histogram accumulation for radix sort preprocessing.
Counts byte-value frequencies in *src* and writes per-row bin counts to *dst*.
Args:
dst: Destination Tile (``pl.DT_UINT32``, cols >= 256)
src: Source Tile (``pl.DT_UINT16``)
idx: Index Tile (``pl.DT_UINT8``, DN layout); used for filtering when ``is_msb=False``
is_msb: ``True`` counts high byte (bits 15-8); ``False`` counts low byte (bits 7-0)
filtered by rows where the high byte matches ``idx``
"""
@_api_decl
def quant(out: Tile, src: Tile, scale: Tile, *, mode: QuantMode = QuantMode.SYM, offset: Optional[Tile] = None) -> None:
"""Quantize Tile (high-precision → low-precision integer).
Args:
out: Destination Tile (quantized output)
src: Source Tile (float input)
scale: Scale Tile
mode: ``pl.QuantMode.SYM`` (symmetric) or ``pl.QuantMode.ASYM`` (asymmetric, requires *offset*)
offset: Offset Tile (required for ``mode=pl.QuantMode.ASYM``)
"""
@_api_decl
def dequant(out: Tile, src: Tile, scale: Tile, offset: Tile) -> None:
"""Dequantize Tile (low-precision integer → high-precision).
Args:
out: Destination Tile (float output)
src: Source Tile (quantized input)
scale: Scale Tile
offset: Offset Tile
"""
@_api_decl
def getval(container: "Tile | Tensor", offset: int) -> Scalar:
"""Read a scalar at the given linear offset, widening integer elements except UINT64 to INT64.
Inside SIMT vector functions and their SIMT helpers, use element indexing instead;
those reads preserve the Tile or Tensor's element dtype.
"""
@_api_decl
def setval(container: "Tile | Tensor", offset: int, value: Union[int, float]) -> None:
"""Write a scalar value into a Tile or Tensor at the given linear offset."""
@_api_decl
def set_validshape(tile: "Tile | TileGroup", /, shape: Shape) -> None:
"""Set the valid shape of a Tile or tile_group (for partial-tile / tail-block operations).
When a tile_group is passed, valid_shape is set on all tiles in the group.
"""
@_api_decl
def reinterpret(tile: "Tile | TileGroup", /, *, dtype: Optional[DType] = None, shape: Optional[Shape] = None,
layout: Optional[TensorLayout] = None) -> "Tile | TileGroup":
"""Reinterpret a Tile or tile_group's dtype/shape/layout metadata without data movement.
Returns a new handle over the same on-chip buffer: the new tile is
re-allocated at the ORIGINAL address and size (same-buffer semantics hold),
with memory space, fractal, pad, compact and mutex mapping inherited; only
the given properties are overridden. valid_shape is NOT inherited (the new
handle starts with a dynamic valid shape, equivalent to -1; set it with
``pl.set_validshape``). The original handle is unchanged.
``reinterpret`` is a compile-time view declaration, not a conversion — no
load/move/store/cast instruction is generated. The caller must guarantee
that the physical data actually matches the new declaration (e.g. declaring
``layout=pl.ZN`` on data that really is ZN-arranged).
Constraints:
- at least one of ``dtype`` / ``shape`` / ``layout`` must be given
- when ``dtype`` changes, ``shape`` must be given as well (the element
count is re-stated explicitly)
- ``shape`` must be compile-time constants; use ``pl.set_validshape``
for runtime windows
- the new footprint (shape x dtype bytes) must not exceed the original
buffer size, and the base address must be aligned to the new element
size (both checked at parse time)
Example::
t2 = pl.reinterpret(tile_a, shape=[64, 64], dtype=pl.DT_BF16) # same buffer as BF16 (dtype change needs shape)
g2 = pl.reinterpret(group_a, shape=[128, 32]) # whole group, mutex kept
Reinterpreting a rotating group: pass the GROUP, not a Tile that ``next()`` returned.
A view of ``group.next()`` is one fixed Tile at the group's base address and does not
rotate, so from the second buffer on it reads the wrong one -- silently, since the shapes
still agree. The group view rotates correctly, but the two handles share one cursor, so
advance it once per iteration and read the view with ``current()``::
g_zn = pl.reinterpret(g, shape=[K, M], layout=pl.ZN) # transposed view of the group
t = g.next() # advances the shared cursor
pl.load(t, ...)
pl.move(dst, g_zn.current()) # same buffer as t; next() would skip one
"""
@_api_decl
def set_mask_count() -> None:
"""Switch mask to counting mode."""
@_api_decl
def set_mask_norm() -> None:
"""Switch mask to per-bit normalization mode."""
@_api_decl
def set_vec_mask(mask_high: int, mask_low: int) -> None:
"""Explicitly set the 128-bit vector mask from two 64-bit integers."""
@_api_decl
def reset_mask() -> None:
"""Reset the mask to all-ones (no masking)."""
@_api_decl
def fill_index(out: Tile, start: Union[int, float]) -> None:
"""Fill target tile with sequential indices starting from *start*."""
@_api_decl
def pto_assert(condition: bool, /, format_str: Optional[str] = None, *args, loc: bool = False) -> None:
"""Runtime assert: abort if condition is false, optionally print error message.
Args:
condition: Scalar boolean condition (dtype must be BOOL)
format_str: Optional compile-time constant format string (printf-style)
*args: Scalar values to print in the format string
loc: Show source location in the error message
"""
@_api_decl
def printf(format_str: str, *args, loc: bool = False) -> None:
"""Print scalar values using a compile-time format string (printf-style).
Supported conversions: ``%d``/``%i`` (signed int), ``%u`` (unsigned int),
``%x`` (hex), ``%f`` (float, FP32 only).
Args:
format_str: Compile-time constant format string
*args: Scalar values (int, float, or ``pl.Scalar``)
loc: Show source location
"""
@_api_decl
def dump_data(
data: Union[Tensor, Tile],
offsets: Optional[Offset] = None,
shapes: Optional[Shape] = None,
*,
workspace: Optional[Tensor] = None,
loc: bool = False,
flag: Optional[str] = None,
) -> None:
"""Print Tensor or Tile contents for debugging.
Automatically dispatches based on input type:
- TensorType (GM Tensor): prints GM tensor data
- TileType (on-chip Tile): prints tile data (Acc tiles require workspace)
Args:
data: Tensor or Tile to dump
offsets: Optional window start offsets (per dimension); must pair with *shapes*
shapes: Optional window shape (per dimension); must pair with *offsets*
workspace: GM Tensor used as temporary space (only valid for Acc Tile inputs,
size >= tile_numel * sizeof(element_type))
loc: Show source location
flag: Optional compile-time string label printed as a standalone marker line
before the dump output, to distinguish multiple dump sites
"""
@_api_decl
def trap() -> None:
"""Insert a trap instruction to unconditionally abort execution."""
@_api_decl
def min(lhs: Union[int, float], rhs: Union[int, float], /) -> Scalar:
"""Return the minimum of two scalars.
Scalar-only operation for loop-bound calculations etc.
For tile element-wise minimum, use ``pl.minimum``.
Args:
lhs: Left operand (scalar)
rhs: Right operand (scalar)
Returns:
Scalar result.
"""
@_api_decl
def max(lhs: Union[int, float], rhs: Union[int, float], /) -> Scalar:
"""Return the maximum of two scalars.
Scalar-only operation for loop-bound calculations etc.
For tile element-wise maximum, use ``pl.maximum``.
Args:
lhs: Left operand (scalar)
rhs: Right operand (scalar)
Returns:
Scalar result.
"""
@_api_decl
def minimum(out: Tile, lhs: Tile, rhs: Union[Tile, int, float], *, dim: Optional[int] = None) -> None:
"""Element-wise minimum or dimension-wise min reduction.
Without ``dim`` (element-wise): ``out = min(lhs, rhs)``
- ``minimum(out, tile_a, tile_b)`` -> tile-tile
- ``minimum(out, tile_a, scalar)`` -> tile-scalar
With ``dim`` (reduce): min reduction along the specified dimension.
- ``minimum(out, src, tmp, dim=0)`` -> row-wise min (last axis)
- ``minimum(out, src, tmp, dim=1)`` -> column-wise min (first axis)
Args:
out: Destination Tile
lhs: Source Tile (or left operand in element-wise mode)
rhs: Right operand — Tile/Scalar in element-wise mode, or workspace
Tile in reduce mode
dim: Reduction dimension — None=element-wise, 0=row, 1=column
"""
@_api_decl
def maximum(out: Tile, lhs: Tile, rhs: Union[Tile, int, float], *, dim: Optional[int] = None) -> None:
"""Element-wise maximum or dimension-wise max reduction.
Without ``dim`` (element-wise): ``out = max(lhs, rhs)``
- ``maximum(out, tile_a, tile_b)`` -> tile-tile
- ``maximum(out, tile_a, scalar)`` -> tile-scalar
With ``dim`` (reduce): max reduction along the specified dimension.
- ``maximum(out, src, tmp, dim=0)`` -> row-wise max (last axis)
- ``maximum(out, src, tmp, dim=1)`` -> column-wise max (first axis)
Args:
out: Destination Tile
lhs: Source Tile (or left operand in element-wise mode)
rhs: Right operand — Tile/Scalar in element-wise mode, or workspace
Tile in reduce mode
dim: Reduction dimension — None=element-wise, 0=row, 1=column
"""
@_api_decl
def const(value: Union[int, float], dtype: DType, /) -> Scalar:
"""Create a typed compile-time constant.
Args:
value: Numeric literal (int or float)
dtype: Data type, e.g. ``pl.DT_INT32``, ``pl.DT_FP16``
"""
@_api_decl
def astype(x: Union[int, float], dtype: DType, /) -> Scalar:
"""Convert a runtime scalar expression to ``dtype``.
Args:
x: Source scalar expression
dtype: Destination data type, e.g. ``pl.DT_INT32``
"""
def range(start: int, stop: Optional[int] = None, step: int = 1, /):
"""Loop iterator for ``for`` loops.
Usage::
for i in pl.range(10): # 0..9
for i in pl.range(0, 10, 2): # 0, 2, 4, 6, 8
"""
if stop is None:
start, stop = 0, start
return builtins.range(start, stop, step)
@contextmanager
def section_vector():
"""Context manager for a Vector-pipe section scope."""
yield
@contextmanager
def section_cube():
"""Context manager for a Cube-pipe section scope."""
yield
@_api_decl
def get_block_idx() -> int:
"""Get the current block (AI Core) index."""
@_api_decl
def get_subblock_idx() -> int:
"""Get the current sub-block index (0 or 1)."""
@_api_decl
def get_block_num() -> int:
"""Get the launched worker block count after applying the stream's core limits.
This can be smaller than the host's requested ``kernel[stream, block_dim]``.
Use this runtime count as the work-distribution stride so limiting cores does
not leave tiles unprocessed. Mixed Vector sections have this many blocks times
``get_subblock_num()`` vector workers.
"""
@_api_decl
def get_subblock_num() -> int:
"""Get the sub-block count per AI Core (task ration).
Returns 1 on AIC binaries, get_subblockdim() on AIV binaries.
Matches AscendC GetTaskRation().
"""
@_api_decl
def get_spr() -> int:
"""Read a special purpose register value (get_ar instruction).
Currently only the AR register is supported. The AR register stores
the total byte count of valid elements produced by Squeeze.
``get_ar()`` is an ``__aicore__`` instruction and cannot be used inside
``@pl.vector_function``. Call this in the ``@pl.jit`` kernel body.
Returns:
int64_t scalar value from the SPR
"""
@_api_decl
def get_cycle() -> int:
"""Read the device system cycle counter (get_sys_cnt instruction).
Returns the current value of the AI Core system counter. The counter
frequency differs by chip (e.g. 50MHz on A2/A3, 1000MHz on A5), so use
differences between two reads to measure elapsed cycles.
``get_sys_cnt()`` is an ``__aicore__`` instruction and cannot be used
inside ``@pl.vector_function``. Call this in the ``@pl.jit`` kernel body.
Returns:
int64_t scalar value of the system cycle counter
"""
@_api_decl
def set_saturation_flag(*, mode: SaturationFlagMode, enable: bool) -> None:
"""Set the saturation flag in the CTRL special purpose register.
Controls the global saturation mode for Cast (vcvt) and other
vector compute instructions. Must be called outside
``@pl.vector_function`` (in the ``@pl.jit`` kernel body),
before the ``pl.section_vector()`` block that uses ``vf.astype``
with the corresponding saturation mode.
Args:
mode: Saturation mode category, one of:
- ``pl.SaturationFlagMode.FLOAT`` — float compute/convert (CTRL bit 48)
- ``pl.SaturationFlagMode.FLOAT8`` — float8 compute (CTRL bit 50)
- ``pl.SaturationFlagMode.INT`` — int compute (CTRL bit 53)
- ``pl.SaturationFlagMode.CAST`` — float→int / int→int convert (CTRL bit 59)
enable: ``True`` to enable saturation (clamp to target type's
min/max), ``False`` to disable (truncate).
"""
@_api_decl
def get_saturation_flag(*, mode: SaturationFlagMode) -> bool:
"""Read the saturation flag from the CTRL special purpose register.
Returns the current saturation state for the given mode category.
Must be called outside ``@pl.vector_function``.
Args:
mode: Saturation mode category (same as ``set_saturation_flag``).
Returns:
``True`` if saturation is enabled, ``False`` otherwise.
"""
@_api_decl
def set_ctrl_spr(start_bit: int, end_bit: int, value: int, /) -> None:
"""Set a bit range in the CTRL special purpose register.
Writes ``value`` into the CTRL register bits ``[start_bit, end_bit]``,
preserving all other bits. This is the low-level counterpart to
``set_saturation_flag`` — use it when direct CTRL bit manipulation
is needed (e.g., setting the global override bit CTRL[60]).
Must be called outside ``@pl.vector_function``.
Args:
start_bit: Start bit index (0-63), compile-time constant.
end_bit: End bit index (0-63), compile-time constant.
Writable bits on A5: 6-10, 45, 48, 50, 53, 59, 60.
value: Value to write into the bit range.
"""
@_api_decl
def get_ctrl_spr(start_bit: int, end_bit: int, /) -> int:
"""Read a bit range from the CTRL special purpose register.
Returns the value of CTRL register bits ``[start_bit, end_bit]``.
Must be called outside ``@pl.vector_function``.
Args:
start_bit: Start bit index (0-63), compile-time constant.
end_bit: End bit index (0-63), compile-time constant.
Returns:
int64_t value of the extracted bit range.
"""
@_api_decl
def reset_ctrl_spr(start_bit: int, end_bit: int, /) -> None:
"""Reset a bit range in the CTRL register to default values.
Restores CTRL register bits ``[start_bit, end_bit]`` to their
hardware default (CTRL default = 0x1000000000000008).
Must be called outside ``@pl.vector_function``.
Args:
start_bit: Start bit index (0-63), compile-time constant.
end_bit: End bit index (0-63), compile-time constant.
Writable bits on A5: 6-10, 45, 48, 50, 53, 59, 60.
"""
@_api_decl
def make_tile(
tile_type: TileType,
/,
*,
addr: int,
) -> Tile:
"""Place one Tile at a fixed address in the memory space its type names.
The Tile's shape, dtype, memory space and layout all come from ``tile_type``;
this call binds it to an address range whose byte span is derived from the
shape and dtype. For several Tiles of one type
rotating through a ping-pong buffer, use ``pl.make_tile_group``.
``tile_type`` is the only positional argument; ``addr`` is keyword-only.
Args:
tile_type: ``pl.TileType`` descriptor — the only accepted spelling of the
Tile's shape/dtype/memory space/layout
addr: Required byte offset within the memory space. Fixed at parse time,
and aligned to that space (32B Vec/Mat, 512B Left/Right, 64B Acc)
Example::
tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec)
tile_a = pl.make_tile(tt, addr=0x0000) # size derived: 64 * 128 * 2
"""
@_api_decl
def make_tile_group(*, type: TileType, addrs: int | list,
mutex_ids: str | list[int | list[int] | tuple[int, ...]] | tuple | None = None,
depth: int | None = None,
fwd_ids: list[int] | tuple[int, ...] | None = None,
bwd_ids: list[int] | tuple[int, ...] | None = None,
) -> Any:
"""Create a rotating Tile group with optional mutex metadata.
The handle supports ``group.next()``, ``group.current()``,
``group.previous()``, and ``group[i]``. When ``mutex_ids`` is provided,
``auto_mutex`` synchronizes accesses automatically.
Args:
type: ``pl.TileType`` descriptor
addrs: Base address for contiguous Tiles, or one address per Tile
mutex_ids: ``"auto"`` for whole-group automatic allocation, a manual
sequence, or None/empty for user-managed synchronization. Each
manually configured Tile may use one int or a non-empty list/tuple.
Every Tile must use the same mutex ID count, with no duplicates for
one Tile
depth: Number of Tiles. Required when ``mutex_ids`` is ``"auto"``, None,
or empty; otherwise inferred from ``len(mutex_ids)``
fwd_ids: Optional cross-core event IDs (0..15) marking this group as a
producer->consumer channel for the automatic pipeline transform. One
per Tile; the transform picks ``fwd_ids[i % N]`` per iteration
bwd_ids: Optional cross-core event IDs for the consumer->producer
direction (buffer released). Same shape as ``fwd_ids``
"""
@_api_decl
def TileType(
*,
shape: Shape,
dtype: DType,
target_memory: MemorySpace = MemorySpace.Vec,
valid_shape: Optional[Shape] = None,
layout: Optional[TensorLayout] = None,
fractal: Optional[int] = None,
pad: Optional[Union[TilePad, int]] = None,
compact: Optional[int] = None,
) -> TileType:
"""Describe a tile: its shape, dtype, memory space and layout.
A TileType carries everything about a tile except where it lives, which
``pl.make_tile(tile_type, addr=...)`` supplies.
Args:
shape: Tile shape per axis
dtype: Element data type, e.g. ``pl.DT_FP16``
target_memory: On-chip memory space the tile occupies
valid_shape: Optional smaller shape actually holding data
layout: Optional tensor layout (NZ/ZN/NN/ZZ)
fractal: Optional fractal size
pad: Optional padding mode, a ``pl.TilePad`` or the integer 0/1/2/3 standing for one
compact: Optional compact flag
"""
@_api_decl
def addptr(ptr: Ptr, offset: int) -> Ptr:
"""Advance a raw pointer by an integer offset.
Args:
ptr: Raw pointer expression (``pl.Ptr[dtype]``)
offset: Element offset, an int or an integer scalar expression
"""
@_api_decl
def make_ptr(ptr: Union[Ptr, Tensor], dtype: Optional[DType] = None) -> Ptr:
"""Extract a raw pointer from a Tensor, or reinterpret a pointer's element dtype.
Args:
ptr: A ``pl.Tensor`` to take the data pointer of, or a ``pl.Ptr[dtype]`` to recast
dtype: Optional element type for the result
"""
@_api_decl
def make_tensor(
ptr: Union[Ptr, Tensor],
shape: Shape,
stride: Optional[Shape] = None,
dtype: Optional[DType] = None,
*,
depth: int | None = None,
fwd_ids: list[int] | tuple[int, ...] | None = None,
bwd_ids: list[int] | tuple[int, ...] | None = None,
) -> Tensor:
"""Create a Tensor view over a raw pointer or an existing Tensor.
Args:
ptr: A ``pl.Ptr[dtype]`` or an existing ``pl.Tensor``
shape: Shape of the view per axis
stride: Optional stride per axis; contiguous when omitted
dtype: Optional element type for the view
depth: Number of slots when used as a cross-core buffer
fwd_ids: Optional cross-core event IDs (0..15) marking this view as a
producer->consumer channel for the automatic pipeline transform. One per slot
bwd_ids: Optional cross-core event IDs for the consumer->producer
direction. Same shape as ``fwd_ids``
"""