surrealml_core/
lib.rs

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