Skip to content

Repository files navigation

Zig wrapper for ONNX Runtime

Work in progress, implementing vertical slices of ONNX Runtime API surface as they're needed.

Usage

Add the dependency:

zig fetch --save git+https://github.com/recursiveGecko/onnxruntime.zig

Then wire it into build.zig:

conststd=@import("std");
pubfnbuild(b: *std.Build) !void {
consttarget=b.standardTargetOptions(.{});
constoptimize=b.standardOptimizeOption(.{});
// the wrapperconstzig_onnx_dep=b.dependency("onnxruntime_zig", .{
.target=target,
.optimize=optimize,
});
// onnx runtime shared libraryconstonnxruntime_dep=b.dependency("onnxruntime_linux_x64", .{});
constexe=b.addExecutable(.{
.name="app",
.root_module=b.createModule(.{
.root_source_file=b.path("src/main.zig"),
.target=target,
.optimize=optimize,
}),
});
exe.root_module.addImport("onnxruntime", zig_onnx_dep.module("onnxruntime"));
exe.root_module.addLibraryPath(onnxruntime_dep.path("lib"));
exe.linkLibC();
exe.linkSystemLibrary("onnxruntime");
// installconstinstall_exe=b.addInstallArtifact(exe, .{});
b.getInstallStep().dependOn(&install_exe.step);
{
// onnxruntime shared libraryconstinstall_onnxruntime_libs=b.addInstallLibFile(
onnxruntime_dep.path("lib/libonnxruntime.so.1"),
"libonnxruntime.so.1",
);
install_exe.step.dependOn(&install_onnxruntime_libs.step);
// optional, load library from relative direxe.root_module.addRPathSpecial("$ORIGIN/../lib");
exe.each_lib_rpath=false;
}
}

And in src/main.zig:

constonnxruntime=@import("onnxruntime");
pubfnmain() !void {
_=onnxruntime;
}

Examples

Please note that examples don't have a functioning CLI interface at this point, some paths are hardcoded at the top of main.zig.

Both examples currently require libsndfile to be installed on the system. On Debian/Ubuntu, install libsndfile1-dev.

To build or run the examples, run:

# Run Silero VADcd examples/silero_vad
zig build run
# Run NSNet2cd examples/nsnet2
zig build run

Licensing

/src/*, /examples/*/src/* and any other first party code - Mozilla Public License 2.0 (/LICENSE.txt)

/examples/nsnet2/data/*.onnx - NSNet2 ONNX models are MIT licensed by Microsoft.

/examples/silero_vad/data/*.onnx - Silero VAD ONNX models are MIT licensed by Silero Team.

/examples/*/data/*.wav - Example WAV files are unlicensed. Please open a GitHub issue to request removal if you believe these files are infringing on your copyright and fall outside the scope of fair use.

About

Incomplete experimental Zig wrapper for ONNX Runtime with examples (Silero VAD, NSNet2)

Topics

Resources

Stars

36 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages