Files
izackp 2af97cc6bf
CodeQL Advanced / Analyze (swift) (push) Has been cancelled
Replace Objective-C bridge with Swift ONNX runtime backend
2026-06-09 09:38:43 -04:00

1.5 KiB

Swift Package Manager for ONNX Runtime

Swift-native ONNX Runtime bindings derived from the public Objective-C wrapper surface, but implemented without the Objective-C bridge so the package can build on Linux.

Status

  • Public API covers inference, session/run options, tensor values, checkpoint state, and training session entry points.
  • Backend is wired to the ONNX Runtime C API and training C API through a Swift system library target.
  • The package expects libonnxruntime and headers to be available on the host, typically via pkg-config.

Requirements

  • Swift 6.1+
  • ONNX Runtime installed on the system with:
    • libonnxruntime
    • onnxruntime_c_api.h
    • onnxruntime_training_c_api.h
    • pkg-config metadata for libonnxruntime

Usage

import OnnxRuntimeBindings

let environment = try ORTEnv(loggingLevel: .warning)
let sessionOptions = try ORTSessionOptions()
let session = try ORTSession(
    env: environment,
    modelPath: "/path/to/model.onnx",
    sessionOptions: sessionOptions
)

let inputTensor = try ORTValue(
    tensorData: NSMutableData(data: inputData),
    elementType: .float,
    shape: [1, 224, 224, 3]
)

let outputs = try session.run(
    inputs: ["input": inputTensor],
    outputNames: ["output"]
)

Notes

  • The objectivec/ directory remains in the repo as the reference contract surface from upstream ONNX Runtime.
  • The shipped Swift package target is the pure Swift OnnxRuntimeBindings implementation under swift/OnnxRuntimeBindings.