utils
Operators Utilities API
The utils module provides utility functions for AOT operators.
Copyright 2025 Ambiq. All Rights Reserved.
Functions
pad_shape_to_4d
Pad a tensor shape to 4D (N, H, W, C) order, filling missing dims with 1.
clamp_to_int_range
lut_populate_s16
lut_populate_s16(input_scale: float, input_zero_point: int, output_scale: float, output_zero_point: int, transform_fn) -> np.ndarray
Recreates TFLM's LUTPopulate
Args input_scale, input_zero_point: affine scale for LUT input domain output_scale, output_zero_point: affine scale for LUT output domain transform_fn: function f: R -> R (e.g., exp, 1/(1+x))
Returns np.int16 array of length 513.
calculate_input_radius
Mimics litert::CalculateInputRadius (non-emulated version).
This computes the maximum representable difference in the scaled domain.
Parameters:
-
(input_integer_bitsint) –Number of integer bits reserved (e.g., kScaledDiffIntegerBits, typically 5).
-
(input_left_shiftint) –The left shift computed from PreprocessSoftmaxScaling.
-
(total_signed_bitsint, default:31) –Total bits available in the representation (typically 31 for Q31 arithmetic).
Returns:
-
int(int) –The computed input radius.
tflite_round
TFLM's TfLiteRound: round half away from zero, implemented with +/-0.5 then trunc. Args: x (float): The float to round.
Returns:
-
int(int) –The rounded integer.
checked_log2
Exact port of TFLM's CheckedLog2: - Uses log(x) / log(2) (not math.log2) - Rounds with TfLiteRound - 'Power-of-two' if |fracpart| < 1e-3
Parameters:
-
(xfloat) –The positive float to compute log2 for.
-
(tolfloat, default:0.001) –The tolerance for determining if the value is a power of two.
Returns:
downscale_q31_to_q15
vector_sum_s8
vector_sum_s8(vector_data: ndarray, vector_cols: int, vector_rows: int, lhs_offset: int, rhs_offset: int, bias_data: ndarray | None = None) -> np.ndarray
Pure-Python port of CMSIS-NN's arm_vector_sum_s8 helper.
Parameters:
-
(vector_datandarray) –Flattened (rows * cols) int8 buffer containing the kernel matrix.
-
(vector_colsint) –Number of columns per output channel (accumulation depth).
-
(vector_rowsint) –Number of rows/output channels.
-
(lhs_offsetint) –Input offset applied during accumulation.
-
(rhs_offsetint) –Filter/weight offset applied during accumulation.
-
(bias_datandarray | None, default:None) –Optional per-output bias (length == vector_rows), int32.
Returns:
-
ndarray–np.ndarray: int32 array with the kernel sums, matching CMSIS-NN behaviour.