#![cfg(feature = "tensorrt-onnx")]
#![allow(dead_code)]
use std::sync::Arc;
use tokio::sync::oneshot;
use crate::builder::IBuilderConfig;
use crate::engine::EnginePlan;
use crate::error::TrtError;
use crate::sys;
pub struct OnnxParser {
raw: *mut sys::IOnnxParser,
}
unsafe impl Send for OnnxParser {}
unsafe impl Sync for OnnxParser {}
impl OnnxParser {
pub unsafe fn from_raw(raw: *mut sys::IOnnxParser) -> Result<Self, TrtError> {
if raw.is_null() {
Err(TrtError::Onnx("null parser".into()))
} else {
Ok(Self { raw })
}
}
pub(crate) fn for_test() -> Self {
Self {
raw: std::ptr::null_mut(),
}
}
pub fn raw(&self) -> *mut sys::IOnnxParser {
self.raw
}
}
impl Drop for OnnxParser {
fn drop(&mut self) {
#[cfg(feature = "tensorrt-link")]
unsafe {
if !self.raw.is_null() {
sys::atomr_trt_onnx_parser_destroy(self.raw);
}
}
}
}
pub type ParseReply = oneshot::Sender<Result<EnginePlan, TrtError>>;
pub enum OnnxMsg {
Parse {
bytes: Arc<Vec<u8>>,
config: Box<IBuilderConfig>,
reply: ParseReply,
},
}
#[cfg(test)]
mod tests {
use super::*;
use crate::builder::Precision;
#[test]
fn parser_msg_constructs() {
let (tx, _rx) = oneshot::channel();
let _msg = OnnxMsg::Parse {
bytes: Arc::new(b"\x08\x07onnx-bytes-here".to_vec()),
config: Box::new(IBuilderConfig::new().with_precision(Precision::Fp16)),
reply: tx,
};
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<OnnxParser>();
let p = OnnxParser::for_test();
assert!(p.raw().is_null());
}
}