This document is relevant for: Trn2, Trn3
nki.isa.NkiScalar#
- class nki.isa.NkiScalar(state, dtype=None, engine=None)[source]#
NkiScalar is the core scalar type in NKI. It holds one integer, computed on device, and can be used as an operand of arithmetic, as the
scalar_offsetof an access pattern, and as a bound ofnki.language.fori_loop().NkiScalars are instantiated from values in a NkiTensor where dtype is either
nl.int32,nl.uint32ornl.int64, vianki.isa.scalar_load().A NkiScalar can be used almost anywhere a Python
intliteral is used, and the other operand of an arithmetic operator may be a Pythonintor another NkiScalar so long as the dtypes match. Operators callnki.isa.scalar_alu()and return a new NkiScalar.Width is fixed at creation. A 32-bit scalar is never widened, so arithmetic that outgrows 32 bits wraps rather than promoting:
100000 * 100000yields1410065408, and adding 1 to the largestnl.int32yields the smallest. Load from annl.int64tile when a value needs more range.Not every operation exists at both widths. At 64 bits only addition, subtraction and multiplication are available:
Operation
nl.int32nl.uint32nl.int64+,-(binary and unary),*Supported
Supported
Supported
<,<=,>,>=,==,!=Returns a
nl.int32scalar containing 0 or 1Returns a
nl.int32scalar containing 0 or 1Rejected at compile time
nki.isa.scalar_alu()withnl.divide,nl.fmod,nl.maximumornl.minimumSupported
Supported
Rejected at compile time
Bound of
nki.language.fori_loop()Supported, including an arithmetic expression
Not supported
Not supported; the trip-count comparison has no 64-bit form
scalar_offseton an HBM access patternSupported
Not supported
Supported, including offsets beyond 4GB
scalar_offseton an SBUF or PSUM access patternSupported
Not supported
Not supported; an on-chip offset must be 32 bits
strideorcountslot of an access patternSupported
Not supported
Not supported; a pattern slot becomes a 32-bit index in the IR
A scalar has no Python value.
bool(s),int(s),float(s), using a scalar as a Python index (range(s),some_list[s]), and using one in anif,while, orassertcondition are all rejected, including undernki.simulate(), because the value is only resolved when the kernel runs. Indexing a tensor with a scalar works throughnki.language.ds():t[nl.ds(s, 8)]andselect()both take one, as doesscalar_offset. A slice bound cannot be a scalar, because a slice needs both bounds while the kernel is built.nki.language.fori_loop()takes a scalar bound directly.nki.language.if_else()takes a scalar condition, including a comparison result.Comparison results are ordinary
nl.int32scalars. They support arithmetic,nki.isa.scalar_store(), loop bounds and access pattern offsets.Unsigned arithmetic wraps modulo
2**32and comparisons use unsigned ordering. Signed and unsigned scalars cannot be combined in one operation. A Python integer operand must fit the scalar’s dtype.nl.uint32supports every arithmetic and comparison operation listed fornl.int32. Unsigned scalars cannot be used as loop bounds, access-pattern offsets, strides or counts.Choosing a width for addressing.
scalar_offsetcounts elements of the access pattern’s indirect dimension, not bytes, and neuronx-cc scales that count by the dimension’s stride when it forms the address, so a 32-bit index reaches well past 4GB: rows of 512nl.float32values are 2KB apart, so2**31of them span roughly 4TB. Annl.int64offset is needed when the index value itself passes2**31– when element-indexing a tensor of more than2**31elements, for instance.Which engines a scalar is available on. A scalar is either available on all engines or bound to a single one. A constant, a loop variable and the result of
nki.isa.scalar_load()withoutengineare available on all engines. An operation with at least one single-engine operand produces a single-engine result, mixing two different engines is an error, and a single-engine value cannot serve as a loop bound, a loop-carried value, a condition or an access pattern’sscalar_offset, stride or count.A scalar can select a runtime grid coordinate directly or be marked as a source-element offset with
nki.language.grid_element_offset(). HBM grids acceptnl.int32andnl.int64scalars; SBUF grids requirenl.int32. Runtime PSUM grid indexing is not supported.Create scalars via
nki.isa.scalar_load(). See NKI Scalar Values for a walkthrough with runnable examples.Attributes
The NKI dtype name, e.g.
The sequencer this value is bound to, e.g.
- property dtype#
The NKI dtype name, e.g.
"int32".
- property engine#
The sequencer this value is bound to, e.g.
nisa.engine.sync, or None when available on all engines.
This document is relevant for: Trn2, Trn3