STRA-338 size-fn: example helpers trimmed further
This commit is contained in:
@@ -170,25 +170,30 @@ fn policy_grid() -> Vec<Policy> {
|
||||
let mut grid = Vec::new();
|
||||
for min_match in [2, 3, 4] {
|
||||
for prior in [(2.0, 1.0), (4.0, 1.0), (9.0, 1.0)] {
|
||||
for forget in [0.8, 0.9, 0.95] {
|
||||
for min_gain in [1.0, 1.03, 1.1] {
|
||||
for probe_after in [8, 16, 24, 48] {
|
||||
grid.push(Policy {
|
||||
min_match,
|
||||
max_draft: MAX_DRAFT,
|
||||
prior,
|
||||
forget,
|
||||
min_gain,
|
||||
probe_after,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
extend_grid(&mut grid, min_match, prior);
|
||||
}
|
||||
}
|
||||
grid
|
||||
}
|
||||
|
||||
/// The policies with this `min_match` and `prior` over the grid of the other settings.
|
||||
fn extend_grid(grid: &mut Vec<Policy>, min_match: usize, prior: (f64, f64)) {
|
||||
for forget in [0.8, 0.9, 0.95] {
|
||||
for min_gain in [1.0, 1.03, 1.1] {
|
||||
for probe_after in [8, 16, 24, 48] {
|
||||
grid.push(Policy {
|
||||
min_match,
|
||||
max_draft: MAX_DRAFT,
|
||||
prior,
|
||||
forget,
|
||||
min_gain,
|
||||
probe_after,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A policy with its speed-up on every task and their geometric mean.
|
||||
type PolicyRow = (Policy, Vec<f64>, f64);
|
||||
|
||||
@@ -234,17 +239,28 @@ fn prompt_head(name: &str, source: &str) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
/// Alternating plain and speculative runs of one task; the summary line.
|
||||
fn measure_task(
|
||||
/// The rates of the plain and the speculative runs of one task, whether every output matched the first one, and
|
||||
/// that first output.
|
||||
struct Arms {
|
||||
plain: Vec<f64>,
|
||||
spec: Vec<f64>,
|
||||
identical: bool,
|
||||
reference: Option<Vec<u32>>,
|
||||
}
|
||||
|
||||
/// Alternating plain and speculative runs of one task.
|
||||
fn run_arms(
|
||||
engine: &mut engine::GptOssEngine,
|
||||
stops: &[u32],
|
||||
(i, prompt): (usize, &[u32]),
|
||||
(pairs, max_new): (usize, usize),
|
||||
tokenizer: &GgufBpe,
|
||||
) -> String {
|
||||
let (mut plain, mut spec) = (Vec::new(), Vec::new());
|
||||
let mut reference: Option<Vec<u32>> = None;
|
||||
let mut identical = true;
|
||||
) -> Arms {
|
||||
let mut arms = Arms {
|
||||
plain: Vec::new(),
|
||||
spec: Vec::new(),
|
||||
identical: true,
|
||||
reference: None,
|
||||
};
|
||||
for pair in 0..pairs {
|
||||
let order = if (pair + i) % 2 == 0 {
|
||||
[false, true]
|
||||
@@ -254,8 +270,8 @@ fn measure_task(
|
||||
for speculate in order {
|
||||
engine.set_speculation(speculate);
|
||||
let r = run(engine, stops, prompt, max_new);
|
||||
let same = reference.get_or_insert_with(|| r.tokens.clone()) == &r.tokens;
|
||||
identical &= same;
|
||||
let same = arms.reference.get_or_insert_with(|| r.tokens.clone()) == &r.tokens;
|
||||
arms.identical &= same;
|
||||
let rate = r.tokens.len() as f64 / (r.decode_ms / 1000.0);
|
||||
println!(
|
||||
"{} pair {pair} speculate={speculate}: {} tokens, prompt {:.0} ms, decode {:.0} ms ({rate:.2} tok/s), identical={same}",
|
||||
@@ -264,29 +280,81 @@ fn measure_task(
|
||||
r.prompt_ms,
|
||||
r.decode_ms
|
||||
);
|
||||
if speculate { &mut spec } else { &mut plain }.push(rate);
|
||||
if speculate { &mut arms.spec } else { &mut arms.plain }.push(rate);
|
||||
}
|
||||
}
|
||||
let median = |v: &mut Vec<f64>| {
|
||||
arms
|
||||
}
|
||||
|
||||
/// The summary line of one task's runs.
|
||||
fn summary_line(i: usize, arms: &Arms) -> String {
|
||||
let median = |v: &[f64]| {
|
||||
let mut v = v.to_vec();
|
||||
v.sort_by(f64::total_cmp);
|
||||
v[v.len() / 2]
|
||||
};
|
||||
let ratios: Vec<f64> = spec.iter().zip(&plain).map(|(s, p)| s / p).collect();
|
||||
let line = format!(
|
||||
"{}: plain median {:.2} tok/s, speculate median {:.2} tok/s, paired ratio mean {:.2} (runs {:?}), outputs identical: {identical}",
|
||||
let ratios: Vec<f64> = arms.spec.iter().zip(&arms.plain).map(|(s, p)| s / p).collect();
|
||||
format!(
|
||||
"{}: plain median {:.2} tok/s, speculate median {:.2} tok/s, paired ratio mean {:.2} (runs {:?}), outputs identical: {}",
|
||||
TASKS[i].0,
|
||||
median(&mut plain.clone()),
|
||||
median(&mut spec.clone()),
|
||||
median(&arms.plain),
|
||||
median(&arms.spec),
|
||||
ratios.iter().sum::<f64>() / ratios.len() as f64,
|
||||
ratios.iter().map(|r| (r * 100.0).round() / 100.0).collect::<Vec<_>>()
|
||||
);
|
||||
if let Some(tokens) = reference {
|
||||
ratios.iter().map(|r| (r * 100.0).round() / 100.0).collect::<Vec<_>>(),
|
||||
arms.identical
|
||||
)
|
||||
}
|
||||
|
||||
/// Alternating plain and speculative runs of one task; the summary line.
|
||||
fn measure_task(
|
||||
engine: &mut engine::GptOssEngine,
|
||||
stops: &[u32],
|
||||
task: (usize, &[u32]),
|
||||
settings: (usize, usize),
|
||||
tokenizer: &GgufBpe,
|
||||
) -> String {
|
||||
let i = task.0;
|
||||
let arms = run_arms(engine, stops, task, settings);
|
||||
let line = summary_line(i, &arms);
|
||||
if let Some(tokens) = arms.reference {
|
||||
println!("{} output: {:?}", TASKS[i].0, tokenizer.decode(&tokens));
|
||||
println!("{} ids: {tokens:?}", TASKS[i].0);
|
||||
}
|
||||
line
|
||||
}
|
||||
|
||||
fn read_tokenizer(model: &Path) -> GgufBpe {
|
||||
let size = std::fs::metadata(model).expect("model file").len();
|
||||
let gguf = Gguf::read(
|
||||
BufReader::with_capacity(1 << 20, File::open(model).expect("open")),
|
||||
size,
|
||||
)
|
||||
.expect("valid GGUF");
|
||||
GgufBpe::from_gguf(&gguf).expect("tokenizer")
|
||||
}
|
||||
|
||||
/// The prompt of every task over the first `chars` characters of the source file.
|
||||
fn task_prompts(tokenizer: &GgufBpe, (file, chars): (&str, usize)) -> Vec<Vec<u32>> {
|
||||
let source: String = std::fs::read_to_string(file)
|
||||
.expect("source file")
|
||||
.chars()
|
||||
.take(chars)
|
||||
.collect();
|
||||
let name = Path::new(file)
|
||||
.file_name()
|
||||
.map_or_else(String::new, |n| n.to_string_lossy().into_owned());
|
||||
let head = prompt_head(&name, &source);
|
||||
TASKS
|
||||
.iter()
|
||||
.map(|(_, task)| {
|
||||
tokenizer.encode(
|
||||
&format!("{head}<|start|>user<|message|>{task}<|end|><|start|>assistant"),
|
||||
true,
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
if args.len() < 2 {
|
||||
@@ -298,41 +366,18 @@ fn main() {
|
||||
let threads = arg(4, std::thread::available_parallelism().map_or(1, |n| n.get()));
|
||||
let chars = arg(5, 2400);
|
||||
let model = Path::new(&args[0]);
|
||||
let source: String = std::fs::read_to_string(&args[1])
|
||||
.expect("source file")
|
||||
.chars()
|
||||
.take(chars)
|
||||
.collect();
|
||||
let name = Path::new(&args[1])
|
||||
.file_name()
|
||||
.map_or_else(String::new, |n| n.to_string_lossy().into_owned());
|
||||
|
||||
let size = std::fs::metadata(model).expect("model file").len();
|
||||
let gguf = Gguf::read(
|
||||
BufReader::with_capacity(1 << 20, File::open(model).expect("open")),
|
||||
size,
|
||||
)
|
||||
.expect("valid GGUF");
|
||||
let tokenizer = GgufBpe::from_gguf(&gguf).expect("tokenizer");
|
||||
let head = prompt_head(&name, &source);
|
||||
let prompts: Vec<Vec<u32>> = TASKS
|
||||
.iter()
|
||||
.map(|(_, task)| {
|
||||
tokenizer.encode(
|
||||
&format!("{head}<|start|>user<|message|>{task}<|end|><|start|>assistant"),
|
||||
true,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let tokenizer = read_tokenizer(model);
|
||||
let prompts = task_prompts(&tokenizer, (&args[1], chars));
|
||||
if let Ok(path) = std::env::var("LELEKA_AB_REPLAY") {
|
||||
replay_policies(&tokenizer, &prompts, &path);
|
||||
return;
|
||||
}
|
||||
|
||||
let context = prompts.iter().map(Vec::len).max().unwrap_or(0) + max_new + 64;
|
||||
let loaded = engine::load(&LoadOptions {
|
||||
model,
|
||||
threads,
|
||||
context: prompts.iter().map(Vec::len).max().unwrap_or(0) + max_new + 64,
|
||||
context,
|
||||
warm: true,
|
||||
})
|
||||
.expect("load");
|
||||
|
||||
@@ -128,12 +128,17 @@ fn warm_up(model: &Model, accel: &mut GpuPrefill, prompt: &[u32]) {
|
||||
.expect("warm gpu");
|
||||
}
|
||||
|
||||
/// Alternating pairs of runs; the seconds of each path and the speed-up ratios.
|
||||
fn run_pairs(model: &Model, accel: &mut GpuPrefill, prompt: &[u32], pairs: usize) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
|
||||
/// The column header of the per-pair lines.
|
||||
fn print_pairs_header() {
|
||||
println!(
|
||||
"{:>4} {:>6} {:>10} {:>10} {:>9} next tokens",
|
||||
"pair", "first", "cpu s", "gpu s", "speed-up"
|
||||
);
|
||||
}
|
||||
|
||||
/// Alternating pairs of runs; the seconds of each path and the speed-up ratios.
|
||||
fn run_pairs(model: &Model, accel: &mut GpuPrefill, prompt: &[u32], pairs: usize) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
|
||||
print_pairs_header();
|
||||
let (mut cpu_s, mut gpu_s, mut ratios) = (Vec::new(), Vec::new(), Vec::new());
|
||||
for i in 0..pairs {
|
||||
let gpu_first = i % 2 == 1;
|
||||
@@ -188,6 +193,17 @@ fn summarise(n: usize, pairs: usize, (cpu_s, gpu_s, ratios): (&[f64], &[f64], &[
|
||||
);
|
||||
}
|
||||
|
||||
/// Each prompt size: warm-up, alternating pairs and the summary.
|
||||
fn run_sizes(model: &Model, accel: &mut GpuPrefill, sizes: &[usize], pairs: usize) {
|
||||
for &n in sizes {
|
||||
let prompt = synth(n);
|
||||
warm_up(model, accel, &prompt);
|
||||
println!("\n## {n} prompt tokens, {pairs} pairs");
|
||||
let (cpu_s, gpu_s, ratios) = run_pairs(model, accel, &prompt, pairs);
|
||||
summarise(n, pairs, (&cpu_s, &gpu_s, &ratios));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
if args.len() < 3 {
|
||||
@@ -208,14 +224,5 @@ fn main() {
|
||||
let (model, isa, size) = load_model(path, threads);
|
||||
let mut accel = GpuPrefill::new().expect("a GPU").with_batch(batch);
|
||||
print_header(path, size, (threads, isa), (&accel, batch));
|
||||
for n in sizes {
|
||||
let prompt = synth(n);
|
||||
warm_up(&model, &mut accel, &prompt);
|
||||
println!(
|
||||
"
|
||||
## {n} prompt tokens, {pairs} pairs"
|
||||
);
|
||||
let (cpu_s, gpu_s, ratios) = run_pairs(&model, &mut accel, &prompt, pairs);
|
||||
summarise(n, pairs, (&cpu_s, &gpu_s, &ratios));
|
||||
}
|
||||
run_sizes(&model, &mut accel, &sizes, pairs);
|
||||
}
|
||||
|
||||
@@ -129,12 +129,17 @@ fn warm_up(model: &Model, accel: &mut NpuPrefill, prompt: &[u32]) {
|
||||
.expect("warm npu");
|
||||
}
|
||||
|
||||
/// Alternating pairs of runs; the seconds of each path and the speed-up ratios.
|
||||
fn run_pairs(model: &Model, accel: &mut NpuPrefill, prompt: &[u32], pairs: usize) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
|
||||
/// The column header of the per-pair lines.
|
||||
fn print_pairs_header() {
|
||||
println!(
|
||||
"{:>4} {:>6} {:>10} {:>10} {:>9} next tokens",
|
||||
"pair", "first", "cpu s", "npu s", "speed-up"
|
||||
);
|
||||
}
|
||||
|
||||
/// Alternating pairs of runs; the seconds of each path and the speed-up ratios.
|
||||
fn run_pairs(model: &Model, accel: &mut NpuPrefill, prompt: &[u32], pairs: usize) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
|
||||
print_pairs_header();
|
||||
let (mut cpu_s, mut npu_s, mut ratios) = (Vec::new(), Vec::new(), Vec::new());
|
||||
for i in 0..pairs {
|
||||
let npu_first = i % 2 == 1;
|
||||
@@ -189,6 +194,17 @@ fn summarise(n: usize, pairs: usize, (cpu_s, npu_s, ratios): (&[f64], &[f64], &[
|
||||
);
|
||||
}
|
||||
|
||||
/// Each prompt size: warm-up, alternating pairs and the summary.
|
||||
fn run_sizes(model: &Model, accel: &mut NpuPrefill, sizes: &[usize], pairs: usize) {
|
||||
for &n in sizes {
|
||||
let prompt = synth(n);
|
||||
warm_up(model, accel, &prompt);
|
||||
println!("\n## {n} prompt tokens, {pairs} pairs");
|
||||
let (cpu_s, npu_s, ratios) = run_pairs(model, accel, &prompt, pairs);
|
||||
summarise(n, pairs, (&cpu_s, &npu_s, &ratios));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
if args.len() < 6 {
|
||||
@@ -218,14 +234,5 @@ fn main() {
|
||||
};
|
||||
let mut accel = NpuPrefill::start(&config, batch).expect("start the NPU service");
|
||||
print_header(path, size, (threads, isa), (&accel, batch));
|
||||
for n in sizes {
|
||||
let prompt = synth(n);
|
||||
warm_up(&model, &mut accel, &prompt);
|
||||
println!(
|
||||
"
|
||||
## {n} prompt tokens, {pairs} pairs"
|
||||
);
|
||||
let (cpu_s, npu_s, ratios) = run_pairs(&model, &mut accel, &prompt, pairs);
|
||||
summarise(n, pairs, (&cpu_s, &npu_s, &ratios));
|
||||
}
|
||||
run_sizes(&model, &mut accel, &sizes, pairs);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user