interplane.Get Omega
Read in your language

aien-sovereign-core

Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core / crates / spark-max-rs / src / lib.rs

79 lines · 2211 bytes Raw Blame History
1use spark_max_cabi::SparkMaxBindings;
2use std::fmt;
3
4#[derive(Debug)]
5pub enum SparkMaxError {
6 LoadError(String),
7 SessionInitFailed(i32),
8 InferenceFailed(String),
9}
10
11impl fmt::Display for SparkMaxError {
12 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13 match self {
14 SparkMaxError::LoadError(e) => write!(f, "Modular MAX load error: {}", e),
15 SparkMaxError::SessionInitFailed(code) => write!(f, "Failed to create Modular MAX session: code {}", code),
16 SparkMaxError::InferenceFailed(e) => write!(f, "Modular MAX inference failed: {}", e),
17 }
18 }
19}
20
21impl std::error::Error for SparkMaxError {}
22
23pub struct SparkMaxSession {
24 session_id: i32,
25 bindings: &'static SparkMaxBindings,
26}
27
28impl SparkMaxSession {
29 pub fn new(device_id: i32) -> Result<Self, SparkMaxError> {
30 let bindings = SparkMaxBindings::global().map_err(SparkMaxError::LoadError)?;
31 let session_id = unsafe { (bindings.session_create)(device_id) };
32 if session_id <= 0 {
33 return Err(SparkMaxError::SessionInitFailed(session_id));
34 }
35 Ok(Self { session_id, bindings })
36 }
37
38 pub fn version(&self) -> i32 {
39 unsafe { (self.bindings.version)() }
40 }
41
42 pub fn session_id(&self) -> i32 {
43 self.session_id
44 }
45
46 pub fn compute_scalar(&self, input_val: f32) -> f32 {
47 unsafe { (self.bindings.compute_scalar)(self.session_id, input_val) }
48 }
49}
50
51impl Drop for SparkMaxSession {
52 fn drop(&mut self) {
53 unsafe {
54 let _ = (self.bindings.session_destroy)(self.session_id);
55 }
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_spark_max_safe_session() {
65 let session = match SparkMaxSession::new(0) {
66 Ok(s) => s,
67 Err(e) => {
68 eprintln!("Notice: SparkMaxSession::new failed ({}), skipping test", e);
69 return;
70 }
71 };
72 assert_eq!(session.version(), 1);
73 assert!(session.session_id() > 1000);
74
75 let out = session.compute_scalar(4.0);
76 let expected = 4.0 * 2.5 + session.session_id() as f32;
77 assert_eq!(out, expected);
78 }
79}