litert
LiteRT Utility API
This module provides utility functions for working with the LiteRT schema.
Classes:
-
BuiltinOperatorMap
–Mapping of builtin operator codes to their names.
-
BuiltinOptionsMap
–Mapping of builtin options codes to their names.
Functions:
-
quantize_multiplier
–Convert a floating point multiplier into a fixed-point multiplier and shift.
-
compute_fused_activation_range
–Compute the activation min and max values based on the fused activation function.
-
get_builtin_code
–Return the builtin code of the given operator code.
-
compute_padding_with_offset
–Computes a single padding value (top/left) for one dimension with offset.
-
compute_padding
–Computes a single padding value (top/left) for one dimension.
-
compute_tensor_bytes
–Compute the number of bytes for a tensor based on its type and shape.
-
compute_tensor_ctype
–Compute the C type for a tensor based on its type.
-
get_tensor_dtype
–Get the numpy dtype of a tensor.
Copyright 2025 Ambiq. All Rights Reserved.
Functions
quantize_multiplier
Convert a floating point multiplier into a fixed-point multiplier and shift.
Given a real multiplier, this function computes a pair (quantized_multiplier, shift) such that the fixed-point multiplication approximates the real multiplier:
real_multiplier ~ quantized_multiplier / (2^(31 - shift))
This is typically used in quantized inference to convert floating-point scaling factors to fixed-point representation.
Parameters:
-
real_multiplier
float
) –The floating-point multiplier.
Returns:
compute_fused_activation_range
compute_fused_activation_range(activation_function: ActivationFunctionType, output_zero_point: int, output_scale: float, qmin: int = -128, qmax: int = 127) -> tuple[int, int]
Compute the activation min and max values based on the fused activation function.
Parameters:
-
activation_function
ActivationFunctionType
) –The activation function type.
-
output_zero_point
int
) –The zero point for the output tensor.
-
output_scale
float
) –The scale for the output tensor.
-
qmin
int
, default:-128
) –Minimum quantized value. Defaults to -128.
-
qmax
int
, default:127
) –Maximum quantized value. Defaults to 127.
Returns:
get_builtin_code
Return the builtin code of the given operator code.
This function serves as a workaround for older versions of LiteRT that do not support/populate the BuiltinCode property.
Parameters:
-
opcode
OperatorCodeT | OperatorCode
) –Operator code.
Returns:
-
int
(int
) –Builtin code.
compute_padding_with_offset
compute_padding_with_offset(in_size: int, filter_size: int, stride: int, dilation_rate: int = 1, padding_type: int = litert.Padding.SAME) -> tuple[int, int]
Computes a single padding value (top/left) for one dimension with offset.
Parameters:
-
in_size
int
) –Input dimension (height or width).
-
filter_size
int
) –Kernel dimension (height or width).
-
stride
int
) –Stride for that dimension.
-
dilation_rate
int
, default:1
) –Dilation rate for that dimension (default is 1).
-
padding_type
int
, default:SAME
) –0 for SAME padding, 1 for VALID padding (default is SAME).
Returns:
compute_padding
compute_padding(in_size: int, filter_size: int, stride: int, dilation_rate: int = 1, padding_type: int = litert.Padding.SAME) -> int
Computes a single padding value (top/left) for one dimension.
Parameters:
-
in_size
int
) –Input dimension (height or width).
-
filter_size
int
) –Kernel dimension (height or width).
-
stride
int
) –Stride for that dimension.
-
dilation_rate
int
, default:1
) –Dilation rate for that dimension (default is 1).
-
padding_type
int
, default:SAME
) –0 for SAME padding, 1 for VALID padding (default is SAME).
Returns:
-
int
(int
) –The computed top or left padding value.
compute_tensor_bytes
Compute the number of bytes for a tensor based on its type and shape.
Parameters:
-
tensor
TensorT
) –Tensor object.
Returns:
-
int
(int
) –Number of bytes for the tensor.
compute_tensor_ctype
Compute the C type for a tensor based on its type.
Parameters:
-
tensor
TensorT
) –Tensor object.
Returns:
-
str
(str
) –C type of the tensor.
get_tensor_dtype
Get the numpy dtype of a tensor.
Parameters:
-
tensor
TensorT
) –Tensor object.
Returns:
-
dtype
–np.dtype: Numpy dtype of the tensor.
get_expanded_dims_shape
is_constant_tensor
Check if a tensor is a constant tensor in the LiteRT model.
Parameters:
-
tensor
TensorT
) –Tensor object.
-
model
ModelT
) –LiteRT model object.
-
subgraph
SubGraphT
) –Subgraph object from LiteRT model.
Returns:
-
bool
(bool
) –True if the tensor is constant, False otherwise.
load_tensor_data
Load the data of a tensor from the LiteRT model.
Parameters:
-
tensor
TensorT
) –Tensor object.
-
model
ModelT
) –LiteRT model object.
Returns:
-
ndarray
–np.ndarray: Numpy array containing the tensor data.