axonml-serialize 0.6.2

Model serialization for Axonml ML framework
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
//! State Dictionary - Model parameter storage
//!
//! # File
//! `crates/axonml-serialize/src/state_dict.rs`
//!
//! # Author
//! Andrew Jewell Sr. — AutomataNexus LLC
//! ORCID: 0009-0005-2158-7060
//!
//! # Updated
//! April 14, 2026 11:15 PM EST
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

use axonml_core::Result;
use axonml_nn::Module;
use axonml_tensor::Tensor;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// =============================================================================
// TensorData
// =============================================================================

/// Serializable tensor data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TensorData {
    /// Shape of the tensor.
    pub shape: Vec<usize>,
    /// Flattened f32 values.
    pub values: Vec<f32>,
}

impl TensorData {
    /// Create `TensorData` from a Tensor.
    #[must_use]
    pub fn from_tensor(tensor: &Tensor<f32>) -> Self {
        Self {
            shape: tensor.shape().to_vec(),
            values: tensor.to_vec(),
        }
    }

    /// Convert `TensorData` back to a Tensor.
    pub fn to_tensor(&self) -> Result<Tensor<f32>> {
        Tensor::from_vec(self.values.clone(), &self.shape)
    }

    /// Get the number of elements.
    #[must_use]
    pub fn numel(&self) -> usize {
        self.values.len()
    }

    /// Get the shape.
    #[must_use]
    pub fn shape(&self) -> &[usize] {
        &self.shape
    }
}

// =============================================================================
// StateDictEntry
// =============================================================================

/// An entry in the state dictionary.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateDictEntry {
    /// The tensor data.
    pub data: TensorData,
    /// Whether this parameter requires gradients.
    pub requires_grad: bool,
    /// Optional metadata.
    #[serde(default)]
    pub metadata: HashMap<String, String>,
}

impl StateDictEntry {
    /// Create a new entry from tensor data.
    #[must_use]
    pub fn new(data: TensorData, requires_grad: bool) -> Self {
        Self {
            data,
            requires_grad,
            metadata: HashMap::new(),
        }
    }

    /// Add metadata to the entry.
    #[must_use]
    pub fn with_metadata(mut self, key: &str, value: &str) -> Self {
        self.metadata.insert(key.to_string(), value.to_string());
        self
    }
}

// =============================================================================
// StateDict
// =============================================================================

/// State dictionary for storing model parameters.
///
/// This is similar to `PyTorch`'s `state_dict`, mapping parameter names to tensors.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StateDict {
    entries: HashMap<String, StateDictEntry>,
    #[serde(default)]
    metadata: HashMap<String, String>,
}

impl StateDict {
    /// Create an empty state dictionary.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a state dictionary from a module.
    ///
    /// Uses `named_parameters()` to get fully-qualified parameter names
    /// (e.g., "encoder_lstm1.weight_ih") that are unique across sub-modules.
    /// Falls back to `parameters()` if `named_parameters()` returns empty.
    pub fn from_module<M: Module>(module: &M) -> Self {
        let mut state_dict = Self::new();

        let named = module.named_parameters();
        if named.is_empty() {
            // Fallback for modules that don't implement named_parameters.
            // Always use indexed keys (param_0, param_1, ...) to avoid HashMap
            // collisions when multiple sub-modules share parameter names like
            // "weight" and "bias".
            for (i, param) in module.parameters().iter().enumerate() {
                let name = format!("param_{i}");
                let tensor_data = TensorData::from_tensor(&param.data());
                let entry = StateDictEntry::new(tensor_data, param.requires_grad());
                state_dict.entries.insert(name, entry);
            }
        } else {
            for (name, param) in named {
                let tensor_data = TensorData::from_tensor(&param.data());
                let entry = StateDictEntry::new(tensor_data, param.requires_grad());
                state_dict.entries.insert(name, entry);
            }
        }

        state_dict
    }

    /// Insert a tensor into the state dictionary.
    pub fn insert(&mut self, name: String, data: TensorData) {
        let entry = StateDictEntry::new(data, true);
        self.entries.insert(name, entry);
    }

    /// Insert an entry into the state dictionary.
    pub fn insert_entry(&mut self, name: String, entry: StateDictEntry) {
        self.entries.insert(name, entry);
    }

    /// Get an entry by name.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&StateDictEntry> {
        self.entries.get(name)
    }

    /// Get a mutable entry by name.
    pub fn get_mut(&mut self, name: &str) -> Option<&mut StateDictEntry> {
        self.entries.get_mut(name)
    }

    /// Check if the state dictionary contains a key.
    #[must_use]
    pub fn contains(&self, name: &str) -> bool {
        self.entries.contains_key(name)
    }

    /// Get the number of entries.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if the state dictionary is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get all keys.
    pub fn keys(&self) -> impl Iterator<Item = &String> {
        self.entries.keys()
    }

    /// Get all entries.
    pub fn entries(&self) -> impl Iterator<Item = (&String, &StateDictEntry)> {
        self.entries.iter()
    }

    /// Remove an entry.
    pub fn remove(&mut self, name: &str) -> Option<StateDictEntry> {
        self.entries.remove(name)
    }

    /// Merge another state dictionary into this one.
    pub fn merge(&mut self, other: StateDict) {
        for (name, entry) in other.entries {
            self.entries.insert(name, entry);
        }
    }

    /// Get a subset of entries matching a prefix.
    #[must_use]
    pub fn filter_prefix(&self, prefix: &str) -> StateDict {
        let mut filtered = StateDict::new();
        for (name, entry) in &self.entries {
            if name.starts_with(prefix) {
                filtered.entries.insert(name.clone(), entry.clone());
            }
        }
        filtered
    }

    /// Strip a prefix from all keys.
    #[must_use]
    pub fn strip_prefix(&self, prefix: &str) -> StateDict {
        let mut stripped = StateDict::new();
        for (name, entry) in &self.entries {
            let new_name = name.strip_prefix(prefix).unwrap_or(name).to_string();
            stripped.entries.insert(new_name, entry.clone());
        }
        stripped
    }

    /// Add a prefix to all keys.
    #[must_use]
    pub fn add_prefix(&self, prefix: &str) -> StateDict {
        let mut prefixed = StateDict::new();
        for (name, entry) in &self.entries {
            let new_name = format!("{prefix}{name}");
            prefixed.entries.insert(new_name, entry.clone());
        }
        prefixed
    }

    /// Set metadata on the state dictionary.
    pub fn set_metadata(&mut self, key: &str, value: &str) {
        self.metadata.insert(key.to_string(), value.to_string());
    }

    /// Get metadata from the state dictionary.
    #[must_use]
    pub fn get_metadata(&self, key: &str) -> Option<&String> {
        self.metadata.get(key)
    }

    /// Get total number of parameters (elements across all tensors).
    #[must_use]
    pub fn total_params(&self) -> usize {
        self.entries.values().map(|e| e.data.numel()).sum()
    }

    /// Get total size in bytes.
    #[must_use]
    pub fn size_bytes(&self) -> usize {
        self.total_params() * std::mem::size_of::<f32>()
    }

    /// Print a summary of the state dictionary.
    #[must_use]
    pub fn summary(&self) -> String {
        let mut lines = Vec::new();
        lines.push(format!("StateDict with {} entries:", self.len()));
        lines.push(format!("  Total parameters: {}", self.total_params()));
        lines.push(format!("  Size: {} bytes", self.size_bytes()));
        lines.push("  Entries:".to_string());

        for (name, entry) in &self.entries {
            lines.push(format!(
                "    {} - shape: {:?}, numel: {}",
                name,
                entry.data.shape,
                entry.data.numel()
            ));
        }

        lines.join("\n")
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tensor_data_roundtrip() {
        let original = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3]).unwrap();
        let data = TensorData::from_tensor(&original);
        let restored = data.to_tensor().unwrap();

        assert_eq!(original.shape(), restored.shape());
        assert_eq!(original.to_vec(), restored.to_vec());
    }

    #[test]
    fn test_state_dict_operations() {
        let mut state_dict = StateDict::new();

        let data1 = TensorData {
            shape: vec![10, 5],
            values: vec![0.0; 50],
        };
        let data2 = TensorData {
            shape: vec![5],
            values: vec![0.0; 5],
        };

        state_dict.insert("linear.weight".to_string(), data1);
        state_dict.insert("linear.bias".to_string(), data2);

        assert_eq!(state_dict.len(), 2);
        assert_eq!(state_dict.total_params(), 55);
        assert!(state_dict.contains("linear.weight"));
        assert!(state_dict.contains("linear.bias"));
    }

    #[test]
    fn test_state_dict_filter_prefix() {
        let mut state_dict = StateDict::new();

        state_dict.insert(
            "encoder.layer1.weight".to_string(),
            TensorData {
                shape: vec![10],
                values: vec![0.0; 10],
            },
        );
        state_dict.insert(
            "encoder.layer1.bias".to_string(),
            TensorData {
                shape: vec![10],
                values: vec![0.0; 10],
            },
        );
        state_dict.insert(
            "decoder.layer1.weight".to_string(),
            TensorData {
                shape: vec![10],
                values: vec![0.0; 10],
            },
        );

        let encoder_dict = state_dict.filter_prefix("encoder.");
        assert_eq!(encoder_dict.len(), 2);
        assert!(encoder_dict.contains("encoder.layer1.weight"));
    }

    #[test]
    fn test_state_dict_strip_prefix() {
        let mut state_dict = StateDict::new();

        state_dict.insert(
            "model.linear.weight".to_string(),
            TensorData {
                shape: vec![10],
                values: vec![0.0; 10],
            },
        );

        let stripped = state_dict.strip_prefix("model.");
        assert!(stripped.contains("linear.weight"));
    }

    #[test]
    fn test_state_dict_merge() {
        let mut dict1 = StateDict::new();
        dict1.insert(
            "a".to_string(),
            TensorData {
                shape: vec![1],
                values: vec![1.0],
            },
        );

        let mut dict2 = StateDict::new();
        dict2.insert(
            "b".to_string(),
            TensorData {
                shape: vec![1],
                values: vec![2.0],
            },
        );

        dict1.merge(dict2);
        assert_eq!(dict1.len(), 2);
        assert!(dict1.contains("a"));
        assert!(dict1.contains("b"));
    }

    #[test]
    fn test_state_dict_summary() {
        let mut state_dict = StateDict::new();
        state_dict.insert(
            "weight".to_string(),
            TensorData {
                shape: vec![10, 5],
                values: vec![0.0; 50],
            },
        );

        let summary = state_dict.summary();
        assert!(summary.contains("1 entries"));
        assert!(summary.contains("50"));
    }
}