Skip to main content

LiteRT tuner Python library.

Project description

litert-tunner

Fine-tune fully-quantized INT8 LiteRT (TFLite) models after export — without retraining from scratch.

What is this?

litert-tunner parses an exported INT8 .tflite model, reconstructs it as a Keras 3 model with simulated quantization, lets you fine-tune parameters (biases, scales, weights), and writes them back into the flatbuffer. Graph topology stays intact.

Note: Even though this library simulates the quantization process during fine-tuning, in some cases the resulting INT8 model may still perform worse than a full-precision float32 model.

Installation

From source (recommended for development)

# Clone the repository
git clone https://github.com/kmkolasinski/litert-tunner.git
cd litert-tunner

# Create a virtual environment and install uv
make venv
make init

# Install the project in editable mode with dev dependencies
make install

As a dependency (pip)

pip install litert-tunner

(Core dependencies: keras>=3.0, numpy, tflite>=2.18.0, flatbuffers. tensorflow and ai-edge-litert are only needed for dev/tests).

Quickstart

For a complete, runnable end-to-end example, see the Example Notebook.

import litert_tunner

# 1. Load an INT8 LiteRT model → trainable Keras replica
model = litert_tunner.load_model("model_int8.tflite")

# 2. Inference — should match LiteRT Interpreter output
predictions = model.predict(inputs)

# 3. Prepare for fine-tuning (freeze everything except biases & scales)
litert_tunner.prepare_for_finetuning(model, trainable_pattern=".*bias")

# 4. Fine-tune with any Keras optimizer / loss
model.compile(optimizer="adam", loss="mse")
model.fit(x_train, y_train, epochs=5)

# 5. Export — writes updated parameters back into the flatbuffer
litert_tunner.save_model(model, "model_int8_finetuned.tflite")

Distillation Fine-Tuning (Recommended)

import litert_tunner

# 1. Load an INT8 LiteRT model → trainable Keras replica
student_model = litert_tunner.load_model("model_int8.tflite")
teacher_model = litert_tunner.load_model("model_float32.tflite")

# 2. Freeze everything except biases
litert_tunner.prepare_for_finetuning(student_model, trainable_pattern=".*bias")

# 3. Fine-tune using Trainer (handles distillation & weight drift)
trainer = litert_tunner.Trainer(
    student_model=student_model,
    teacher_model=teacher_model,  # Original float32 model
)
trainer.compile(optimizer="adam", loss="mse")
trainer.fit(train_ds, validation_data=val_ds, epochs=5)

# 4. Save updated parameters to flatbuffer
litert_tunner.save_model(student_model, "model_int8_finetuned.tflite")

Supported Operations

  • Linear: FULLY_CONNECTED, CONV_2D, DEPTHWISE_CONV_2D, TRANSPOSE_CONV
  • Arithmetic: ADD, SUB, MUL, DIV, SQUARED_DIFFERENCE, NEG
  • Activation: RELU, GELU, LOGISTIC, SOFTMAX
  • Pooling: AVERAGE_POOL_2D, MAX_POOL_2D, MEAN
  • Reshape/Resize: RESHAPE, TRANSPOSE, PACK, STRIDED_SLICE, RESIZE_NEAREST_NEIGHBOR, SHAPE, EXPAND_DIMS, PAD, TILE
  • Other: CONCATENATION, RSQRT, QUANTIZE, DEQUANTIZE

Fused activations (RELU, RELU6, RELU_N1_TO_1) are supported.

License

MIT — Copyright © 2026 Krzysztof Kolasinski

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

litert_tunner-0.3.0.tar.gz (61.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

litert_tunner-0.3.0-py3-none-any.whl (91.9 kB view details)

Uploaded Python 3

File details

Details for the file litert_tunner-0.3.0.tar.gz.

File metadata

  • Download URL: litert_tunner-0.3.0.tar.gz
  • Upload date:
  • Size: 61.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for litert_tunner-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3d40a602a378ef219c6f259b9ed1f3c551fee3279eee548802877f62878c4f53
MD5 14d418cdc85a2a4950f7bd8cea2640bd
BLAKE2b-256 8efd9027b5ea3641178a741816c8920c626810c93c01617f96c6933b4dc705fc

See more details on using hashes here.

File details

Details for the file litert_tunner-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: litert_tunner-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 91.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for litert_tunner-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bd24d866308ec9ba285c425cd29069e3311cf6c7249c722db7dfbc11bdca07ad
MD5 8e6693bd8010c3e1ec63aeb4ac6f0e6e
BLAKE2b-256 f70559903e13c28be1703ba1015a706917a46ed8318efe9b12c16a89613bc95b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page