# helia_edge.export.litert

LiteRT (.tflite) conversion on the TensorFlow backend.

## helia_edge.export.litert.convert_litert

`function` · `python`

```python
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**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| model | keras.Model | Required | The Keras model, on the TensorFlow backend. |
| precision | Precision | Required | The numeric format; FP16 is TensorFlow's float16 weight storage rewritten to a native float16 graph (``to_native_fp16``). |
| io_dtype | IODType | Required | Inputs and outputs of the calibrated precisions. FP32 graphs have float32 and FP16 graphs float16 inputs and outputs whatever ``io_dtype`` says, so pass those. |
| mode | ConversionMode | Required | How the model is traced; SAVED_MODEL exports to a temporary directory removed after conversion. |
| strict | bool | Required | For calibrated precisions, refuse operators without an integer kernel. |
| calibration | npt.NDArray \| Mapping[str, npt.NDArray] \| None | Required | Samples 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**

| Name | Type | Description |
| --- | --- | --- |
| bytes | bytes | The LiteRT flatbuffer. |

**Raises**

| Name | Description |
| --- | --- |
| ValueError | If ``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. |

Source: `helia_edge/export/litert.py:27`

## helia_edge.export.litert.tensor_records

`function` · `python`

```python
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.

Source: `helia_edge/export/litert.py:183`

## helia_edge.export.litert.operator_names

`function` · `python`

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

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

Source: `helia_edge/export/litert.py:228`

## helia_edge.export.litert.tie_state_scales

`function` · `python`

```python
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**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| content | bytes | Required | Calibrated ``.tflite`` flatbuffer. |
| tolerance | float | Required | Largest difference between the tied scale and either original scale, relative to that original. |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
| bytes | bytes | ``content`` itself when every pair is already tied or float; otherwise the rewritten model. |

**Raises**

| Name | Description |
| --- | --- |
| ValueError | If 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). |

Source: `helia_edge/export/litert.py:360`

## helia_edge.export.litert.export_litert

`function` · `python`

```python
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.

Source: `helia_edge/export/litert.py:424`
