onnxruntime: CPU inference of ONNX models in Racket
| (require onnxruntime) | package: onnxruntime |
This package provides Racket FFI bindings to the ONNX Runtime C API for CPU inference of ONNX models. It is the runtime leg of the “train in Python once, export .onnx, infer anywhere” workflow: train with PyTorch/TensorFlow/scikit-learn, export the model, then load and run it from Racket with a small, dependency-light API.
Version 0.1 scope. float32 tensors only, CPU execution provider only, models loaded from a file path or from bytes. Sequence, map, and string tensors are out of scope; sessions cannot be closed explicitly (native memory is reclaimed when the Racket process exits).
1 Installation
- Racket package:
raco pkg install onnxruntime
Native library: the loader must find libonnxruntime (.dylib on macOS, .so on Linux).
On macOS with Homebrew:
brew install onnxruntime
The dylibs land in /opt/homebrew/opt/onnxruntime/lib/ (Apple silicon), which the loader probes automatically as a fallback. You can also export DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/opt/onnxruntime/lib to make the system dynamic linker find it directly.
On Linux, download and extract an official release tarball, e.g.
ORT_VERSION=1.19.2
curl -fLO "https://github.com/microsoft/onnxruntime/releases/download/v${ORT_VERSION}/onnxruntime-linux-x64-${ORT_VERSION}.tgz"
tar -xzf onnxruntime-linux-x64-${ORT_VERSION}.tgz
export LD_LIBRARY_PATH="$PWD/onnxruntime-linux-x64-${ORT_VERSION}/lib:$LD_LIBRARY_PATH"
Explicit override: set ONNXRUNTIME_LIB to the full path of the library file (or a directory containing it) and it is tried first.
2 API version negotiation
The ORT C API is a versioned function table: OrtGetApiBase() yields a GetApi(version) accessor, and each API version’s function table is a fixed, append-only ordering of function pointers. At load time this package asks for the newest table it knows (version 31, from the onnxruntime 1.31.0 header its index was generated from) and walks down to version 10 until the runtime answers with a non-null table. Consequently:
against onnxruntime >= 1.31 the negotiated version is 31;
older runtimes work too, down to API version 10 (onnxruntime 1.10.x), and (onnx-api-version) reports what was negotiated;
while negotiating downward on an older runtime, ORT prints one warning line per unavailable version to stderr – that is expected.
3 Quick start
Given a model add.onnx computing Z = X + [[1,2],[3,4]] over a float32 [2,2] input X:
(require onnxruntime racket/flonum) (define s (open-onnx-session "add.onnx")) (session-input-names s) (session-output-names s) (session-input-shape s "X") (define x (make-ort-tensor '(2 2) (flvector 1.0 1.0 1.0 1.0))) (define results (onnx-run s (list (cons "X" x)))) (results-ref results "Z")
procedure
(results-ref results name) → ort-tensor?
results : (listof (cons/c string? ort-tensor?)) name : string?
4 Reference
procedure
(onnx-version) → string?
procedure
struct
(struct ort-tensor (shape data))
shape : (listof exact-nonnegative-integer?) data : flvector?
procedure
(make-ort-tensor shape data) → ort-tensor?
shape : (listof exact-nonneg-integer?) data : flvector?
procedure
(open-onnx-session src [ #:optimization-level level]) → onnx-session? src : (or/c path-string? bytes?) level : (or/c 'disable 'basic 'extended 'all) = 'all
procedure
(onnx-session? v) → boolean?
v : any/c
procedure
(session-input-names s) → (listof string?)
s : onnx-session?
procedure
(session-output-names s) → (listof string?)
s : onnx-session?
procedure
(session-input-shape s name)
→ (listof (or/c exact-positive-integer? 'dynamic)) s : onnx-session? name : string?
procedure
(session-output-shape s name)
→ (listof (or/c exact-positive-integer? 'dynamic)) s : onnx-session? name : string?
procedure
(onnx-run s inputs) → (listof (cons/c string? ort-tensor?))
s : onnx-session? inputs : (listof (cons/c string? ort-tensor?))
5 Documented limits (v0.1)
float32 tensors only (other element types raise exn:fail);
CPU execution provider only;
all model inputs must be passed on every run;
no explicit session close: native session memory lives until process exit;
6 License
MIT. ONNX Runtime itself is separately licensed by Microsoft under the MIT license.