yobx.litert.litert_helper#

Minimal pure-Python TFLite/LiteRT FlatBuffer parser and helper utilities for the LiteRT→ONNX converter.

The parser requires no external dependencies beyond the Python standard library. It reads the binary FlatBuffer format that every .tflite file uses and exposes the model graph as plain Python dataclasses so that the converter can inspect operator types, tensor shapes, weights, and graph connectivity.

TFLite dtype enum mapping#

Value

TFLite TensorType

0

FLOAT32

1

FLOAT16

2

INT32

3

UINT8

4

INT64

5

STRING

6

BOOL

7

INT16

8

COMPLEX64

9

INT8

10

FLOAT64

11

COMPLEX128

12

UINT64

14

UINT32

15

UINT16

16

INT4

class yobx.litert.litert_helper.ActivationFunctionType[source]#
NONE = 0#
RELU = 1#
RELU6 = 3#
RELU_N1_TO_1 = 2#
SIGN_BIT = 5#
TANH = 4#
class yobx.litert.litert_helper.BuiltinOperator[source]#

Subset of the TFLite BuiltinOperator enum.

ABS = 101#
ADD = 0#
AVERAGE_POOL_2D = 1#
BATCH_MATMUL = 126#
CEIL = 104#
CONCATENATION = 2#
CONV_2D = 3#
CUSTOM = 32#
DEPTHWISE_CONV_2D = 4#
DEQUANTIZE = 6#
DIV = 42#
ELU = 111#
EXP = 47#
EXPAND_DIMS = 70#
FLOOR = 8#
FLOOR_DIV = 90#
FULLY_CONNECTED = 9#
GELU = 150#
HARD_SWISH = 117#
LEAKY_RELU = 98#
LOG = 73#
LOGICAL_AND = 86#
LOGICAL_NOT = 87#
LOGICAL_OR = 84#
LOG_SOFTMAX = 50#
MAX_POOL_2D = 17#
MEAN = 40#
MUL = 18#
NEG = 59#
PAD = 34#
POW = 78#
REDUCE_MAX = 82#
REDUCE_MIN = 89#
RELU = 19#
RELU_N1_TO_1 = 20#
RESHAPE = 22#
ROUND = 116#
RSQRT = 76#
SIN = 66#
SOFTMAX = 25#
SQRT = 75#
SQUARED_DIFFERENCE = 99#
SQUEEZE = 43#
SUB = 41#
SUM = 74#
TANH = 28#
TRANSPOSE = 39#
TRANSPOSE_CONV = 67#
class yobx.litert.litert_helper.Padding[source]#
SAME = 0#
VALID = 1#
class yobx.litert.litert_helper.TFLiteModel(version: int, subgraphs: List[TFLiteSubgraph] | None = None)[source]#

Top-level parsed TFLite model.

class yobx.litert.litert_helper.TFLiteOperator(opcode: int, custom_code: str, inputs: Tuple[int, ...], outputs: Tuple[int, ...], builtin_options: Dict)[source]#

Parsed representation of a TFLite Operator.

The builtin_options dict contains decoded attributes for the current opcode (e.g. {"fused_activation": 0} for FULLY_CONNECTED). Unrecognised op options are left unparsed (empty dict).

property name: str#

Human-readable op name for diagnostics.

class yobx.litert.litert_helper.TFLiteSubgraph(name: str, tensors: List[TFLiteTensor], inputs: Tuple[int, ...], outputs: Tuple[int, ...], operators: List[TFLiteOperator])[source]#

Parsed representation of a TFLite SubGraph.

class yobx.litert.litert_helper.TFLiteTensor(index: int, name: str, dtype: int, shape: Tuple[int, ...], data: ndarray | None)[source]#

Parsed representation of a TFLite Tensor.

yobx.litert.litert_helper.builtin_op_name(code: int) str[source]#

Return the name of a TFLite BuiltinOperator code, e.g. 'RELU'.

yobx.litert.litert_helper.litert_dtype_to_np_dtype(dtype_int: int) dtype[source]#

Map a TFLite TensorType integer to a numpy.dtype.

Parameters:

dtype_int – TFLite dtype integer from the FlatBuffer

Returns:

corresponding numpy.dtype

Raises:

ValueError – if the dtype is not supported

yobx.litert.litert_helper.parse_tflite_model(model: str | PathLike | bytes) TFLiteModel[source]#

Parse a .tflite file (or raw bytes) and return a TFLiteModel.

Parameters:

model – path to a .tflite file or its raw bytes

Returns:

parsed TFLiteModel