Repository navigation
Use quantized matvec kernels for CPU generation #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,9 @@ struct Engine { | |
| /// Run on the GPU (experts that do not fit in VRAM run on the CPU) instead of the CPU reference. | ||
| #[arg(long)] | ||
| gpu: bool, | ||
| /// Accelerate CPU generation using Q8_K activations. This can change logits and generated tokens. | ||
| #[arg(long)] | ||
| fast_cpu: bool, | ||
| /// GPU context length (KV cache size) in tokens. Inkling 2, Hy4, GLM, DeepSeek and Gemma 4: the prompt plus reply | ||
| /// limit, the model's trained context when not given (their caches grow with the text). | ||
| #[arg(long, default_value_t = 4096)] | ||
|
|
@@ -786,6 +789,10 @@ fn chunk_env_updates(n: usize, present: impl Fn(&str) -> bool) -> Vec<(&'static | |
| } | ||
|
|
||
| impl Engine { | ||
| fn load_inference_model<'a>(&self, g: &'a Gguf, aux: Option<&'a Gguf>) -> anyhow::Result<Model<'a>> { | ||
| Model::load_with_matvec(g, aux, &self.lora_specs(), self.fast_cpu && !self.gpu) | ||
| } | ||
|
|
||
| pub fn lora_specs(&self) -> Vec<(String, f32)> { | ||
| self.lora.iter().map(|s| { | ||
| if let Some(idx) = s.rfind(':') { | ||
|
|
@@ -1365,7 +1372,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> { | |
| return rusting_strata::inkling2::chat::generate_image_opts(&g, &prompt, Some((&input.grids, &input.embeddings)), n, opts); | ||
| } | ||
| let aux_g = open_mtp_gguf(engine.aux_model_path())?; | ||
| let m = Model::load_with_aux(&g, aux_g.as_ref(), &engine.lora_specs())?; | ||
| let m = engine.load_inference_model(&g, aux_g.as_ref())?; | ||
| let tok = Tokenizer::from_gguf(&g)?; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Maintainability · 4 of 6 The same load-then-set-flag chain is copied into four call sites. Each of the four sites (three in run, one in serve_model) repeats load_with_aux followed by .with_fast_matvec(!engine.gpu). A future load site that omits the call silently gets the slow path, and the GPU-to-CPU mapping lives in four places. Passing the flag into Model::load_with_aux once would remove the duplication. Prompt for your AI agentReply |
||
| let prompt = if prompt == "-" { | ||
| let mut s = String::new(); | ||
|
|
@@ -1470,7 +1477,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> { | |
| } | ||
| } | ||
| let aux_g = open_mtp_gguf(engine.aux_model_path())?; | ||
| let m = Model::load_with_aux(&g, aux_g.as_ref(), &engine.lora_specs())?; | ||
| let m = engine.load_inference_model(&g, aux_g.as_ref())?; | ||
| let tok = Tokenizer::from_gguf(&g)?; | ||
| let mut r = engine.runner(&m, &model)?; | ||
| let mut results = Vec::with_capacity(pp.len()); | ||
|
|
@@ -1706,7 +1713,7 @@ fn run(cmd: Cmd) -> anyhow::Result<()> { | |
| }; | ||
| } | ||
| let aux_g = open_mtp_gguf(engine.aux_model_path())?; | ||
| let m = Model::load_with_aux(&g, aux_g.as_ref(), &engine.lora_specs())?; | ||
| let m = engine.load_inference_model(&g, aux_g.as_ref())?; | ||
| let tok = Tokenizer::from_gguf(&g)?; | ||
| let image_input = if image.is_empty() { None } else { | ||
| let mmproj = engine.mmproj.clone().or_else(|| vision::find_sibling(&model)); | ||
|
|
@@ -3635,7 +3642,7 @@ fn serve_model(model: &Path, mut engine: Engine, s: &Serving, w: &mut Option<Tcp | |
| }) | ||
| } else { | ||
| aux_g = open_mtp_gguf(engine.aux_model_path())?; | ||
| m = { let mut m = Model::load_with_aux(&g, aux_g.as_ref(), &engine.lora_specs())?; if !engine.lora.is_empty() { m.apply_lora(&engine.lora_specs())?; } m }; | ||
| m = engine.load_inference_model(&g, aux_g.as_ref())?; | ||
| engine.runner(&m, model)? | ||
| }; | ||
| // Serving decodes one reply after a (usually cached) prompt: follow the reply's routing, not the prompt's. | ||
|
|
@@ -4320,6 +4327,29 @@ mod bench_tests { | |
| use serde_json::Value; | ||
| use std::path::Path; | ||
|
|
||
| #[test] | ||
| fn cpu_inference_loader_selects_requested_numerics() { | ||
| use super::{Cmd, Gguf, Model, State, ops}; | ||
| let Some(path) = std::env::var_os("STRATA_CPU_PARITY_MODEL") else { return }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Maintainability · 2 of 4 CPU parity test returns early and passes silently when the env var is unset. Without STRATA_CPU_PARITY_MODEL set, the test returns immediately and reports success. CI never checks that CPU generation logits match the reference path, so a regression in the loader or kernels goes unnoticed. Prompt for your AI agentReply |
||
| let g = Gguf::open(Path::new(&path)).unwrap(); | ||
| let reference = Model::load(&g).unwrap(); | ||
| let Some(Cmd::Bench { mut engine, .. }) = Cli::parse_from(["rustingstrata", "bench", "--model", "fixture"]).cmd else { unreachable!() }; | ||
| let model = engine.load_inference_model(&g, None).unwrap(); | ||
| let mut a = State::new(&reference.cfg); | ||
| let mut b = State::new(&model.cfg); | ||
| let mut token = 42; | ||
| for _ in 0..4 { | ||
| let want = reference.step(&mut a, token); | ||
| let got = model.step(&mut b, token); | ||
| assert_eq!(got, want, "CPU generation logits changed at step {}", b.pos); | ||
| token = ops::argmax(&want) as u32; | ||
| } | ||
| engine.fast_cpu = true; | ||
| let fast = engine.load_inference_model(&g, None).unwrap(); | ||
| let expected = Model::load_with_matvec(&g, None, &[], true).unwrap(); | ||
| assert_eq!(fast.step(&mut State::new(&fast.cfg), token), expected.step(&mut State::new(&expected.cfg), token)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Maintainability · 3 of 4 Final assertion compares two identically configured models, so it checks nothing. expected is built with load_with_matvec(..., true) and fast is built from engine.load_inference_model with fast_cpu=true, which is the same configuration. The assertion passes whether or not the engine wiring is correct, so the test does not verify that the flag selects the requested numerics. Prompt for your AI agentReply |
||
| } | ||
|
|
||
| fn meta() -> BenchMeta<'static> { | ||
| BenchMeta { | ||
| backend: "cpu", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High · Correctness · 1 of 4
New --fast-cpu flag defaults to off, so default CPU generation loses the quantized kernels the PR enables. Before this push, CPU generate/chat/serve/bench used with_fast_matvec(!engine.gpu), so the quantized kernels ran by default. Now load_inference_model only enables them when the bool --fast-cpu is passed. A user running the reported model with the same flags as before gets the floating-point path again, so the claimed 0.6 to 5.7 tps prefill and 0.4 to 5.5 tps decode gain does not occur.
Prompt for your AI agent
Reply
/dismiss(optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.