trustformers-core 0.2.1

Core traits and utilities for TrustformeRS
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
//! Reading pretrained weights from checkpoint files.
//!
//! Two formats are supported end to end:
//!
//! * **safetensors** via [`SafeTensorsReader`];
//! * **PyTorch** (`.bin`, `.pt`, `.pth`) via [`PyTorchReader`], which reads the real
//!   ZIP + pickle checkpoint with the pure-Rust implementation in
//!   [`zip_archive`], [`pickle`] and [`torch`].
//!
//! An earlier revision of `PyTorchReader` never parsed anything: it ran
//! `String::from_utf8_lossy` over the file, looked for fifteen hard-coded substrings
//! such as `"attention.self.query.weight"`, and handed back all-zero tensors with
//! guessed BERT-base dimensions — or, when even that failed, a fabricated ten-tensor
//! BERT state dict. Loading a checkpoint silently produced a zeroed model. All of
//! that is gone.

#[path = "weight_loading/pickle.rs"]
pub mod pickle;
#[path = "weight_loading/torch.rs"]
pub mod torch;
#[path = "weight_loading/zip_archive.rs"]
pub mod zip_archive;

use crate::errors::{Result, TrustformersError};
use crate::tensor::Tensor;
use crate::traits::WeightReader;
use safetensors::{SafeTensors, View};
use scirs2_core::ndarray::{ArrayD, IxDyn};
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::Path;

/// Convert IEEE 754 half-precision (F16) to single-precision (F32).
///
/// Delegates to `half::f16`, the same shared dtype helper used by
/// [`crate::quantization::ggml_advanced`] and
/// [`crate::quantization::gguf_k_quants`], instead of a third hand-rolled
/// bit-twiddling implementation. The previous version here duplicated (and
/// could silently drift from) the other two.
#[inline]
fn f16_to_f32(bits: u16) -> f32 {
    half::f16::from_bits(bits).to_f32()
}

pub struct SafeTensorsReader {
    data: Vec<u8>,
    tensors: HashMap<String, TensorInfo>,
}

#[derive(Debug)]
struct TensorInfo {
    dtype: String,
    shape: Vec<usize>,
    #[allow(dead_code)] // Reserved for future streaming/partial loading features
    data_offsets: (usize, usize),
}

impl SafeTensorsReader {
    pub fn from_file(path: &Path) -> Result<Self> {
        let mut file = File::open(path)?;
        let mut data = Vec::new();
        file.read_to_end(&mut data)?;

        let tensors = SafeTensors::deserialize(&data)
            .map_err(|e| TrustformersError::safe_tensors_error(e.to_string()))?;
        let mut tensor_map = HashMap::new();

        for (name, tensor_view) in tensors.tensors() {
            let info = TensorInfo {
                dtype: format!("{:?}", tensor_view.dtype()),
                shape: tensor_view.shape().to_vec(),
                data_offsets: (0, tensor_view.data_len()),
            };
            tensor_map.insert(name.to_string(), info);
        }

        Ok(Self {
            data,
            tensors: tensor_map,
        })
    }
}

impl WeightReader for SafeTensorsReader {
    fn read_tensor(&mut self, name: &str) -> Result<Tensor> {
        let info = self.tensors.get(name).ok_or_else(|| {
            TrustformersError::weight_load_error(format!("Tensor {} not found", name))
        })?;

        let tensors = SafeTensors::deserialize(&self.data)
            .map_err(|e| TrustformersError::safe_tensors_error(e.to_string()))?;
        let tensor_view = tensors
            .tensor(name)
            .map_err(|e| TrustformersError::safe_tensors_error(e.to_string()))?;

        match info.dtype.as_str() {
            "F32" => {
                let data = tensor_view.data();
                let values: Vec<f32> = data
                    .chunks_exact(4)
                    .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
                    .collect();

                let arr = ArrayD::from_shape_vec(IxDyn(&info.shape), values)
                    .map_err(|e| TrustformersError::shape_error(e.to_string()))?;
                Ok(Tensor::F32(arr))
            },
            "F16" => {
                // Convert F16 to F32
                let data = tensor_view.data();
                let values: Vec<f32> = data
                    .chunks_exact(2)
                    .map(|chunk| {
                        let bits = u16::from_le_bytes([chunk[0], chunk[1]]);
                        f16_to_f32(bits)
                    })
                    .collect();

                let arr = ArrayD::from_shape_vec(IxDyn(&info.shape), values)
                    .map_err(|e| TrustformersError::shape_error(e.to_string()))?;
                Ok(Tensor::F32(arr))
            },
            _ => Err(TrustformersError::weight_load_error(format!(
                "Unsupported dtype: {}",
                info.dtype
            ))),
        }
    }

    fn list_tensors(&self) -> Vec<String> {
        self.tensors.keys().cloned().collect()
    }
}

/// Reader for PyTorch checkpoints (`.pt`, `.bin`, `.pth`).
///
/// The file is parsed for real: the ZIP central directory is walked, `data.pkl` is
/// interpreted by a pickle virtual machine that records rather than executes the
/// `torch._utils._rebuild_tensor_v2` calls it finds, and each tensor is
/// reconstructed from its storage record with the checkpoint's own dtype, shape and
/// bytes.
///
/// # Errors
///
/// Construction fails, with an explanatory message, when the file is not a PyTorch
/// checkpoint, uses the legacy pre-1.6 non-ZIP layout, has deflate-compressed
/// members, contains no tensors, or holds a non-contiguous tensor view.
#[derive(Debug)]
pub struct PyTorchReader {
    state_dict: torch::TorchStateDict,
}

impl PyTorchReader {
    /// Parse a PyTorch checkpoint from disk.
    pub fn from_file(path: &Path) -> Result<Self> {
        let state_dict = torch::read_torch_file(path).map_err(|e| {
            TrustformersError::weight_load_error(format!("failed to read PyTorch file: {e}"))
        })?;
        Ok(Self { state_dict })
    }

    /// Parse a PyTorch checkpoint held in memory.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        let state_dict = torch::read_torch_bytes(bytes).map_err(|e| {
            TrustformersError::weight_load_error(format!("failed to read PyTorch data: {e}"))
        })?;
        Ok(Self { state_dict })
    }

    /// The parsed state dict.
    pub fn state_dict(&self) -> &torch::TorchStateDict {
        &self.state_dict
    }

    /// The element type a tensor had in the checkpoint.
    pub fn dtype_of(&self, name: &str) -> Option<torch::TorchDType> {
        self.state_dict.get(name).map(|tensor| tensor.dtype)
    }
}

impl WeightReader for PyTorchReader {
    fn read_tensor(&mut self, name: &str) -> Result<Tensor> {
        self.state_dict.get(name).map(|record| record.tensor.clone()).ok_or_else(|| {
            TrustformersError::weight_load_error(format!(
                "Tensor {name} not found in the checkpoint"
            ))
        })
    }

    fn list_tensors(&self) -> Vec<String> {
        self.state_dict.names()
    }
}

/// Report from [`WeightLoader::load_weights_into_model`].
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct WeightLoadReport {
    /// Parameters whose checkpoint tensor was found and copied in.
    pub loaded: Vec<String>,
    /// Parameters the checkpoint had no tensor for.
    pub missing: Vec<String>,
    /// Checkpoint tensors that matched no model parameter.
    pub unused: Vec<String>,
}

impl WeightLoadReport {
    /// Whether every model parameter was filled from the checkpoint.
    pub fn is_complete(&self) -> bool {
        self.missing.is_empty()
    }
}

/// Helpers for loading checkpoints into models.
pub struct WeightLoader;

impl WeightLoader {
    /// Copy a checkpoint's tensors into a model's live parameters.
    ///
    /// Parameters are matched by the names the model reports through
    /// [`Model::named_tensors_mut`](crate::traits::Model::named_tensors_mut).
    /// Every copy is shape-checked, so a mismatched checkpoint fails instead of
    /// leaving the model half-initialised.
    ///
    /// A previous revision serialised the tensors into an invented JSON envelope
    /// and handed that to `Model::load_pretrained`, which no model implementation
    /// could parse; the weights never reached the model.
    ///
    /// # Errors
    ///
    /// * The model exposes no named parameters (`named_tensors_mut` not implemented).
    /// * A checkpoint tensor's shape differs from the parameter it matches.
    /// * A tensor cannot be read from the checkpoint.
    pub fn load_weights_into_model<M>(
        model: &mut M,
        reader: &mut dyn WeightReader,
    ) -> Result<WeightLoadReport>
    where
        M: crate::traits::Model,
    {
        let available: Vec<String> = reader.list_tensors();
        let mut loaded_tensors: HashMap<String, Tensor> = HashMap::with_capacity(available.len());
        for name in &available {
            loaded_tensors.insert(name.clone(), reader.read_tensor(name)?);
        }

        let parameter_names: Vec<String> =
            model.named_tensors().into_iter().map(|(name, _)| name).collect();
        if parameter_names.is_empty() {
            return Err(TrustformersError::weight_load_error(
                "the model exposes no named parameters: `Model::named_tensors_mut` is not \
                 implemented for this type, so there is nowhere to put the checkpoint's tensors"
                    .to_string(),
            ));
        }

        let mut report = WeightLoadReport::default();
        for (name, parameter) in model.named_tensors_mut() {
            match loaded_tensors.get(&name) {
                Some(source) => {
                    if source.shape() != parameter.shape() {
                        return Err(TrustformersError::weight_load_error(format!(
                            "checkpoint tensor '{name}' has shape {:?} but the model parameter \
                             has shape {:?}",
                            source.shape(),
                            parameter.shape()
                        )));
                    }
                    *parameter = source.clone();
                    report.loaded.push(name);
                },
                None => report.missing.push(name),
            }
        }

        let matched: std::collections::HashSet<&String> = report.loaded.iter().collect();
        report.unused = available.into_iter().filter(|name| !matched.contains(name)).collect();

        report.loaded.sort();
        report.missing.sort();
        report.unused.sort();
        Ok(report)
    }

    /// Load weights from a SafeTensors file
    pub fn load_from_safetensors<P: AsRef<Path>>(path: P) -> Result<SafeTensorsReader> {
        SafeTensorsReader::from_file(path.as_ref())
    }

    /// List all available tensors in a SafeTensors file
    pub fn list_tensors_in_file<P: AsRef<Path>>(path: P) -> Result<Vec<String>> {
        let reader = SafeTensorsReader::from_file(path.as_ref())?;
        Ok(reader.list_tensors())
    }

    /// Load a specific tensor from a SafeTensors file
    pub fn load_tensor_from_file<P: AsRef<Path>>(path: P, tensor_name: &str) -> Result<Tensor> {
        let mut reader = SafeTensorsReader::from_file(path.as_ref())?;
        reader.read_tensor(tensor_name)
    }

    /// Load weights from a PyTorch file
    pub fn load_from_pytorch<P: AsRef<Path>>(path: P) -> Result<PyTorchReader> {
        PyTorchReader::from_file(path.as_ref())
    }

    /// List all available tensors in a PyTorch file
    pub fn list_tensors_in_pytorch_file<P: AsRef<Path>>(path: P) -> Result<Vec<String>> {
        let reader = PyTorchReader::from_file(path.as_ref())?;
        Ok(reader.list_tensors())
    }

    /// Load a specific tensor from a PyTorch file
    pub fn load_tensor_from_pytorch_file<P: AsRef<Path>>(
        path: P,
        tensor_name: &str,
    ) -> Result<Tensor> {
        let mut reader = PyTorchReader::from_file(path.as_ref())?;
        reader.read_tensor(tensor_name)
    }

    /// Auto-detect format and load weights
    pub fn load_weights_auto<P: AsRef<Path>>(path: P) -> Result<Box<dyn WeightReader>> {
        let path = path.as_ref();

        // Determine format by file extension
        if let Some(extension) = path.extension() {
            match extension.to_str().unwrap_or("").to_lowercase().as_str() {
                "safetensors" => {
                    let reader = SafeTensorsReader::from_file(path)?;
                    Ok(Box::new(reader))
                },
                "pt" | "pth" | "bin" => {
                    let reader = PyTorchReader::from_file(path)?;
                    Ok(Box::new(reader))
                },
                _ => Err(TrustformersError::weight_load_error(format!(
                    "Unsupported file format: {}",
                    extension.to_string_lossy()
                ))),
            }
        } else {
            Err(TrustformersError::weight_load_error(
                "Unable to determine file format from extension".into(),
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::torch::fixture::{build_checkpoint, FixtureTensor};
    use super::*;
    use crate::traits::{Config, Model};
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Serialize, Deserialize)]
    struct TinyConfig;

    impl Config for TinyConfig {
        fn architecture(&self) -> &'static str {
            "tiny"
        }
    }

    /// A model with two real parameters, so weight loading has somewhere to write.
    struct TinyModel {
        config: TinyConfig,
        weight: Tensor,
        bias: Tensor,
    }

    impl TinyModel {
        fn zeros() -> Self {
            Self {
                config: TinyConfig,
                weight: Tensor::zeros(&[2, 3]).expect("weight"),
                bias: Tensor::zeros(&[2]).expect("bias"),
            }
        }
    }

    impl Model for TinyModel {
        type Config = TinyConfig;
        type Input = Tensor;
        type Output = Tensor;

        fn forward(&self, input: Self::Input) -> Result<Self::Output> {
            Ok(input)
        }

        fn load_pretrained(&mut self, _reader: &mut dyn Read) -> Result<()> {
            Ok(())
        }

        fn get_config(&self) -> &Self::Config {
            &self.config
        }

        fn num_parameters(&self) -> usize {
            self.weight.len() + self.bias.len()
        }

        fn named_tensors(&self) -> Vec<(String, &Tensor)> {
            vec![
                ("weight".to_string(), &self.weight),
                ("bias".to_string(), &self.bias),
            ]
        }

        fn named_tensors_mut(&mut self) -> Vec<(String, &mut Tensor)> {
            vec![
                ("weight".to_string(), &mut self.weight),
                ("bias".to_string(), &mut self.bias),
            ]
        }
    }

    /// A model that does not implement the enumeration API.
    struct OpaqueModel {
        config: TinyConfig,
    }

    impl Model for OpaqueModel {
        type Config = TinyConfig;
        type Input = Tensor;
        type Output = Tensor;

        fn forward(&self, input: Self::Input) -> Result<Self::Output> {
            Ok(input)
        }

        fn load_pretrained(&mut self, _reader: &mut dyn Read) -> Result<()> {
            Ok(())
        }

        fn get_config(&self) -> &Self::Config {
            &self.config
        }

        fn num_parameters(&self) -> usize {
            0
        }
    }

    fn checkpoint_bytes(weight: &[f32], bias: &[f32]) -> Vec<u8> {
        build_checkpoint(
            "archive",
            &[
                FixtureTensor {
                    name: "weight".to_string(),
                    storage_class: "FloatStorage",
                    storage_key: "0".to_string(),
                    bytes: weight.iter().flat_map(|v| v.to_le_bytes()).collect(),
                    element_count: weight.len(),
                    storage_offset: 0,
                    shape: vec![2, 3],
                    stride: vec![3, 1],
                },
                FixtureTensor {
                    name: "bias".to_string(),
                    storage_class: "FloatStorage",
                    storage_key: "1".to_string(),
                    bytes: bias.iter().flat_map(|v| v.to_le_bytes()).collect(),
                    element_count: bias.len(),
                    storage_offset: 0,
                    shape: vec![2],
                    stride: vec![1],
                },
            ],
        )
    }

    fn temp_path(name: &str) -> std::path::PathBuf {
        let dir = std::env::temp_dir().join("trustformers_weight_loading_tests");
        std::fs::create_dir_all(&dir).expect("temp dir");
        dir.join(name)
    }

    /// Regression test: the old reader returned zeros with guessed BERT shapes.
    #[test]
    fn pytorch_reader_returns_the_checkpoints_real_values() {
        let weight: Vec<f32> = (0..6).map(|i| i as f32 * 0.5 - 1.0).collect();
        let bias = vec![2.0f32, -3.0];
        let path = temp_path("real_values.bin");
        std::fs::write(&path, checkpoint_bytes(&weight, &bias)).expect("write");

        let mut reader = PyTorchReader::from_file(&path).expect("read");
        let mut names = reader.list_tensors();
        names.sort();
        assert_eq!(names, vec!["bias".to_string(), "weight".to_string()]);

        let loaded = reader.read_tensor("weight").expect("weight");
        assert_eq!(loaded.shape(), vec![2, 3]);
        assert_eq!(loaded.to_vec_f32().expect("f32"), weight);
        assert!(
            loaded.to_vec_f32().expect("f32").iter().any(|v| *v != 0.0),
            "the old reader returned all zeros"
        );

        assert!(reader.read_tensor("nonexistent").is_err());
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn pytorch_reader_output_depends_on_the_file() {
        let path_a = temp_path("varies_a.bin");
        let path_b = temp_path("varies_b.bin");
        std::fs::write(&path_a, checkpoint_bytes(&[1.0; 6], &[1.0; 2])).expect("write");
        std::fs::write(&path_b, checkpoint_bytes(&[9.0; 6], &[9.0; 2])).expect("write");

        let mut a = PyTorchReader::from_file(&path_a).expect("read");
        let mut b = PyTorchReader::from_file(&path_b).expect("read");
        assert_ne!(
            a.read_tensor("weight").expect("a").to_vec_f32().expect("f32"),
            b.read_tensor("weight").expect("b").to_vec_f32().expect("f32")
        );

        let _ = std::fs::remove_file(&path_a);
        let _ = std::fs::remove_file(&path_b);
    }

    /// Regression test: the old reader invented a BERT state dict for any file.
    #[test]
    fn pytorch_reader_refuses_files_that_are_not_checkpoints() {
        let path = temp_path("not_a_checkpoint.bin");
        std::fs::write(&path, b"this file mentions state_dict and weight and bias").expect("write");

        let err = PyTorchReader::from_file(&path).expect_err("must not invent a state dict");
        let message = err.to_string();
        assert!(
            !message.is_empty() && message.contains("PyTorch"),
            "expected an explanatory error, got: {message}"
        );

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn pytorch_reader_records_the_source_dtype() {
        let path = temp_path("dtype.bin");
        std::fs::write(&path, checkpoint_bytes(&[0.0; 6], &[0.0; 2])).expect("write");

        let reader = PyTorchReader::from_file(&path).expect("read");
        assert_eq!(reader.dtype_of("weight"), Some(torch::TorchDType::F32));
        assert_eq!(reader.dtype_of("absent"), None);

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn load_weights_into_model_actually_writes_the_parameters() {
        let weight: Vec<f32> = (0..6).map(|i| i as f32 + 1.0).collect();
        let bias = vec![7.0f32, 8.0];
        let path = temp_path("into_model.bin");
        std::fs::write(&path, checkpoint_bytes(&weight, &bias)).expect("write");

        let mut model = TinyModel::zeros();
        assert!(model.weight.to_vec_f32().expect("f32").iter().all(|v| *v == 0.0));

        let mut reader = PyTorchReader::from_file(&path).expect("read");
        let report = WeightLoader::load_weights_into_model(&mut model, &mut reader).expect("load");

        assert!(report.is_complete(), "missing: {:?}", report.missing);
        assert_eq!(
            report.loaded,
            vec!["bias".to_string(), "weight".to_string()]
        );
        assert!(report.unused.is_empty());
        assert_eq!(model.weight.to_vec_f32().expect("f32"), weight);
        assert_eq!(model.bias.to_vec_f32().expect("f32"), bias);

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn load_weights_into_model_rejects_a_model_without_named_parameters() {
        let path = temp_path("opaque.bin");
        std::fs::write(&path, checkpoint_bytes(&[0.0; 6], &[0.0; 2])).expect("write");

        let mut model = OpaqueModel { config: TinyConfig };
        let mut reader = PyTorchReader::from_file(&path).expect("read");
        let err = WeightLoader::load_weights_into_model(&mut model, &mut reader)
            .expect_err("nowhere to load into");
        assert!(err.to_string().contains("named_tensors_mut"), "{err}");

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn load_weights_into_model_rejects_shape_mismatches() {
        // A checkpoint whose "weight" is [3, 2] rather than the model's [2, 3].
        let values: Vec<f32> = (0..6).map(|i| i as f32).collect();
        let bytes = build_checkpoint(
            "archive",
            &[FixtureTensor {
                name: "weight".to_string(),
                storage_class: "FloatStorage",
                storage_key: "0".to_string(),
                bytes: values.iter().flat_map(|v| v.to_le_bytes()).collect(),
                element_count: 6,
                storage_offset: 0,
                shape: vec![3, 2],
                stride: vec![2, 1],
            }],
        );
        let path = temp_path("shape_mismatch.bin");
        std::fs::write(&path, bytes).expect("write");

        let mut model = TinyModel::zeros();
        let mut reader = PyTorchReader::from_file(&path).expect("read");
        let err = WeightLoader::load_weights_into_model(&mut model, &mut reader)
            .expect_err("shape mismatch");
        assert!(err.to_string().contains("shape"), "{err}");

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn load_weights_into_model_reports_missing_and_unused_tensors() {
        let values: Vec<f32> = (0..6).map(|i| i as f32).collect();
        let bytes = build_checkpoint(
            "archive",
            &[
                FixtureTensor {
                    name: "weight".to_string(),
                    storage_class: "FloatStorage",
                    storage_key: "0".to_string(),
                    bytes: values.iter().flat_map(|v| v.to_le_bytes()).collect(),
                    element_count: 6,
                    storage_offset: 0,
                    shape: vec![2, 3],
                    stride: vec![3, 1],
                },
                FixtureTensor {
                    name: "extra".to_string(),
                    storage_class: "FloatStorage",
                    storage_key: "1".to_string(),
                    bytes: vec![0u8; 4],
                    element_count: 1,
                    storage_offset: 0,
                    shape: vec![1],
                    stride: vec![1],
                },
            ],
        );
        let path = temp_path("partial.bin");
        std::fs::write(&path, bytes).expect("write");

        let mut model = TinyModel::zeros();
        let mut reader = PyTorchReader::from_file(&path).expect("read");
        let report = WeightLoader::load_weights_into_model(&mut model, &mut reader).expect("load");

        assert_eq!(report.loaded, vec!["weight".to_string()]);
        assert_eq!(report.missing, vec!["bias".to_string()]);
        assert_eq!(report.unused, vec!["extra".to_string()]);
        assert!(!report.is_complete());

        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn load_weights_auto_dispatches_on_the_extension() {
        let path = temp_path("auto.bin");
        std::fs::write(&path, checkpoint_bytes(&[1.0; 6], &[1.0; 2])).expect("write");

        let reader = WeightLoader::load_weights_auto(&path).expect("auto");
        assert_eq!(reader.list_tensors().len(), 2);

        let unknown = temp_path("auto.unknown");
        std::fs::write(&unknown, b"x").expect("write");
        assert!(WeightLoader::load_weights_auto(&unknown).is_err());

        let _ = std::fs::remove_file(&path);
        let _ = std::fs::remove_file(&unknown);
    }

    /// `f16_to_f32` now delegates to `half::f16` instead of a third hand-rolled
    /// bit-twiddler (the other two live in `quantization/ggml_advanced.rs` and
    /// `quantization/gguf_k_quants.rs`). The old version already got every
    /// *finite* value right, but for NaN it always returned the canonical
    /// `f32::NAN` bit pattern, discarding the half-precision sign and payload.
    /// `half::f16` performs a real bit-correct widening (payload shifted into
    /// the wider mantissa, sign preserved), which the hand-rolled version
    /// could never reproduce.
    #[test]
    fn f16_to_f32_matches_the_shared_half_precision_decoder() {
        // Representative finite values.
        assert_eq!(f16_to_f32(0x0000), 0.0);
        assert!(
            f16_to_f32(0x8000).is_sign_negative(),
            "negative zero must keep its sign"
        );
        assert_eq!(f16_to_f32(0x3C00), 1.0);
        assert_eq!(f16_to_f32(0xC000), -2.0);
        // Smallest positive subnormal half value: 2^-24.
        assert_eq!(f16_to_f32(0x0001), 2.0f32.powi(-24));
        assert_eq!(f16_to_f32(0x7C00), f32::INFINITY);
        assert_eq!(f16_to_f32(0xFC00), f32::NEG_INFINITY);

        // The bit the old hand-rolled decoder got wrong: a negative,
        // payload-carrying NaN (sign=1, exponent=0x1F, mantissa=0x123) must
        // keep its sign after widening to f32. The previous implementation
        // ignored the input entirely for any NaN and always produced the
        // canonical *positive* `f32::NAN`, so this assertion would have
        // failed against the old code.
        let negative_nan_bits: u16 = 0xFD23;
        let widened = f16_to_f32(negative_nan_bits);
        assert!(widened.is_nan(), "must still decode to a NaN");
        assert!(
            widened.is_sign_negative(),
            "half::f16 preserves the sign bit through NaN widening"
        );
    }
}