dump_logprobs writes the top-20 next-token log-probabilities after every position of a fixed token sequence; compare_logprobs compares two such dumps (best-token agreement, log-probability differences, KL on the shared top tokens, next-token NLL, statistics by position). The committed reference holds llama.cpp's numbers for a 383-token Harmony exchange. Result: Leleka (AVX-VNNI) vs llama.cpp agrees on the best token at 97.1% of positions, mean KL 0.0080, next-token NLL 1.2859 vs 1.2798. Leleka's own scalar and SIMD kernel levels, which differ only in float summation order, differ from each other by mean KL 0.0048 with the same early-position pattern, so the earlier first-step discrepancy is numerical noise amplified by the 8-bit activation quantization, not a systematic error. The README records the procedure and the pass criteria for later changes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
82 lines
3.1 KiB
Rust
82 lines
3.1 KiB
Rust
//! Teacher-forced log-probabilities: feed a fixed token sequence and write, after every position, the
|
|
//! top-N log-probabilities of the next token (one JSON object per line), for comparison with another
|
|
//! implementation fed the same tokens.
|
|
//!
|
|
//! Run: `cargo run --release -p leleka-gptoss --example dump_logprobs -- <model.gguf> <ids.txt> <out.jsonl> [threads] [top_n]`
|
|
//! `ids.txt` holds comma-separated token ids. Line `i` of the output describes the distribution after
|
|
//! token `i` (so it predicts token `i + 1`): `{"pos":i,"next":<id of token i+1 or null>,"top":[[id,logprob],...]}`.
|
|
|
|
use leleka_cpu_kernels::Dispatch;
|
|
use leleka_cpu_sys::mapping::Mapping;
|
|
use leleka_gguf::Gguf;
|
|
use leleka_gptoss::{Model, State};
|
|
use std::fs::File;
|
|
use std::io::{BufReader, BufWriter, Write};
|
|
use std::path::Path;
|
|
|
|
fn main() {
|
|
let args: Vec<String> = std::env::args().skip(1).collect();
|
|
if args.len() < 3 {
|
|
eprintln!("usage: dump_logprobs <model.gguf> <ids.txt> <out.jsonl> [threads] [top_n]");
|
|
std::process::exit(2);
|
|
}
|
|
let ids: Vec<u32> = std::fs::read_to_string(&args[1])
|
|
.expect("ids file")
|
|
.trim()
|
|
.split(',')
|
|
.map(|s| s.trim().parse().expect("token id"))
|
|
.collect();
|
|
let threads: usize = args
|
|
.get(3)
|
|
.and_then(|s| s.parse().ok())
|
|
.unwrap_or_else(|| std::thread::available_parallelism().map_or(1, |n| n.get()));
|
|
let top_n: usize = args.get(4).and_then(|s| s.parse().ok()).unwrap_or(20);
|
|
|
|
let path = Path::new(&args[0]);
|
|
let size = std::fs::metadata(path).expect("model file").len();
|
|
let gguf = Gguf::read(
|
|
BufReader::with_capacity(1 << 20, File::open(path).expect("open")),
|
|
size,
|
|
)
|
|
.expect("valid GGUF");
|
|
let model = Model::load(
|
|
std::sync::Arc::new(Mapping::open(path).expect("map model")),
|
|
&gguf,
|
|
Dispatch::from_env().expect("kernel level"),
|
|
threads,
|
|
)
|
|
.expect("load model");
|
|
let mut state = State::new(&model.config, ids.len() + 1);
|
|
let mut out = BufWriter::new(File::create(&args[2]).expect("output file"));
|
|
for (pos, token) in ids.iter().enumerate() {
|
|
model.forward(&mut state, *token, pos).expect("forward");
|
|
let max = state
|
|
.logits
|
|
.iter()
|
|
.copied()
|
|
.fold(f32::NEG_INFINITY, f32::max);
|
|
let z: f64 = state
|
|
.logits
|
|
.iter()
|
|
.map(|l| f64::from(l - max).exp())
|
|
.sum::<f64>();
|
|
let mut order: Vec<usize> = (0..state.logits.len()).collect();
|
|
order.sort_by(|a, b| state.logits[*b].total_cmp(&state.logits[*a]).then(a.cmp(b)));
|
|
let top: Vec<String> = order
|
|
.iter()
|
|
.take(top_n)
|
|
.map(|i| format!("[{},{:.6}]", i, f64::from(state.logits[*i] - max) - z.ln()))
|
|
.collect();
|
|
let next = ids
|
|
.get(pos + 1)
|
|
.map_or("null".to_owned(), |n| n.to_string());
|
|
writeln!(
|
|
out,
|
|
"{{\"pos\":{pos},\"next\":{next},\"top\":[{}]}}",
|
|
top.join(",")
|
|
)
|
|
.expect("write");
|
|
}
|
|
eprintln!("wrote {} positions", ids.len());
|
|
}
|