Skip to content

inkling2: use few-row batched GEMM for draft verification - #125

Open
sayampy wants to merge 1 commit into
RustingStudio:mainfrom
sayampy:gemm/batched-verify
Open

sayampy wants to merge 1 commit into
RustingStudio:mainfrom
sayampy:gemm/batched-verify

Conversation

@sayampy

@sayampy sayampy commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Inkling 2 verifies up to 16 draft rows per step, but its GEMMs used the prefill tile path, which decodes weights per tile. This marks verified-draft GEMMs (attention/router projections and the batched LM head) with BATCHED on CUDA so the launcher uses the existing few-row int8 kernel, which decodes weights once into MMA fragments — the same idea as llama.cpp's few-row MMQ for speculative verification, using this repo's existing kernel. Plain prefill, the matvec path (nt <= 6), Vulkan and HIP are unchanged.

Verification

  • tests/inkling2_forward.rs 4/4 and tests/kernels.rs::small_batch_gemm_matches_tile_gemm pass on CUDA.
  • 1024-token speculative run: 960/960 drafts accepted, output byte-identical to baseline (top-1 1024/1024). Teacher-forced check over 1024 positions: top-1 1024/1024, mean KL 2.15e-5 nats, PPL 1.000629 -> 1.000625.
  • Tiel Coder (unchanged code path, run as a control): bench decode 28.9 -> 35.0 tok/s at pp 512 and 31.5 -> 34.8 at pp 2048; teacher-forced top-1 493/510, mean KL 1.49e-2. This variation is run to run and is not caused by this change.

I did not get any measurable throughput improvement from this change.

be.gemm_dense(out_w, nt, pf.p.hn, n, pf.logits_buf.addr(), vrows, 0)?;
be.gemm_dense(out_w, nt, pf.p.hn, n, pf.logits_buf.addr(), vrows,
if be.name() == "cuda" { crate::backend::BATCHED } else { 0 })?;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Correctness · 1 of 5

BATCHED is applied to the logits GEMM on CUDA regardless of verify mode, so plain prefill with many rows may take the few-row kernel. The logits GEMM now passes BATCHED whenever the backend is CUDA, not only during draft verification. A large plain-prefill batch with many vrows is then sent to the few-row int8 kernel, which is meant for a handful of rows. That can be much slower than the tile-list path, or incorrect if the kernel assumes a small row count.

Prompt for your AI agent
File: src/inkling2/forward/device.rs
Line: 1118
Symbol: verify_forward
Issue: BATCHED is applied to the logits GEMM on CUDA regardless of verify mode, so plain prefill with many rows may take the few-row kernel. The logits GEMM now passes BATCHED whenever the backend is CUDA, not only during draft verification. A large plain-prefill batch with many vrows is then sent to the few-row int8 kernel, which is meant for a handful of rows. That can be much slower than the tile-list path, or incorrect if the kernel assumes a small row count.
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.

@@ -1211,6 +1215,7 @@ impl Device {
let be = &*d.parts[0].be;
let w = |x: &Mat| d.tensor(x.gpu.unwrap());
let small = Self::small(be, nt, verify_mode);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low · Maintainability · 2 of 5

Backend is identified by string comparison in device code instead of a capability query. Whether BATCHED is used depends on the literal string "cuda". A renamed or new CUDA-like backend silently falls back to the slow path. A backend with a different name that supports the kernel is never used. Changing the name would silently disable the optimisation everywhere.

Prompt for your AI agent
File: src/inkling2/forward/device.rs
Line: 1217
Symbol: prefill_layer
Issue: Backend is identified by string comparison in device code instead of a capability query. Whether BATCHED is used depends on the literal string "cuda". A renamed or new CUDA-like backend silently falls back to the slow path. A backend with a different name that supports the kernel is never used. Changing the name would silently disable the optimisation everywhere.
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.

@@ -1211,6 +1215,7 @@ impl Device {
let be = &*d.parts[0].be;
let w = |x: &Mat| d.tensor(x.gpu.unwrap());
let small = Self::small(be, nt, verify_mode);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low · Maintainability · 3 of 5

The batched decision belongs in the launcher, which already sees nt and the shape, not in each caller. Every call site must remember to pass the right flag. The new flag is threaded through proj_n and pf_ffn_core, and it is easy to omit at a new call site, so that projection silently takes the slower kernel. The launcher could choose the few-row kernel itself from nt and the backend's capabilities.

Prompt for your AI agent
File: src/inkling2/forward/device.rs
Line: 1217
Symbol: prefill_layer
Issue: The batched decision belongs in the launcher, which already sees nt and the shape, not in each caller. Every call site must remember to pass the right flag. The new flag is threaded through proj_n and pf_ffn_core, and it is easy to omit at a new call site, so that projection silently takes the slower kernel. The launcher could choose the few-row kernel itself from nt and the backend's capabilities.
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.

@@ -1340,7 +1345,7 @@ impl Device {
m.lap(0, t_prep);
let t_gpu = std::time::Instant::now();
if fe.part == 0 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low · Maintainability · 4 of 5

Positional bool parameters are growing (batched added to pf_ffn_core with an allow(clippy::too_many_arguments)). pf_ffn_core now takes nine arguments, two of them bools whose meaning is set only by position. Callers can swap small, batched and use_gemm without a compile error. Deriving batched from verify_mode and small inside the callee, or bundling flags in a struct, would remove this risk.

Prompt for your AI agent
File: src/inkling2/forward/device.rs
Line: 1347
Symbol: prefill_layer
Issue: Positional bool parameters are growing (batched added to pf_ffn_core with an allow(clippy::too_many_arguments)). pf_ffn_core now takes nine arguments, two of them bools whose meaning is set only by position. Callers can swap small, batched and use_gemm without a compile error. Deriving batched from verify_mode and small inside the callee, or bundling flags in a struct, would remove this risk.
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.

}
Self::pf_ffn_core(be1, m, ip, fe, pf1, nt, small, use_gemm)?;
Self::pf_ffn_core(be1, m, ip, fe, pf1, nt, small, batched, use_gemm)?;
let acc: Vec<f32> = be1.get(&pf1.acc_buf, 0, nt * n)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low · Correctness · 5 of 5

batched is computed from device 0's backend (be) but reused for the be1 path. In the multi-device branch, pf_ffn_core runs on be1 with the batched value computed from be. If be1 is a different backend, or the name check differs between devices, the flag is wrong for that device, and its GEMMs take a kernel it does not support or skip one it could use.

Prompt for your AI agent
File: src/inkling2/forward/device.rs
Line: 1363
Symbol: prefill_layer
Issue: batched is computed from device 0's backend (be) but reused for the be1 path. In the multi-device branch, pf_ffn_core runs on be1 with the batched value computed from be. If be1 is a different backend, or the name check differs between devices, the flag is wrong for that device, and its GEMMs take a kernel it does not support or skip one it could use.
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

aletheore Bot commented Nov 26, 2026

Copy link
Copy Markdown

Aletheore Flash Lite review

What this PR does. This pull request changes the CUDA verification path for the inkling2 model so that draft-token checks use a few-row batched int8 GEMM kernel instead of the tile-list prefill GEMM, by threading a new batched flag through the projection and FFN helpers. It affects the inkling2 forward pass on CUDA, specifically speculative-decoding verification (verify_mode) for projections, FFN gate, and output logits, while the non-CUDA backends, plain prefill, and the matvec path stay as before.

Merge confidence Review effort Merge risk
3 / 5 1 / 5 Medium
Area Files Lines
src/inkling2 1 +21 −16

5 finding(s) posted as inline review comment(s).

# Severity Category Where
1 🟠 High Correctness device.rs:1118 BATCHED is applied to the logits GEMM on CUDA regardless of verify mode, so plain prefill with many rows may…
2 🔵 Low Maintainability device.rs:1217 Backend is identified by string comparison in device code instead of a capability query. Whether BATCHED is…
3 🔵 Low Maintainability device.rs:1217 The batched decision belongs in the launcher, which already sees nt and the shape, not in each caller. Every…
4 🔵 Low Maintainability device.rs:1347 Positional bool parameters are growing (batched added to pf_ffn_core with an…
5 🔵 Low Correctness device.rs:1363 batched is computed from device 0's backend (be) but reused for the be1 path. In the multi-device branch…
Review details

Reviewed 1 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 5 of 5 proposed finding(s).

Fix all with your agent
File: src/inkling2/forward/device.rs
Line: 1118
Symbol: verify_forward
Issue: BATCHED is applied to the logits GEMM on CUDA regardless of verify mode, so plain prefill with many rows may take the few-row kernel. The logits GEMM now passes BATCHED whenever the backend is CUDA, not only during draft verification. A large plain-prefill batch with many vrows is then sent to the few-row int8 kernel, which is meant for a handful of rows. That can be much slower than the tile-list path, or incorrect if the kernel assumes a small row count.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/inkling2/forward/device.rs
Line: 1217
Symbol: prefill_layer
Issue: Backend is identified by string comparison in device code instead of a capability query. Whether BATCHED is used depends on the literal string "cuda". A renamed or new CUDA-like backend silently falls back to the slow path. A backend with a different name that supports the kernel is never used. Changing the name would silently disable the optimisation everywhere.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/inkling2/forward/device.rs
Line: 1217
Symbol: prefill_layer
Issue: The batched decision belongs in the launcher, which already sees nt and the shape, not in each caller. Every call site must remember to pass the right flag. The new flag is threaded through proj_n and pf_ffn_core, and it is easy to omit at a new call site, so that projection silently takes the slower kernel. The launcher could choose the few-row kernel itself from nt and the backend's capabilities.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/inkling2/forward/device.rs
Line: 1347
Symbol: prefill_layer
Issue: Positional bool parameters are growing (batched added to pf_ffn_core with an allow(clippy::too_many_arguments)). pf_ffn_core now takes nine arguments, two of them bools whose meaning is set only by position. Callers can swap small, batched and use_gemm without a compile error. Deriving batched from verify_mode and small inside the callee, or bundling flags in a struct, would remove this risk.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/inkling2/forward/device.rs
Line: 1363
Symbol: prefill_layer
Issue: batched is computed from device 0's backend (be) but reused for the be1 path. In the multi-device branch, pf_ffn_core runs on be1 with the batched value computed from be. If be1 is a different backend, or the name check differs between devices, the flag is wrong for that device, and its GEMMs take a kernel it does not support or skip one it could use.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

@aletheore

aletheore Bot commented Nov 26, 2026

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph LR
    n0["device.rs"]
    n1["forward.rs"]
    n0 --> n1
    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 dependent
Loading

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • src/inkling2/forward/device.rs · modified · +21/-16 · 1 other dependent

Aggregate deltas

  • Modules: +0
  • Dependency graph edges: +0
  • Commits: 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant