Files
leleka/control-plane/src/native_pack.rs
T

93 lines
2.4 KiB
Rust

//! Native GGUF serving-pack preparation. Quantized tensors remain in source
//! shards; float conversions are exact unless `--compat-bf16` is given. No runtime Python.
use std::{
collections::{BTreeMap, HashSet},
fs::{self, File},
io::{Read, Seek, SeekFrom, Write},
path::{Path, PathBuf},
};
use serde_json::json;
use sha2::{Digest, Sha256};
use crate::{gguf, gguf_f32_stream, kar_codec, model, tokenizer, tokenizer_assets};
type Result<T> = std::result::Result<T, crate::BoxError>;
struct Shard {
path: PathBuf,
index: gguf::TensorIndex,
}
struct Layout<'a> {
text: String,
n_expert: u64,
total: u64,
/// Per layer: the gate, up and down tensors with the index of the shard holding each.
layers: Vec<[(usize, &'a gguf::TensorInfo); 3]>,
}
/// Running comparison of converted values with their sources, as `iq_pack.convert` reports it.
struct Error64 {
exact: bool,
max_abs_err: f64,
}
struct Record {
name: String,
shard: String,
src_type: &'static str,
dst_type: &'static str,
method: String,
exact: bool,
max_abs_err: f64,
source_sha256: String,
}
struct Converted {
/// Index `kind` column.
index_kind: u32,
written: u64,
dst: u64,
/// `(method, exact, max_abs_err)` when `conversions.json` lists the tensor.
record: Option<(String, bool, f64)>,
source_sha256: String,
}
/// Optional parts of `tools/iq_pack.py` beyond the exact default pack.
#[derive(Clone, Debug, Default)]
pub struct Options {
/// Dequantize/round the small projections the engine reads as BF16/F16/F32 (`--compat-bf16`).
pub compat_bf16: bool,
/// Also write `experts.bin` and its `experts.bin.src.json` sidecar (`--experts-bin`).
pub experts_bin: bool,
/// Reuse the float tensors of a canonical base pack (`--base`): `index.txt` and `extra.bin` are derived from
/// it, `dense.bin` is linked.
pub base: Option<PathBuf>,
}
#[cfg(test)]
mod tests;
mod safe_name;
#[allow(unused_imports)]
pub use safe_name::*;
mod expert_layout;
#[allow(unused_imports)]
pub use expert_layout::*;
mod convert_tensor;
#[allow(unused_imports)]
pub use convert_tensor::*;
mod dense_files;
#[allow(unused_imports)]
pub use dense_files::*;
mod pack_publication;
#[allow(unused_imports)]
pub use pack_publication::*;
mod conversions_inexact;
#[allow(unused_imports)]
pub use conversions_inexact::*;
mod same_bytes;
#[allow(unused_imports)]
pub use same_bytes::*;