Repository navigation
inkling2: batch host expert matvecs (with natural-prompt benchmarks) - #80
Conversation
`run_experts` computed each host expert with one `Mat::matvec` call, which starts a nested Rayon pass per matrix and quantizes the activation once per expert. `matvec_many_cpu` does all matrices in a single parallel sweep: the activation is quantized once and the weight rows are read in the same pass. Measured on RTX 3050 Laptop (4 GB), Inkling 2 Mini UD-Q3_K_XL, natural prose prompt, `--pp 256 --tg 32`, interleaved A/B/A/B runs: GPU hybrid decode 5.5-5.6 -> 7.0-7.5 tok/s (+25-36%) GPU hybrid prefill ~40 -> ~45 tok/s Host-only natural decode 4.3 -> 5.0 tok/s Zeros-temperature generation with natural prose and a Go coding task is byte-identical to HEAD with and without --gpu. Co-authored-by: HY4-Flash <hy4-flash@users.noreply.github.com>
🔍 Aletheore evidence diffgraph LR
n0["mat.rs"]
n1["src/inkling2/forward.rs"]
n2["src/deepseek/forward.rs"]
n3["src/deepseek/mod.rs"]
n4["src/gemma4/forward.rs"]
n5["src/glm/forward.rs"]
n6["src/hy4/forward.rs"]
n7["chat.rs"]
n8["dspark.rs"]
n9["device.rs"]
n10["src/inkling2/mod.rs"]
n11["main.rs"]
n12["quant.rs"]
n13["vision.rs"]
n0 --> n2
n0 --> n3
n0 --> n4
n0 --> n5
n0 --> n6
n0 --> n8
n0 --> n9
n0 --> n11
n0 --> n12
n1 --> n7
n1 --> n8
n1 --> n9
n1 --> n10
n1 --> n11
n1 --> n13
classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
class n0 changed
class n1 changed
class n2 dependent
class n3 dependent
class n4 dependent
class n5 dependent
class n6 dependent
class n7 dependent
class n8 dependent
class n9 dependent
class n10 dependent
class n11 dependent
class n12 dependent
class n13 dependent
What changed
No new secrets, vulnerabilities, or layer violations. ✅ |
| } | ||
|
|
||
| /// The batched path of `matvec_many`, or None when the matrices cannot share one GPU batch (or batching is off). | ||
| /// Host integer dot of several matrices, one activation each, in a single Rayon pass. |
There was a problem hiding this comment.
🟠 High · Correctness · 1 of 6
New fn is inserted between matvec_many_opt's doc comment and its definition, so the doc now describes matvec_many_cpu and matvec_many_opt loses its doc. Rustdoc renders the batched-path description under matvec_many_cpu and leaves matvec_many_opt undocumented. Maintainers reading the docs or hovering in an IDE get the wrong description for both functions.
Prompt for your AI agent
File: src/deepseek/mat.rs
Line: 490
Symbol: matvec_many_opt
Issue: New fn is inserted between matvec_many_opt's doc comment and its definition, so the doc now describes matvec_many_cpu and matvec_many_opt loses its doc. Rustdoc renders the batched-path description under matvec_many_cpu and leaves matvec_many_opt undocumented. Maintainers reading the docs or hovering in an IDE get the wrong description for both functions.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| let downs: Vec<Mat> = list.iter().map(|e| e.1).collect(); | ||
| let gs: Vec<f32> = list.iter().map(|e| e.3).collect(); | ||
| let gx: Vec<&[f32]> = vec![x; list.len()]; | ||
| if let Some(y) = matvec_many_cpu(&gus, &gx) { |
There was a problem hiding this comment.
🔵 Low · Performance · 2 of 6
Passing vec![x; n] makes matvec_many_cpu quantize the same activation n times, once per expert. With top-k experts per token, quantize_q8k runs on an identical input k times per layer per token, multiplying the Q8K quantization cost on the decode hot path. Quantizing x once and sharing the result would avoid it.
Prompt for your AI agent
File: src/inkling2/forward.rs
Line: 435
Symbol: run_experts
Issue: Passing vec![x; n] makes matvec_many_cpu quantize the same activation n times, once per expert. With top-k experts per token, quantize_q8k runs on an identical input k times per layer per token, multiplying the Q8K quantization cost on the decode hot path. Quantizing x once and sharing the result would avoid it.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| } | ||
| } | ||
| None => { | ||
| let gus: Vec<Mat> = list.iter().map(|e| e.0).collect(); |
There was a problem hiding this comment.
🟠 High · Correctness · 3 of 6
list.iter().map(|e| e.0) moves Mat out of a shared reference, which only compiles if Mat is Copy. If Mat is not Copy, the build fails with E0507 (cannot move out of a shared reference). Cloning or borrowing into a Vec<&Mat> would be needed.
Prompt for your AI agent
File: src/inkling2/forward.rs
Line: 431
Symbol: run_experts
Issue: list.iter().map(|e| e.0) moves Mat out of a shared reference, which only compiles if Mat is Copy. If Mat is not Copy, the build fails with E0507 (cannot move out of a shared reference). Cloning or borrowing into a Vec<&Mat> would be needed.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| let r = c * 32 + k; | ||
| let i = starts.partition_point(|(s, _)| *s <= r) - 1; | ||
| let (s, m) = starts[i]; | ||
| *o = dot(&m.data[(r - s) * rb..(r - s + 1) * rb], &xq[i]) * m.scale; |
There was a problem hiding this comment.
🟡 Medium · Correctness · 4 of 6
Row slice m.data[(r-s)rb..] is indexed with no check that data holds rowsrb bytes. A Mat whose data buffer is shorter than rows*row_bytes (for example a truncated mmap or a mismatched cols/ty) makes the Rayon worker panic on an out-of-range slice, which aborts the whole forward pass instead of returning None and falling back.
Prompt for your AI agent
File: src/deepseek/mat.rs
Line: 509
Symbol: matvec_rows
Issue: Row slice m.data[(r-s)*rb..] is indexed with no check that data holds rows*rb bytes. A Mat whose data buffer is shorter than rows*row_bytes (for example a truncated mmap or a mismatched cols/ty) makes the Rayon worker panic on an out-of-range slice, which aborts the whole forward pass instead of returning None and falling back.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
|
|
||
| /// The batched path of `matvec_many`, or None when the matrices cannot share one GPU batch (or batching is off). | ||
| /// Host integer dot of several matrices, one activation each, in a single Rayon pass. | ||
| pub fn matvec_many_cpu<'a>(ms: &[Mat<'a>], xs: &[&[f32]]) -> Option<Vec<f32>> { |
There was a problem hiding this comment.
🔵 Low · Maintainability · 5 of 6
New integer-dot CPU path has no test comparing it against the float reference path. A wrong row-to-matrix mapping in the partition_point logic, or a scale error, would produce wrong expert outputs with no failing test. The PR title mentions benchmarks, but no correctness test appears in the diff.
Prompt for your AI agent
File: src/deepseek/mat.rs
Line: 491
Symbol: matvec_many_opt
Issue: New integer-dot CPU path has no test comparing it against the float reference path. A wrong row-to-matrix mapping in the partition_point logic, or a scale error, would produce wrong expert outputs with no failing test. The PR title mentions benchmarks, but no correctness test appears in the diff.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| let ty = ms.first()?.ty; | ||
| if ms.len() != xs.len() || ms.iter().any(|m| m.ty != ty || m.gpu.is_some() || m.cols != xs[0].len()) | ||
| || xs.iter().any(|x| x.len() != xs[0].len()) { return None; } | ||
| if std::env::var_os("STRATA_CPU_REF").is_some() || xs[0].len() % ty.block_elems() != 0 |
There was a problem hiding this comment.
🔵 Low · Performance · 6 of 6
std::env::var_os(STRATA_CPU_REF) is read on every call to matvec_many_cpu in the per-token expert path. The environment is queried once per layer per token. Caching the flag in a OnceLock or passing it through the config would remove that repeated lookup.
Prompt for your AI agent
File: src/deepseek/mat.rs
Line: 495
Issue: std::env::var_os(STRATA_CPU_REF) is read on every call to matvec_many_cpu in the per-token expert path. The environment is queried once per layer per token. Caching the flag in a OnceLock or passing it through the config would remove that repeated lookup.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
Aletheore Flash Lite reviewWhat this PR does. This PR adds a CPU path that computes integer dot products for multiple MoE expert matrices in one Rayon-parallel pass, and wires it into the Inkling2
6 finding(s) posted as inline review comment(s).
Review detailsReviewed 2 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 6 of 6 proposed finding(s). Fix all with your agentBlast radius: 53 other file(s) depend on what this PR changes (12 directly, 41 indirectly)Computed from the import graph of this exact commit, not guessed by a model. It sees imports only, so dynamic imports, reflection and non-code references are not counted.
Indirectly affected: |
|
Hi, Hi, ty for PR
Greedy output is byte-identical to main, host-only and with --gpu. Two things I changed in the merge:
|
Thanks to you for pointing those out and fixing it! Kudos |
Summary
Batch the host experts of one Inkling 2 layer into a single Rayon pass.
run_expertscurrently callsMat::matveconce per expert. Each call starts its own nested parallel loop and quantizes the activation again.matvec_many_cpuquantizes once and reads all weight rows in one sweep; the existing per-expert loop remains as a fallback for quant types with no integer dot kernel.This is the
matvec_many_cpuchange from PR #74, rebased onto currentmainand measured on natural prose and a coding task rather than the repeating bench paragraph.Benchmarks — RTX 3050 Laptop (4 GB), Inkling 2 Mini UD-Q3_K_XL
bench --prompt-file <natural prose> --pp 256 --tg 32 --reps 2, four interleaved A/B/B/A runs (baseline A = current main, B = this PR):Decode improves ~25-36%; prefill is modestly better. Host-only (
STRATA_DS_GPU=0) natural prose: decode 4.3 → 5.0 tok/s. A Go coding-task prompt: GPU hybrid decode 7.3 → 7.7 tok/s. The coding-task host-only case was flat (4.1 → 3.8), so the gain is prompt-shape dependent but consistently positive where the hybrid path actually uses host experts.Quality
generate --temp 0 --seed 1 -n 48with both the natural prose and Go coding prompts, with and without--gpu: byte-identical output to currentmainin all four cases (the helper returns the same sums, only the order of the parallel work changes).Scope
Only
src/deepseek/mat.rs(new helper) andsrc/inkling2/forward.rs(call site). No spec-decoding defaults, no draft-length changes, no KV changes, no docs moved.Co-authored-by: HY4-Flash