Skip to main content

surrealml_core/
lib.rs

1//! An embedded ONNX runtime directly in the Rust binary when compiling result in no need for
2//! installing ONNX runtime separately or worrying about version clashes with other runtimes.
3//!
4//! This crate is just the Rust implementation of the Surml API. It is advised that you just use
5//! this crate directly if you are running a Rust server. It must be noted that the version of ONNX
6//! needs to be the same as the client when using this crate. For this current version of Surml, the
7//! ONNX version is `1.16.0`.
8//!
9//! ## Compilation config
10//! If nothing is configured the crate will compiled the ONNX runtime into the binary. This is the
11//! default behaviour. However, if you want to use an ONNX runtime that is installed on your system,
12//! you can set the environment variable `ONNXRUNTIME_LIB_PATH` before you compile the crate. This
13//! will make the crate use the ONNX runtime that is installed on your system.
14//!
15//! ## Usage
16//! Surml can be used to store, load, and execute ONNX models.
17//!
18//! ### Storing and accessing models
19//! We can store models and meta data around the models with the following code:
20//! ```rust
21//! use std::fs::File;
22//! use std::io::{self, Read, Write};
23//!
24//! use surrealml_core::storage::surml_file::SurMlFile;
25//! use surrealml_core::storage::header::Header;
26//! use surrealml_core::storage::header::normalisers::{
27//!     wrapper::NormaliserType,
28//!     linear_scaling::LinearScaling
29//! };
30//!
31//!
32//! // load your own model here (surrealml python package can be used to convert PyTorch,
33//! // and Sklearn models to ONNX or package them as surml files)
34//! let mut file = File::open("./stash/linear_test.onnx").unwrap();
35//! let mut model_bytes = Vec::new();
36//! file.read_to_end(&mut model_bytes).unwrap();
37//!
38//! // create a header for the model
39//! let mut header = Header::fresh();
40//! header.add_column(String::from("squarefoot"));
41//! header.add_column(String::from("num_floors"));
42//! header.add_output(String::from("house_price"), None);
43//!
44//! // add normalisers if needed
45//! header.add_normaliser(
46//!     "squarefoot".to_string(),
47//!     NormaliserType::LinearScaling(LinearScaling { min: 0.0, max: 1.0 })
48//! );
49//! header.add_normaliser(
50//!     "num_floors".to_string(),
51//!     NormaliserType::LinearScaling(LinearScaling { min: 0.0, max: 1.0 })
52//! );
53//!
54//! // create a surml file
55//! let surml_file = SurMlFile::new(header, model_bytes);
56//!
57//! // read and write surml files
58//! surml_file.write("./stash/test.surml").unwrap();
59//! let new_file = SurMlFile::from_file("./stash/test.surml").unwrap();
60//! let file_from_bytes = SurMlFile::from_bytes(surml_file.to_bytes()).unwrap();
61//! ```
62//!
63//! ### Executing models
64//! We you load a `surml` file, you can execute the model with the following code:
65//! ```no_run
66//! use surrealml_core::storage::surml_file::SurMlFile;
67//! use surrealml_core::execution::compute::ModelComputation;
68//! use ndarray::ArrayD;
69//! use std::collections::HashMap;
70//!
71//!
72//! let mut file = SurMlFile::from_file("./stash/test.surml").unwrap();
73//!
74//! let compute_unit = ModelComputation {
75//!     surml_file: &mut file,
76//! };
77//!
78//! // automatically map inputs and apply normalisers to the compute if this data was put in the header
79//! let mut input_values = HashMap::new();
80//! input_values.insert(String::from("squarefoot"), 1000.0);
81//! input_values.insert(String::from("num_floors"), 2.0);
82//!
83//! let output = compute_unit.buffered_compute(&mut input_values).unwrap();
84//!
85//! // feed a raw ndarray into the model if no header was provided or if you want to bypass the header
86//! let x = vec![1000.0, 2.0];
87//! let data: ArrayD<f32> = ndarray::arr1(&x).into_dyn();
88//!
89//! // None input can be a tuple of dimensions of the input data
90//! let output = compute_unit.raw_compute(data, None).unwrap();
91//! ```
92pub mod errors;
93pub mod execution;
94pub mod storage;
95pub use ndarray;
96
97/// Returns the version of the ONNX runtime that is used.
98pub fn onnx_runtime() -> &'static str {
99	"1.20.0"
100}