mod error;
mod install;
mod layout;
mod manifest;
mod verify;
pub use error::FormatError;
pub use install::{
MAX_JSON_BYTES, PARTIAL_SUFFIX, is_complete, load_layout, load_manifest, partial_dir, promote,
write_layout, write_manifest,
};
pub use layout::{ExpertsLayout, LayerLayout, Projection, ProjectionName};
pub use manifest::{
ArchInfo, CommonTensor, FileEntry, MAX_EXPERTS, MAX_LAYERS, Manifest, QuantInfo, SourceInfo,
layer_file_name,
};
pub use verify::{sha256_bytes, sha256_file, verify_files};
pub const RVMP_VERSION: u32 = 1;
pub const COMMON_TENSOR_ALIGN: u64 = 64;
pub const EXPERT_BLOB_ALIGN: u64 = 4096;
pub const MANIFEST_FILE: &str = "manifest.json";
pub const COMMON_FILE: &str = "common.bin";
pub const LAYOUT_FILE: &str = "experts/layout.json";
pub const KNOWN_QUANT_SCHEMES: &[&str] = &["gguf"];
#[cfg(test)]
pub(crate) mod testutil {
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use super::{
ArchInfo, CommonTensor, ExpertsLayout, FileEntry, LayerLayout, Manifest, Projection,
ProjectionName, QuantInfo, RVMP_VERSION, SourceInfo,
};
pub(crate) const ZERO_SHA: &str =
"0000000000000000000000000000000000000000000000000000000000000000";
pub(crate) fn sample_manifest() -> Manifest {
let mut files = BTreeMap::new();
files.insert(
"common.bin".to_owned(),
FileEntry {
size: 4096,
sha256: ZERO_SHA.to_owned(),
},
);
files.insert(
"experts/layout.json".to_owned(),
FileEntry {
size: 2,
sha256: ZERO_SHA.to_owned(),
},
);
files.insert(
"experts/layer_00.bin".to_owned(),
FileEntry {
size: 4 * 12288,
sha256: ZERO_SHA.to_owned(),
},
);
files.insert(
"experts/layer_01.bin".to_owned(),
FileEntry {
size: 4 * 16384,
sha256: ZERO_SHA.to_owned(),
},
);
let mut common_tensors = BTreeMap::new();
common_tensors.insert(
"token_embd.weight".to_owned(),
CommonTensor {
offset: 0,
len: 2048,
dtype: "q4_k".to_owned(),
},
);
common_tensors.insert(
"output_norm.weight".to_owned(),
CommonTensor {
offset: 2048,
len: 256,
dtype: "f32".to_owned(),
},
);
let mut tensor_types = BTreeMap::new();
tensor_types.insert("ffn_gate_exps".to_owned(), "q4_k".to_owned());
tensor_types.insert("ffn_down_exps".to_owned(), "q6_k".to_owned());
Manifest {
rvmp_version: RVMP_VERSION,
model_id: "test-moe-2l".to_owned(),
source: SourceInfo {
hf_repo: "test/repo".to_owned(),
revision: "0123abcd".to_owned(),
file: "test-Q4_K_M.gguf".to_owned(),
sha256: ZERO_SHA.to_owned(),
},
arch: ArchInfo {
n_layers: 2,
n_experts: 4,
top_k: 2,
hidden: 64,
moe_intermediate: 96,
n_heads: 8,
n_kv_heads: 2,
head_dim: 8,
vocab: 512,
context_length: 4096,
rope_theta: 1e7,
rms_eps: 1e-6,
norm_topk_prob: true,
tie_embeddings: false,
shared_expert: false,
sliding_window: None,
},
quant: QuantInfo {
scheme: "gguf".to_owned(),
tensor_types,
},
common_tensors,
files,
}
}
pub(crate) fn sample_layout() -> ExpertsLayout {
ExpertsLayout {
layers: vec![
LayerLayout {
file: "experts/layer_00.bin".to_owned(),
stride: 12288,
n_experts: 4,
projections: vec![
Projection {
name: ProjectionName::Gate,
offset_in_blob: 0,
len: 1024,
quant: "q4_k".to_owned(),
},
Projection {
name: ProjectionName::Up,
offset_in_blob: 4096,
len: 1024,
quant: "q4_k".to_owned(),
},
Projection {
name: ProjectionName::Down,
offset_in_blob: 8192,
len: 2048,
quant: "q6_k".to_owned(),
},
],
},
LayerLayout {
file: "experts/layer_01.bin".to_owned(),
stride: 16384,
n_experts: 4,
projections: vec![
Projection {
name: ProjectionName::Gate,
offset_in_blob: 0,
len: 2048,
quant: "q4_k".to_owned(),
},
Projection {
name: ProjectionName::Up,
offset_in_blob: 4096,
len: 2048,
quant: "q4_k".to_owned(),
},
Projection {
name: ProjectionName::Down,
offset_in_blob: 8192,
len: 4096,
quant: "q6_k".to_owned(),
},
],
},
],
}
}
pub(crate) struct TempDir(PathBuf);
impl TempDir {
pub(crate) fn new(tag: &str) -> Self {
static COUNTER: AtomicU64 = AtomicU64::new(0);
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_nanos();
let pid = std::process::id();
let path = std::env::temp_dir().join(format!("ramvamp-format-{tag}-{pid}-{n}-{nanos}"));
std::fs::create_dir_all(&path).expect("create test temp dir");
TempDir(path)
}
pub(crate) fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
}