mattn/go-tflite

Go binding for TensorFlow Lite

Overview

go‑tflite is a Go language binding for the TensorFlow Lite C API. It lets Go programs load a .tflite model, allocate tensors, feed input data, invoke inference, and read the results – all without having to compile the full TensorFlow library. The project ships pre‑built TensorFlow Lite “buildkits” for the major OS/CPU combos and provides instructions for building the C library yourself if you need a different TensorFlow version.

Key Features

Feature What it means for you
Go‑native API Simple Go functions such as NewModelFromFile, NewInterpreter, AllocateTensors, GetInputTensor, Invoke, and GetOutputTensor wrap the underlying C calls.
Pre‑built binaries Release tarballs contain libtensorflowlite_c.so (or .dll on Windows) and XNNPACK delegate libraries, so you can install them with a couple of commands.
Edge‑TPU support Instructions are provided to link the Google Coral Edge‑TPU delegate, enabling hardware‑accelerated inference on supported devices.
Docker / Binder demo A ready‑to‑run Jupyter notebook (iris.ipynb) is hosted on Binder, showing how to train a tiny Iris classifier using go‑tflite directly in the browser.
Cross‑platform Buildkits for Linux (amd64/arm64), macOS (amd64/arm64) and Windows (amd64‑mingw) are available.
MIT license Free to use, modify, and embed in commercial projects.

Typical Workflow (from the README example)

model := tflite.NewModelFromFile("sin_model.tflite")
interpreter := tflite.NewInterpreter(model, tflite.NewInterpreterOptions())
interpreter.AllocateTensors()

// Fill the first input tensor
v := float64(1.2) * math.Pi / 180.0
interpreter.GetInputTensor(0).Float32s()[0] = float32(v)

// Run inference
interpreter.Invoke()

// Read the first output tensor
result := float64(interpreter.GetOutputTensor(0).Float32s()[0])
  1. Load a compiled TensorFlow Lite model (*.tflite).
  2. Create an interpreter with optional configuration (InterpreterOptions).
  3. Allocate memory for all tensors.
  4. Write input values via the tensor’s typed slice helpers (Float32s()).
  5. Invoke the model.
  6. Read the output values.

All resources are manually released with Delete() calls to avoid memory leaks.

Getting Started

  1. Install the C library – either download the appropriate tflite-buildkit‑<tag>-<target>.tar.gz from the releases page and extract it to /usr/local (Linux/macOS) or a custom folder (Windows), then run ldconfig on Linux.
  2. Set CGO flags if you placed the headers/libraries in a non‑standard location (CGO_CFLAGS, CGO_LDFLAGS, LD_LIBRARY_PATH).
  3. Write Go code using the API shown above; the repository includes an _example directory with more complete samples.
  4. Run go build (add -tags xnnpack_builtin if you built TensorFlow Lite with XNNPACK baked in).

When to Use go‑tflite

  • You are building a Go service or CLI that needs on‑device inference (e.g., edge devices, IoT, or low‑latency servers).
  • You prefer not to embed the heavyweight TensorFlow runtime; TensorFlow Lite’s C API is lightweight and fast.
  • You want to take advantage of hardware delegates like XNNPACK or Google’s Edge TPU from Go.

Limitations

  • The binding is thin; you still need to manage tensor memory manually and understand the shape/types expected by your model.
  • Training is not supported – go‑tflite is inference‑only, as is TensorFlow Lite.
  • You must supply a compatible libtensorflowlite_c library; mismatched versions can cause runtime errors.

Resources

  • Binder notebook – interactive Iris classifier demo.
  • Docker image – ghcr.io/mattn/go-tflite/gophernotes for notebook use.
  • Edge‑TPU guide – links to Google Coral repository and install steps.
  • Source examples – see the _example folder in the repo.

All information above is taken directly from the repository’s README.

Related

  • Project
  • Project
  • Project
  • Project
  • Project