Repository navigation
Enable trtx by default and load TensorRT from pip package if installed #22
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ crate-type = ["cdylib", "rlib"] | |
| name = "pywebnn" | ||
|
|
||
| [features] | ||
| default = ["onnx-runtime", "dynamic-inputs"] | ||
| default = ["onnx-runtime", "trtx-runtime", "dynamic-inputs"] | ||
| onnx-runtime = ["rustnn/onnx-runtime", "ort"] | ||
| coreml-runtime = ["rustnn/coreml-runtime"] | ||
| trtx-runtime = ["rustnn/trtx-runtime"] | ||
|
|
@@ -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" } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"] } | ||
|
|
||
|
|
||
| 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}; | ||
|
|
@@ -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" { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
| } | ||
|
|
@@ -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)?; | ||
|
|
||
There was a problem hiding this comment.
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
trtxrequireslibclangfor building.