Skip to content

Commit

Permalink
mapping build steps
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellflitton committed Jan 14, 2025
1 parent e6ec80c commit f4145ae
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 38 deletions.
10 changes: 8 additions & 2 deletions modules/c-wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
surrealml-core = { path = "../core" }
uuid = { version = "1.4.1", features = ["v4"] }
surrealml-core = { path = "../core", features = ["dynamic"] }
uuid = { version = "1.11.1", features = ["v4"] }
ndarray = "0.16.1"

# for the uploading the model to the server
Expand All @@ -15,3 +15,9 @@ base64 = "0.13"

[lib]
crate-type = ["cdylib"]

[build-dependencies]
reqwest = { version = "0.12.12", features = ["blocking", "json"] }
# tokio = { version = "1", features = ["full"] } # Required for reqwest
tar = "0.4" # For extracting tar files
flate2 = "1.0" # For handling gzip
17 changes: 12 additions & 5 deletions modules/c-wrapper/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN apt-get update && apt-get install -y \
curl \
gnupg \
lsb-release \
vim \
&& rm -rf /var/lib/apt/lists/*

# Set the working directory
Expand All @@ -20,19 +21,25 @@ WORKDIR /app
COPY . .

# Download ONNX Runtime 1.20.0
RUN wget https://github.com/microsoft/onnxruntime/releases/download/v1.20.0/onnxruntime-linux-x64-1.20.0.tgz \
&& tar -xvf onnxruntime-linux-x64-1.20.0.tgz \
&& mv onnxruntime-linux-x64-1.20.0 /onnxruntime
# RUN wget https://github.com/microsoft/onnxruntime/releases/download/v1.20.0/onnxruntime-linux-x64-1.20.0.tgz \
# && tar -xvf onnxruntime-linux-x64-1.20.0.tgz \
# && mv onnxruntime-linux-x64-1.20.0 /onnxruntime

# Set the ONNX Runtime library path
ENV ORT_LIB_LOCATION=/onnxruntime/lib
ENV LD_LIBRARY_PATH=$ORT_LIB_LOCATION:$LD_LIBRARY_PATH
# ENV ORT_LIB_LOCATION=/onnxruntime/lib
# ENV LD_LIBRARY_PATH=$ORT_LIB_LOCATION:$LD_LIBRARY_PATH

# Set the ONNX Runtime library path
# ENV ORT_LIB_LOCATION=$(pwd)/c-wrapper/tests/test_utils/onnxruntime/lib
# ENV LD_LIBRARY_PATH=$ORT_LIB_LOCATION:$LD_LIBRARY_PATH

# install python for the tests
RUN apt-get update && apt-get install -y python3 python3-pip

# Clean and build the Rust project
RUN cd c-wrapper/scripts && bash prep_tests.sh

# RUN rm /onnxruntime

# Run the tests
CMD ["bash", "c-wrapper/scripts/run_tests.sh"]
20 changes: 0 additions & 20 deletions modules/c-wrapper/README.md

This file was deleted.

21 changes: 16 additions & 5 deletions modules/c-wrapper/build-context/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ RUN apt-get update && apt-get install -y \
build-essential \
libssl-dev \
pkg-config \
ca-certificates \
curl \
gnupg \
lsb-release \
vim \
&& rm -rf /var/lib/apt/lists/*

# Set the working directory
Expand All @@ -16,19 +21,25 @@ WORKDIR /app
COPY . .

# Download ONNX Runtime 1.20.0
RUN wget https://github.com/microsoft/onnxruntime/releases/download/v1.20.0/onnxruntime-linux-x64-1.20.0.tgz \
&& tar -xvf onnxruntime-linux-x64-1.20.0.tgz \
&& mv onnxruntime-linux-x64-1.20.0 /onnxruntime
# RUN wget https://github.com/microsoft/onnxruntime/releases/download/v1.20.0/onnxruntime-linux-x64-1.20.0.tgz \
# && tar -xvf onnxruntime-linux-x64-1.20.0.tgz \
# && mv onnxruntime-linux-x64-1.20.0 /onnxruntime

# Set the ONNX Runtime library path
ENV ORT_LIB_LOCATION=/onnxruntime/lib
ENV LD_LIBRARY_PATH=$ORT_LIB_LOCATION:$LD_LIBRARY_PATH
# ENV ORT_LIB_LOCATION=/onnxruntime/lib
# ENV LD_LIBRARY_PATH=$ORT_LIB_LOCATION:$LD_LIBRARY_PATH

# Set the ONNX Runtime library path
# ENV ORT_LIB_LOCATION=$(pwd)/c-wrapper/tests/test_utils/onnxruntime/lib
# ENV LD_LIBRARY_PATH=$ORT_LIB_LOCATION:$LD_LIBRARY_PATH

# install python for the tests
RUN apt-get update && apt-get install -y python3 python3-pip

# Clean and build the Rust project
RUN cd c-wrapper/scripts && bash prep_tests.sh

# RUN rm /onnxruntime

# Run the tests
CMD ["bash", "c-wrapper/scripts/run_tests.sh"]
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ cd "$BUILD_DIR"
docker build --no-cache -t c-wrapper-tests .

docker run c-wrapper-tests

# docker run -it c-wrapper-tests /bin/bash
# docker run -it c-wrapper-tests /bin/bash
22 changes: 21 additions & 1 deletion modules/c-wrapper/build-context/c-wrapper/scripts/prep_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ cd $SCRIPTPATH

cd ..

# download onnxruntime
# Detect operating system
OS=$(uname -s | tr '[:upper:]' '[:lower:]')

# Detect architecture
ARCH=$(uname -m)

# Download the correct onnxruntime
if [ "$ARCH" == "x86_64" ] && [ "$OS" == "linux" ]; then
wget https://github.com/microsoft/onnxruntime/releases/download/v1.20.0/onnxruntime-linux-x64-1.20.0.tgz
tar -xvf onnxruntime-linux-x64-1.20.0.tgz
mv onnxruntime-linux-x64-1.20.0 tests/test_utils/onnxruntime
else
echo "Unsupported operating system and arch: $OS $ARCH"
exit 1
fi

export ORT_LIB_LOCATION=$(pwd)/tests/test_utils/onnxruntime/lib
export LD_LIBRARY_PATH=$ORT_LIB_LOCATION:$LD_LIBRARY_PATH

cargo build

# Get the operating system
Expand Down Expand Up @@ -42,4 +62,4 @@ if [ -f "$SOURCE_DIR/$LIB_NAME" ]; then
else
echo "Library not found: $SOURCE_DIR/$LIB_NAME"
exit 1
fi
fi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ctypes
import platform
from pathlib import Path
import os


def load_library(lib_name: str = "libc_wrapper") -> ctypes.CDLL:
Expand All @@ -16,14 +17,28 @@ def load_library(lib_name: str = "libc_wrapper") -> ctypes.CDLL:
current_dir = Path(__file__).parent
system_name = platform.system()

# os.environ["ORT_LIB_LOCATION"] = str(current_dir.joinpath("onnxruntime.dll"))

if system_name == "Windows":
lib_path = current_dir.joinpath(f"{lib_name}.dll")
onnx_path = current_dir.joinpath("onnxruntime").joinpath("lib").joinpath("onnxruntime.dll")
elif system_name == "Darwin": # macOS
lib_path = current_dir.joinpath(f"{lib_name}.dylib")
onnx_path = current_dir.joinpath("onnxruntime").joinpath("lib").joinpath("onnxruntime.dylib")
elif system_name == "Linux":
lib_path = current_dir.joinpath(f"{lib_name}.so")
onnx_path = current_dir.joinpath("onnxruntime").joinpath("lib").joinpath("onnxruntime.so.1")
else:
raise OSError(f"Unsupported operating system: {system_name}")


# onnx_lib_path = current_dir.joinpath("onnxruntime").joinpath("lib")
# current_ld_library_path = os.environ.get("LD_LIBRARY_PATH", "")
# # Update LD_LIBRARY_PATH
# os.environ["LD_LIBRARY_PATH"] = f"{onnx_lib_path}:{current_ld_library_path}"
# os.environ["ORT_LIB_LOCATION"] = str(onnx_lib_path)

ctypes.CDLL(str(onnx_path), mode=ctypes.RTLD_GLOBAL)

if not lib_path.exists():
raise FileNotFoundError(f"Shared library not found at: {lib_path}")
Expand Down
1 change: 1 addition & 0 deletions modules/c-wrapper/scripts/build-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ cd "$BUILD_DIR"
docker build --no-cache -t c-wrapper-tests .

docker run c-wrapper-tests
# docker run -it c-wrapper-tests /bin/bash
22 changes: 21 additions & 1 deletion modules/c-wrapper/scripts/prep_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ cd $SCRIPTPATH

cd ..

# download onnxruntime
# Detect operating system
OS=$(uname -s | tr '[:upper:]' '[:lower:]')

# Detect architecture
ARCH=$(uname -m)

# Download the correct onnxruntime
if [ "$ARCH" == "x86_64" ] && [ "$OS" == "linux" ]; then
wget https://github.com/microsoft/onnxruntime/releases/download/v1.20.0/onnxruntime-linux-x64-1.20.0.tgz
tar -xvf onnxruntime-linux-x64-1.20.0.tgz
mv onnxruntime-linux-x64-1.20.0 tests/test_utils/onnxruntime
else
echo "Unsupported operating system and arch: $OS $ARCH"
exit 1
fi

export ORT_LIB_LOCATION=$(pwd)/tests/test_utils/onnxruntime/lib
export LD_LIBRARY_PATH=$ORT_LIB_LOCATION:$LD_LIBRARY_PATH

cargo build

# Get the operating system
Expand Down Expand Up @@ -42,4 +62,4 @@ if [ -f "$SOURCE_DIR/$LIB_NAME" ]; then
else
echo "Library not found: $SOURCE_DIR/$LIB_NAME"
exit 1
fi
fi
15 changes: 15 additions & 0 deletions modules/c-wrapper/tests/test_utils/c_lib_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ctypes
import platform
from pathlib import Path
import os


def load_library(lib_name: str = "libc_wrapper") -> ctypes.CDLL:
Expand All @@ -16,14 +17,28 @@ def load_library(lib_name: str = "libc_wrapper") -> ctypes.CDLL:
current_dir = Path(__file__).parent
system_name = platform.system()

# os.environ["ORT_LIB_LOCATION"] = str(current_dir.joinpath("onnxruntime.dll"))

if system_name == "Windows":
lib_path = current_dir.joinpath(f"{lib_name}.dll")
onnx_path = current_dir.joinpath("onnxruntime").joinpath("lib").joinpath("onnxruntime.dll")
elif system_name == "Darwin": # macOS
lib_path = current_dir.joinpath(f"{lib_name}.dylib")
onnx_path = current_dir.joinpath("onnxruntime").joinpath("lib").joinpath("onnxruntime.dylib")
elif system_name == "Linux":
lib_path = current_dir.joinpath(f"{lib_name}.so")
onnx_path = current_dir.joinpath("onnxruntime").joinpath("lib").joinpath("onnxruntime.so.1")
else:
raise OSError(f"Unsupported operating system: {system_name}")


# onnx_lib_path = current_dir.joinpath("onnxruntime").joinpath("lib")
# current_ld_library_path = os.environ.get("LD_LIBRARY_PATH", "")
# # Update LD_LIBRARY_PATH
# os.environ["LD_LIBRARY_PATH"] = f"{onnx_lib_path}:{current_ld_library_path}"
# os.environ["ORT_LIB_LOCATION"] = str(onnx_lib_path)

ctypes.CDLL(str(onnx_path), mode=ctypes.RTLD_GLOBAL)

if not lib_path.exists():
raise FileNotFoundError(f"Shared library not found at: {lib_path}")
Expand Down
2 changes: 1 addition & 1 deletion modules/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ onnx-tests = []
torch-tests = []
tensorflow-tests = []
gpu = []
dynamic = ["ort/load-dynamic"]

[dependencies]
regex = "1.9.3"
# ort = { version = "1.16.2", features = ["load-dynamic"], default-features = false }
ort = { version = "2.0.0-rc.9", features = [ "cuda", "ndarray" ]}
ndarray = "0.16.1"
once_cell = "1.18.0"
Expand Down
31 changes: 30 additions & 1 deletion modules/core/src/execution/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ use ort::session::Session;
use crate::errors::error::{SurrealError, SurrealErrorStatus};
use crate::safe_eject;

#[cfg(feature = "dynamic")]
use once_cell::sync::Lazy;
#[cfg(feature = "dynamic")]
use ort::environment::{EnvironmentBuilder, Environment};
#[cfg(feature = "dynamic")]
use std::sync::{Arc, Mutex};

use std::sync::LazyLock;


/// Creates a session for a model.
///
Expand All @@ -26,4 +35,24 @@ pub fn get_session(model_bytes: Vec<u8>) -> Result<Session, SurrealError> {
let session: Session = safe_eject!(builder
.commit_from_memory(&model_bytes), SurrealErrorStatus::Unknown);
Ok(session)
}
}


#[cfg(feature = "dynamic")]
pub static ORT_ENV: LazyLock<Arc<Mutex<Option<Arc<Environment>>>>> = LazyLock::new(|| Arc::new(Mutex::new(None)));


#[cfg(feature = "dynamic")]
pub fn set_environment(dylib_path: String) -> Result<(), SurrealError> {

let outcome: EnvironmentBuilder = ort::init_from(dylib_path);
match outcome.commit() {
Ok(env) => {
ORT_ENV.lock().unwrap().replace(env);
},
Err(e) => {
return Err(SurrealError::new(e.to_string(), SurrealErrorStatus::Unknown));
}
}
Ok(())
}

0 comments on commit f4145ae

Please sign in to comment.