Skip to content
heliaEDGE
Reference
HELIA

litert

LiteRT (.tflite) conversion on the TensorFlow backend.

Machine-readable model

  • convert_litertfunctionConvert a Keras model to LiteRT bytes; exportmodel validates the arguments first.
  • tensor_recordsfunctionRead the main subgraph's input and output tensors from a .tflite flatbuffer.
  • operator_namesfunctionBuiltin operator names of every subgraph's operators, in execution order.
  • tie_state_scalesfunctionGive both tensors of each integer state pair one scale and zero point.
  • export_litertfunctionExport with an already validated spec and calibration array.
function

Convert a Keras model to LiteRT bytes; exportmodel validates the arguments first.

helia_edge/export/litert.py:27

convert_litert(
model: keras.Model,
*,
precision: Precision,
io_dtype: IODType,
mode: ConversionMode,
strict: bool,
calibration: npt.NDArray | Mapping[str, npt.NDArray] | None,
) -> bytes

Convert a Keras model to LiteRT bytes; export_model validates the arguments first.

A model with state inputs is converted with its outputs keyed by output name, so the signature names state_in_k and state_out_k.

Parameters of convert_litert
NameTypeDefaultDescription
modelkeras.ModelRequiredThe Keras model, on the TensorFlow backend.
precisionPrecisionRequiredThe numeric format; FP16 is TensorFlow's float16 weight storage rewritten to a native float16 graph (``to_native_fp16``).
io_dtypeIODTypeRequiredInputs and outputs of the calibrated precisions. FP32 graphs have float32 and FP16 graphs float16 inputs and outputs whatever ``io_dtype`` says, so pass those.
modeConversionModeRequiredHow the model is traced; SAVED_MODEL exports to a temporary directory removed after conversion.
strictboolRequiredFor calibrated precisions, refuse operators without an integer kernel.
calibrationnpt.NDArray | Mapping[str, npt.NDArray] | NoneRequiredSamples for the calibrated precisions, used one at a time in stored order; for a model with several inputs, a mapping of each input name to its samples, fed by name because the converter orders inputs its own way.
Returns of convert_litert
ValueTypeDescription
bytesbytesThe LiteRT flatbuffer.
Errors raised by convert_litert
TypeDescription
ValueErrorIf ``mode`` is CONCRETE and the model has several inputs or state inputs: a concrete function converts to a graph without a signature, which name-keyed calibration and state tensors need. Also if a model with state inputs repeats an output name, as two outputs of one layer do.
function

Read the main subgraph's input and output tensors from a .tflite flatbuffer.

helia_edge/export/litert.py:183

tensor_records(content: bytes) -> tuple[tuple[TensorRecord, ...], tuple[TensorRecord, ...]]

Read the main subgraph’s input and output tensors from a .tflite flatbuffer.

Inputs and outputs are in subgraph order. Tensors whose signature names are state_in_k and state_out_k, with equal shapes and types, are STATE tensors of pair k; the others, including unpaired state names of an imported model, are SIGNAL tensors.

function

Builtin operator names of every subgraph's operators, in execution order.

helia_edge/export/litert.py:228

operator_names(content: bytes) -> list[str]

Builtin operator names of every subgraph’s operators, in execution order.

function

Give both tensors of each integer state pair one scale and zero point.

helia_edge/export/litert.py:360

tie_state_scales(content: bytes, tolerance: float) -> bytes

Give both tensors of each integer state pair one scale and zero point.

A runtime carries state_out_k back into state_in_k as raw integers, which keeps the state’s value only if both tensors have the same quantization. For each pair whose parameters differ, both tensors take the parameters of the tensor whose range covers the other’s (the larger scale of a symmetric int16 pair), or else parameters whose range covers both ranges. The kernels reading or writing either tensor recompute their scaling from the new parameters when prepared, and the bias of a FULLY_CONNECTED or convolution reading a state tensor is requantized to its new input scale. A RESHAPE or SQUEEZE reading a state tensor copies its bytes, so its output takes the same parameters, and its own readers are checked and requantized in turn.

Parameters of tie_state_scales
NameTypeDefaultDescription
contentbytesRequiredCalibrated ``.tflite`` flatbuffer.
tolerancefloatRequiredLargest difference between the tied scale and either original scale, relative to that original.
Returns of tie_state_scales
ValueTypeDescription
bytesbytes``content`` itself when every pair is already tied or float; otherwise the rewritten model.
Errors raised by tie_state_scales
TypeDescription
ValueErrorIf state names do not pair up or a pair's tensors differ in shape or type, a pair mixes float and integer quantization, the tied scale differs from an original scale by more than ``tolerance``, an operator that reads or writes a state tensor needs its parameters unchanged, or a state tensor is a reader's weight or bias, or a reader's bias cannot be requantized (shared, of an unexpected type or scale count, or overflowing).
function

Export with an already validated spec and calibration array.

helia_edge/export/litert.py:424

export_litert(
model: keras.Model,
spec: ExportSpec,
calibration: npt.NDArray | Mapping[str, npt.NDArray] | None,
) -> ExportResult

Export with an already validated spec and calibration array.