Skip to content
Draft
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
23 changes: 4 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["cdylib", "rlib"]
name = "pywebnn"

[features]
default = ["onnx-runtime", "dynamic-inputs"]
default = ["onnx-runtime", "trtx-runtime", "dynamic-inputs"]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Counter argument against enabling by default is that trtx requires libclang for building.

onnx-runtime = ["rustnn/onnx-runtime", "ort"]
coreml-runtime = ["rustnn/coreml-runtime"]
trtx-runtime = ["rustnn/trtx-runtime"]
Expand All @@ -26,12 +26,13 @@ dynamic-inputs = ["rustnn/dynamic-inputs"]
[dependencies]
pyo3 = { version = "0.28", features = ["extension-module"] }
bytemuck = "1.14"
rustnn = { git = "https://github.com/rustnn/rustnn", branch = "main" }
rustnn = { git = "https://github.com/theHamsta/rustnn", branch = "bring-dynamically_load_tensorrt-back" }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Depends on rustnn/rustnn#280 (therefore git dep until merged)

serde_json = "1.0"
webnn-graph = { git = "https://github.com/rustnn/webnn-graph", branch = "main" }
half = "2.4"
regex = "1.11"
pretty_env_logger = "0.5"
log = "0.4"
# Optional runtime dependencies
ort = { version = "=2.0.0-rc.13", optional = true, features = ["half", "load-dynamic"] }

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ dependencies = [
]

[project.optional-dependencies]
trtx = [
"tensorrt_rtx_cu13_libs==1.6.1.120",
]
test = [
"pytest>=7.0",
"pytest-asyncio",
Expand All @@ -46,4 +49,4 @@ Issues = "https://github.com/rustnn/pywebnn/issues"
module-name = "webnn._rustnn"
python-source = "python"
# CoreML is opt-in because it requires Apple's Objective-C runtime.
features = ["pyo3/extension-module", "onnx-runtime", "dynamic-inputs"]
features = ["pyo3/extension-module", "onnx-runtime", "trtx-runtime", "dynamic-inputs"]
35 changes: 35 additions & 0 deletions src/python/context_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Internal rustnn `MLContext` state shared by pywebnn bindings.

#[cfg(feature = "trtx-runtime")]
use log::warn;
use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyDict, PyTuple};
Expand Down Expand Up @@ -39,6 +41,35 @@ fn init_rust_logging_if_requested() {
}
}

/// Prefer the compatible optional Python runtime package over library discovery.
#[cfg(feature = "trtx-runtime")]
fn try_setting_tensorrt_libs(py: Python<'_>) -> PyResult<()> {
let metadata = py.import("importlib.metadata")?;
let version = match metadata.call_method1("version", ("tensorrt_rtx_cu13_libs",)) {
Ok(version) => version.extract::<String>()?,
Err(err) if err.matches(py, &metadata.getattr("PackageNotFoundError")?)? => return Ok(()),
Err(err) => return Err(err),
};
if version != "1.6.1.120" {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we can get this versions automatically if we reexport the version methods from trtx_sys in trtx and rustnn rustnn/trtx-rs#146

return Ok(());
}

let libs = py.import("tensorrt_rtx_libs")?;
let mut library_path: std::path::PathBuf = libs.getattr("__file__")?.extract()?;
let filename = if cfg!(target_os = "windows") {
"tensorrt_rtx_1_6.dll"
} else {
"libtensorrt_rtx.so.1"
};
library_path.set_file_name(filename);
rustnn::backends::trtx::dynamically_load_tensorrt(Some(&library_path)).map_err(|err| {
PyRuntimeError::new_err(format!(
"Failed to load the packaged TensorRT RTX runtime at {}: {err}",
library_path.display()
))
})
}

pub(crate) fn map_rustnn_error(err: rustnn::error::Error) -> PyErr {
PyRuntimeError::new_err(err.to_string())
}
Expand Down Expand Up @@ -211,6 +242,10 @@ pub(crate) fn ml_tensor_descriptor(
impl ContextState {
pub fn new(options: MLContextOptions) -> PyResult<Self> {
init_rust_logging_if_requested();
#[cfg(feature = "trtx-runtime")]
let _ = Python::attach(try_setting_tensorrt_libs)
.map_err(|err| warn!("Failed to load TensorRT library from pip package: {err:?}"));

// SAFETY: `MLContext` and backend builders do not expose references tied to caller
// stack frames; storing in this struct is the intended ownership model for pywebnn.
let ml_context = MLContext::create(&options).map_err(map_rustnn_error)?;
Expand Down
Loading