engram-parser 0.3.0

Pure-Rust GGUF + Safetensors checkpoint/tensor substrate: format-independent tensor inventory, metadata, and raw payload access for ANN/SNN research. Default builds are zero-dependency (`fs::read` into `Vec<u8>`). Optional `mmap` feature adds memmap2. Packed dequant for Q8_0/Q5_K/Q6_K/IQ3_M. Off-by-default `safetensors` feature adds manifest/discovery plus raw tensor payload access via the upstream safetensors crate. MoE per-expert weight extraction ships as a GGUF model-analysis specialization.
Documentation
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Header-only Safetensors inspection, deterministic manifests, and MoE
//! candidate discovery.
//!
//! Gated behind the off-by-default `safetensors` cargo feature, which
//! pulls in the upstream `safetensors` crate for canonical header
//! validation. Engram keeps ownership of checkpoint discovery, shard
//! path policy, deterministic manifests, MoE candidate discovery, and
//! raw tensor payload access; upstream types are not part of the public
//! API. Hugging Face `config.json` policy is intentionally not ported:
//! this crate models containers, not model-configuration conventions.
//!
//! ```no_run
//! use engram_parser::safetensors::inspect_safetensors_checkpoint;
//!
//! let manifest = inspect_safetensors_checkpoint("model.safetensors")?;
//! println!("tensors = {}", manifest.checkpoint.tensor_count);
//! # Ok::<(), engram_parser::ParserError>(())
//! ```

mod checkpoint;
mod discovery;
mod json;
mod manifest;
#[cfg(feature = "mmap")]
mod map;
mod paths;
mod validate;

#[cfg(test)]
mod tests;

use crate::error::ParserError;
use std::path::Path;

pub use checkpoint::{SafetensorsCheckpoint, open_safetensors_checkpoint};
pub use discovery::{
    SafetensorsCandidateSummary, SafetensorsExpertGroup, SafetensorsRouterCandidate,
    classify_tensor, discover_candidates,
};
pub use manifest::{
    INDEX_UNREFERENCED_SHARDS_KEY, SafetensorsCheckpointSource, SafetensorsManifest,
    SafetensorsTensorRecord, inspect_safetensors_checkpoint, write_safetensors_manifest,
};
#[cfg(feature = "mmap")]
pub use map::{SafetensorsCheckpointMmap, open_safetensors_checkpoint_mmap};
pub use validate::dtype_size_bytes;

pub(super) fn relative_path(path: &Path, root: &Path) -> String {
    let stripped = path.strip_prefix(root).unwrap_or(path);
    let lossy = stripped.to_string_lossy();
    #[cfg(windows)]
    {
        lossy.replace('\\', "/")
    }
    #[cfg(not(windows))]
    {
        lossy.into_owned()
    }
}

pub(super) fn model_load(path: &Path, reason: String) -> ParserError {
    ParserError::InvalidLayout {
        path: path.display().to_string(),
        reason,
    }
}

pub(super) fn io_error(path: &Path, source: std::io::Error) -> ParserError {
    ParserError::Io {
        path: path.display().to_string(),
        source,
    }
}

pub(super) fn unsupported(path: &Path, reason: String) -> ParserError {
    ParserError::UnsupportedFormat {
        path: path.display().to_string(),
        reason,
    }
}

pub(super) fn duplicate_tensor_ownership(
    path: &Path,
    name: impl Into<String>,
    mut shards: Vec<String>,
) -> ParserError {
    shards.sort();
    shards.dedup();
    ParserError::DuplicateTensorOwnership {
        name: name.into(),
        shards,
        path: path.display().to_string(),
    }
}

pub(super) fn missing_shard(path: &Path, shard: impl Into<String>) -> ParserError {
    ParserError::MissingShard {
        shard: shard.into(),
        path: path.display().to_string(),
    }
}