use super::discovery::{classify_tensor, discover_candidates};
use super::manifest::{
INDEX_UNREFERENCED_SHARDS_KEY, build_manifest, merge_shard_metadata,
shard_metadata_namespaced_key,
};
use super::paths::{canonical_existing_or_parent, parent_or_current};
use super::*;
use crate::ParserError;
use std::collections::BTreeMap;
use std::fs::{self, File};
use std::io::Write;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(feature = "mmap")]
#[test]
fn mmap_upstream_failures_keep_distinct_public_error_categories() {
use ::safetensors::SafeTensorError as E;
let path = Path::new("bad.safetensors");
let missing = super::map::safetensors_error(path, E::TensorNotFound("lost".into()));
assert!(matches!(
missing,
ParserError::MissingTensor { name, path } if name == "lost" && path == "bad.safetensors"
));
let invalid_utf8 = String::from_utf8(vec![0xff]).unwrap_err().utf8_error();
let malformed = super::map::safetensors_error(path, E::InvalidHeader(invalid_utf8));
assert!(matches!(
malformed,
ParserError::UnsupportedFormat { path, .. } if path == "bad.safetensors"
));
let invalid_offsets = super::map::safetensors_error(path, E::InvalidOffset("w".into()));
assert!(matches!(
invalid_offsets,
ParserError::InvalidLayout { path, .. } if path == "bad.safetensors"
));
let io = super::map::safetensors_error(
path,
E::IoError(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"truncated",
)),
);
assert!(matches!(
io,
ParserError::Io { path, source }
if path == "bad.safetensors" && source.kind() == std::io::ErrorKind::UnexpectedEof
));
}
struct TestDir(PathBuf);
impl Deref for TestDir {
type Target = Path;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<Path> for TestDir {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
fn temp_dir(name: &str) -> TestDir {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let path = std::env::temp_dir().join(format!(
"engram-safetensors-{name}-{}-{nanos}",
std::process::id()
));
fs::create_dir_all(&path).unwrap();
TestDir(path)
}
fn write_safetensors(path: &Path, header: &str, data_len: usize) {
let mut file = File::create(path).unwrap();
file.write_all(&(header.len() as u64).to_le_bytes())
.unwrap();
file.write_all(header.as_bytes()).unwrap();
file.write_all(&vec![0u8; data_len]).unwrap();
}
#[test]
fn parses_single_file_manifest_and_labels_candidates() {
let dir = temp_dir("single");
let path = dir.join("model.safetensors");
write_safetensors(
&path,
r#"{
"__metadata__": {"format": "pt"},
"model.layers.0.mlp.gate.weight": {"dtype": "F32", "shape": [64, 2048], "data_offsets": [0, 524288]},
"model.layers.0.mlp.experts.0.w1.weight": {"dtype": "F16", "shape": [2048, 4096], "data_offsets": [524288, 17301504]}
}"#,
17_301_504,
);
let manifest = inspect_safetensors_checkpoint(&path).unwrap();
assert_eq!(manifest.checkpoint.input_kind, "single_file");
assert_eq!(manifest.checkpoint.shard_count, 1);
assert_eq!(manifest.checkpoint.tensor_count, 2);
assert_eq!(manifest.checkpoint.metadata.get("format").unwrap(), "pt");
assert_eq!(
manifest.tensors[0].name,
"model.layers.0.mlp.experts.0.w1.weight"
);
assert_eq!(manifest.tensors[0].source_shard, "model.safetensors");
assert_eq!(manifest.candidates.router_tensors.len(), 1);
assert_eq!(manifest.candidates.expert_tensors.len(), 1);
assert_eq!(
manifest.candidates.detected_layout_family,
Some("generic_moe")
);
assert_eq!(manifest.candidates.router_candidates[0].layer_hint, Some(0));
assert_eq!(manifest.candidates.expert_groups.len(), 1);
assert_eq!(manifest.candidates.expert_groups[0].layer_hint, Some(0));
assert_eq!(manifest.candidates.expert_groups[0].expert_indices, vec![0]);
}
#[test]
fn reads_hugging_face_index_and_orders_deterministically() {
let dir = temp_dir("index");
let shard_a = dir.join("model-00001-of-00002.safetensors");
let shard_b = dir.join("model-00002-of-00002.safetensors");
write_safetensors(
&shard_b,
r#"{"z.weight": {"dtype": "F16", "shape": [2, 2], "data_offsets": [0, 8]}}"#,
8,
);
write_safetensors(
&shard_a,
r#"{"a.router.weight": {"dtype": "F32", "shape": [8, 2048], "data_offsets": [0, 65536]}}"#,
65_536,
);
fs::write(
dir.join("model.safetensors.index.json"),
r#"{
"metadata": {"total_size": 65544},
"weight_map": {
"z.weight": "model-00002-of-00002.safetensors",
"a.router.weight": "model-00001-of-00002.safetensors"
}
}"#,
)
.unwrap();
let manifest = inspect_safetensors_checkpoint(&dir).unwrap();
assert_eq!(manifest.checkpoint.input_kind, "hf_index");
assert_eq!(
manifest.checkpoint.index_file.as_deref(),
Some("model.safetensors.index.json")
);
assert_eq!(manifest.checkpoint.shard_count, 2);
assert_eq!(
manifest
.checkpoint
.metadata
.get("index:total_size")
.unwrap(),
"65544"
);
assert_eq!(manifest.tensors[0].name, "a.router.weight");
assert_eq!(manifest.tensors[1].name, "z.weight");
assert_eq!(manifest.candidates.router_tensors, vec!["a.router.weight"]);
}
#[test]
fn user_index_metadata_cannot_spoof_unreferenced_shards() {
let dir = temp_dir("reserved-index-metadata");
write_safetensors(
&dir.join("model.safetensors"),
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
fs::write(
dir.join("model.safetensors.index.json"),
r#"{
"metadata": {"unreferenced_shards": "spoof.safetensors"},
"weight_map": {"a.weight": "model.safetensors"}
}"#,
)
.unwrap();
let manifest = inspect_safetensors_checkpoint(&dir).unwrap();
assert!(
!manifest
.checkpoint
.metadata
.contains_key(INDEX_UNREFERENCED_SHARDS_KEY)
);
}
#[test]
fn index_shard_paths_are_normalized_before_deduplication() {
let dir = temp_dir("normalized-index-paths");
write_safetensors(
&dir.join("model.safetensors"),
r#"{
"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]},
"b.weight": {"dtype": "F16", "shape": [1], "data_offsets": [2, 4]}
}"#,
4,
);
fs::write(
dir.join("model.safetensors.index.json"),
r#"{
"weight_map": {
"a.weight": "model.safetensors",
"b.weight": "./model.safetensors"
}
}"#,
)
.unwrap();
let manifest = inspect_safetensors_checkpoint(&dir).unwrap();
assert_eq!(manifest.checkpoint.shard_count, 1);
assert_eq!(manifest.checkpoint.tensor_count, 2);
assert_eq!(manifest.tensors[0].source_shard, "model.safetensors");
assert_eq!(manifest.tensors[1].source_shard, "model.safetensors");
}
#[test]
fn rejects_tensor_offsets_beyond_data_section() {
let dir = temp_dir("bounds");
let path = dir.join("bad.safetensors");
write_safetensors(
&path,
r#"{"bad.weight": {"dtype": "F16", "shape": [4], "data_offsets": [0, 16]}}"#,
8,
);
let err = inspect_safetensors_checkpoint(&path).unwrap_err();
assert!(
err.to_string()
.contains("extends beyond Safetensors data section")
);
}
#[test]
fn rejects_index_shard_paths_that_escape_checkpoint_directory() {
let dir = temp_dir("escape");
fs::write(
dir.join("model.safetensors.index.json"),
r#"{
"weight_map": {
"a.weight": "../outside.safetensors"
}
}"#,
)
.unwrap();
let err = inspect_safetensors_checkpoint(&dir).unwrap_err();
assert!(
err.to_string()
.contains("must stay within the checkpoint directory")
);
}
#[test]
fn rejects_absolute_index_shard_paths() {
let dir = temp_dir("absolute");
let outside = temp_dir("absolute-outside");
let outside_shard = outside.join("outside.safetensors");
write_safetensors(
&outside_shard,
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let escaped = outside_shard.to_string_lossy().replace('\\', "\\\\");
fs::write(
dir.join("model.safetensors.index.json"),
format!(r#"{{"weight_map": {{"a.weight": "{escaped}"}}}}"#),
)
.unwrap();
let err = inspect_safetensors_checkpoint(&dir).unwrap_err();
assert!(
err.to_string()
.contains("must be checkpoint-relative, not absolute")
);
}
#[test]
fn accepts_parent_dir_index_paths_that_stay_within_checkpoint_root() {
let dir = temp_dir("nested-parent");
write_safetensors(
&dir.join("model.safetensors"),
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
fs::write(
dir.join("model.safetensors.index.json"),
r#"{
"weight_map": {
"a.weight": "nested/../model.safetensors"
}
}"#,
)
.unwrap();
let manifest = inspect_safetensors_checkpoint(&dir).unwrap();
assert_eq!(manifest.checkpoint.shard_count, 1);
assert_eq!(manifest.tensors[0].source_shard, "model.safetensors");
}
#[test]
fn missing_referenced_shard_fails_before_manifest() {
let dir = temp_dir("missing-shard");
fs::write(
dir.join("model.safetensors.index.json"),
r#"{
"weight_map": {
"a.weight": "missing.safetensors"
}
}"#,
)
.unwrap();
let err = inspect_safetensors_checkpoint(&dir).unwrap_err();
match err {
ParserError::MissingShard { shard, path } => {
assert_eq!(shard, "missing.safetensors");
assert!(path.ends_with("model.safetensors.index.json"));
}
other => panic!("expected MissingShard, got {other}"),
}
}
#[test]
fn duplicate_directory_tensor_ownership_is_typed() {
let dir = temp_dir("dup-dir");
write_safetensors(
&dir.join("a.safetensors"),
r#"{"shared.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
write_safetensors(
&dir.join("b.safetensors"),
r#"{"shared.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let err = inspect_safetensors_checkpoint(&dir).unwrap_err();
match err {
ParserError::DuplicateTensorOwnership { name, shards, path } => {
assert_eq!(name, "shared.weight");
assert_eq!(
shards,
vec!["a.safetensors".to_string(), "b.safetensors".to_string()]
);
assert_eq!(Path::new(&path), dir.as_ref());
}
other => panic!("expected DuplicateTensorOwnership, got {other}"),
}
}
#[test]
fn duplicate_index_tensor_ownership_is_typed() {
let dir = temp_dir("dup-index");
write_safetensors(
&dir.join("model-00001-of-00002.safetensors"),
r#"{"shared.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
write_safetensors(
&dir.join("model-00002-of-00002.safetensors"),
r#"{
"shared.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]},
"other.weight": {"dtype": "F16", "shape": [1], "data_offsets": [2, 4]}
}"#,
4,
);
fs::write(
dir.join("model.safetensors.index.json"),
r#"{
"weight_map": {
"shared.weight": "model-00001-of-00002.safetensors",
"other.weight": "model-00002-of-00002.safetensors"
}
}"#,
)
.unwrap();
let err = inspect_safetensors_checkpoint(&dir).unwrap_err();
match err {
ParserError::DuplicateTensorOwnership { name, shards, .. } => {
assert_eq!(name, "shared.weight");
assert_eq!(
shards,
vec![
"model-00001-of-00002.safetensors".to_string(),
"model-00002-of-00002.safetensors".to_string()
]
);
}
other => panic!("expected DuplicateTensorOwnership, got {other}"),
}
}
#[test]
fn shuffled_directory_creation_order_yields_identical_manifests() {
let first = temp_dir("shuffle-a");
write_safetensors(
&first.join("z.safetensors"),
r#"{"z.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
write_safetensors(
&first.join("a.safetensors"),
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let second = temp_dir("shuffle-b");
write_safetensors(
&second.join("a.safetensors"),
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
write_safetensors(
&second.join("z.safetensors"),
r#"{"z.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let manifest_a = inspect_safetensors_checkpoint(&first).unwrap();
let manifest_b = inspect_safetensors_checkpoint(&second).unwrap();
let json_a = manifest_a.to_pretty_json();
let json_b = manifest_b.to_pretty_json();
assert_eq!(json_a, json_b);
assert_eq!(json_a, manifest_a.to_pretty_json());
assert_eq!(manifest_a.tensors[0].name, "a.weight");
assert_eq!(manifest_a.tensors[1].name, "z.weight");
}
#[cfg(unix)]
#[test]
fn rejects_index_shard_paths_that_escape_via_symlink() {
let dir = temp_dir("symlink");
let outside = temp_dir("outside");
let outside_shard = outside.join("outside.safetensors");
write_safetensors(
&outside_shard,
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
std::os::unix::fs::symlink(&outside_shard, dir.join("link.safetensors")).unwrap();
fs::write(
dir.join("model.safetensors.index.json"),
r#"{
"weight_map": {
"a.weight": "link.safetensors"
}
}"#,
)
.unwrap();
let err = inspect_safetensors_checkpoint(&dir).unwrap_err();
assert!(
err.to_string()
.contains("must stay within the checkpoint directory")
);
}
#[cfg(unix)]
#[test]
fn rejects_directory_index_that_escapes_via_symlink() {
let dir = temp_dir("index-symlink");
let outside = temp_dir("outside-index");
fs::write(
outside.join("external.safetensors.index.json"),
r#"{"weight_map": {}}"#,
)
.unwrap();
std::os::unix::fs::symlink(
outside.join("external.safetensors.index.json"),
dir.join("model.safetensors.index.json"),
)
.unwrap();
let err = inspect_safetensors_checkpoint(&dir).unwrap_err();
assert!(
err.to_string()
.contains("must stay within the checkpoint directory")
);
}
#[cfg(not(unix))]
#[test]
fn non_unix_symlink_escape_falls_back_to_lexical_path_guards() {
let dir = temp_dir("non-unix-symlink-fallback");
fs::write(
dir.join("model.safetensors.index.json"),
r#"{"weight_map": {"a.weight": "../outside.safetensors"}}"#,
)
.unwrap();
let parent_err = inspect_safetensors_checkpoint(&dir).unwrap_err();
assert!(
parent_err
.to_string()
.contains("must stay within the checkpoint directory")
);
let outside = temp_dir("non-unix-absolute-outside");
let outside_shard = outside.join("outside.safetensors");
write_safetensors(
&outside_shard,
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let escaped = outside_shard.to_string_lossy().replace('\\', "\\\\");
fs::write(
dir.join("model.safetensors.index.json"),
format!(r#"{{"weight_map": {{"a.weight": "{escaped}"}}}}"#),
)
.unwrap();
let absolute_err = inspect_safetensors_checkpoint(&dir).unwrap_err();
assert!(
absolute_err
.to_string()
.contains("must be checkpoint-relative, not absolute")
);
}
#[test]
fn rejects_multiple_directory_indexes() {
let dir = temp_dir("multiple-indexes");
fs::write(
dir.join("a.safetensors.index.json"),
r#"{"weight_map": {}}"#,
)
.unwrap();
fs::write(
dir.join("b.safetensors.index.json"),
r#"{"weight_map": {}}"#,
)
.unwrap();
let err = inspect_safetensors_checkpoint(&dir).unwrap_err();
assert!(err.to_string().contains("multiple Safetensors index files"));
}
#[test]
fn hf_index_filters_extra_tensors_and_requires_mapped_tensors() {
let dir = temp_dir("index-filter");
let shard = dir.join("model-00001-of-00001.safetensors");
write_safetensors(
&shard,
r#"{
"a.router.weight": {"dtype": "F32", "shape": [8, 2048], "data_offsets": [0, 65536]},
"stale.router.weight": {"dtype": "F32", "shape": [8, 2048], "data_offsets": [65536, 131072]}
}"#,
131_072,
);
write_safetensors(
&dir.join("unused.safetensors"),
r#"{"unused.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let index = dir.join("model.safetensors.index.json");
fs::write(
&index,
r#"{"weight_map": {"a.router.weight": "model-00001-of-00001.safetensors"}}"#,
)
.unwrap();
let manifest = inspect_safetensors_checkpoint(&dir).unwrap();
assert_eq!(manifest.checkpoint.tensor_count, 1);
assert_eq!(manifest.tensors[0].name, "a.router.weight");
assert_eq!(
manifest
.checkpoint
.metadata
.get(INDEX_UNREFERENCED_SHARDS_KEY)
.map(String::as_str),
Some(r#"["unused.safetensors"]"#)
);
fs::write(
&index,
r#"{"weight_map": {"missing.weight": "model-00001-of-00001.safetensors"}}"#,
)
.unwrap();
let err = inspect_safetensors_checkpoint(&dir).unwrap_err();
assert!(err.to_string().contains("does not contain it"));
}
#[test]
fn rejects_shape_dtype_byte_size_mismatch() {
let dir = temp_dir("byte-size");
let path = dir.join("bad.safetensors");
write_safetensors(
&path,
r#"{"bad.weight": {"dtype": "F16", "shape": [4], "data_offsets": [0, 4]}}"#,
4,
);
let err = inspect_safetensors_checkpoint(&path).unwrap_err();
assert!(err.to_string().contains("byte size mismatch"));
}
#[test]
fn rejects_unknown_safetensors_dtype() {
let dir = temp_dir("unknown-dtype");
let path = dir.join("bad.safetensors");
write_safetensors(
&path,
r#"{"bad.weight": {"dtype": "F128", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let err = inspect_safetensors_checkpoint(&path).unwrap_err();
assert!(err.to_string().contains("unsupported Safetensors dtype"));
}
#[test]
fn rejects_duplicate_header_keys() {
let dir = temp_dir("duplicate-keys");
let path = dir.join("bad.safetensors");
write_safetensors(
&path,
r#"{
"dup.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]},
"dup.weight": {"dtype": "F16", "shape": [1], "data_offsets": [2, 4]}
}"#,
4,
);
let err = inspect_safetensors_checkpoint(&path).unwrap_err();
assert!(err.to_string().contains("duplicate JSON key"));
}
#[test]
fn rejects_non_string_metadata_values() {
let dir = temp_dir("metadata-values");
let path = dir.join("bad.safetensors");
write_safetensors(
&path,
r#"{
"__metadata__": {"format": 1},
"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}
}"#,
2,
);
let err = inspect_safetensors_checkpoint(&path).unwrap_err();
assert!(err.to_string().contains("must be a string"));
}
#[test]
fn rejects_overlapping_tensor_ranges() {
let dir = temp_dir("overlap");
let path = dir.join("bad.safetensors");
write_safetensors(
&path,
r#"{
"a.weight": {"dtype": "F16", "shape": [4], "data_offsets": [0, 8]},
"b.weight": {"dtype": "F16", "shape": [4], "data_offsets": [4, 12]}
}"#,
12,
);
let err = inspect_safetensors_checkpoint(&path).unwrap_err();
assert!(err.to_string().contains("data range overlaps"));
}
#[test]
fn rejects_tensor_data_gaps_and_trailing_bytes() {
let dir = temp_dir("data-gaps");
let gap_path = dir.join("gap.safetensors");
write_safetensors(
&gap_path,
r#"{
"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]},
"b.weight": {"dtype": "F16", "shape": [1], "data_offsets": [4, 6]}
}"#,
6,
);
let err = inspect_safetensors_checkpoint(&gap_path).unwrap_err();
assert!(err.to_string().contains("leaves a gap"));
let trailing_path = dir.join("trailing.safetensors");
write_safetensors(
&trailing_path,
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
4,
);
let err = inspect_safetensors_checkpoint(&trailing_path).unwrap_err();
assert!(err.to_string().contains("data section is 4 bytes"));
}
#[test]
fn conflicting_shard_metadata_is_only_namespaced() {
let dir = temp_dir("metadata-conflict");
write_safetensors(
&dir.join("a.safetensors"),
r#"{
"__metadata__": {"format": "pt"},
"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}
}"#,
2,
);
write_safetensors(
&dir.join("b.safetensors"),
r#"{
"__metadata__": {"format": "np"},
"b.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}
}"#,
2,
);
let manifest = inspect_safetensors_checkpoint(&dir).unwrap();
assert!(!manifest.checkpoint.metadata.contains_key("format"));
assert_eq!(
manifest
.checkpoint
.metadata
.get(&shard_metadata_namespaced_key("a.safetensors", "format"))
.map(String::as_str),
Some("pt")
);
assert_eq!(
manifest
.checkpoint
.metadata
.get(&shard_metadata_namespaced_key("b.safetensors", "format"))
.map(String::as_str),
Some("np")
);
}
#[test]
fn colon_in_metadata_logical_key_conflict_does_not_restore_base_key() {
let mut metadata = BTreeMap::new();
let mut first = BTreeMap::new();
first.insert("my:key".to_string(), "a".to_string());
merge_shard_metadata(&mut metadata, "x.safetensors", first);
let mut second = BTreeMap::new();
second.insert("my:key".to_string(), "b".to_string());
merge_shard_metadata(&mut metadata, "y.safetensors", second);
assert!(!metadata.contains_key("my:key"));
let mut third = BTreeMap::new();
third.insert("my:key".to_string(), "c".to_string());
merge_shard_metadata(&mut metadata, "z.safetensors", third);
assert!(!metadata.contains_key("my:key"));
assert_eq!(
metadata
.get(&shard_metadata_namespaced_key("z.safetensors", "my:key"))
.map(String::as_str),
Some("c")
);
}
#[test]
fn rejects_manifest_output_that_overwrites_unreferenced_index_shard_with_comma_in_name() {
let dir = temp_dir("output-unreferenced-comma");
let main_shard = dir.join("model-00001-of-00001.safetensors");
write_safetensors(
&main_shard,
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let unreferenced = dir.join("a,b.safetensors");
write_safetensors(
&unreferenced,
r#"{"b.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
fs::write(
dir.join("model.safetensors.index.json"),
r#"{"weight_map": {"a.weight": "model-00001-of-00001.safetensors"}}"#,
)
.unwrap();
let err = write_safetensors_manifest(&dir, &unreferenced).unwrap_err();
assert!(err.to_string().contains("must not overwrite"));
}
#[test]
fn shard_metadata_key_detection_uses_exact_key_match() {
let mut metadata = BTreeMap::new();
let mut first = BTreeMap::new();
first.insert("xfoo".to_string(), "1".to_string());
merge_shard_metadata(&mut metadata, "a.safetensors", first);
let mut second = BTreeMap::new();
second.insert("foo".to_string(), "2".to_string());
merge_shard_metadata(&mut metadata, "b.safetensors", second);
assert_eq!(metadata.get("foo").map(String::as_str), Some("2"));
}
#[test]
fn directory_scan_ignores_non_file_safetensors_entries() {
let dir = temp_dir("non-file-entry");
fs::create_dir(dir.join("scratch.safetensors")).unwrap();
write_safetensors(
&dir.join("model.safetensors"),
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let manifest = inspect_safetensors_checkpoint(&dir).unwrap();
assert_eq!(manifest.checkpoint.shard_count, 1);
assert_eq!(manifest.checkpoint.tensor_count, 1);
assert_eq!(manifest.tensors[0].source_shard, "model.safetensors");
}
#[test]
fn bare_paths_resolve_parent_as_current_directory() {
assert_eq!(
parent_or_current(Path::new("model.safetensors")),
Path::new(".")
);
assert!(canonical_existing_or_parent(Path::new("manifest.json")).is_ok());
}
#[test]
fn rejects_manifest_output_that_overwrites_checkpoint_file() {
let dir = temp_dir("output-conflict");
let path = dir.join("model.safetensors");
write_safetensors(
&path,
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let err = write_safetensors_manifest(&path, &path).unwrap_err();
assert!(err.to_string().contains("must not overwrite"));
}
#[test]
fn rejects_manifest_output_that_overwrites_unreferenced_index_shard() {
let dir = temp_dir("output-unreferenced");
let main_shard = dir.join("model-00001-of-00001.safetensors");
write_safetensors(
&main_shard,
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let unreferenced = dir.join("unused.safetensors");
write_safetensors(
&unreferenced,
r#"{"b.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
fs::write(
dir.join("model.safetensors.index.json"),
r#"{"weight_map": {"a.weight": "model-00001-of-00001.safetensors"}}"#,
)
.unwrap();
let err = write_safetensors_manifest(&dir, &unreferenced).unwrap_err();
assert!(err.to_string().contains("must not overwrite"));
}
#[test]
fn accepts_current_additional_safetensors_dtypes() {
let dir = temp_dir("extra-dtypes");
let path = dir.join("model.safetensors");
write_safetensors(
&path,
r#"{
"a.weight": {"dtype": "F8_E8M0", "shape": [4], "data_offsets": [0, 4]},
"b.weight": {"dtype": "C64", "shape": [2], "data_offsets": [4, 20]}
}"#,
20,
);
let manifest = inspect_safetensors_checkpoint(&path).unwrap();
assert_eq!(manifest.checkpoint.tensor_count, 2);
}
#[test]
fn generic_dense_ffn_names_are_not_moe_expert_candidates() {
let labels = classify_tensor("model.layers.0.mlp.gate_proj.weight", &[4096, 2048]);
assert!(!labels.contains(&"moe_expert_candidate"));
let labels = classify_tensor("model.layers.0.block_sparse_moe.gate.weight", &[8, 2048]);
assert!(labels.contains(&"moe_router_candidate"));
assert!(!labels.contains(&"moe_expert_candidate"));
}
#[test]
fn detects_deepseek_v3_family_block_sparse_moe_layout() {
let dir = temp_dir("deepseek-v3-family");
let path = dir.join("model.safetensors");
write_safetensors(
&path,
r#"{
"model.layers.0.block_sparse_moe.gate.weight": {"dtype": "F32", "shape": [8, 2048], "data_offsets": [0, 65536]},
"model.layers.0.block_sparse_moe.experts.0.w1.weight": {"dtype": "F16", "shape": [2, 2], "data_offsets": [65536, 65544]},
"model.layers.0.block_sparse_moe.experts.0.w2.weight": {"dtype": "F16", "shape": [2, 2], "data_offsets": [65544, 65552]},
"model.layers.0.block_sparse_moe.experts.0.w3.weight": {"dtype": "F16", "shape": [2, 2], "data_offsets": [65552, 65560]}
}"#,
65_560,
);
let manifest = inspect_safetensors_checkpoint(&path).unwrap();
assert_eq!(
manifest.candidates.detected_layout_family,
Some("deepseek_v3_family")
);
assert_eq!(
manifest.candidates.router_tensors,
vec!["model.layers.0.block_sparse_moe.gate.weight"]
);
assert_eq!(manifest.candidates.expert_groups.len(), 1);
let group = &manifest.candidates.expert_groups[0];
assert_eq!(group.group_key, "model.layers.0.block_sparse_moe");
assert_eq!(group.layer_hint, Some(0));
assert_eq!(group.expert_indices, vec![0]);
assert_eq!(group.weight_kinds, vec!["w1", "w2", "w3"]);
assert_eq!(group.source_shards, vec!["model.safetensors"]);
}
#[test]
fn detects_requested_named_moe_families() {
for (family, router_name, expert_name) in [
(
"phimoe",
"phimoe.model.layers.0.moe.gate.weight",
"phimoe.model.layers.0.moe.experts.1.fc1.weight",
),
(
"granitemoe",
"granitemoe.model.layers.0.moe.gate.weight",
"granitemoe.model.layers.0.moe.experts.1.gate_proj.weight",
),
(
"afmoe",
"afmoe.model.layers.0.moe.gating.weight",
"afmoe.model.layers.0.moe.experts.1.linear_1.weight",
),
(
"lfm2_moe",
"lfm2_moe.model.layers.0.feed_forward.gate.weight",
"lfm2_moe.model.layers.0.feed_forward.experts.1.ffn_up.weight",
),
] {
let router = SafetensorsTensorRecord {
name: router_name.to_string(),
dtype: "F32".into(),
shape: vec![16, 2048],
byte_size: 131_072,
source_shard: "router.safetensors".into(),
data_offsets: [0, 131_072],
labels: classify_tensor(router_name, &[16, 2048]),
};
let expert = SafetensorsTensorRecord {
name: expert_name.to_string(),
dtype: "F16".into(),
shape: vec![2, 2],
byte_size: 8,
source_shard: "experts.safetensors".into(),
data_offsets: [0, 8],
labels: classify_tensor(expert_name, &[2, 2]),
};
let candidates = discover_candidates(&[router, expert]);
assert_eq!(candidates.detected_layout_family, Some(family));
assert_eq!(candidates.router_tensors, vec![router_name]);
assert_eq!(candidates.expert_tensors, vec![expert_name]);
assert_eq!(candidates.expert_groups.len(), 1);
assert_eq!(candidates.expert_groups[0].expert_indices, vec![1]);
}
}
#[test]
fn shape_guard_prevents_ambiguous_dense_gate_router_candidate() {
let labels = classify_tensor("model.layers.0.mlp.gate.weight", &[4096, 2048]);
assert!(!labels.contains(&"moe_router_candidate"));
let labels = classify_tensor("model.layers.0.mlp.gate.weight", &[8, 2048]);
assert!(labels.contains(&"moe_router_candidate"));
let labels = classify_tensor("model.layers.0.gating.weight", &[4096, 2048]);
assert!(!labels.contains(&"moe_router_candidate"));
}
#[test]
fn discovery_does_not_double_list_experts_as_routers() {
let expert_name = "model.router.moe.experts.0.down_proj.weight";
let tensor = SafetensorsTensorRecord {
name: expert_name.to_string(),
dtype: "F16".into(),
shape: vec![2, 2],
byte_size: 8,
source_shard: "experts.safetensors".into(),
data_offsets: [0, 8],
labels: classify_tensor(expert_name, &[2, 2]),
};
let manifest = build_manifest(
"single_file",
None,
vec![PathBuf::from("experts.safetensors")],
BTreeMap::new(),
vec![tensor],
Path::new("experts.safetensors"),
)
.unwrap();
assert_eq!(manifest.candidates.router_tensors, Vec::<String>::new());
assert_eq!(manifest.candidates.expert_tensors, vec![expert_name]);
assert_eq!(manifest.tensors[0].labels, vec!["moe_expert_candidate"]);
}
#[test]
fn named_family_requires_router_and_expert_evidence() {
let tensor = SafetensorsTensorRecord {
name: "granitemoe.adapter.weight".into(),
dtype: "F16".into(),
shape: vec![2, 2],
byte_size: 8,
source_shard: "model.safetensors".into(),
data_offsets: [0, 8],
labels: classify_tensor("granitemoe.adapter.weight", &[2, 2]),
};
let manifest = build_manifest(
"single_file",
None,
vec![PathBuf::from("model.safetensors")],
BTreeMap::new(),
vec![tensor],
Path::new("model.safetensors"),
)
.unwrap();
assert_eq!(manifest.candidates.detected_layout_family, None);
assert!(manifest.candidates.router_tensors.is_empty());
assert!(manifest.candidates.expert_tensors.is_empty());
}
#[test]
fn unknown_expert_weight_kind_is_retained() {
let router_name = "model.layers.0.moe.gate.weight";
let expert_name = "model.layers.0.moe.experts.0.adapter.weight";
let router = SafetensorsTensorRecord {
name: router_name.into(),
dtype: "F32".into(),
shape: vec![4, 2048],
byte_size: 32_768,
source_shard: "router.safetensors".into(),
data_offsets: [0, 32_768],
labels: classify_tensor(router_name, &[4, 2048]),
};
let expert = SafetensorsTensorRecord {
name: expert_name.into(),
dtype: "F16".into(),
shape: vec![2, 2],
byte_size: 8,
source_shard: "experts.safetensors".into(),
data_offsets: [0, 8],
labels: classify_tensor(expert_name, &[2, 2]),
};
let manifest = build_manifest(
"directory",
None,
vec![
PathBuf::from("router.safetensors"),
PathBuf::from("experts.safetensors"),
],
BTreeMap::new(),
vec![router, expert],
Path::new("checkpoint"),
)
.unwrap();
assert_eq!(
manifest.candidates.detected_layout_family,
Some("generic_moe")
);
assert_eq!(manifest.candidates.expert_tensors, vec![expert_name]);
assert_eq!(
manifest.candidates.expert_groups[0].weight_kinds,
vec!["unknown"]
);
}
#[test]
fn expert_groups_keep_parallel_layer_stacks_separate() {
let encoder = "encoder.layers.0.moe.experts.0.w1.weight";
let decoder = "decoder.layers.0.moe.experts.0.w1.weight";
let tensors = [encoder, decoder]
.into_iter()
.map(|name| SafetensorsTensorRecord {
name: name.into(),
dtype: "F16".into(),
shape: vec![2, 2],
byte_size: 8,
source_shard: "experts.safetensors".into(),
data_offsets: [0, 8],
labels: classify_tensor(name, &[2, 2]),
})
.collect::<Vec<_>>();
let manifest = build_manifest(
"single_file",
None,
vec![PathBuf::from("experts.safetensors")],
BTreeMap::new(),
tensors,
Path::new("experts.safetensors"),
)
.unwrap();
let group_keys = manifest
.candidates
.expert_groups
.iter()
.map(|group| group.group_key.as_str())
.collect::<Vec<_>>();
assert_eq!(
group_keys,
vec!["decoder.layers.0.moe", "encoder.layers.0.moe"]
);
}
#[test]
fn groups_experts_across_hugging_face_index_shards() {
let dir = temp_dir("hf-expert-groups");
let router_shard = dir.join("model-00001-of-00002.safetensors");
let expert_shard = dir.join("model-00002-of-00002.safetensors");
write_safetensors(
&router_shard,
r#"{"model.layers.2.block_sparse_moe.gate.weight": {"dtype": "F32", "shape": [8, 2048], "data_offsets": [0, 65536]}}"#,
65_536,
);
write_safetensors(
&expert_shard,
r#"{
"model.layers.2.block_sparse_moe.experts.0.w1.weight": {"dtype": "F16", "shape": [2, 2], "data_offsets": [0, 8]},
"model.layers.2.block_sparse_moe.experts.1.w1.weight": {"dtype": "F16", "shape": [2, 2], "data_offsets": [8, 16]}
}"#,
16,
);
fs::write(
dir.join("model.safetensors.index.json"),
r#"{
"weight_map": {
"model.layers.2.block_sparse_moe.gate.weight": "model-00001-of-00002.safetensors",
"model.layers.2.block_sparse_moe.experts.0.w1.weight": "model-00002-of-00002.safetensors",
"model.layers.2.block_sparse_moe.experts.1.w1.weight": "model-00002-of-00002.safetensors"
}
}"#,
)
.unwrap();
let manifest = inspect_safetensors_checkpoint(&dir).unwrap();
assert_eq!(manifest.checkpoint.input_kind, "hf_index");
assert_eq!(manifest.candidates.expert_groups.len(), 1);
let group = &manifest.candidates.expert_groups[0];
assert_eq!(group.group_key, "model.layers.2.block_sparse_moe");
assert_eq!(group.expert_indices, vec![0, 1]);
assert_eq!(
group.source_shards,
vec!["model-00002-of-00002.safetensors"]
);
assert!(
manifest
.tensors
.iter()
.all(|tensor| tensor.source_shard.ends_with(".safetensors"))
);
}
#[test]
fn write_manifest_is_deterministic_and_sorted() {
let dir = temp_dir("write-order");
write_safetensors(
&dir.join("z.safetensors"),
r#"{"z.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
write_safetensors(
&dir.join("a.safetensors"),
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let out = dir.join("manifest.json");
let first = write_safetensors_manifest(&dir, &out).unwrap();
let json_a = fs::read_to_string(&out).unwrap();
let second = write_safetensors_manifest(&dir, &out).unwrap();
let json_b = fs::read_to_string(&out).unwrap();
assert_eq!(json_a, json_b);
assert_eq!(first.tensors[0].name, "a.weight");
assert_eq!(second.tensors[1].name, "z.weight");
assert!(json_a.contains("\"manifest_version\": 2"));
}
#[test]
fn dtype_size_bytes_covers_known_types() {
assert_eq!(dtype_size_bytes("F32"), Some(4));
assert_eq!(dtype_size_bytes("F16"), Some(2));
assert_eq!(dtype_size_bytes("F8_E8M0"), Some(1));
assert_eq!(dtype_size_bytes("C64"), Some(8));
assert_eq!(dtype_size_bytes("INT4"), None);
assert_eq!(dtype_size_bytes("nope"), None);
}
#[cfg(windows)]
#[test]
fn windows_same_file_detects_hardlinks_via_file_id() {
let dir = temp_dir("windows-hardlink");
let original = dir.join("original.safetensors");
write_safetensors(
&original,
r#"{"a.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
let hardlink = dir.join("hardlink.safetensors");
std::fs::hard_link(&original, &hardlink).unwrap();
assert!(super::paths::paths_refer_to_same_file(&original, &hardlink).unwrap());
let different = dir.join("different.safetensors");
write_safetensors(
&different,
r#"{"b.weight": {"dtype": "F16", "shape": [1], "data_offsets": [0, 2]}}"#,
2,
);
assert!(!super::paths::paths_refer_to_same_file(&original, &different).unwrap());
}