tflite clib Machine Learning / Inference

TensorFlow Lite inference for PascalAI. Run .tflite models on-device with GPU/NNAPI delegates, INT8 quantization, image classification, and object detection — no cloud required.

ppm install tflite

Overview

tflite wraps TensorFlow Lite — Google's framework for on-device ML inference. Models trained in TensorFlow, Keras, or converted from PyTorch/ONNX are compiled to the .tflite FlatBuffer format and executed directly on CPU, GPU, or accelerator hardware without a network connection.

The package covers the full inference lifecycle: loading models, creating interpreters, allocating tensor memory, filling inputs, running inference, and reading outputs. Hardware delegates (GPU, NNAPI) plug in with a single call.

CLib package

Requires libtflite-dev. Build from source: https://github.com/tensorflow/tensorflow — follow the tensorflow/lite CMake build instructions, then install the resulting libtensorflowlite.so as libpai_tflite.so.

Inference Workflow

Every TFLite inference follows the same six-step sequence. Skipping or reordering steps causes undefined behavior or crashes.

1
LoadModel(path) — parse the .tflite FlatBuffer from disk
2
NewInterpreter(model, threads) — allocate an interpreter (optionally apply delegates first)
3
AllocateTensors(interp) — reserve memory for all input/output tensors
4
SetInputFloat / SetInputUInt8 / SetInputInt8 — copy data into the input tensor
5
Invoke(interp) — run the model; blocks until inference completes
6
GetOutputFloat / GetOutputUInt8 / GetOutputInt8 — copy results out of the output tensor

Key Capabilities

CapabilityDetail
DelegatesGPU (OpenGL ES on Android/Linux, Metal on Apple), NNAPI (Android Neural Networks API), Core ML, Hexagon DSP. Apply with ApplyDelegate before AllocateTensors.
QuantizationINT8 and INT16 post-training quantization for 4× smaller models and faster CPU inference. Use SetInputInt8 / GetOutputInt8 with quantized models.
Operations count170+ built-in ops covering convolutions, LSTM, GRU, attention, depthwise separable conv, batch norm, and common activations.
MetadataModels may embed input/output labels, normalization parameters, and descriptions. Read with GetModelInfo and GetTensorInfo.

vs Other Inference Runtimes

RuntimeTFLite advantageWhen to prefer the other
ONNX RuntimeSmaller binary, better mobile/edge support, Android NNAPI integrationONNX Runtime when your model originates in PyTorch and you need CUDA on desktop
PyTorch MobileTFLite binary is significantly smaller; broader hardware delegate ecosystemPyTorch Mobile when you need full PyTorch op coverage or custom C++ extensions
OpenVINOTFLite is hardware-agnostic; runs on ARM, Android, microcontrollersOpenVINO when you target Intel CPUs/GPUs/VPUs and need maximum throughput on x86

Quick Start

Image classification (one-liner)

uses tflite;

{ High-level helper: loads model, runs inference, returns top-3 label strings }
var labels := ClassifyImage('mobilenet_v2.tflite', imageRGBBytes, 224, 224, 3);

for var i := 0 to Length(labels) - 1 do
  WriteLn(labels[i]);

Custom model with float32 tensors

uses tflite;

{ Load and inspect }
var model := LoadModel('my_model.tflite');
var info  := GetModelInfo(model);
WriteLn('Inputs: '  + IntToStr(info.InputCount));
WriteLn('Outputs: ' + IntToStr(info.OutputCount));

{ Create interpreter (4 threads) }
var interp := NewInterpreter(model, 4);
AllocateTensors(interp);

{ Check input tensor shape }
var tin  := InputTensor(interp, 0);
var tinfo := GetTensorInfo(tin);
WriteLn('Input: ' + tinfo.Name + ' type=' + IntToStr(Ord(tinfo.TypeKind)));

{ Fill input with normalized float32 pixel data }
SetInputFloat(interp, 0, pixelFloats);  { array of Single, length = 1*224*224*3 }

{ Run inference }
Invoke(interp);

{ Read scores from output tensor 0 }
var scores := GetOutputFloat(interp, 0);
WriteLn('Output classes: ' + IntToStr(Length(scores)));

{ Cleanup }
FreeInterpreter(interp);
FreeModel(model);

GPU delegate

uses tflite;

var model  := LoadModel('detector.tflite');
var interp := NewInterpreter(model, 1);

{ Create GPU delegate and apply BEFORE AllocateTensors }
var gpu := NewGPUDelegate();
ApplyDelegate(interp, gpu);

AllocateTensors(interp);

{ Fill inputs, run, read outputs as usual }
SetInputUInt8(interp, 0, imageBytes);
Invoke(interp);
var boxes := GetOutputFloat(interp, 0);

FreeDelegate(gpu);
FreeInterpreter(interp);
FreeModel(model);

TTFLiteType Enum

Tensor element types. Check TTFLiteTensorInfo.TypeKind to select the correct SetInput* / GetOutput* variant.

ValueOrdinalDescription
tftNoType0Unset / unknown type
tftFloat32132-bit float — most common for non-quantized models
tftInt322Signed 32-bit integer
tftUInt83Unsigned 8-bit — default quantized type for Android deployment
tftInt644Signed 64-bit integer (e.g. token IDs in NLP models)
tftString5Variable-length string tensor
tftBool6Boolean tensor
tftInt167Signed 16-bit integer (INT16 quantization)
tftComplex64864-bit complex number (32-bit real + 32-bit imaginary)
tftInt89Signed 8-bit integer — INT8 quantized models
tftFloat161016-bit float (half precision)
tftFloat641164-bit float (double precision)

Types

Handle types

type
  TTFLiteModel       = Integer;  { loaded .tflite model }
  TTFLiteInterpreter = Integer;  { inference interpreter }
  TTFLiteTensor      = Integer;  { tensor handle }
  TTFLiteDelegate    = Integer;  { hardware delegate handle }

TTFLiteType

type
  TTFLiteType = (
    tftNoType    = 0,  tftFloat32 = 1,  tftInt32   = 2,
    tftUInt8     = 3,  tftInt64   = 4,  tftString  = 5,
    tftBool      = 6,  tftInt16   = 7,  tftComplex64 = 8,
    tftInt8      = 9,  tftFloat16 = 10, tftFloat64 = 11
  );

TTFLiteTensorInfo

type
  TTFLiteTensorInfo = record
    Name     : string;               { tensor name from model metadata }
    TypeKind : TTFLiteType;          { element type (see TTFLiteType enum) }
    Dims     : array of Integer;    { shape, e.g. [1, 224, 224, 3] }
    ByteSize : Integer;              { total bytes in the tensor }
    IsInput  : Boolean;
    IsOutput : Boolean;
  end;

TTFLiteModelInfo

type
  TTFLiteModelInfo = record
    InputCount  : Integer;  { number of input tensors }
    OutputCount : Integer;  { number of output tensors }
    Description : string;  { embedded model description, if any }
  end;

Model Functions

FunctionDescription
LoadModel(Path)Load a .tflite FlatBuffer from the filesystem. Returns a model handle.
LoadModelBuffer(Data)Load from an in-memory string buffer (e.g. after downloading over the network).
FreeModel(M)Release the model and its memory. Do not use the handle after freeing.
GetModelInfo(M)Returns a TTFLiteModelInfo with input/output counts and the embedded description string.

Interpreter Functions

FunctionDescription
NewInterpreter(M, NumThreads)Create an interpreter for model M. Pass 0 for NumThreads to let TFLite choose automatically.
FreeInterpreter(I)Destroy the interpreter and release tensor buffers.
AllocateTensors(I)Allocate memory for all input and output tensors. Must be called after NewInterpreter (and after ApplyDelegate) before the first Invoke.
ApplyDelegate(I, D)Attach a hardware delegate. Must be called before AllocateTensors.
Invoke(I)Execute inference. Blocks until complete. Input tensors must be filled first.

Tensor Functions

FunctionDescription
InputCount(I)Number of input tensors the model expects.
OutputCount(I)Number of output tensors the model produces.
InputTensor(I, Index)Handle to input tensor Index (zero-based).
OutputTensor(I, Index)Handle to output tensor Index (zero-based).
GetTensorInfo(T)Returns TTFLiteTensorInfo: name, type, shape, byte size, and I/O flags.
ResizeTensor(I, Index, NewDims)Resize an input tensor for dynamic-shape models. Call AllocateTensors again after resizing.

Fill Input Tensor Functions

Select the function that matches TTFLiteTensorInfo.TypeKind for tensor 0 (or whichever input index you are filling).

FunctionElement typeDescription
SetInputFloat(I, Index, Data)tftFloat32Copy a array of Single into the input tensor. Length must match the tensor's total element count.
SetInputUInt8(I, Index, Data)tftUInt8Copy a array of Byte. Typical for quantized MobileNet / EfficientDet models.
SetInputInt8(I, Index, Data)tftInt8Copy a array of ShortInt. Use with INT8-quantized models that store signed values.

Read Output Tensor Functions

FunctionElement typeDescription
GetOutputFloat(I, Index)tftFloat32Returns array of Single with the raw logits or scores.
GetOutputUInt8(I, Index)tftUInt8Returns array of Byte for quantized output tensors.
GetOutputInt8(I, Index)tftInt8Returns array of ShortInt for signed INT8 output tensors.

Delegate Functions

FunctionDescription
NewGPUDelegate()Create a GPU delegate. Uses OpenGL ES on Android/Linux; Metal on Apple platforms. Apply before AllocateTensors.
NewNNAPIDelegate()Create an Android Neural Networks API delegate. Routes ops to DSP, NPU, or GPU as available on the device.
FreeDelegate(D)Release the delegate after the interpreter is also freed.
Tip — delegate ordering

Always call ApplyDelegate before AllocateTensors. The delegate may not support all ops in your model; TFLite falls back to CPU for unsupported ops automatically.

Convenience Functions

FunctionDescription
ClassifyImage(ModelPath, ImageRGB, Width, Height, TopN)Load model, run inference on an RGB byte buffer (Height × Width × 3), and return the top-N label strings with scores. One-shot, manages its own interpreter lifecycle.
DetectObjects(ModelPath, ImageRGB, Width, Height, Threshold)Run object detection and return a JSON string with bounding boxes, class labels, and confidence scores above Threshold.
TFLiteVersion()Returns the linked TFLite runtime version string (e.g. "2.14.0").

Package Info

Version1.0.0
Typeclib
C Librarylibtflite-dev
Authorgustavo

System Requirements

libtflite-dev

Build from source:
github.com/tensorflow/tensorflow

Shared lib name:
libpai_tflite.so

TTFLiteType Reference

tftNoType = 0
tftFloat32 = 1
tftInt32 = 2
tftUInt8 = 3
tftInt64 = 4
tftString = 5
tftBool = 6
tftInt16 = 7
tftComplex64 = 8
tftInt8 = 9
tftFloat16 = 10
tftFloat64 = 11

Functions

  • LoadModel / LoadModelBuffer
  • FreeModel / GetModelInfo
  • NewInterpreter / FreeInterpreter
  • AllocateTensors / Invoke
  • ApplyDelegate
  • InputCount / OutputCount
  • InputTensor / OutputTensor
  • GetTensorInfo / ResizeTensor
  • SetInputFloat
  • SetInputUInt8 / SetInputInt8
  • GetOutputFloat
  • GetOutputUInt8 / GetOutputInt8
  • NewGPUDelegate / NewNNAPIDelegate
  • FreeDelegate
  • ClassifyImage / DetectObjects
  • TFLiteVersion

See Also