oxigaf-flame 0.1.0

FLAME parametric head model — LBS, normal maps, mesh sampling
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
//! Load and save FLAME models using the safetensors format.
//!
//! This module provides an alternative to NPY files for storing FLAME models.
//! Safetensors is a modern, efficient format widely used in ML frameworks.
//!
//! ## Advantages of safetensors
//!
//! - **Single file**: All model data in one file instead of multiple `.npy` files
//! - **Metadata support**: Store model version, source, creation date, etc.
//! - **Fast loading**: Memory-mapped access for large models
//! - **Cross-platform**: Better compatibility than pickle-based formats
//! - **Type-safe**: Built-in validation of tensor shapes and dtypes
//!
//! ## Format
//!
//! The safetensors file contains the following tensors:
//!
//! | Tensor Name      | Shape              | dtype   |
//! |------------------|--------------------|---------|
//! | `v_template`     | `[5023, 3]`        | float32 |
//! | `faces`          | `[9976, 3]`        | int32   |
//! | `shapedirs`      | `[5023, 3, 300]`   | float32 |
//! | `expressiondirs` | `[5023, 3, 100]`   | float32 |
//! | `posedirs`       | `[5023, 3, 36]`    | float32 |
//! | `j_regressor`    | `[5, 5023]`        | float32 |
//! | `kintree_table`  | `[2, 5]`           | int32   |
//! | `lbs_weights`    | `[5023, 5]`        | float32 |
//!
//! Plus optional metadata:
//! - `__metadata__`: JSON object with model info (version, source, date, etc.)

use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;

use ndarray::{Array2, Array3};
use safetensors::tensor::{Dtype, TensorView};
use safetensors::SafeTensors;

use crate::error::FlameError;
use crate::model::FlameModel;

/// Load a [`FlameModel`] from a safetensors file.
///
/// # Arguments
///
/// * `path` - Path to the safetensors file
///
/// # Errors
///
/// Returns an error if:
/// - The file does not exist or cannot be read
/// - The file is not a valid safetensors file
/// - Required tensors are missing
/// - Tensor shapes do not match expected dimensions
///
/// # Example
///
/// ```rust,no_run
/// use oxigaf_flame::io_safetensors::load_flame_model_safetensors;
/// use std::path::Path;
///
/// let model = load_flame_model_safetensors(Path::new("flame_model.safetensors"))?;
/// println!("Loaded FLAME model with {} vertices", model.num_vertices());
/// # Ok::<(), oxigaf_flame::FlameError>(())
/// ```
pub fn load_flame_model_safetensors(path: &Path) -> Result<FlameModel, FlameError> {
    tracing::debug!("Loading FLAME model from safetensors: {}", path.display());

    // Read the entire file into memory
    let buffer = std::fs::read(path).map_err(|e| FlameError::IoError {
        source: e,
        path: path.to_path_buf(),
    })?;

    // Deserialize safetensors
    let tensors = SafeTensors::deserialize(&buffer).map_err(|e| FlameError::SafeTensorsLoad {
        path: path.to_path_buf(),
        message: e.to_string(),
    })?;

    // Load each tensor
    let v_template = load_tensor_f32_2d(&tensors, "v_template")?;
    let faces_i32 = load_tensor_i32_2d(&tensors, "faces")?;
    let shapedirs = load_tensor_f32_3d(&tensors, "shapedirs")?;
    let expressiondirs = load_tensor_f32_3d(&tensors, "expressiondirs")?;
    let posedirs = load_tensor_f32_3d(&tensors, "posedirs")?;
    let j_regressor = load_tensor_f32_2d(&tensors, "j_regressor")?;
    let kintree_i32 = load_tensor_i32_2d(&tensors, "kintree_table")?;
    let lbs_weights = load_tensor_f32_2d(&tensors, "lbs_weights")?;

    // Convert faces from i32 → Vec<[u32; 3]>
    let faces: Vec<[u32; 3]> = faces_i32
        .rows()
        .into_iter()
        .map(|row| [row[0] as u32, row[1] as u32, row[2] as u32])
        .collect();

    // Extract parent indices from kintree_table row 0
    let n_joints = kintree_i32.ncols();
    let parents: Vec<i32> = (0..n_joints).map(|j| kintree_i32[[0, j]]).collect();

    // Validate shapes
    let n_verts = v_template.nrows();

    tracing::info!(
        n_verts,
        n_faces = faces.len(),
        n_joints,
        n_shape = shapedirs.shape()[2],
        n_expr = expressiondirs.shape()[2],
        "FLAME model loaded from safetensors"
    );

    Ok(FlameModel {
        v_template,
        faces,
        shapedirs,
        expressiondirs,
        posedirs,
        j_regressor,
        parents,
        lbs_weights,
        n_joints,
    })
}

/// Save a [`FlameModel`] to a safetensors file.
///
/// # Arguments
///
/// * `model` - The FLAME model to save
/// * `path` - Output path for the safetensors file
/// * `metadata` - Optional metadata to include (version, source, etc.)
///
/// # Errors
///
/// Returns an error if the file cannot be written.
///
/// # Example
///
/// ```rust,no_run
/// use oxigaf_flame::{FlameModel, io_safetensors::save_flame_model_safetensors};
/// use std::path::Path;
/// use std::collections::HashMap;
///
/// # fn load_model() -> Result<FlameModel, Box<dyn std::error::Error>> {
/// #     todo!()
/// # }
/// let model = load_model()?;
///
/// let mut metadata = HashMap::new();
/// metadata.insert("version".to_string(), "1.0".to_string());
/// metadata.insert("source".to_string(), "FLAME 2020".to_string());
///
/// save_flame_model_safetensors(&model, Path::new("flame_model.safetensors"), Some(&metadata))?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[allow(clippy::implicit_hasher)]
pub fn save_flame_model_safetensors(
    model: &FlameModel,
    path: &Path,
    metadata: Option<&HashMap<String, String>>,
) -> Result<(), FlameError> {
    tracing::debug!("Saving FLAME model to safetensors: {}", path.display());

    // Extract model data into slices
    let slices = extract_model_slices(model, path)?;

    // Create tensor views
    let tensors = create_tensor_views(model, &slices, path)?;

    // Serialize and write to file
    write_safetensors_to_file(tensors, metadata, path)?;

    tracing::info!("Successfully saved FLAME model to safetensors");

    Ok(())
}

// ---------------------------------------------------------------------------
// Helper functions for saving
// ---------------------------------------------------------------------------

/// Holds references to model data slices for serialization.
struct ModelDataSlices<'a> {
    v_template: &'a [f32],
    shapedirs: &'a [f32],
    expressiondirs: &'a [f32],
    posedirs: &'a [f32],
    j_regressor: &'a [f32],
    lbs_weights: &'a [f32],
    faces_i32: Vec<i32>,
    kintree_i32: Vec<i32>,
}

/// Extract and convert model data to slices for serialization.
fn extract_model_slices<'a>(
    model: &'a FlameModel,
    path: &Path,
) -> Result<ModelDataSlices<'a>, FlameError> {
    let v_template = model
        .v_template
        .as_slice()
        .ok_or_else(|| FlameError::SafeTensorsSave {
            path: path.to_path_buf(),
            message: "v_template is not contiguous".to_string(),
        })?;

    let shapedirs = model
        .shapedirs
        .as_slice()
        .ok_or_else(|| FlameError::SafeTensorsSave {
            path: path.to_path_buf(),
            message: "shapedirs is not contiguous".to_string(),
        })?;

    let expressiondirs =
        model
            .expressiondirs
            .as_slice()
            .ok_or_else(|| FlameError::SafeTensorsSave {
                path: path.to_path_buf(),
                message: "expressiondirs is not contiguous".to_string(),
            })?;

    let posedirs = model
        .posedirs
        .as_slice()
        .ok_or_else(|| FlameError::SafeTensorsSave {
            path: path.to_path_buf(),
            message: "posedirs is not contiguous".to_string(),
        })?;

    let j_regressor = model
        .j_regressor
        .as_slice()
        .ok_or_else(|| FlameError::SafeTensorsSave {
            path: path.to_path_buf(),
            message: "j_regressor is not contiguous".to_string(),
        })?;

    let lbs_weights = model
        .lbs_weights
        .as_slice()
        .ok_or_else(|| FlameError::SafeTensorsSave {
            path: path.to_path_buf(),
            message: "lbs_weights is not contiguous".to_string(),
        })?;

    // Convert faces to i32 array for serialization
    let faces_i32: Vec<i32> = model
        .faces
        .iter()
        .flat_map(|face| face.iter().map(|&idx| idx.cast_signed()))
        .collect();

    // Convert parents to kintree_table format
    let kintree_i32: Vec<i32> = model.parents.clone();

    Ok(ModelDataSlices {
        v_template,
        shapedirs,
        expressiondirs,
        posedirs,
        j_regressor,
        lbs_weights,
        faces_i32,
        kintree_i32,
    })
}

/// Create tensor views from model data for safetensors serialization.
#[allow(clippy::type_complexity)]
fn create_tensor_views<'a>(
    model: &FlameModel,
    slices: &'a ModelDataSlices<'a>,
    path: &Path,
) -> Result<Vec<(&'static str, TensorView<'a>)>, FlameError> {
    // Convert f32 slices to bytes
    let v_template_bytes = bytemuck::cast_slice(slices.v_template);
    let shapedirs_bytes = bytemuck::cast_slice(slices.shapedirs);
    let expressiondirs_bytes = bytemuck::cast_slice(slices.expressiondirs);
    let posedirs_bytes = bytemuck::cast_slice(slices.posedirs);
    let j_regressor_bytes = bytemuck::cast_slice(slices.j_regressor);
    let lbs_weights_bytes = bytemuck::cast_slice(slices.lbs_weights);
    let faces_bytes = bytemuck::cast_slice(&slices.faces_i32);
    let kintree_bytes = bytemuck::cast_slice(&slices.kintree_i32);

    // Create tensor views
    let tensors = vec![
        (
            "v_template",
            TensorView::new(
                Dtype::F32,
                model.v_template.shape().to_vec(),
                v_template_bytes,
            )
            .map_err(|e| FlameError::SafeTensorsSave {
                path: path.to_path_buf(),
                message: e.to_string(),
            })?,
        ),
        (
            "faces",
            TensorView::new(Dtype::I32, vec![model.faces.len(), 3], faces_bytes).map_err(|e| {
                FlameError::SafeTensorsSave {
                    path: path.to_path_buf(),
                    message: e.to_string(),
                }
            })?,
        ),
        (
            "shapedirs",
            TensorView::new(
                Dtype::F32,
                model.shapedirs.shape().to_vec(),
                shapedirs_bytes,
            )
            .map_err(|e| FlameError::SafeTensorsSave {
                path: path.to_path_buf(),
                message: e.to_string(),
            })?,
        ),
        (
            "expressiondirs",
            TensorView::new(
                Dtype::F32,
                model.expressiondirs.shape().to_vec(),
                expressiondirs_bytes,
            )
            .map_err(|e| FlameError::SafeTensorsSave {
                path: path.to_path_buf(),
                message: e.to_string(),
            })?,
        ),
        (
            "posedirs",
            TensorView::new(Dtype::F32, model.posedirs.shape().to_vec(), posedirs_bytes).map_err(
                |e| FlameError::SafeTensorsSave {
                    path: path.to_path_buf(),
                    message: e.to_string(),
                },
            )?,
        ),
        (
            "j_regressor",
            TensorView::new(
                Dtype::F32,
                model.j_regressor.shape().to_vec(),
                j_regressor_bytes,
            )
            .map_err(|e| FlameError::SafeTensorsSave {
                path: path.to_path_buf(),
                message: e.to_string(),
            })?,
        ),
        (
            "kintree_table",
            TensorView::new(Dtype::I32, vec![1, model.n_joints], kintree_bytes).map_err(
                |e: safetensors::SafeTensorError| FlameError::SafeTensorsSave {
                    path: path.to_path_buf(),
                    message: e.to_string(),
                },
            )?,
        ),
        (
            "lbs_weights",
            TensorView::new(
                Dtype::F32,
                model.lbs_weights.shape().to_vec(),
                lbs_weights_bytes,
            )
            .map_err(|e: safetensors::SafeTensorError| {
                FlameError::SafeTensorsSave {
                    path: path.to_path_buf(),
                    message: e.to_string(),
                }
            })?,
        ),
    ];

    Ok(tensors)
}

/// Serialize and write safetensors data to file.
fn write_safetensors_to_file(
    tensors: Vec<(&str, TensorView<'_>)>,
    metadata: Option<&HashMap<String, String>>,
    path: &Path,
) -> Result<(), FlameError> {
    // Serialize with optional metadata
    let metadata_owned = metadata.cloned();
    let serialized = safetensors::tensor::serialize(tensors, metadata_owned).map_err(
        |e: safetensors::SafeTensorError| FlameError::SafeTensorsSave {
            path: path.to_path_buf(),
            message: e.to_string(),
        },
    )?;

    // Write to file
    let mut file = File::create(path).map_err(|e| FlameError::IoError {
        source: e,
        path: path.to_path_buf(),
    })?;
    file.write_all(&serialized)
        .map_err(|e| FlameError::IoError {
            source: e,
            path: path.to_path_buf(),
        })?;
    file.flush().map_err(|e| FlameError::IoError {
        source: e,
        path: path.to_path_buf(),
    })?;

    Ok(())
}

// ---------------------------------------------------------------------------
// Helper functions for loading
// ---------------------------------------------------------------------------

/// Load a 2D f32 tensor from safetensors
fn load_tensor_f32_2d(tensors: &SafeTensors, name: &str) -> Result<Array2<f32>, FlameError> {
    let tensor_view = tensors
        .tensor(name)
        .map_err(
            |e: safetensors::SafeTensorError| FlameError::SafeTensorsMissing {
                name: name.to_string(),
                message: e.to_string(),
            },
        )?;

    // Verify dtype
    if tensor_view.dtype() != safetensors::Dtype::F32 {
        return Err(FlameError::SafeTensorsInvalidDtype {
            name: name.to_string(),
            expected: "F32".to_string(),
            got: format!("{:?}", tensor_view.dtype()),
        });
    }

    // Get shape
    let shape = tensor_view.shape();
    if shape.len() != 2 {
        return Err(FlameError::ShapeMismatch {
            name: name.to_string(),
            expected: "2D array".to_string(),
            got: format!("{shape:?}"),
        });
    }

    // Convert bytes to f32 slice
    let data_bytes = tensor_view.data();
    let data_f32: &[f32] = bytemuck::cast_slice(data_bytes);

    // Create ndarray
    Array2::from_shape_vec((shape[0], shape[1]), data_f32.to_vec()).map_err(|e| {
        FlameError::ShapeMismatch {
            name: name.to_string(),
            expected: format!("{shape:?}"),
            got: e.to_string(),
        }
    })
}

/// Load a 3D f32 tensor from safetensors
fn load_tensor_f32_3d(tensors: &SafeTensors, name: &str) -> Result<Array3<f32>, FlameError> {
    let tensor_view = tensors
        .tensor(name)
        .map_err(
            |e: safetensors::SafeTensorError| FlameError::SafeTensorsMissing {
                name: name.to_string(),
                message: e.to_string(),
            },
        )?;

    // Verify dtype
    if tensor_view.dtype() != safetensors::Dtype::F32 {
        return Err(FlameError::SafeTensorsInvalidDtype {
            name: name.to_string(),
            expected: "F32".to_string(),
            got: format!("{:?}", tensor_view.dtype()),
        });
    }

    // Get shape
    let shape = tensor_view.shape();
    if shape.len() != 3 {
        return Err(FlameError::ShapeMismatch {
            name: name.to_string(),
            expected: "3D array".to_string(),
            got: format!("{shape:?}"),
        });
    }

    // Convert bytes to f32 slice
    let data_bytes = tensor_view.data();
    let data_f32: &[f32] = bytemuck::cast_slice(data_bytes);

    // Create ndarray
    Array3::from_shape_vec((shape[0], shape[1], shape[2]), data_f32.to_vec()).map_err(|e| {
        FlameError::ShapeMismatch {
            name: name.to_string(),
            expected: format!("{shape:?}"),
            got: e.to_string(),
        }
    })
}

/// Load a 2D i32 tensor from safetensors
fn load_tensor_i32_2d(tensors: &SafeTensors, name: &str) -> Result<Array2<i32>, FlameError> {
    let tensor_view = tensors
        .tensor(name)
        .map_err(
            |e: safetensors::SafeTensorError| FlameError::SafeTensorsMissing {
                name: name.to_string(),
                message: e.to_string(),
            },
        )?;

    // Verify dtype
    if tensor_view.dtype() != safetensors::Dtype::I32 {
        return Err(FlameError::SafeTensorsInvalidDtype {
            name: name.to_string(),
            expected: "I32".to_string(),
            got: format!("{:?}", tensor_view.dtype()),
        });
    }

    // Get shape
    let shape = tensor_view.shape();
    if shape.len() != 2 {
        return Err(FlameError::ShapeMismatch {
            name: name.to_string(),
            expected: "2D array".to_string(),
            got: format!("{shape:?}"),
        });
    }

    // Convert bytes to i32 slice
    let data_bytes = tensor_view.data();
    let data_i32: &[i32] = bytemuck::cast_slice(data_bytes);

    // Create ndarray
    Array2::from_shape_vec((shape[0], shape[1]), data_i32.to_vec()).map_err(|e| {
        FlameError::ShapeMismatch {
            name: name.to_string(),
            expected: format!("{shape:?}"),
            got: e.to_string(),
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use ndarray::{Array2, Array3};
    use std::collections::HashMap;
    use tempfile::TempDir;

    fn create_minimal_flame_model() -> FlameModel {
        // Create a minimal FLAME model for testing
        let n_verts = 10;
        let n_faces = 5;
        let n_joints = 5;
        let n_shape = 3;
        let n_expr = 2;
        let n_pose_dirs = 4;

        FlameModel {
            v_template: Array2::zeros((n_verts, 3)),
            faces: vec![[0, 1, 2]; n_faces],
            shapedirs: Array3::zeros((n_verts, 3, n_shape)),
            expressiondirs: Array3::zeros((n_verts, 3, n_expr)),
            posedirs: Array3::zeros((n_verts, 3, n_pose_dirs)),
            j_regressor: Array2::zeros((n_joints, n_verts)),
            parents: vec![-1, 0, 1, 2, 3],
            lbs_weights: Array2::zeros((n_verts, n_joints)),
            n_joints,
        }
    }

    #[test]
    fn test_save_and_load_round_trip() {
        let temp_dir = TempDir::new().expect("test: temp dir creation should succeed");
        let safetensors_path = temp_dir.path().join("test_model.safetensors");

        // Create test model
        let model = create_minimal_flame_model();

        // Save to safetensors
        save_flame_model_safetensors(&model, &safetensors_path, None)
            .expect("test: save should succeed");

        // Load back
        let loaded_model =
            load_flame_model_safetensors(&safetensors_path).expect("test: load should succeed");

        // Verify shapes match
        assert_eq!(loaded_model.v_template.shape(), model.v_template.shape());
        assert_eq!(loaded_model.faces.len(), model.faces.len());
        assert_eq!(loaded_model.shapedirs.shape(), model.shapedirs.shape());
        assert_eq!(
            loaded_model.expressiondirs.shape(),
            model.expressiondirs.shape()
        );
        assert_eq!(loaded_model.posedirs.shape(), model.posedirs.shape());
        assert_eq!(loaded_model.j_regressor.shape(), model.j_regressor.shape());
        assert_eq!(loaded_model.parents.len(), model.parents.len());
        assert_eq!(loaded_model.lbs_weights.shape(), model.lbs_weights.shape());
        assert_eq!(loaded_model.n_joints, model.n_joints);
    }

    #[test]
    fn test_metadata_preservation() {
        let temp_dir = TempDir::new().expect("test: temp dir creation should succeed");
        let safetensors_path = temp_dir.path().join("test_model_meta.safetensors");

        // Create test model
        let model = create_minimal_flame_model();

        // Add metadata
        let mut metadata = HashMap::new();
        metadata.insert("version".to_string(), "1.0".to_string());
        metadata.insert("source".to_string(), "test".to_string());
        metadata.insert("author".to_string(), "oxigaf-flame".to_string());

        // Save with metadata
        save_flame_model_safetensors(&model, &safetensors_path, Some(&metadata))
            .expect("test: save should succeed");

        // Verify file was created and can be loaded
        assert!(safetensors_path.exists());
        let loaded_model =
            load_flame_model_safetensors(&safetensors_path).expect("test: load should succeed");

        // Verify model structure
        assert_eq!(loaded_model.num_vertices(), model.num_vertices());
        assert_eq!(loaded_model.n_joints, model.n_joints);
    }

    #[test]
    fn test_save_with_non_contiguous_arrays() {
        let temp_dir = TempDir::new().expect("test: temp dir creation should succeed");
        let safetensors_path = temp_dir.path().join("test_model_slice.safetensors");

        // Create test model with potentially non-contiguous arrays
        let mut model = create_minimal_flame_model();

        // Make array contiguous by cloning (this is what as_slice requires)
        model.v_template = model.v_template.as_standard_layout().into_owned();

        // Should succeed with contiguous arrays
        let result = save_flame_model_safetensors(&model, &safetensors_path, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_load_missing_file() {
        let temp_dir = TempDir::new().expect("test: temp dir creation should succeed");
        let missing_path = temp_dir.path().join("nonexistent.safetensors");

        let result = load_flame_model_safetensors(&missing_path);
        assert!(result.is_err());

        if let Err(FlameError::IoError { source: _, path }) = result {
            assert_eq!(path, missing_path);
        } else {
            panic!("Expected IoError");
        }
    }

    #[test]
    fn test_round_trip_preserves_data() {
        let temp_dir = TempDir::new().expect("test: temp dir creation should succeed");
        let safetensors_path = temp_dir.path().join("test_data_preservation.safetensors");

        // Create model with specific values
        let mut model = create_minimal_flame_model();
        model.v_template[[0, 0]] = 1.5;
        model.v_template[[0, 1]] = -2.3;
        model.v_template[[1, 2]] = 0.7;
        model.shapedirs[[2, 1, 0]] = std::f32::consts::PI;
        model.parents[2] = 1;

        // Save and load
        save_flame_model_safetensors(&model, &safetensors_path, None)
            .expect("test: save should succeed");
        let loaded =
            load_flame_model_safetensors(&safetensors_path).expect("test: load should succeed");

        // Verify data preservation
        assert!((loaded.v_template[[0, 0]] - 1.5).abs() < 1e-6);
        assert!((loaded.v_template[[0, 1]] - (-2.3)).abs() < 1e-6);
        assert!((loaded.v_template[[1, 2]] - 0.7).abs() < 1e-6);
        assert!((loaded.shapedirs[[2, 1, 0]] - std::f32::consts::PI).abs() < 1e-6);
        assert_eq!(loaded.parents[2], 1);
    }

    #[test]
    fn test_faces_conversion() {
        let temp_dir = TempDir::new().expect("test: temp dir creation should succeed");
        let safetensors_path = temp_dir.path().join("test_faces.safetensors");

        // Create model with specific face indices
        let mut model = create_minimal_flame_model();
        model.faces = vec![[0, 1, 2], [3, 4, 5], [6, 7, 8]];

        // Save and load
        save_flame_model_safetensors(&model, &safetensors_path, None)
            .expect("test: save should succeed");
        let loaded =
            load_flame_model_safetensors(&safetensors_path).expect("test: load should succeed");

        // Verify faces preserved correctly
        assert_eq!(loaded.faces.len(), 3);
        assert_eq!(loaded.faces[0], [0, 1, 2]);
        assert_eq!(loaded.faces[1], [3, 4, 5]);
        assert_eq!(loaded.faces[2], [6, 7, 8]);
    }
}