Skip to content

Commit

Permalink
feat(candle,tract): standalone builds
Browse files Browse the repository at this point in the history
Allows the candle/tract backends to be compiled as a standalone dylib/staticlib so they can be directly swapped into any application in place of `onnxruntime.dll` or `libonnxruntime.a` (if anybody could figure out how to make Rust static libs work outside of Rust)
  • Loading branch information
decahedron1 committed Feb 24, 2025
1 parent 0539cd5 commit 62d40b3
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 4 deletions.
16 changes: 15 additions & 1 deletion backends/candle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ authors = [
"pyke.io <contact@pyke.io>"
]

[workspace]
resolver = "2"
members = [ "standalone" ]
default-members = []

[profile.release]
opt-level = 3
lto = true
strip = true
codegen-units = 1

[lib]
name = "ort_candle"
path = "lib.rs"

[features]

[dependencies]
[workspace.dependencies]
ort-sys = { version = "=2.0.0-rc.9", path = "../../ort-sys", default-features = false }

[dependencies]
ort-sys = { workspace = true }
candle-core = { version = "0.8.1", default-features = false }
candle-onnx = { version = "0.8.1" }
prost = { version = "0.12.1", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion backends/candle/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1975,7 +1975,7 @@ unsafe extern "system" fn SetEpDynamicOptions(
Error::new_sys(OrtErrorCode::ORT_NOT_IMPLEMENTED, "Unimplemented")
}

pub fn api() -> OrtApi {
pub const fn api() -> OrtApi {
OrtApi {
CreateStatus,
GetErrorCode,
Expand Down
23 changes: 23 additions & 0 deletions backends/candle/standalone/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "ort-candle-standalone"
publish = false
version = "1.20.0"
edition = "2021"

[package.metadata.winresource]
ProductName = "ONNX Runtime (candle)"
FileDescription = "API-compatible ONNX Runtime implementation using candle"
LegalCopyright = "Copyright ©️ 2025 pyke.io"
OriginalFilename = "onnxruntime+candle.dll"

[lib]
name = "onnxruntime"
crate-type = [ "staticlib", "cdylib" ]
path = "lib.rs"

[dependencies]
ort-candle = { path = "../" }
ort-sys = { workspace = true, features = [ "disable-linking" ] }

[build-dependencies]
winresource = "0.1"
6 changes: 6 additions & 0 deletions backends/candle/standalone/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
let res = winresource::WindowsResource::new();
res.compile().unwrap();
}
}
18 changes: 18 additions & 0 deletions backends/candle/standalone/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
static API_BASE: ort_sys::OrtApiBase = ort_sys::OrtApiBase {
GetVersionString: get_version_string,
GetApi: get_api
};
static API: ort_sys::OrtApi = ort_candle::api();

unsafe extern "system" fn get_version_string() -> *const ort_sys::c_char {
c"1.20.0+candle@0.8-wrapper@0.1.0".as_ptr()
}

unsafe extern "system" fn get_api(version: u32) -> *const ort_sys::OrtApi {
if version <= 20 { &API as *const _ } else { core::ptr::null() }
}

#[no_mangle]
pub unsafe extern "C" fn OrtGetApiBase() -> *const ort_sys::OrtApiBase {
&API_BASE as *const _
}
16 changes: 15 additions & 1 deletion backends/tract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ authors = [
"pyke.io <contact@pyke.io>"
]

[workspace]
resolver = "2"
members = [ "standalone" ]
default-members = []

[profile.release]
opt-level = 3
lto = true
strip = true
codegen-units = 1

[lib]
name = "ort_tract"
path = "lib.rs"

[features]

[dependencies]
[workspace.dependencies]
ort-sys = { version = "=2.0.0-rc.9", path = "../../ort-sys", default-features = false }

[dependencies]
ort-sys = { workspace = true }
tract-onnx = "0.21"
parking_lot = "0.12"

Expand Down
2 changes: 1 addition & 1 deletion backends/tract/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,7 @@ unsafe extern "system" fn SetEpDynamicOptions(
Error::new_sys(OrtErrorCode::ORT_NOT_IMPLEMENTED, "Unimplemented")
}

pub fn api() -> OrtApi {
pub const fn api() -> OrtApi {
OrtApi {
CreateStatus,
GetErrorCode,
Expand Down
23 changes: 23 additions & 0 deletions backends/tract/standalone/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "ort-tract-standalone"
publish = false
version = "1.20.0"
edition = "2021"

[package.metadata.winresource]
ProductName = "ONNX Runtime (tract)"
FileDescription = "API-compatible ONNX Runtime implementation using tract"
LegalCopyright = "Copyright ©️ 2025 pyke.io"
OriginalFilename = "onnxruntime+tract.dll"

[lib]
name = "onnxruntime"
crate-type = [ "staticlib", "cdylib" ]
path = "lib.rs"

[dependencies]
ort-tract = { path = "../" }
ort-sys = { workspace = true, features = [ "disable-linking" ] }

[build-dependencies]
winresource = "0.1"
6 changes: 6 additions & 0 deletions backends/tract/standalone/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
let res = winresource::WindowsResource::new();
res.compile().unwrap();
}
}
18 changes: 18 additions & 0 deletions backends/tract/standalone/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
static API_BASE: ort_sys::OrtApiBase = ort_sys::OrtApiBase {
GetVersionString: get_version_string,
GetApi: get_api
};
static API: ort_sys::OrtApi = ort_tract::api();

unsafe extern "system" fn get_version_string() -> *const ort_sys::c_char {
c"1.20.0+tract@0.21-wrapper@0.1.0".as_ptr()
}

unsafe extern "system" fn get_api(version: u32) -> *const ort_sys::OrtApi {
if version <= 20 { &API as *const _ } else { core::ptr::null() }
}

#[no_mangle]
pub unsafe extern "C" fn OrtGetApiBase() -> *const ort_sys::OrtApiBase {
&API_BASE as *const _
}

0 comments on commit 62d40b3

Please sign in to comment.