Arithmetic Operations¶
Legato arithmetic uses Python operators inside compiled functions. Operators work on scalar values and tensors when their types and shapes are compatible.
Elementwise Arithmetic¶
1 2 3 4 5 6 | |
Tensor arithmetic follows the compiler's tensor shape rules, including broadcasting.
On EVT1, matching-shape BF16 tensor subtraction, multiplication and division
now lower to the VPU alongside addition. The numerical coverage uses
[16, 256] inputs. Select backend=bertha.evt1 explicitly; this does not imply
EVT1 support for every broadcast, dtype or other operator in the list above.
See Verified EVT1 paths.
Matrix Multiplication¶
Use Python's @ operator for matrix multiplication.
1 | |
On Bertha, matmul weights must use a storage kind accepted by the selected backend. The compiler prepares compatible SRAM weights for computation while the tensor remains in SRAM.
An SRAM weight may be read again for a batch prefix. Unit leading dimensions
are squeezed, and varying batch dimensions are sliced per iteration.
A "mpu_buffer" stream cannot be reused as a persistent SRAM tensor; unsupported
replay reports LE0184. See
Weight storage requirements.
1 2 3 4 5 6 7 8 9 10 | |
Batch rules are in Broadcasting. See Bertha examples.
Bitwise Operations¶
Bitwise operators are supported on integer scalars and follow Python syntax.
>> is always an arithmetic (sign-extending) shift; there is no separate
unsigned right-shift operator.
1 2 3 4 5 6 | |
Comparisons¶
Comparisons such as <, <=, >, >=, ==, and != produce boolean values
or boolean tensors.
On the current EVT0 backend, scalar integer/index ordering comparisons must
use nonnegative values: signed ordering for negative operands is not
guaranteed. This restriction also applies to comparisons controlling if,
for, and while. Equality, boolean handling, and the EVT1 implementation
are separate paths.
1 | |
Boolean tensors are often combined with legato.tensor.where.
1 2 | |
Casts¶
Use legato.language.arith.cast to convert scalar values to a target Legato
type.
1 2 3 4 | |
Floating-point and integer scalar conversions¶
Import arith from legato.language, then use arith.fp2int(value, policy)
for an explicit rounding policy.
"round_to_even" rounds to the nearest integer with ties to even;
"towards_zero" discards the fractional part. The policy is a compile-time
string and the result is int32. These are scalar operations; cast is not a
replacement for choosing the rounding policy.
arith.int2fp(value) converts an int16 or int32 scalar to float32.
Cast wider integer types to int32 first when the value fits that type.
Runtime backend support for these conversions is currently EVT0; a successful
frontend compile does not establish EVT1 support.
1 2 3 4 5 6 | |
fp2int¶
1 | |
Convert a float scalar to an int32 scalar.
Parameters:
value(Value): The float scalar to convert.policy(Value): Interpreted rounding policy string."round_to_even"rounds to the nearest integer, and ties go to the even integer."towards_zero"drops the fractional part.
Returns:
- The int32 scalar.
Example:
1 2 3 4 5 6 7 | |
int2fp¶
1 | |
Convert an int16 or int32 scalar to a float32 scalar.
Parameters:
value(Value): The integer scalar to convert. Cast other integer types to int32 first.
Returns:
- The float32 scalar.
Example:
1 2 3 4 5 6 7 | |
Math And Reductions¶
Elementwise math and reductions live under legato.math.
1 2 3 | |
See Math Operations for the full legato.math API.