gonnx is a thin packaging wrapper on the onnxruntime-purego package. A library which loads libonnx using purego.
While there are other non-cgo options for ONNX inference, I found inference to be orders of magnitudes slower than libonnx. Of course, there are also cgo options for libonnx, but they evidently come with the downsides of cgo. Thus for me (and I wager for many) libonnx with purego is the best option.
Along with helpers for embedding batteries-included ONNX models directly in your go binary, gonnx has helpers for embedding libonnx (required by purego).
The available libonnx runtime packages follow:
github.com/mackross/gonnx/runtimes/linuxamd64
github.com/mackross/gonnx/runtimes/linuxarm64
github.com/mackross/gonnx/runtimes/darwinamd64
github.com/mackross/gonnx/runtimes/darwinarm64
github.com/mackross/gonnx/runtimes/windowsamd64
github.com/mackross/gonnx/runtimes/windowsarm64
Simply _ import these packages for architectures where you wish to use libonnx with purego.
gonnx-models contain ONNX models that have already been prepared with gonnx. They are ready to run without download at runtime. The ONNX model data has been embedded into the go packages using go embed. While this increases the size of your binary, the ease of running a model that "just works" is often worth it.
import (
"context"
"github.com/gonnx-models/bertuncased"
"github.com/mackross/gonnx"
_ "github.com/mackross/gonnx/runtimes/linuxamd64"
)
recognizer, err := bertuncased.Open(gonnx.WithThreads(1))
if err != nil {
return err
}
defer recognizer.Close()
entities, err := recognizer.Recognize(context.Background(), "Bill Gates worked at Microsoft in Seattle.")
// entities = Bill Gates, Microsoft, Seattle.The models repository contains a list of the ready to drop in gonnx (go module) models.