pub mod types;
#[cfg(feature = "onnx")]
pub mod convert;
pub mod lowering;
#[cfg(feature = "onnx")]
pub mod proto;
pub use lowering::{lower_graph, StandardOpMapping};
pub use types::*;
use crate::{Result, TensorError};
use std::path::Path;
pub struct OnnxImporter {
config: OnnxConfig,
}
pub struct OnnxExporter {
config: OnnxConfig,
}
impl OnnxImporter {
pub fn new() -> Self {
Self {
config: OnnxConfig::default(),
}
}
pub fn with_config(config: OnnxConfig) -> Self {
Self { config }
}
#[cfg(feature = "onnx")]
pub fn import_from_file<P: AsRef<Path>>(&self, path: P) -> Result<OnnxModel> {
let bytes = std::fs::read(path.as_ref()).map_err(|e| {
TensorError::serialization_error_simple(format!(
"Failed to read ONNX file {:?}: {e}",
path.as_ref()
))
})?;
self.import_from_bytes(&bytes)
}
#[cfg(not(feature = "onnx"))]
pub fn import_from_file<P: AsRef<Path>>(&self, path: P) -> Result<OnnxModel> {
let _ = self.config.opset_version;
Err(TensorError::not_implemented_simple(format!(
"ONNX protobuf import requires the 'onnx' feature; cannot load model from {:?}. \
Rebuild with `--features onnx` to enable.",
path.as_ref()
)))
}
#[cfg(feature = "onnx")]
pub fn import_from_bytes(&self, bytes: &[u8]) -> Result<OnnxModel> {
let _ = self.config.opset_version;
OnnxModel::from_protobuf(bytes)
}
#[cfg(not(feature = "onnx"))]
pub fn import_from_bytes(&self, _bytes: &[u8]) -> Result<OnnxModel> {
let _ = self.config.opset_version;
Err(TensorError::not_implemented_simple(
"ONNX protobuf import requires the 'onnx' feature; rebuild with `--features onnx` \
to enable."
.to_string(),
))
}
}
impl OnnxExporter {
pub fn new() -> Self {
Self {
config: OnnxConfig::default(),
}
}
pub fn with_config(config: OnnxConfig) -> Self {
Self { config }
}
#[cfg(feature = "onnx")]
pub fn export_to_file<P: AsRef<Path>>(&self, model: &OnnxModel, path: P) -> Result<()> {
let bytes = self.export_to_bytes(model)?;
std::fs::write(path.as_ref(), bytes).map_err(|e| {
TensorError::serialization_error_simple(format!(
"Failed to write ONNX file {:?}: {e}",
path.as_ref()
))
})
}
#[cfg(not(feature = "onnx"))]
pub fn export_to_file<P: AsRef<Path>>(&self, model: &OnnxModel, path: P) -> Result<()> {
let _ = (model, &self.config);
Err(TensorError::not_implemented_simple(format!(
"ONNX protobuf export requires the 'onnx' feature; cannot write model to {:?}. \
Rebuild with `--features onnx` to enable.",
path.as_ref()
)))
}
#[cfg(feature = "onnx")]
pub fn export_to_bytes(&self, model: &OnnxModel) -> Result<Vec<u8>> {
use prost::Message;
let mut proto_model = model.to_protobuf()?;
if proto_model.opset_import.is_empty() {
proto_model.opset_import.push(proto::OperatorSetIdProto {
domain: Some(String::new()),
version: Some(self.config.opset_version),
});
}
Ok(proto_model.encode_to_vec())
}
#[cfg(not(feature = "onnx"))]
pub fn export_to_bytes(&self, model: &OnnxModel) -> Result<Vec<u8>> {
let _ = (model, &self.config);
Err(TensorError::not_implemented_simple(
"ONNX protobuf export requires the 'onnx' feature; rebuild with `--features onnx` \
to enable."
.to_string(),
))
}
}
impl Default for OnnxImporter {
fn default() -> Self {
Self::new()
}
}
impl Default for OnnxExporter {
fn default() -> Self {
Self::new()
}
}