torsh-nn 0.1.2

Neural network modules for ToRSh with PyTorch-compatible API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Model serialization and deserialization for ToRSh neural networks
//!
//! Note: This module requires std for file operations and is only available with the "std" feature.

#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use serde_json;
#[cfg(feature = "std")]
use std::{
    collections::HashMap,
    fs::File,
    io::{Read, Write},
    path::Path,
};
#[cfg(feature = "std")]
use torsh_core::error::{Result, TorshError};
#[cfg(feature = "std")]
use torsh_tensor::Tensor;

#[cfg(feature = "safetensors")]
use safetensors::SafeTensors;

/// Represents a serializable model state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelState {
    /// Model parameters (weights and biases)
    pub parameters: HashMap<String, SerializableTensor>,
    /// Model hyperparameters and configuration
    pub config: HashMap<String, SerializableValue>,
    /// Model metadata
    pub metadata: ModelMetadata,
}

/// Metadata for the serialized model
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelMetadata {
    /// Model architecture name
    pub architecture: String,
    /// Model version
    pub version: String,
    /// Timestamp of serialization
    pub created_at: String,
    /// Framework version used
    pub framework_version: String,
    /// Additional tags or labels
    pub tags: Vec<String>,
}

/// Serializable representation of a tensor
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializableTensor {
    /// Tensor shape
    pub shape: Vec<usize>,
    /// Tensor data type
    pub dtype: String,
    /// Flattened tensor data
    pub data: Vec<f32>,
    /// Whether the tensor requires gradients
    pub requires_grad: bool,
}

/// Serializable value for hyperparameters
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SerializableValue {
    Bool(bool),
    Int(i64),
    Float(f64),
    String(String),
    Array(Vec<SerializableValue>),
}

impl From<Tensor<f32>> for SerializableTensor {
    fn from(tensor: Tensor<f32>) -> Self {
        let shape = tensor.shape().dims().to_vec();
        let data = tensor.to_vec().expect("Failed to extract tensor data");
        let requires_grad = tensor.requires_grad();

        Self {
            shape,
            dtype: "f32".to_string(),
            data,
            requires_grad,
        }
    }
}

impl TryFrom<SerializableTensor> for Tensor<f32> {
    type Error = TorshError;

    fn try_from(serializable: SerializableTensor) -> Result<Self> {
        let tensor = Tensor::from_vec(serializable.data, &serializable.shape)?;
        if serializable.requires_grad {
            Ok(tensor.requires_grad_(true))
        } else {
            Ok(tensor)
        }
    }
}

impl ModelState {
    /// Create a new model state
    pub fn new(architecture: String) -> Self {
        let metadata = ModelMetadata {
            architecture,
            version: "1.0.0".to_string(),
            created_at: chrono::Utc::now().to_rfc3339(),
            framework_version: env!("CARGO_PKG_VERSION").to_string(),
            tags: Vec::new(),
        };

        Self {
            parameters: HashMap::new(),
            config: HashMap::new(),
            metadata,
        }
    }

    /// Add a parameter tensor to the model state
    pub fn add_parameter(&mut self, name: String, tensor: Tensor<f32>) {
        self.parameters
            .insert(name, SerializableTensor::from(tensor));
    }

    /// Add a configuration value to the model state
    pub fn add_config<T>(&mut self, key: String, value: T)
    where
        T: Into<SerializableValue>,
    {
        self.config.insert(key, value.into());
    }

    /// Get a parameter tensor by name
    pub fn get_parameter(&self, name: &str) -> Option<Result<Tensor<f32>>> {
        self.parameters.get(name).map(|t| t.clone().try_into())
    }

    /// Get a configuration value by name
    pub fn get_config(&self, key: &str) -> Option<&SerializableValue> {
        self.config.get(key)
    }

    /// Save the model state to a JSON file
    pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let mut file = File::create(path)?;
        let json = serde_json::to_string_pretty(self)?;
        file.write_all(json.as_bytes())?;
        Ok(())
    }

    /// Load the model state from a JSON file
    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let mut file = File::open(path)?;
        let mut contents = String::new();
        file.read_to_string(&mut contents)?;
        let model_state: ModelState = serde_json::from_str(&contents)?;
        Ok(model_state)
    }

    /// Save the model state to binary format (more efficient)
    pub fn save_to_binary<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let mut file = File::create(path)?;
        let data = oxicode::serde::encode_to_vec(self, oxicode::config::standard())
            .map_err(|e| TorshError::SerializationError(e.to_string()))?;
        file.write_all(&data)?;
        Ok(())
    }

    /// Load the model state from binary format
    pub fn load_from_binary<P: AsRef<Path>>(path: P) -> Result<Self> {
        let mut file = File::open(path)?;
        let mut data = Vec::new();
        file.read_to_end(&mut data)?;
        let (model_state, _): (ModelState, usize) =
            oxicode::serde::decode_from_slice(&data, oxicode::config::standard())
                .map_err(|e| TorshError::SerializationError(e.to_string()))?;
        Ok(model_state)
    }

    /// Save parameters to SafeTensors format
    #[cfg(feature = "safetensors")]
    pub fn save_to_safetensors<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        use safetensors::tensor::{Dtype, TensorView};
        use std::collections::BTreeMap;

        let mut tensors = BTreeMap::new();

        for (name, serializable_tensor) in &self.parameters {
            let data_bytes: &[u8] = bytemuck::cast_slice(&serializable_tensor.data);
            let shape: Vec<usize> = serializable_tensor.shape.clone();

            let tensor_view = TensorView::new(Dtype::F32, shape, data_bytes)
                .map_err(|e| TorshError::SerializationError(format!("SafeTensors error: {}", e)))?;

            tensors.insert(name.clone(), tensor_view);
        }

        // Create metadata with config information
        let metadata = serde_json::to_string(&self.metadata)
            .map_err(|e| TorshError::SerializationError(e.to_string()))?;
        let mut metadata_map = HashMap::new();
        metadata_map.insert("torsh_metadata".to_string(), metadata);

        if !self.config.is_empty() {
            let config_json = serde_json::to_string(&self.config)
                .map_err(|e| TorshError::SerializationError(e.to_string()))?;
            metadata_map.insert("torsh_config".to_string(), config_json);
        }

        let safetensors_data =
            safetensors::serialize(&tensors, Some(metadata_map)).map_err(|e| {
                TorshError::SerializationError(format!("SafeTensors serialization error: {}", e))
            })?;

        std::fs::write(path, safetensors_data)?;
        Ok(())
    }

    /// Load parameters from SafeTensors format
    #[cfg(feature = "safetensors")]
    pub fn load_from_safetensors<P: AsRef<Path>>(path: P) -> Result<Self> {
        let data = std::fs::read(path)?;

        // Use SafeTensors::deserialize method
        let safetensors = SafeTensors::deserialize(&data).map_err(|e| {
            TorshError::SerializationError(format!("SafeTensors deserialization error: {}", e))
        })?;

        let mut parameters = HashMap::new();

        for (name, tensor_view) in safetensors.tensors() {
            if tensor_view.dtype() != safetensors::tensor::Dtype::F32 {
                return Err(TorshError::SerializationError(format!(
                    "Unsupported dtype: {:?}. Only F32 is currently supported.",
                    tensor_view.dtype()
                )));
            }

            let shape = tensor_view.shape().to_vec();
            let data: Vec<f32> = bytemuck::cast_slice(tensor_view.data()).to_vec();

            let serializable_tensor = SerializableTensor {
                shape,
                dtype: "f32".to_string(),
                data,
                requires_grad: false, // SafeTensors doesn't store grad info
            };

            parameters.insert(name.to_string(), serializable_tensor);
        }

        // Create default metadata since we can't access it easily from this version
        let metadata = ModelMetadata {
            architecture: "unknown".to_string(),
            version: "1.0.0".to_string(),
            created_at: chrono::Utc::now().to_rfc3339(),
            framework_version: env!("CARGO_PKG_VERSION").to_string(),
            tags: vec!["safetensors".to_string()],
        };

        // No config available in this implementation
        let config = HashMap::new();

        Ok(ModelState {
            parameters,
            config,
            metadata,
        })
    }
}

// Implement conversions for common types to SerializableValue
impl From<bool> for SerializableValue {
    fn from(value: bool) -> Self {
        SerializableValue::Bool(value)
    }
}

impl From<i32> for SerializableValue {
    fn from(value: i32) -> Self {
        SerializableValue::Int(value as i64)
    }
}

impl From<i64> for SerializableValue {
    fn from(value: i64) -> Self {
        SerializableValue::Int(value)
    }
}

impl From<f32> for SerializableValue {
    fn from(value: f32) -> Self {
        SerializableValue::Float(value as f64)
    }
}

impl From<f64> for SerializableValue {
    fn from(value: f64) -> Self {
        SerializableValue::Float(value)
    }
}

impl From<String> for SerializableValue {
    fn from(value: String) -> Self {
        SerializableValue::String(value)
    }
}

impl From<&str> for SerializableValue {
    fn from(value: &str) -> Self {
        SerializableValue::String(value.to_string())
    }
}

/// Trait for models that can be serialized
pub trait Serializable {
    /// Convert the model to a serializable state
    fn to_state(&self) -> ModelState;

    /// Load the model from a serializable state
    fn from_state(state: &ModelState) -> Result<Self>
    where
        Self: Sized;

    /// Save the model to a file
    fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let state = self.to_state();
        state.save_to_file(path)
    }

    /// Load the model from a file
    fn load<P: AsRef<Path>>(path: P) -> Result<Self>
    where
        Self: Sized,
    {
        let state = ModelState::load_from_file(path)?;
        Self::from_state(&state)
    }

    /// Save the model to binary format
    fn save_binary<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let state = self.to_state();
        state.save_to_binary(path)
    }

    /// Load the model from binary format
    fn load_binary<P: AsRef<Path>>(path: P) -> Result<Self>
    where
        Self: Sized,
    {
        let state = ModelState::load_from_binary(path)?;
        Self::from_state(&state)
    }

    /// Save the model to SafeTensors format
    #[cfg(feature = "safetensors")]
    fn save_safetensors<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let state = self.to_state();
        state.save_to_safetensors(path)
    }

    /// Load the model from SafeTensors format
    #[cfg(feature = "safetensors")]
    fn load_safetensors<P: AsRef<Path>>(path: P) -> Result<Self>
    where
        Self: Sized,
    {
        let state = ModelState::load_from_safetensors(path)?;
        Self::from_state(&state)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;
    use torsh_tensor::creation;

    #[test]
    fn test_model_state_creation() -> Result<()> {
        let mut state = ModelState::new("test_model".to_string());

        // Add some parameters
        let tensor = creation::ones(&[2, 3])?;
        state.add_parameter("weight".to_string(), tensor);

        // Add some config
        state.add_config("learning_rate".to_string(), 0.001_f32);
        state.add_config("epochs".to_string(), 100_i32);
        state.add_config("optimizer".to_string(), "adam");

        assert_eq!(state.parameters.len(), 1);
        assert_eq!(state.config.len(), 3);
        assert_eq!(state.metadata.architecture, "test_model");
        Ok(())
    }

    #[test]
    fn test_tensor_serialization() -> Result<()> {
        let original = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2])?;
        let serializable = SerializableTensor::from(original.clone());
        let deserialized: Tensor<f32> = serializable.try_into().unwrap();

        assert_eq!(original.shape().dims(), deserialized.shape().dims());
        assert_eq!(original.to_vec()?, deserialized.to_vec()?);
        Ok(())
    }

    #[test]
    fn test_file_serialization() -> Result<()> {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test_model.json");

        let mut state = ModelState::new("test_model".to_string());
        let tensor = creation::randn(&[3, 4])?;
        state.add_parameter("weight".to_string(), tensor);
        state.add_config("learning_rate".to_string(), 0.01_f32);

        // Save to file
        state.save_to_file(&file_path).unwrap();

        // Load from file
        let loaded_state = ModelState::load_from_file(&file_path).unwrap();

        assert_eq!(
            state.metadata.architecture,
            loaded_state.metadata.architecture
        );
        assert_eq!(state.parameters.len(), loaded_state.parameters.len());
        assert_eq!(state.config.len(), loaded_state.config.len());
        Ok(())
    }

    #[test]
    fn test_binary_serialization() -> Result<()> {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test_model.bin");

        let mut state = ModelState::new("binary_test".to_string());
        let tensor = creation::zeros(&[2, 2])?;
        state.add_parameter("bias".to_string(), tensor);

        // Save to binary
        state.save_to_binary(&file_path).unwrap();

        // Load from binary
        let loaded_state = ModelState::load_from_binary(&file_path).unwrap();

        assert_eq!(
            state.metadata.architecture,
            loaded_state.metadata.architecture
        );
        assert_eq!(state.parameters.len(), loaded_state.parameters.len());
        Ok(())
    }

    #[test]
    #[cfg(feature = "safetensors")]
    fn test_safetensors_serialization() -> Result<()> {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test_model.safetensors");

        let mut state = ModelState::new("safetensors_test".to_string());
        let weight_tensor = creation::randn(&[3, 4])?;
        let bias_tensor = creation::zeros(&[4])?;

        state.add_parameter("weight".to_string(), weight_tensor.clone());
        state.add_parameter("bias".to_string(), bias_tensor.clone());
        state.add_config("learning_rate".to_string(), 0.001_f32);
        state.add_config("batch_size".to_string(), 32_i32);

        // Save to SafeTensors
        state.save_to_safetensors(&file_path).unwrap();

        // Load from SafeTensors
        let loaded_state = ModelState::load_from_safetensors(&file_path).unwrap();

        // Note: SafeTensors implementation doesn't preserve metadata in this version
        assert_eq!(loaded_state.metadata.architecture, "unknown");
        assert_eq!(state.parameters.len(), loaded_state.parameters.len());

        // Check parameter data
        let loaded_weight = loaded_state.get_parameter("weight").unwrap().unwrap();
        let loaded_bias = loaded_state.get_parameter("bias").unwrap().unwrap();

        assert_eq!(weight_tensor.shape().dims(), loaded_weight.shape().dims());
        assert_eq!(bias_tensor.shape().dims(), loaded_bias.shape().dims());

        // Verify data integrity (approximately, since serialization may have small differences)
        let weight_data = weight_tensor.to_vec()?;
        let loaded_weight_data = loaded_weight.to_vec()?;
        for (a, b) in weight_data.iter().zip(loaded_weight_data.iter()) {
            assert!((a - b).abs() < 1e-6);
        }
        Ok(())
    }
}