Skip to main content

Crate engram_parser

Crate engram_parser 

Source
Expand description

Pure-Rust checkpoint and tensor inspection for GGUF and Safetensors.

The primary contract is the format-independent checkpoint module: open_checkpoint detects the container format and returns a Checkpoint handle that inventories tensors (TensorInfo, TensorShape, TensorDType, TensorLocation), reads metadata, and fetches raw payload bytes without the caller branching on GGUF vs. Safetensors.

Beneath that contract, gguf and (feature-gated) safetensors are format-specific backends: each exposes its own full parsing surface for callers that need format semantics — GGML wire types, K-quant dequant, Safetensors manifests and MoE candidate discovery.

Model-analysis specializations live under analysis. Today that is analysis::moe: per-expert raw-weight extraction over a parsed GgufLayout. MoE is one analysis specialization built above the parsers, not the identity of the crate.

§Features

  • No dependencies enabled by default: the default path is pure Rust. The optional mmap feature adds memmap2; safetensors adds the upstream safetensors crate.
  • Parse limits: ParseLimits is a documented budget for KV/tensor counts, string sizes, array work, tensor rank, and metadata bytes. File-declared u64 sizes convert with HostSizeField errors. Defaults stay generous; trusted callers override explicitly.
  • GGUF v3 support: Full parsing of headers, metadata, and tensor directories
  • GGUF wire-type metadata: labels + packed byte_len for known quant codes (F32/F16/BF16, Q*/IQ*, integers, historical wire 31 = Q4_0_4_4). Packed dequant is implemented for Q8_0, Q5_K, Q6_K, and the internal IQ3_M block layout. No GGML kernels, no ggml runtime, no CUDA.
  • Type labels: Human-readable names via ggml_type_label (maps the on-wire ggml_type integer used by GGUF)
  • MoE analysis: Extract expert raw weights (byte buffers + shape) via analysis::moe; GGUF-specific.
  • Metadata helpers: Architecture-aware convenience methods for common fields
  • Optional Safetensors: enable --features safetensors for inspection, deterministic manifests, MoE candidate discovery, and raw tensor payload access via the upstream safetensors crate. Combine with --features mmap for borrowed mmap-backed payload slices. No Hugging Face config.json policy.
  • Format-independent access: Checkpoint + open_checkpoint inventory tensors, read metadata, and fetch raw payloads from GGUF or Safetensors without branching on format. See checkpoint.

§Example

Format-agnostic inventory:

use engram_parser::{Checkpoint, open_checkpoint};

let ckpt = open_checkpoint("model.gguf").unwrap();
for t in ckpt.tensors() {
    println!("{} {} {:?} {}B", t.name, t.dtype, t.shape.dims(), t.byte_len);
}

MoE expert analysis (GGUF-specific) uses the canonical analysis::moe path:

use engram_parser::analysis::moe::{extract_expert, list_experts};
use engram_parser::load_gguf;

let layout = load_gguf("moe-model.gguf").unwrap();
for (block, expert) in list_experts(&layout) {
    let weights = extract_expert(&layout, block, expert).unwrap();
    println!("blk.{block} expert {expert}: complete={}", weights.is_complete());
}

§Compatibility

The legacy engram_parser::moe module and the crate-root MoE symbols (extract_expert, list_experts, MoeExpertWeights, RawTensor) remain as re-exports of analysis::moe and keep compiling unchanged.

Re-exports§

pub use checkpoint::SafetensorsBackend;
pub use checkpoint::SafetensorsMmapBackend;
pub use checkpoint::AnyCheckpoint;
pub use checkpoint::Checkpoint;
pub use checkpoint::CheckpointFormat;
pub use checkpoint::CheckpointMetadata;
pub use checkpoint::CheckpointSource;
pub use checkpoint::DimOrder;
pub use checkpoint::GgufBackend;
pub use checkpoint::MetadataValue;
pub use checkpoint::SourceKind;
pub use checkpoint::TensorDType;
pub use checkpoint::TensorInfo;
pub use checkpoint::TensorLocation;
pub use checkpoint::TensorShape;
pub use checkpoint::open_checkpoint;
pub use checkpoint::GgufMmapBackend;
pub use checkpoint::open_checkpoint_mmap;
pub use error::HostSizeField;
pub use error::ParseLimitKind;
pub use error::ParserError;
pub use error::Result;
pub use gguf::DType;
pub use gguf::GGML_TYPE_BF16;
pub use gguf::GGML_TYPE_F16;
pub use gguf::GGML_TYPE_F32;
pub use gguf::GGML_TYPE_F64;
pub use gguf::GGML_TYPE_I8;
pub use gguf::GGML_TYPE_I16;
pub use gguf::GGML_TYPE_I32;
pub use gguf::GGML_TYPE_I64;
pub use gguf::GGML_TYPE_IQ1_M;
pub use gguf::GGML_TYPE_IQ1_S;
pub use gguf::GGML_TYPE_IQ2_S;
pub use gguf::GGML_TYPE_IQ2_XS;
pub use gguf::GGML_TYPE_IQ2_XXS;
pub use gguf::GGML_TYPE_IQ3_M_BLOCK;
pub use gguf::GGML_TYPE_IQ3_S;
pub use gguf::GGML_TYPE_IQ3_XXS;
pub use gguf::GGML_TYPE_IQ4_NL;
pub use gguf::GGML_TYPE_IQ4_XS;
pub use gguf::GGML_TYPE_Q2_K;
pub use gguf::GGML_TYPE_Q3_K;
pub use gguf::GGML_TYPE_Q4_0;
pub use gguf::GGML_TYPE_Q4_0_4_4;
pub use gguf::GGML_TYPE_Q4_1;
pub use gguf::GGML_TYPE_Q4_K;
pub use gguf::GGML_TYPE_Q5_0;
pub use gguf::GGML_TYPE_Q5_1;
pub use gguf::GGML_TYPE_Q5_K;
pub use gguf::GGML_TYPE_Q6_K;
pub use gguf::GGML_TYPE_Q8_0;
pub use gguf::GGML_TYPE_Q8_1;
pub use gguf::GGML_TYPE_Q8_K;
pub use gguf::GGUF_VALUE_TYPE_ARRAY;
pub use gguf::GGUF_VALUE_TYPE_BOOL;
pub use gguf::GGUF_VALUE_TYPE_FLOAT32;
pub use gguf::GGUF_VALUE_TYPE_FLOAT64;
pub use gguf::GGUF_VALUE_TYPE_INT8;
pub use gguf::GGUF_VALUE_TYPE_INT16;
pub use gguf::GGUF_VALUE_TYPE_INT32;
pub use gguf::GGUF_VALUE_TYPE_INT64;
pub use gguf::GGUF_VALUE_TYPE_STRING;
pub use gguf::GGUF_VALUE_TYPE_UINT8;
pub use gguf::GGUF_VALUE_TYPE_UINT16;
pub use gguf::GGUF_VALUE_TYPE_UINT32;
pub use gguf::GGUF_VALUE_TYPE_UINT64;
pub use gguf::GgufLayout;
pub use gguf::GgufMetadata;
pub use gguf::ParseLimits;
pub use gguf::Tensor;
pub use gguf::dequantize_iq3_m;
pub use gguf::dequantize_packed;
pub use gguf::dequantize_q5_k;
pub use gguf::dequantize_q6_k;
pub use gguf::dequantize_q8_0;
pub use gguf::f16_bits_to_f32;
pub use gguf::ggml_type_label;
pub use gguf::load_gguf;
pub use gguf::load_gguf_with_limits;
pub use gguf::packed_row_size;
pub use gguf::parse_bytes;
pub use gguf::parse_bytes_with_limits;
pub use gguf::GgufLayoutMmap;
pub use gguf::PageAlignedTensorBytes;
pub use gguf::load_gguf_mmap;
pub use gguf::load_gguf_mmap_with_limits;
pub use gguf::os_page_size;
pub use analysis::moe::MoeExpertWeights;
pub use analysis::moe::RawTensor;
pub use analysis::moe::extract_expert;
pub use analysis::moe::list_experts;

Modules§

analysis
Model-analysis specializations built on top of format parsing.
checkpoint
Format-independent checkpoint and tensor access.
error
Hand-rolled error types for engram-parser.
gguf
GGUF file format: header + metadata + tensor directory.
moe
Compatibility re-exports for the legacy engram_parser::moe path.
safetensors
Header-only Safetensors inspection, deterministic manifests, and MoE candidate discovery.