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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Copyright 2023 MLMF Contributors
// Licensed under Apache License v2.0

//! # Model Conversion API
//!
//! Provides unified format-to-format model conversion capabilities with streaming
//! and memory-efficient processing. Supports conversion between SafeTensors, ONNX,
//! GGUF, PyTorch, and AWQ formats.
//!
//! ## Features
//!
//! - **Memory Efficient**: Stream-based conversion avoids loading entire models into memory
//! - **Batch Operations**: Convert multiple models with a single API call
//! - **Format Auto-Detection**: Automatically determines source format
//! - **Metadata Preservation**: Maintains model metadata during conversion when possible
//! - **Progress Tracking**: Optional progress callbacks for long-running conversions
//!
//! ## Example
//!
//! ```no_run
//! use mlmf::conversion::{convert_model, ConversionOptions, ConversionFormat};
//!
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> {
//! // Convert PyTorch model to SafeTensors
//! convert_model(
//!     "model.pth",
//!     "model.safetensors",
//!     ConversionOptions {
//!         target_format: ConversionFormat::SafeTensors,
//!         preserve_metadata: true,
//!         ..Default::default()
//!     }
//! )?;
//!
//! // Convert ONNX to GGUF with quantization
//! convert_model(
//!     "model.onnx",
//!     "model.gguf",
//!     ConversionOptions {
//!         target_format: ConversionFormat::GGUF,
//!         quantization: Some("q4_0".to_string()),
//!         ..Default::default()
//!     }
//! )?;
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::{Error, LoadedModel};

#[cfg(feature = "progress")]
use indicatif::{ProgressBar, ProgressStyle};

/// Supported conversion target formats
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConversionFormat {
    /// SafeTensors format (memory-safe, fast loading)
    SafeTensors,
    /// ONNX format (production inference)
    #[cfg(feature = "onnx")]
    ONNX,
    /// GGUF format (quantized models, llama.cpp compatible)
    #[cfg(feature = "gguf")]
    GGUF,
    /// PyTorch format (research and training)
    #[cfg(feature = "pytorch")]
    PyTorch,
    /// AWQ format (efficient quantization)
    #[cfg(feature = "awq")]
    AWQ,
}

/// Configuration options for model conversion
#[derive(Debug, Clone, Default)]
pub struct ConversionOptions {
    /// Target format for conversion
    pub target_format: ConversionFormat,
    /// Whether to preserve original metadata
    pub preserve_metadata: bool,
    /// Optional quantization specification (format-dependent)
    pub quantization: Option<String>,
    /// Maximum memory usage in bytes (0 = unlimited)
    pub max_memory_usage: usize,
    /// Number of worker threads (0 = auto)
    pub worker_threads: usize,
    /// Whether to validate conversion results
    pub validate_output: bool,
    /// Custom metadata to add during conversion
    pub custom_metadata: HashMap<String, String>,
}

impl Default for ConversionFormat {
    fn default() -> Self {
        Self::SafeTensors
    }
}

/// Batch conversion job specification
#[derive(Debug, Clone)]
pub struct ConversionJob {
    /// Source model path
    pub source: PathBuf,
    /// Target model path  
    pub target: PathBuf,
    /// Conversion options
    pub options: ConversionOptions,
}

/// Results of a conversion operation
#[derive(Debug)]
pub struct ConversionResult {
    /// Whether conversion succeeded
    pub success: bool,
    /// Source file path
    pub source_path: PathBuf,
    /// Target file path
    pub target_path: PathBuf,
    /// Original format detected
    pub source_format: String,
    /// Target format used
    pub target_format: ConversionFormat,
    /// Number of tensors converted
    pub tensors_converted: usize,
    /// Total size in bytes processed
    pub bytes_processed: usize,
    /// Conversion time in milliseconds
    pub duration_ms: u64,
    /// Any warning messages
    pub warnings: Vec<String>,
    /// Error message if conversion failed
    pub error: Option<String>,
}

/// Convert a single model from one format to another
///
/// # Arguments
///
/// * `source_path` - Path to the source model file
/// * `target_path` - Path where the converted model should be saved
/// * `options` - Conversion configuration options
///
/// # Returns
///
/// Returns `Ok(ConversionResult)` on success, or `Err(Error)` on failure.
///
/// # Example
///
/// ```no_run
/// use mlmf::conversion::{convert_model, ConversionOptions, ConversionFormat};
///
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let result = convert_model(
///     "model.pth",
///     "model.safetensors",
///     ConversionOptions {
///         target_format: ConversionFormat::SafeTensors,
///         preserve_metadata: true,
///         validate_output: true,
///         ..Default::default()
///     }
/// )?;
///
/// println!("Converted {} tensors in {}ms",
///          result.tensors_converted, result.duration_ms);
/// # Ok(())
/// # }
/// ```
pub fn convert_model<P: AsRef<Path>>(
    source_path: P,
    target_path: P,
    options: ConversionOptions,
) -> Result<ConversionResult, Error> {
    let start_time = std::time::Instant::now();
    let source_path = source_path.as_ref().to_path_buf();
    let target_path = target_path.as_ref().to_path_buf();

    // Detect source format
    let source_format = detect_format(&source_path)?;

    #[cfg(feature = "progress")]
    #[cfg(feature = "progress")]
    let progress = Some(create_progress_bar("Converting model"));
    #[cfg(not(feature = "progress"))]
    let progress: Option<()> = None;

    // Perform the actual conversion based on source and target formats
    // For now, just return an error indicating conversion is not yet fully implemented
    let result = Err(Error::other(format!(
        "Model conversion from {} to {:?} is not yet implemented. \
        The conversion API framework is ready but individual format conversions \
        need to be completed. Source: {}, Target: {}",
        source_format,
        options.target_format,
        source_path.display(),
        target_path.display()
    )));

    #[cfg(feature = "progress")]
    if let Some(pb) = progress {
        pb.finish_with_message("Conversion complete");
    }

    let duration_ms = start_time.elapsed().as_millis() as u64;

    match result {
        Ok((tensors_converted, bytes_processed, warnings)) => Ok(ConversionResult {
            success: true,
            source_path,
            target_path,
            source_format,
            target_format: options.target_format,
            tensors_converted,
            bytes_processed,
            duration_ms,
            warnings,
            error: None,
        }),
        Err(e) => Ok(ConversionResult {
            success: false,
            source_path,
            target_path,
            source_format,
            target_format: options.target_format,
            tensors_converted: 0,
            bytes_processed: 0,
            duration_ms,
            warnings: Vec::new(),
            error: Some(e.to_string()),
        }),
    }
}

/// Convert multiple models in batch
///
/// # Arguments
///
/// * `jobs` - Vector of conversion jobs to process
/// * `parallel` - Whether to process jobs in parallel
///
/// # Returns
///
/// Returns a vector of conversion results, one for each job.
///
/// # Example
///
/// ```no_run
/// use mlmf::conversion::{convert_batch, ConversionJob, ConversionOptions, ConversionFormat};
/// use std::path::PathBuf;
///
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let jobs = vec![
///     ConversionJob {
///         source: PathBuf::from("model1.pth"),
///         target: PathBuf::from("model1.safetensors"),
///         options: ConversionOptions {
///             target_format: ConversionFormat::SafeTensors,
///             ..Default::default()
///         },
///     },
///     ConversionJob {
///         source: PathBuf::from("model2.onnx"),
///         target: PathBuf::from("model2.gguf"),
///         options: ConversionOptions {
///             target_format: ConversionFormat::GGUF,
///             quantization: Some("q4_0".to_string()),
///             ..Default::default()
///         },
///     },
/// ];
///
/// let results = convert_batch(jobs, true)?;
/// for result in results {
///     if result.success {
///         println!("✓ Converted {}", result.source_path.display());
///     } else {
///         println!("✗ Failed to convert {}: {}",
///                  result.source_path.display(),
///                  result.error.unwrap_or_default());
///     }
/// }
/// # Ok(())
/// # }
/// ```
pub fn convert_batch(
    jobs: Vec<ConversionJob>,
    parallel: bool,
) -> Result<Vec<ConversionResult>, Error> {
    if parallel {
        // Use rayon for parallel processing if available
        #[cfg(feature = "rayon")]
        {
            use rayon::prelude::*;
            Ok(jobs
                .into_par_iter()
                .map(|job| convert_model(job.source, job.target, job.options))
                .map(|r| {
                    r.unwrap_or_else(|e| ConversionResult {
                        success: false,
                        error: Some(e.to_string()),
                        ..Default::default()
                    })
                })
                .collect())
        }
        #[cfg(not(feature = "rayon"))]
        {
            // Fall back to sequential processing
            convert_batch(jobs, false)
        }
    } else {
        // Sequential processing
        let mut results = Vec::new();
        for job in jobs {
            let result = convert_model(job.source, job.target, job.options).unwrap_or_else(|e| {
                ConversionResult {
                    success: false,
                    error: Some(e.to_string()),
                    ..Default::default()
                }
            });
            results.push(result);
        }
        Ok(results)
    }
}

/// Detect the format of a model file based on its extension and content
fn detect_format(path: &Path) -> Result<String, Error> {
    let extension = path.extension().and_then(|ext| ext.to_str()).unwrap_or("");

    match extension.to_lowercase().as_str() {
        "safetensors" => Ok("SafeTensors".to_string()),
        #[cfg(feature = "onnx")]
        "onnx" => Ok("ONNX".to_string()),
        #[cfg(feature = "gguf")]
        "gguf" => Ok("GGUF".to_string()),
        #[cfg(feature = "pytorch")]
        "pth" | "pt" | "bin" => Ok("PyTorch".to_string()),
        #[cfg(feature = "awq")]
        "awq" => Ok("AWQ".to_string()),
        _ => {
            // Try to detect by reading file header
            detect_format_by_content(path)
        }
    }
}

/// Detect format by examining file content
fn detect_format_by_content(path: &Path) -> Result<String, Error> {
    use std::fs::File;
    use std::io::Read;

    let mut file = File::open(path)
        .map_err(|e| Error::io_error(format!("Failed to open {}: {}", path.display(), e)))?;

    let mut header = [0u8; 16];
    file.read_exact(&mut header)
        .map_err(|e| Error::io_error(format!("Failed to read file header: {}", e)))?;

    // SafeTensors magic number
    if header.starts_with(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) {
        return Ok("SafeTensors".to_string());
    }

    #[cfg(feature = "gguf")]
    // GGUF magic number "GGUF"
    if header.starts_with(b"GGUF") {
        return Ok("GGUF".to_string());
    }

    #[cfg(feature = "onnx")]
    // ONNX protobuf header
    if header.starts_with(&[0x08, 0x01]) || header.starts_with(&[0x08, 0x07]) {
        return Ok("ONNX".to_string());
    }

    #[cfg(feature = "pytorch")]
    // PyTorch pickle magic number
    if header.starts_with(&[0x80, 0x02]) || header.starts_with(b"PK") {
        return Ok("PyTorch".to_string());
    }

    Err(Error::unsupported_format(format!(
        "Could not determine format of file: {}",
        path.display()
    )))
}

/// Copy file with metadata updates (for same-format "conversions")
fn copy_with_metadata(
    source_path: &Path,
    target_path: &Path,
    options: &ConversionOptions,
) -> Result<(usize, usize, Vec<String>), Error> {
    use std::fs;

    if options.custom_metadata.is_empty() && options.preserve_metadata {
        // Simple file copy if no metadata changes needed
        fs::copy(source_path, target_path)
            .map_err(|e| Error::io_error(format!("Failed to copy file: {}", e)))?;

        let file_size = fs::metadata(target_path)
            .map_err(|e| Error::io_error(format!("Failed to get file metadata: {}", e)))?
            .len() as usize;

        Ok((1, file_size, Vec::new()))
    } else {
        // Need to load and re-save to update metadata
        convert_between_formats(
            source_path,
            target_path,
            &detect_format(source_path)?,
            options,
        )
    }
}

/// Perform cross-format conversion
fn convert_between_formats(
    source_path: &Path,
    target_path: &Path,
    source_format: &str,
    options: &ConversionOptions,
) -> Result<(usize, usize, Vec<String>), Error> {
    // Load the source model
    let loaded_model = load_model_by_format(source_path, source_format)?;
    let mut warnings = Vec::new();

    // Convert and save to target format
    let tensors_converted = loaded_model.raw_tensors.len();
    let bytes_processed = calculate_total_size(&loaded_model.raw_tensors);

    save_model_by_format(target_path, &loaded_model, &options.target_format, options)?;

    // Add format-specific warnings based on string formats
    if source_format == "PyTorch" && options.target_format == ConversionFormat::ONNX {
        warnings.push("PyTorch to ONNX conversion may lose dynamic graph information".to_string());
    }
    if source_format == "ONNX" && options.target_format == ConversionFormat::GGUF {
        if options.quantization.is_some() {
            warnings.push(
                "Quantization during ONNX to GGUF conversion may affect model accuracy".to_string(),
            );
        }
    }

    Ok((tensors_converted, bytes_processed, warnings))
}

/// Load a model using the existing universal loader
fn load_model_by_format(path: &Path, _format: &str) -> Result<LoadedModel, Error> {
    // Use the existing universal loader with default options
    let options = crate::LoadOptions::default();
    crate::universal_loader::load_model(path, options)
}

/// Save a model using the appropriate format-specific exporter
fn save_model_by_format(
    path: &Path,
    model: &LoadedModel,
    format: &ConversionFormat,
    options: &ConversionOptions,
) -> Result<(), Error> {
    match format {
        ConversionFormat::SafeTensors => crate::formats::safetensors_export::save_as_safetensors(
            model,
            path,
            options.preserve_metadata,
        ),
        #[cfg(feature = "onnx")]
        ConversionFormat::ONNX => {
            let export_options = crate::formats::onnx_export::OnnxExportOptions {
                preserve_metadata: options.preserve_metadata,
                custom_metadata: options.custom_metadata.clone(),
                ..Default::default()
            };
            crate::formats::onnx_export::save_as_onnx(model, path, export_options)
        }
        #[cfg(feature = "gguf")]
        ConversionFormat::GGUF => {
            let export_options = crate::formats::gguf_export::GgufExportOptions {
                quantization: options.quantization.clone(),
                preserve_metadata: options.preserve_metadata,
                custom_metadata: options.custom_metadata.clone(),
                ..Default::default()
            };
            crate::formats::gguf_export::save_as_gguf(model, path, export_options)
        }
        #[cfg(feature = "pytorch")]
        ConversionFormat::PyTorch => {
            crate::formats::pytorch_export::save_as_pytorch(model, path, options.preserve_metadata)
        }
        #[cfg(feature = "awq")]
        ConversionFormat::AWQ => {
            let export_options = crate::formats::awq_export::AwqExportOptions {
                quantization: options.quantization.clone(),
                preserve_metadata: options.preserve_metadata,
                ..Default::default()
            };
            crate::formats::awq_export::save_as_awq(model, path, export_options)
        }
    }
}

/// Calculate total size of tensors in bytes
fn calculate_total_size(tensors: &HashMap<String, candle_core::Tensor>) -> usize {
    // Calculate total bytes from tensor shapes and dtypes
    tensors
        .values()
        .map(|tensor| {
            let elem_count = tensor.shape().elem_count();
            let dtype_size = match tensor.dtype() {
                candle_core::DType::U8 => 1,
                candle_core::DType::U32 => 4,
                candle_core::DType::I64 => 8,
                candle_core::DType::BF16 | candle_core::DType::F16 => 2,
                candle_core::DType::F32 => 4,
                candle_core::DType::F64 => 8,
            };
            elem_count * dtype_size
        })
        .sum()
}

#[cfg(feature = "progress")]
/// Create a progress bar for conversion operations
fn create_progress_bar(message: &str) -> ProgressBar {
    let pb = ProgressBar::new_spinner();
    pb.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.green} {msg}: {elapsed}")
            .unwrap_or_else(|_| ProgressStyle::default_spinner()),
    );
    pb.set_message(message.to_string());
    pb.enable_steady_tick(std::time::Duration::from_millis(100));
    pb
}

// Provide default implementations for ConversionResult fields that need them
impl Default for ConversionResult {
    fn default() -> Self {
        Self {
            success: false,
            source_path: PathBuf::new(),
            target_path: PathBuf::new(),
            source_format: "Unknown".to_string(),
            target_format: ConversionFormat::SafeTensors,
            tensors_converted: 0,
            bytes_processed: 0,
            duration_ms: 0,
            warnings: Vec::new(),
            error: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_format_detection() {
        // Test extension-based detection
        assert_eq!(
            detect_format(Path::new("model.safetensors")).unwrap(),
            "SafeTensors"
        );

        #[cfg(feature = "onnx")]
        assert_eq!(detect_format(Path::new("model.onnx")).unwrap(), "ONNX");
    }

    #[test]
    fn test_conversion_options_default() {
        let options = ConversionOptions::default();
        assert_eq!(options.target_format, ConversionFormat::SafeTensors);
        assert!(!options.preserve_metadata);
        assert!(options.quantization.is_none());
        assert_eq!(options.max_memory_usage, 0);
    }

    #[test]
    fn test_conversion_result_default() {
        let result = ConversionResult::default();
        assert!(!result.success);
        assert_eq!(result.tensors_converted, 0);
        assert!(result.error.is_none());
    }
}