Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding build using binary downloads #8

Merged
merged 4 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ futures-util = "0.3.29"
[lib]
name = "surrealml"
path = "src/lib.rs"

[build-dependencies]
ort = { version = "1.16.2", default-features = true }
31 changes: 31 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::path::Path;
use std::env;


fn main() {
let target_lib = match env::var("CARGO_CFG_TARGET_OS").unwrap() {
ref s if s.contains("linux") => "libonnxruntime.so",
ref s if s.contains("macos") => "libonnxruntime.dylib",
ref s if s.contains("windows") => "onnxruntime.dll",
// ref s if s.contains("android") => "android", => not building for android
_ => panic!("Unsupported target os")
};
let profile = match env::var("PROFILE").unwrap() {
ref s if s.contains("release") => "release",
ref s if s.contains("debug") => "debug",
_ => panic!("Unsupported profile")
};

// remove ./modules/utils/target folder if there
let _ = std::fs::remove_dir_all(Path::new("modules").join("utils").join("target")).unwrap_or(());

// create the target module folder for the utils module
let _ = std::fs::create_dir(Path::new("modules").join("utils").join("target"));
let _ = std::fs::create_dir(Path::new("modules").join("utils").join("target").join(profile));

// copy target folder to modules/utils/target profile for the utils modules
std::fs::copy(
Path::new("target").join(profile).join(target_lib),
Path::new("modules").join("utils").join("target").join(profile).join(target_lib)
).unwrap();
}
5 changes: 4 additions & 1 deletion modules/utils/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
onnx_driver/
target/
output/
output/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh we could probably move the package to output/downloaded_onnx instead. Because it is a byproduct of building, even if its required for the build.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now removed the reliance on downloads and it's all now in thetarget directory

downloaded_onnx_package/
src/execution/libonnxruntime.a
libonnxruntime.*
5 changes: 4 additions & 1 deletion modules/utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "surrealml-core"
version = "0.0.3"
version = "0.0.4"
edition = "2021"
build = "./build.rs"
description = "The core machine learning library for SurrealML that enables SurrealDB to store and load ML models"
Expand All @@ -23,3 +23,6 @@ tokio = { version = "1.12.0", features = ["full"] }
[lib]
name = "surrealml_core"
path = "src/lib.rs"

[build-dependencies]
ort = { version = "1.16.2", default-features = true }
69 changes: 23 additions & 46 deletions modules/utils/build.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,33 @@
use std::process::Command;
use std::path::Path;
use std::env;
use std::fs;

fn main() {

fn main() -> std::io::Result<()> {

match std::env::var("ONNXRUNTIME_LIB_PATH") {
Ok(_) => {
println!("cargo:rustc-cfg=onnx_runtime_env_var_set");
},
Err(_) => {
#[cfg(not(windows))]
{
let _ = Command::new("sh")
.arg("-c")
.arg("cargo new onnx_driver && cd onnx_driver && echo 'ort = \"1.16.2\"' >> Cargo.toml
")
.status()
.expect("failed to execute process");
}

#[cfg(windows)]
{
// let _ = Command::new("cmd")
// .args(&["/C", "cargo new onnx_driver && cd onnx_driver && echo ort = \"1.16.2\" >> Cargo.toml"])
// .status()
// .expect("failed to execute process");
let _ = Command::new("powershell")
.arg("-Command")
.arg("cargo new onnx_driver; Set-Location onnx_driver; Add-Content -Path .\\Cargo.toml -Value 'ort = \"1.16.2\"'")
.status()
.expect("failed to execute process");
}

#[cfg(not(windows))]
{
let _ = Command::new("sh")
.arg("-c")
.arg("cd onnx_driver && cargo build")
.status()
.expect("failed to execute process");
}

#[cfg(windows)]
{
let _ = Command::new("cmd")
.args(&["/C", "cd onnx_driver && cargo build"])
.status()
.expect("failed to execute process");
}
let target_lib = match env::var("CARGO_CFG_TARGET_OS").unwrap() {
ref s if s.contains("linux") => "libonnxruntime.so",
ref s if s.contains("macos") => "libonnxruntime.dylib",
ref s if s.contains("windows") => "onnxruntime.dll",
// ref s if s.contains("android") => "android", => not building for android
_ => panic!("Unsupported target os")
};
let profile = match env::var("PROFILE").unwrap() {
ref s if s.contains("release") => "release",
ref s if s.contains("debug") => "debug",
_ => panic!("Unsupported profile")
};
let lib_path = Path::new("target").join(profile).join(target_lib);
// put it next to the file of the embedding
let destination = Path::new(target_lib);
fs::copy(lib_path, destination)?;
}

}
Ok(())
}

// fn main() {
// // println!("cargo:rustc-cfg=onnx_runtime_env_var_set");
// println!("test");
// }
16 changes: 10 additions & 6 deletions modules/utils/src/execution/onnx_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::sync::Arc;

// Compiles the ONNX module into the rust binary.
#[cfg(all(target_os = "macos", not(doc), not(onnx_runtime_env_var_set)))]
pub static LIB_BYTES: &'static [u8] = include_bytes!("../../onnx_driver/target/debug/libonnxruntime.dylib");
pub static LIB_BYTES: &'static [u8] = include_bytes!("../../libonnxruntime.dylib");

#[cfg(all(any(target_os = "linux", target_os = "android"), not(doc), not(onnx_runtime_env_var_set)))]
pub static LIB_BYTES: &'static [u8] = include_bytes!("../../onnx_driver/target/debug/libonnxruntime.so");
pub static LIB_BYTES: &'static [u8] = include_bytes!("../../libonnxruntime.so");

#[cfg(all(target_os = "windows", not(doc), not(onnx_runtime_env_var_set)))]
pub static LIB_BYTES: &'static [u8] = include_bytes!("../../onnx_driver/target/debug/onnxruntime.dll");
pub static LIB_BYTES: &'static [u8] = include_bytes!("../../libonnxruntime.dll");

// Fallback for documentation and other targets
#[cfg(any(doc, onnx_runtime_env_var_set, not(any(target_os = "macos", target_os = "linux", target_os = "android", target_os = "windows"))))]
Expand All @@ -37,10 +37,14 @@ pub static ENVIRONMENT: Lazy<Arc<Environment>> = Lazy::new(|| {
// we write the `LIB_BYTES` to a temporary file and then load that file.
Err(_) => {

let current_dir = std::env::current_dir().unwrap();
let current_dir = current_dir.to_str().unwrap();
let write_dir = std::path::Path::new(current_dir).join("libonnxruntime.dylib");

#[cfg(any(not(doc), not(onnx_runtime_env_var_set)))]
let _ = std::fs::write("./libonnxruntime.dylib", LIB_BYTES);
let _ = std::fs::write(write_dir.clone(), LIB_BYTES);

std::env::set_var("ORT_DYLIB_PATH", "./libonnxruntime.dylib");
std::env::set_var("ORT_DYLIB_PATH", write_dir.clone());
let environment = Arc::new(
Environment::builder()
.with_execution_providers([ExecutionProvider::CPU(Default::default())])
Expand All @@ -49,7 +53,7 @@ pub static ENVIRONMENT: Lazy<Arc<Environment>> = Lazy::new(|| {
std::env::remove_var("ORT_DYLIB_PATH");

#[cfg(any(not(doc), not(onnx_runtime_env_var_set)))]
let _ = std::fs::remove_file("./libonnxruntime.dylib");
let _ = std::fs::remove_file(write_dir);

return environment
}
Expand Down
Loading