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
tunner_model = litert_tunner.load_model("model_int8.tflite")

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

# 3. Fine-tune using Trainer (handles distillation & weight drift)
trainer = litert_tunner.Trainer(
    student_model=tunner_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(tunner_model, "model_int8_finetuned.tflite")

Supported Operations

  • Linear: FULLY_CONNECTED, CONV_2D, DEPTHWISE_CONV_2D
  • 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
  • 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.1.1.tar.gz (56.2 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.1.1-py3-none-any.whl (82.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: litert_tunner-0.1.1.tar.gz
  • Upload date:
  • Size: 56.2 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.1.1.tar.gz
Algorithm Hash digest
SHA256 061b6eb596146cbcbf836adff7b6b4c2816d787954d3297588e2c12886f6ffcb
MD5 a641135b5ee1dd2854d3ae7e89b7d041
BLAKE2b-256 8a0a1fceb7fc447aa877c6a6d5cbfd33282c41f8ba6a41843ef83634cc687774

See more details on using hashes here.

File details

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

File metadata

  • Download URL: litert_tunner-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 82.5 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 33ddc0e85db2b8d0edf7c279750747dbfc7f8c499671fedd426f295660a3fda1
MD5 01f239d86aaa1889a36a7be74a6bd9d5
BLAKE2b-256 783fd10f8efbb099e45982ed35a1116a2341096ff806cf14172a6cf908bd78fe

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