Skip to content
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
4 changes: 2 additions & 2 deletions trtexec-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ authors.workspace = true
repository.workspace = true

[dependencies]
rustnn = { git = "https://github.com/rustnn/rustnn/", features = [
rustnn = { git = "https://github.com/rustnn/rustnn/", branch = "update-trtx-1.6", features = [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this intended or just required during development?

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.

no, it needs to be done because of API break and also when we bump a release

"trtx-runtime",
], default-features = false, branch = "main", optional = true }
], default-features = false, optional = true }

nvidia-nvtx = { version = "0.2", git = "https://github.com/NVIDIA/NVTX", branch = "release-v3" }
log = "0.4"
Expand Down
7 changes: 7 additions & 0 deletions trtx-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ fn prepare_transformed_headers(header_dir: &Path, out_dir: &Path) -> PathBuf {
"void log(Severity severity, AsciiChar const* msg)",
"void log(int32_t severity, char const* msg)",
)
// RuntimeCache is internally synchronized via shared_mutex

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.

a bit risky, but currently this just affects RuntimeCache. It avoids use transmuting in Rust code.

The semantics of the const would actually be given for the C++ object. RuntimeCache can be used safely with a shared reference.

.replace(
"bool deserialize(void const* blob, size_t size) noexcept",
"bool deserialize(void const* blob, size_t size) const noexcept",
)
// RuntimeCache is internally synchronized via shared_mutex
.replace("bool reset() noexcept", "bool reset() const noexcept")
.replace("//!", "///")
.replace(r"\returns", " - Returns ");

Expand Down
23 changes: 9 additions & 14 deletions trtx/src/runtime_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,45 @@
//!
//! [`RuntimeCache`] wraps [`trtx_sys::nvinfer1::IRuntimeCache`] (C++ [`nvinfer1::IRuntimeCache`](https://docs.nvidia.com/deeplearning/tensorrt-rtx/latest/_static/cpp-api/classnvinfer1_1_1_i_runtime_cache.html).

use std::marker::PhantomData;

use crate::error::{PropertySetAttempt, Result};
use crate::host_memory::HostMemory;
use crate::Error;
use cxx::UniquePtr;
use trtx_sys::nvinfer1::{self, IRuntimeCache};

/// [`trtx_sys::nvinfer1::IRuntimeCache`] — C++ [`nvinfer1::IRuntimeCache`](https://docs.nvidia.com/deeplearning/tensorrt-rtx/latest/_static/cpp-api/classnvinfer1_1_1_i_runtime_cache.html).
pub struct RuntimeCache<'engine> {
pub struct RuntimeCache {
pub(crate) inner: UniquePtr<IRuntimeCache>,
_engine: PhantomData<&'engine nvinfer1::ICudaEngine>,
}

/// # Safety
///
/// IRuntimeCache is internally protected by a shared mutex and
/// UniquePtr holds after initialization a valid IRuntimeCache (or nullptr in mock mode)
unsafe impl Send for RuntimeCache<'_> {}
unsafe impl Sync for RuntimeCache<'_> {}
unsafe impl Send for RuntimeCache {}
unsafe impl Sync for RuntimeCache {}

impl std::fmt::Debug for RuntimeCache<'_> {
impl std::fmt::Debug for RuntimeCache {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RuntimeCache")
.field("inner", &format!("{:x}", self.inner.as_ptr() as usize))
.finish_non_exhaustive()
}
}

impl<'engine> RuntimeCache<'engine> {
impl RuntimeCache {
pub(crate) fn new(cache: *mut nvinfer1::IRuntimeCache) -> Result<Self> {
#[cfg(not(feature = "mock"))]
if cache.is_null() {
return Err(Error::RuntimeCacheCreationFailed);
}
Ok(Self {
inner: unsafe { UniquePtr::from_raw(cache) },
_engine: Default::default(),
})
}

/// See [IRuntimeCache::serialize].
pub fn serialize(&self) -> Result<HostMemory<'engine>> {
pub fn serialize(&self) -> Result<HostMemory<'_>> {
#[cfg(not(feature = "mock"))]
{
let host_mem = unsafe { self.inner.serialize().as_mut() }
Expand All @@ -56,11 +52,10 @@ impl<'engine> RuntimeCache<'engine> {
}

/// See [IRuntimeCache::deserialize].
pub fn deserialize(&mut self, blob: &[u8]) -> Result<()> {
pub fn deserialize(&self, blob: &[u8]) -> Result<()> {
if cfg!(not(feature = "mock")) {
if unsafe {
self.inner
.pin_mut()
.deserialize(blob.as_ptr() as *const autocxx::c_void, blob.len())
} {
Ok(())
Expand All @@ -75,9 +70,9 @@ impl<'engine> RuntimeCache<'engine> {
}

/// See [IRuntimeCache::reset].
pub fn reset(&mut self) -> Result<()> {
pub fn reset(&self) -> Result<()> {
if cfg!(not(feature = "mock")) {
if self.inner.pin_mut().reset() {
if self.inner.reset() {
Ok(())
} else {
Err(Error::FailedToResetRuntimeCache)
Expand Down
11 changes: 4 additions & 7 deletions trtx/src/runtime_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::marker::PhantomData;
#[cfg(not(feature = "enterprise"))]
use std::sync::{Arc, Mutex};
use std::sync::Arc;

#[cfg(not(feature = "enterprise"))]
use crate::error::PropertySetAttempt;
Expand All @@ -27,8 +27,7 @@ pub struct RuntimeConfig<'engine> {
// this also makes it safe when we modify through our mutex, while cpp calls are made through
// IExecution calls
#[cfg(not(feature = "enterprise"))]
_cache: Option<Arc<Mutex<RuntimeCache<'engine>>>>, // Mutex, could now be removed with a
// breaking change to set_runtime_cache
_cache: Option<Arc<RuntimeCache>>,
}

impl std::fmt::Debug for RuntimeConfig<'_> {
Expand Down Expand Up @@ -83,7 +82,7 @@ impl<'engine> RuntimeConfig<'engine> {

#[cfg(not(feature = "enterprise"))]
/// See [IRuntimeConfig::createRuntimeCache].
pub fn create_runtime_cache(&self) -> Result<RuntimeCache<'engine>> {
pub fn create_runtime_cache(&self) -> Result<RuntimeCache> {
#[cfg(not(feature = "mock"))]
let cache_ptr = self.inner.createRuntimeCache();
#[cfg(feature = "mock")]
Expand All @@ -93,12 +92,10 @@ impl<'engine> RuntimeConfig<'engine> {

#[cfg(not(feature = "enterprise"))]
/// See [IRuntimeConfig::setRuntimeCache].
pub fn set_runtime_cache(&mut self, cache: Arc<Mutex<RuntimeCache<'engine>>>) -> Result<()> {
pub fn set_runtime_cache(&mut self, cache: Arc<RuntimeCache>) -> Result<()> {
if cfg!(not(feature = "mock")) {
if self.inner.pin_mut().setRuntimeCache(
cache
.lock()
.unwrap()
.inner
.as_ref()
.expect("RuntimeCache inner must be non-null"),
Expand Down
Loading