mlmf 0.2.0

Machine Learning Model Files - Loading, saving, and dynamic mapping for ML models
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
//! Progress reporting utilities for model loading operations
//!
//! This module provides progress callback functionality for long-running model loading
//! operations, allowing users to track the status of loading, validation, and processing.

/// Progress callback function type
pub type ProgressFn = Box<dyn Fn(ProgressEvent) + Send + Sync>;

/// Events reported during model loading operations
#[derive(Debug, Clone)]
pub enum ProgressEvent {
    /// Starting to load configuration file
    LoadingConfig {
        /// Path to the config file
        path: String,
    },

    /// Scanning for model files in directory
    ScanningFiles {
        /// Number of files found so far
        count: usize,
    },

    /// Detecting model architecture from tensor names
    DetectingArchitecture,

    /// Loading tensor data from files
    LoadingTensors {
        /// Current file being processed
        current: usize,
        /// Total number of files
        total: usize,
        /// Name of current file
        file_name: Option<String>,
    },

    /// Mapping tensor names between formats
    MappingNames {
        /// Number of tensor names mapped
        count: usize,
    },

    /// Building the model from loaded tensors
    BuildingModel,

    /// Validating model configuration and tensors
    ValidatingModel,

    /// Loading a specific file
    LoadingFile {
        /// Path to the file being loaded
        file: std::path::PathBuf,
        /// Format of the file being loaded  
        format: String,
    },

    /// Loading tensors from file(s)
    LoadingTensorsFromFiles {
        /// Number of tensors to load
        count: usize,
        /// Format being loaded
        format: String,
    },

    /// Saving a specific file  
    SavingFile {
        /// Path to the file being saved
        file: std::path::PathBuf,
        /// Format being saved
        format: String,
    },

    /// Saving tensors to file(s)
    SavingTensors {
        /// Number of tensors to save
        count: usize,
        /// Format being saved
        format: String,
    },

    /// Loading operation completed successfully
    Complete {
        /// Number of tensors loaded/saved
        tensor_count: usize,
        /// Format that was processed
        format: String,
    },

    /// Custom status message
    Status {
        /// Message to display
        message: String,
    },

    /// Saving a checkpoint
    SavingCheckpoint,

    /// Checkpoint saved successfully
    CheckpointSaved,

    /// Parsing metadata from memory-mapped file
    ParsingMetadata,

    /// Prefetching tensors into cache
    PrefetchingTensors {
        /// Number of tensors to prefetch
        count: usize,
    },

    /// Loading a checkpoint
    LoadingCheckpoint {
        /// Path to checkpoint
        path: String,
    },

    /// Checkpoint loaded successfully
    CheckpointLoaded,
}

impl ProgressEvent {
    /// Get a human-readable description of this event
    pub fn description(&self) -> String {
        match self {
            ProgressEvent::LoadingConfig { path } => {
                format!("Loading config from {}", path)
            }
            ProgressEvent::ScanningFiles { count } => {
                if *count == 0 {
                    "Scanning for model files...".to_string()
                } else {
                    format!("Found {} model file(s)", count)
                }
            }
            ProgressEvent::DetectingArchitecture => "Detecting model architecture...".to_string(),
            ProgressEvent::LoadingTensors {
                current,
                total,
                file_name,
            } => {
                if let Some(name) = file_name {
                    format!("Loading tensors [{}/{}]: {}", current, total, name)
                } else {
                    format!("Loading tensors [{}/{}]", current, total)
                }
            }
            ProgressEvent::MappingNames { count } => {
                format!("Mapped {} tensor names", count)
            }
            ProgressEvent::BuildingModel => "Building model from tensors...".to_string(),
            ProgressEvent::ValidatingModel => "Validating model configuration...".to_string(),
            ProgressEvent::LoadingFile { file, format } => {
                format!("Loading {} file: {}", format, file.display())
            }
            ProgressEvent::LoadingTensorsFromFiles { count, format } => {
                format!("Loading {} tensors from {} format", count, format)
            }
            ProgressEvent::SavingFile { file, format } => {
                format!("Saving {} file: {}", format, file.display())
            }
            ProgressEvent::SavingTensors { count, format } => {
                format!("Saving {} tensors to {} format", count, format)
            }
            ProgressEvent::Complete {
                tensor_count,
                format,
            } => {
                format!(
                    "{} format: {} tensors processed successfully",
                    format, tensor_count
                )
            }
            ProgressEvent::Status { message } => message.clone(),
            ProgressEvent::SavingCheckpoint => "Saving checkpoint...".to_string(),
            ProgressEvent::CheckpointSaved => "Checkpoint saved successfully".to_string(),
            ProgressEvent::LoadingCheckpoint { path } => {
                format!("Loading checkpoint from {}", path)
            }
            ProgressEvent::CheckpointLoaded => "Checkpoint loaded successfully".to_string(),
            ProgressEvent::ParsingMetadata => {
                "Parsing tensor metadata from memory-mapped file...".to_string()
            }
            ProgressEvent::PrefetchingTensors { count } => {
                format!("Prefetching {} tensors into cache...", count)
            }
        }
    }

    /// Check if this is a completion event
    pub fn is_complete(&self) -> bool {
        matches!(self, ProgressEvent::Complete { .. })
    }

    /// Check if this is an error-related event
    pub fn is_error(&self) -> bool {
        // Currently no error events, but could be extended
        false
    }
}

/// Default progress reporter that prints to stdout
///
/// # Examples
/// ```rust
/// use mlmf::progress::default_progress;
///
/// let progress_fn = default_progress();
/// // Use with LoadOptions
/// ```
pub fn default_progress() -> ProgressFn {
    Box::new(|event: ProgressEvent| {
        let description = event.description();
        if event.is_complete() {
            println!("{}", description);
        } else {
            println!("📦 {}", description);
        }
    })
}

/// Silent progress reporter (no-op)
///
/// Use this when you don't want any progress output.
///
/// # Examples
/// ```rust
/// use mlmf::progress::silent_progress;
///
/// let progress_fn = silent_progress();
/// // Use with LoadOptions for silent loading
/// ```
pub fn silent_progress() -> ProgressFn {
    Box::new(|_event: ProgressEvent| {
        // Do nothing
    })
}

/// Progress reporter that logs with timestamps
///
/// # Examples
/// ```rust
/// use mlmf::progress::timestamped_progress;
///
/// let progress_fn = timestamped_progress();
/// // Outputs: [2023-10-01 12:34:56] Loading config from ...
/// ```
pub fn timestamped_progress() -> ProgressFn {
    Box::new(|event: ProgressEvent| {
        let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S");
        let description = event.description();

        if event.is_complete() {
            println!("[{}] {}", timestamp, description);
        } else {
            println!("[{}] 📦 {}", timestamp, description);
        }
    })
}

/// Progress reporter with custom prefix
///
/// # Arguments
/// * `prefix` - Custom prefix to add before each message
///
/// # Examples
/// ```rust
/// use mlmf::progress::prefixed_progress;
///
/// let progress_fn = prefixed_progress("MODEL_LOADER".to_string());
/// // Outputs: [MODEL_LOADER] Loading config from ...
/// ```
pub fn prefixed_progress(prefix: String) -> ProgressFn {
    Box::new(move |event: ProgressEvent| {
        let description = event.description();
        if event.is_complete() {
            println!("[{}] {}", prefix, description);
        } else {
            println!("[{}] 📦 {}", prefix, description);
        }
    })
}

#[cfg(feature = "progress")]
/// Progress reporter with a visual progress bar
///
/// Uses the `indicatif` crate to show a progress bar for tensor loading operations.
///
/// # Examples
/// ```rust
/// use mlmf::progress::progress_bar;
///
/// let progress_fn = progress_bar();
/// // Shows: Loading tensors [██████████████████████████████] 100%
/// ```
pub fn progress_bar() -> ProgressFn {
    use indicatif::{ProgressBar, ProgressStyle};
    use std::sync::{Arc, Mutex};

    let pb = Arc::new(Mutex::new(None::<ProgressBar>));

    Box::new(move |event: ProgressEvent| {
        let mut pb_guard = pb.lock().unwrap();

        match event {
            ProgressEvent::LoadingTensors { current, total, .. } => {
                if pb_guard.is_none() {
                    let new_pb = ProgressBar::new(total as u64);
                    new_pb.set_style(
                        ProgressStyle::default_bar()
                            .template("📦 Loading tensors [{bar:40.cyan/blue}] {pos}/{len} {msg}")
                            .unwrap()
                            .progress_chars("█▉▊▋▌▍▎▏ "),
                    );
                    *pb_guard = Some(new_pb);
                }

                if let Some(ref pb) = *pb_guard {
                    pb.set_position(current as u64);
                    if let Some(file_name) = event.description().split(": ").nth(1) {
                        pb.set_message(file_name.to_string());
                    }
                }
            }
            ProgressEvent::Complete { .. } => {
                if let Some(ref pb) = *pb_guard {
                    pb.finish_with_message("✓ Complete");
                }
                *pb_guard = None;
                println!("{}", event.description());
            }
            _ => {
                // For non-tensor events, print normally
                println!("📦 {}", event.description());
            }
        }
    })
}

#[cfg(not(feature = "progress"))]
/// Progress reporter with a visual progress bar (fallback when indicatif not available)
pub fn progress_bar() -> ProgressFn {
    // Fallback to default progress when indicatif feature is not enabled
    default_progress()
}

/// Create a custom progress reporter from a closure
///
/// # Arguments
/// * `f` - Closure that handles progress events
///
/// # Examples
/// ```rust
/// use mlmf::progress::{custom_progress, ProgressEvent};
///
/// let progress_fn = custom_progress(|event: ProgressEvent| {
///     match event {
///         ProgressEvent::Complete { tensor_count, format } => {
///             println!("Loaded {} tensors in {} format!", tensor_count, format);
///         }
///         _ => {
///             // Handle other events
///         }
///     }
/// });
/// ```
pub fn custom_progress<F>(f: F) -> ProgressFn
where
    F: Fn(ProgressEvent) + Send + Sync + 'static,
{
    Box::new(f)
}

/// Utility to time an operation and report completion
pub struct ProgressTimer {
    start_time: std::time::Instant,
    progress_fn: Option<ProgressFn>,
}

impl ProgressTimer {
    /// Create a new progress timer with optional progress reporting
    pub fn new(progress_fn: Option<ProgressFn>) -> Self {
        Self {
            start_time: std::time::Instant::now(),
            progress_fn,
        }
    }

    /// Report a progress event
    pub fn report(&self, event: ProgressEvent) {
        if let Some(ref progress_fn) = self.progress_fn {
            progress_fn(event);
        }
    }

    /// Report completion and return elapsed time
    pub fn complete(&self) -> f64 {
        let _elapsed_secs = self.start_time.elapsed().as_secs_f64();
        self.report(ProgressEvent::Complete {
            tensor_count: 0, // We don't track tensor count in ProgressTimer
            format: "Generic".to_string(),
        });
        _elapsed_secs
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    #[test]
    fn test_progress_event_descriptions() {
        let event = ProgressEvent::LoadingConfig {
            path: "/path/to/config.json".to_string(),
        };
        assert_eq!(
            event.description(),
            "Loading config from /path/to/config.json"
        );

        let event = ProgressEvent::ScanningFiles { count: 3 };
        assert_eq!(event.description(), "Found 3 model file(s)");

        let event = ProgressEvent::Complete {
            tensor_count: 1234,
            format: "SafeTensors".to_string(),
        };
        assert_eq!(
            event.description(),
            "✓ SafeTensors format: 1234 tensors processed successfully"
        );
        assert!(event.is_complete());
    }

    #[test]
    fn test_custom_progress() {
        let events = Arc::new(Mutex::new(Vec::new()));
        let events_clone = events.clone();

        let progress_fn = custom_progress(move |event: ProgressEvent| {
            events_clone.lock().unwrap().push(event);
        });

        progress_fn(ProgressEvent::DetectingArchitecture);
        progress_fn(ProgressEvent::Complete {
            tensor_count: 100,
            format: "SafeTensors".to_string(),
        });

        let captured_events = events.lock().unwrap();
        assert_eq!(captured_events.len(), 2);
        assert!(matches!(
            captured_events[0],
            ProgressEvent::DetectingArchitecture
        ));
        assert!(matches!(captured_events[1], ProgressEvent::Complete { .. }));
    }

    #[test]
    fn test_progress_timer() {
        let events = Arc::new(Mutex::new(Vec::new()));
        let events_clone = events.clone();

        let progress_fn = custom_progress(move |event: ProgressEvent| {
            events_clone.lock().unwrap().push(event);
        });

        let timer = ProgressTimer::new(Some(progress_fn));
        timer.report(ProgressEvent::DetectingArchitecture);
        let elapsed = timer.complete();

        assert!(elapsed >= 0.0);

        let captured_events = events.lock().unwrap();
        assert_eq!(captured_events.len(), 2);
        assert!(matches!(captured_events[1], ProgressEvent::Complete { .. }));
    }
}