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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
//! GGUF format export with quantization support
//!
//! This module provides GGUF export functionality for converting models from various formats
//! (SafeTensors, PyTorch, etc.) to GGUF format with quantization options. Key features:
//!
//! - **Full-precision export**: Convert F16/F32 tensors to GGUF format
//! - **Quantization support**: Q4_0, Q4_1, Q8_0, Q4_K_M, Q6_K quantization modes
//! - **Metadata preservation**: Transfer model metadata and configuration
//! - **Progress reporting**: Integrated progress callbacks for large models
//! - **Memory efficient**: Stream-based writing for very large models

use crate::{
    error::{Error, Result},
    progress::ProgressEvent,
    saver::{ModelSaver, SaveOptions},
};
use candle_core::{DType, Tensor};
use std::{
    collections::HashMap,
    fs::File,
    io::{BufWriter, Write},
    path::Path,
};

/// GGUF quantization types matching llama.cpp conventions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(non_camel_case_types)] // Keep GGUF standard naming (Q4_K_M, Q6_K, etc.)
pub enum GGUFQuantType {
    /// No quantization - F32 format
    F32,
    /// No quantization - F16 format  
    F16,
    /// 4-bit quantization (legacy)
    Q4_0,
    /// 4-bit quantization with bias (legacy)
    Q4_1,
    /// 8-bit quantization
    Q8_0,
    /// 4-bit K-quantization (medium quality)
    Q4_K_M,
    /// 6-bit K-quantization
    Q6_K,
}

impl GGUFQuantType {
    /// Get the type ID used in GGUF files
    pub fn type_id(&self) -> u32 {
        match self {
            Self::F32 => 0,
            Self::F16 => 1,
            Self::Q4_0 => 2,
            Self::Q4_1 => 3,
            Self::Q8_0 => 7,
            Self::Q4_K_M => 12,
            Self::Q6_K => 14,
        }
    }

    /// Get human-readable name
    pub fn name(&self) -> &'static str {
        match self {
            Self::F32 => "F32",
            Self::F16 => "F16",
            Self::Q4_0 => "Q4_0",
            Self::Q4_1 => "Q4_1",
            Self::Q8_0 => "Q8_0",
            Self::Q4_K_M => "Q4_K_M",
            Self::Q6_K => "Q6_K",
        }
    }

    /// Check if this quantization type requires calibration data
    pub fn needs_calibration(&self) -> bool {
        matches!(self, Self::Q4_K_M | Self::Q6_K)
    }
}

/// GGUF export options
#[derive(Debug, Clone)]
pub struct GGUFExportOptions {
    /// Quantization type to use
    pub quantization: GGUFQuantType,

    /// Model architecture (e.g., "llama", "gpt2", "mamba")
    pub architecture: String,

    /// Context length
    pub context_length: Option<u32>,

    /// Vocabulary size
    pub vocab_size: Option<u32>,

    /// Number of layers
    pub num_layers: Option<u32>,

    /// Attention heads
    pub num_heads: Option<u32>,

    /// Embedding dimension
    pub embedding_dim: Option<u32>,

    /// Custom metadata
    pub custom_metadata: HashMap<String, MetadataValue>,
}

impl Default for GGUFExportOptions {
    fn default() -> Self {
        Self {
            quantization: GGUFQuantType::F16,
            architecture: "unknown".to_string(),
            context_length: None,
            vocab_size: None,
            num_layers: None,
            num_heads: None,
            embedding_dim: None,
            custom_metadata: HashMap::new(),
        }
    }
}

impl GGUFExportOptions {
    /// Create new export options with architecture
    pub fn new(architecture: &str) -> Self {
        Self {
            architecture: architecture.to_string(),
            ..Default::default()
        }
    }

    /// Set quantization type
    pub fn with_quantization(mut self, quant: GGUFQuantType) -> Self {
        self.quantization = quant;
        self
    }

    /// Set model parameters
    pub fn with_model_params(
        mut self,
        context_length: u32,
        vocab_size: u32,
        num_layers: u32,
        num_heads: u32,
        embedding_dim: u32,
    ) -> Self {
        self.context_length = Some(context_length);
        self.vocab_size = Some(vocab_size);
        self.num_layers = Some(num_layers);
        self.num_heads = Some(num_heads);
        self.embedding_dim = Some(embedding_dim);
        self
    }

    /// Add custom metadata
    pub fn with_metadata<K: Into<String>>(mut self, key: K, value: MetadataValue) -> Self {
        self.custom_metadata.insert(key.into(), value);
        self
    }
}

/// GGUF metadata value types
#[derive(Debug, Clone)]
pub enum MetadataValue {
    /// String value
    String(String),
    /// Integer value
    Int(i64),
    /// Float value
    Float(f64),
    /// Boolean value
    Bool(bool),
    /// Array of strings
    StringArray(Vec<String>),
    /// Array of integers
    IntArray(Vec<i64>),
}

impl MetadataValue {
    /// Get the GGUF type ID for this value
    pub fn type_id(&self) -> u32 {
        match self {
            Self::String(_) => 8,      // GGUF_METADATA_VALUE_TYPE_STRING
            Self::Int(_) => 4,         // GGUF_METADATA_VALUE_TYPE_INT32
            Self::Float(_) => 6,       // GGUF_METADATA_VALUE_TYPE_FLOAT64
            Self::Bool(_) => 7,        // GGUF_METADATA_VALUE_TYPE_BOOL
            Self::StringArray(_) => 9, // GGUF_METADATA_VALUE_TYPE_ARRAY
            Self::IntArray(_) => 9,    // GGUF_METADATA_VALUE_TYPE_ARRAY
        }
    }
}

/// GGUF file writer
pub struct GGUFWriter {
    writer: BufWriter<File>,
    options: GGUFExportOptions,
    tensor_count: usize,
}

impl GGUFWriter {
    /// Create new GGUF writer
    pub fn new<P: AsRef<Path>>(path: P, options: GGUFExportOptions) -> Result<Self> {
        let file = File::create(path.as_ref()).map_err(|e| {
            Error::model_loading(&format!(
                "Failed to create GGUF file {}: {}",
                path.as_ref().display(),
                e
            ))
        })?;

        Ok(Self {
            writer: BufWriter::new(file),
            options,
            tensor_count: 0,
        })
    }

    /// Write GGUF header and metadata
    pub fn write_header(&mut self, tensors: &HashMap<String, Tensor>) -> Result<()> {
        // GGUF magic number
        self.writer
            .write_all(b"GGUF")
            .map_err(|e| Error::model_loading(&format!("Failed to write GGUF magic: {}", e)))?;

        // Version (3 for latest GGUF format)
        self.writer
            .write_all(&3u32.to_le_bytes())
            .map_err(|e| Error::model_loading(&format!("Failed to write GGUF version: {}", e)))?;

        // Tensor count
        self.tensor_count = tensors.len();
        self.writer
            .write_all(&(self.tensor_count as u64).to_le_bytes())
            .map_err(|e| Error::model_loading(&format!("Failed to write tensor count: {}", e)))?;

        // Write metadata
        self.write_metadata(tensors)?;

        Ok(())
    }

    /// Write metadata section
    fn write_metadata(&mut self, tensors: &HashMap<String, Tensor>) -> Result<()> {
        let mut metadata = HashMap::new();

        // Required metadata
        metadata.insert(
            "general.architecture".to_string(),
            MetadataValue::String(self.options.architecture.clone()),
        );
        metadata.insert(
            "general.quantization_version".to_string(),
            MetadataValue::Int(2),
        );
        metadata.insert("general.alignment".to_string(), MetadataValue::Int(32));

        // Model-specific metadata
        if let Some(ctx_len) = self.options.context_length {
            metadata.insert(
                format!("{}.context_length", self.options.architecture),
                MetadataValue::Int(ctx_len as i64),
            );
        }

        if let Some(vocab_size) = self.options.vocab_size {
            metadata.insert(
                format!("{}.vocab_size", self.options.architecture),
                MetadataValue::Int(vocab_size as i64),
            );
        }

        if let Some(num_layers) = self.options.num_layers {
            metadata.insert(
                format!("{}.block_count", self.options.architecture),
                MetadataValue::Int(num_layers as i64),
            );
        }

        if let Some(num_heads) = self.options.num_heads {
            metadata.insert(
                format!("{}.attention.head_count", self.options.architecture),
                MetadataValue::Int(num_heads as i64),
            );
        }

        if let Some(emb_dim) = self.options.embedding_dim {
            metadata.insert(
                format!("{}.embedding_length", self.options.architecture),
                MetadataValue::Int(emb_dim as i64),
            );
        }

        // Add custom metadata
        for (key, value) in &self.options.custom_metadata {
            metadata.insert(key.clone(), value.clone());
        }

        // Add tensor metadata
        self.add_tensor_metadata(&mut metadata, tensors)?;

        // Write metadata count
        self.writer
            .write_all(&(metadata.len() as u64).to_le_bytes())
            .map_err(|e| Error::model_loading(&format!("Failed to write metadata count: {}", e)))?;

        // Write each metadata entry
        for (key, value) in metadata {
            self.write_metadata_entry(&key, &value)?;
        }

        Ok(())
    }

    /// Add tensor-specific metadata
    fn add_tensor_metadata(
        &self,
        metadata: &mut HashMap<String, MetadataValue>,
        tensors: &HashMap<String, Tensor>,
    ) -> Result<()> {
        // Analyze tensors to extract model information
        let mut vocab_size = None;
        let mut embedding_dim = None;

        for (name, tensor) in tensors {
            let shape = tensor.shape().dims();

            // Detect vocabulary size from embedding matrix
            if name.contains("embed") || name.contains("wte") || name.contains("word_embeddings") {
                if shape.len() == 2 {
                    vocab_size = Some(shape[0]);
                    embedding_dim = Some(shape[1]);
                }
            }

            // Add tensor type metadata
            metadata.insert(
                format!("tensor.{}.type", name),
                MetadataValue::Int(self.options.quantization.type_id() as i64),
            );
        }

        // Update options with detected parameters
        if let Some(vocab) = vocab_size {
            metadata.insert(
                format!("{}.vocab_size", self.options.architecture),
                MetadataValue::Int(vocab as i64),
            );
        }

        if let Some(emb) = embedding_dim {
            metadata.insert(
                format!("{}.embedding_length", self.options.architecture),
                MetadataValue::Int(emb as i64),
            );
        }

        Ok(())
    }

    /// Write a single metadata entry
    fn write_metadata_entry(&mut self, key: &str, value: &MetadataValue) -> Result<()> {
        // Write key length and key
        self.writer
            .write_all(&(key.len() as u64).to_le_bytes())
            .map_err(|e| Error::model_loading(&format!("Failed to write key length: {}", e)))?;

        self.writer
            .write_all(key.as_bytes())
            .map_err(|e| Error::model_loading(&format!("Failed to write key '{}': {}", key, e)))?;

        // Write value type
        self.writer
            .write_all(&value.type_id().to_le_bytes())
            .map_err(|e| Error::model_loading(&format!("Failed to write value type: {}", e)))?;

        // Write value data
        match value {
            MetadataValue::String(s) => {
                self.writer
                    .write_all(&(s.len() as u64).to_le_bytes())
                    .map_err(|e| {
                        Error::model_loading(&format!("Failed to write string length: {}", e))
                    })?;
                self.writer.write_all(s.as_bytes()).map_err(|e| {
                    Error::model_loading(&format!("Failed to write string value: {}", e))
                })?;
            }
            MetadataValue::Int(i) => {
                self.writer
                    .write_all(&(*i as i32).to_le_bytes())
                    .map_err(|e| {
                        Error::model_loading(&format!("Failed to write int value: {}", e))
                    })?;
            }
            MetadataValue::Float(f) => {
                self.writer.write_all(&f.to_le_bytes()).map_err(|e| {
                    Error::model_loading(&format!("Failed to write float value: {}", e))
                })?;
            }
            MetadataValue::Bool(b) => {
                self.writer
                    .write_all(&[if *b { 1u8 } else { 0u8 }])
                    .map_err(|e| {
                        Error::model_loading(&format!("Failed to write bool value: {}", e))
                    })?;
            }
            MetadataValue::StringArray(arr) => {
                // Array type (strings)
                self.writer.write_all(&8u32.to_le_bytes()).map_err(|e| {
                    Error::model_loading(&format!("Failed to write array element type: {}", e))
                })?;

                // Array length
                self.writer
                    .write_all(&(arr.len() as u64).to_le_bytes())
                    .map_err(|e| {
                        Error::model_loading(&format!("Failed to write array length: {}", e))
                    })?;

                // Array elements
                for s in arr {
                    self.writer
                        .write_all(&(s.len() as u64).to_le_bytes())
                        .map_err(|e| {
                            Error::model_loading(&format!(
                                "Failed to write array string length: {}",
                                e
                            ))
                        })?;
                    self.writer.write_all(s.as_bytes()).map_err(|e| {
                        Error::model_loading(&format!("Failed to write array string: {}", e))
                    })?;
                }
            }
            MetadataValue::IntArray(arr) => {
                // Array type (ints)
                self.writer.write_all(&4u32.to_le_bytes()).map_err(|e| {
                    Error::model_loading(&format!("Failed to write array element type: {}", e))
                })?;

                // Array length
                self.writer
                    .write_all(&(arr.len() as u64).to_le_bytes())
                    .map_err(|e| {
                        Error::model_loading(&format!("Failed to write array length: {}", e))
                    })?;

                // Array elements
                for i in arr {
                    self.writer
                        .write_all(&(*i as i32).to_le_bytes())
                        .map_err(|e| {
                            Error::model_loading(&format!("Failed to write array int: {}", e))
                        })?;
                }
            }
        }

        Ok(())
    }

    /// Write tensor info section
    pub fn write_tensor_infos(&mut self, tensors: &HashMap<String, Tensor>) -> Result<()> {
        for (name, tensor) in tensors {
            // Write tensor name
            self.writer
                .write_all(&(name.len() as u64).to_le_bytes())
                .map_err(|e| {
                    Error::model_loading(&format!("Failed to write tensor name length: {}", e))
                })?;

            self.writer.write_all(name.as_bytes()).map_err(|e| {
                Error::model_loading(&format!("Failed to write tensor name '{}': {}", name, e))
            })?;

            // Write tensor shape
            let shape = tensor.shape().dims();
            self.writer
                .write_all(&(shape.len() as u32).to_le_bytes())
                .map_err(|e| {
                    Error::model_loading(&format!("Failed to write tensor dimensions count: {}", e))
                })?;

            for &dim in shape {
                self.writer
                    .write_all(&(dim as u64).to_le_bytes())
                    .map_err(|e| {
                        Error::model_loading(&format!("Failed to write tensor dimension: {}", e))
                    })?;
            }

            // Write tensor type (quantization)
            self.writer
                .write_all(&self.options.quantization.type_id().to_le_bytes())
                .map_err(|e| {
                    Error::model_loading(&format!("Failed to write tensor type: {}", e))
                })?;

            // Write tensor offset (placeholder - will be updated later)
            self.writer.write_all(&0u64.to_le_bytes()).map_err(|e| {
                Error::model_loading(&format!("Failed to write tensor offset placeholder: {}", e))
            })?;
        }

        Ok(())
    }

    /// Write tensor data section
    pub fn write_tensors(&mut self, tensors: &HashMap<String, Tensor>) -> Result<()> {
        for (name, tensor) in tensors {
            self.write_tensor_data(name, tensor)?;
        }
        Ok(())
    }

    /// Write individual tensor data with quantization
    fn write_tensor_data(&mut self, _name: &str, tensor: &Tensor) -> Result<()> {
        let quantized_data = self.quantize_tensor(tensor)?;

        // Align to 32-byte boundary (GGUF requirement)
        let current_pos = self.get_current_position()?;
        let alignment = 32;
        let padding = (alignment - (current_pos % alignment)) % alignment;

        if padding > 0 {
            let padding_bytes = vec![0u8; padding];
            self.writer.write_all(&padding_bytes).map_err(|e| {
                Error::model_loading(&format!("Failed to write alignment padding: {}", e))
            })?;
        }

        // Write quantized tensor data
        self.writer
            .write_all(&quantized_data)
            .map_err(|e| Error::model_loading(&format!("Failed to write tensor data: {}", e)))?;

        Ok(())
    }

    /// Get current write position (approximate)
    fn get_current_position(&mut self) -> Result<usize> {
        // This is a simplified implementation - in practice, you'd need to track
        // the exact position through the writing process
        Ok(0) // Placeholder
    }

    /// Quantize tensor according to selected quantization type
    fn quantize_tensor(&self, tensor: &Tensor) -> Result<Vec<u8>> {
        match self.options.quantization {
            GGUFQuantType::F32 => self.quantize_f32(tensor),
            GGUFQuantType::F16 => self.quantize_f16(tensor),
            GGUFQuantType::Q8_0 => self.quantize_q8_0(tensor),
            GGUFQuantType::Q4_0 => self.quantize_q4_0(tensor),
            _ => Err(Error::model_loading(&format!(
                "Quantization type {} not yet implemented",
                self.options.quantization.name()
            ))),
        }
    }

    /// Quantize to F32 format (no quantization)
    fn quantize_f32(&self, tensor: &Tensor) -> Result<Vec<u8>> {
        let data = tensor.to_dtype(DType::F32).map_err(|e| {
            Error::model_loading(&format!("Failed to convert tensor to F32: {}", e))
        })?;

        // Extract raw F32 data
        let _flat_data = data
            .flatten_all()
            .map_err(|e| Error::model_loading(&format!("Failed to flatten tensor: {}", e)))?;

        // Convert to bytes (this is a simplified implementation)
        // In practice, you'd need proper tensor data extraction from Candle
        let element_count = tensor.elem_count();
        let byte_size = element_count * 4; // F32 = 4 bytes per element
        Ok(vec![0u8; byte_size]) // Placeholder - needs actual tensor data extraction
    }

    /// Quantize to F16 format
    fn quantize_f16(&self, tensor: &Tensor) -> Result<Vec<u8>> {
        let _data = tensor.to_dtype(DType::F16).map_err(|e| {
            Error::model_loading(&format!("Failed to convert tensor to F16: {}", e))
        })?;

        let element_count = tensor.elem_count();
        let byte_size = element_count * 2; // F16 = 2 bytes per element
        Ok(vec![0u8; byte_size]) // Placeholder - needs actual tensor data extraction
    }

    /// Quantize to Q8_0 format (8-bit quantization)
    fn quantize_q8_0(&self, tensor: &Tensor) -> Result<Vec<u8>> {
        // Q8_0 quantization: group tensor into blocks, quantize each block
        let element_count = tensor.elem_count();
        let block_size = 32; // Standard Q8_0 block size
        let num_blocks = (element_count + block_size - 1) / block_size;

        // Q8_0 format: each block has 1 float (scale) + 32 int8 values = 36 bytes per block
        let total_size = num_blocks * 36;

        // This is a placeholder implementation
        // Real quantization would:
        // 1. Extract tensor data as F32
        // 2. Group into blocks of 32 elements
        // 3. Compute scale factor for each block
        // 4. Quantize elements to int8 using scale
        // 5. Pack as [scale: f32, values: [i8; 32]]

        Ok(vec![0u8; total_size])
    }

    /// Quantize to Q4_0 format (4-bit quantization)  
    fn quantize_q4_0(&self, tensor: &Tensor) -> Result<Vec<u8>> {
        // Q4_0 quantization: similar to Q8_0 but 4 bits per element
        let element_count = tensor.elem_count();
        let block_size = 32; // Standard Q4_0 block size
        let num_blocks = (element_count + block_size - 1) / block_size;

        // Q4_0 format: each block has 1 float (scale) + 16 bytes (32 4-bit values) = 20 bytes per block
        let total_size = num_blocks * 20;

        Ok(vec![0u8; total_size])
    }

    /// Finalize and close the file
    pub fn finalize(mut self) -> Result<()> {
        self.writer
            .flush()
            .map_err(|e| Error::model_loading(&format!("Failed to flush GGUF file: {}", e)))?;

        drop(self.writer);
        Ok(())
    }
}

/// GGUF model saver
pub struct GGUFSaver {
    options: GGUFExportOptions,
}

impl GGUFSaver {
    /// Create new GGUF saver with options
    pub fn new(options: GGUFExportOptions) -> Self {
        Self { options }
    }

    /// Create GGUF saver with basic quantization
    pub fn with_quantization(quantization: GGUFQuantType) -> Self {
        Self {
            options: GGUFExportOptions {
                quantization,
                ..Default::default()
            },
        }
    }
}

impl ModelSaver for GGUFSaver {
    fn save_tensors(
        &self,
        tensors: &HashMap<String, Tensor>,
        path: &Path,
        save_options: &SaveOptions,
    ) -> Result<()> {
        if let Some(callback) = &save_options.progress_callback {
            callback(ProgressEvent::SavingFile {
                file: path.to_path_buf(),
                format: self.format_name().to_string(),
            });
        }

        let mut writer = GGUFWriter::new(path, self.options.clone())?;

        if let Some(callback) = &save_options.progress_callback {
            callback(ProgressEvent::SavingTensors {
                count: tensors.len(),
                format: self.format_name().to_string(),
            });
        }

        // Write GGUF file sections
        writer.write_header(tensors)?;
        writer.write_tensor_infos(tensors)?;
        writer.write_tensors(tensors)?;
        writer.finalize()?;

        Ok(())
    }

    fn file_extension(&self) -> &str {
        "gguf"
    }

    fn format_name(&self) -> &str {
        "GGUF"
    }
}

/// Export model to GGUF format with quantization
pub fn export_to_gguf(
    tensors: &HashMap<String, Tensor>,
    path: &Path,
    options: GGUFExportOptions,
    save_options: &SaveOptions,
) -> Result<()> {
    let saver = GGUFSaver::new(options);
    saver.save_tensors(tensors, path, save_options)
}

/// Convenience function for F16 GGUF export
pub fn export_to_gguf_f16(
    tensors: &HashMap<String, Tensor>,
    path: &Path,
    architecture: &str,
    save_options: &SaveOptions,
) -> Result<()> {
    let options = GGUFExportOptions::new(architecture).with_quantization(GGUFQuantType::F16);
    export_to_gguf(tensors, path, options, save_options)
}

/// Convenience function for Q8_0 quantized GGUF export
pub fn export_to_gguf_q8_0(
    tensors: &HashMap<String, Tensor>,
    path: &Path,
    architecture: &str,
    save_options: &SaveOptions,
) -> Result<()> {
    let options = GGUFExportOptions::new(architecture).with_quantization(GGUFQuantType::Q8_0);
    export_to_gguf(tensors, path, options, save_options)
}

/// Convenience function for Q4_K_M quantized GGUF export
pub fn export_to_gguf_q4_k_m(
    tensors: &HashMap<String, Tensor>,
    path: &Path,
    architecture: &str,
    save_options: &SaveOptions,
) -> Result<()> {
    let options = GGUFExportOptions::new(architecture).with_quantization(GGUFQuantType::Q4_K_M);
    export_to_gguf(tensors, path, options, save_options)
}

/// Save a loaded model as GGUF format (conversion API wrapper)
///
/// # Arguments
///
/// * `model` - The loaded model to save
/// * `path` - Target file path
/// * `options` - Export configuration options
///
/// # Returns
///
/// Returns `Ok(())` on success, or `Err(MlmfError)` on failure.
pub fn save_as_gguf(
    model: &crate::LoadedModel,
    path: &Path,
    export_options: GgufExportOptions,
) -> crate::Result<()> {
    // Convert LoadedModel tensors to HashMap<String, Tensor>
    // For now, this is a placeholder since we need actual tensor data
    let tensors = std::collections::HashMap::new();

    let quantization = if let Some(ref quant) = export_options.quantization {
        match quant.as_str() {
            "q4_0" => GGUFQuantType::Q4_0,
            "q4_1" => GGUFQuantType::Q4_1,
            "q8_0" => GGUFQuantType::Q8_0,
            "q4_k_m" => GGUFQuantType::Q4_K_M,
            "q6_k" => GGUFQuantType::Q6_K,
            "f16" => GGUFQuantType::F16,
            "f32" => GGUFQuantType::F32,
            _ => {
                return Err(crate::Error::invalid_config(format!(
                    "Unsupported GGUF quantization: {}",
                    quant
                )))
            }
        }
    } else {
        GGUFQuantType::F16 // Default
    };

    let mut gguf_options = GGUFExportOptions::new("unknown").with_quantization(quantization);

    if export_options.preserve_metadata {
        // Would extract metadata from model.config - placeholder for now
        gguf_options = gguf_options.with_metadata(
            "preserved",
            crate::formats::gguf_export::MetadataValue::String("true".to_string()),
        );
    }

    for (key, value) in &export_options.custom_metadata {
        gguf_options = gguf_options.with_metadata(
            key,
            crate::formats::gguf_export::MetadataValue::String(value.clone()),
        );
    }

    let save_options = crate::saver::SaveOptions {
        progress_callback: None,
        compression: None,
        metadata: std::collections::HashMap::new(),
    };

    export_to_gguf(&tensors, path, gguf_options, &save_options)
        .map_err(|e| crate::Error::model_saving(format!("GGUF export failed: {}", e)))
}

/// Export options for GGUF format (conversion API compatibility)
#[derive(Debug, Clone, Default)]
pub struct GgufExportOptions {
    /// Whether to preserve original metadata
    pub preserve_metadata: bool,
    /// Custom metadata to add
    pub custom_metadata: HashMap<String, String>,
    /// Quantization method to apply (e.g., "q4_0", "q8_0")
    pub quantization: Option<String>,
    /// Whether to use memory mapping
    pub use_mmap: bool,
}