Skip to content

bench: --candidate chooses between implementations of an op in the real graph - #145

Merged
xiaguan merged 3 commits into
masterfrom
bench-candidates
Nov 18, 2026
Merged

xiaguan merged 3 commits into
masterfrom
bench-candidates

Conversation

@xiaguan

@xiaguan xiaguan commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

An op is an interface and an implementation, so "which kernel, at which launch geometry, for which batch size" is a question about one op, and every answer is a manifest that differs in that op's impl. Today that answer is tuned by hand in a microbench outside the graph: a launcher for that kernel's parameter layout, inputs, weights the cache has not seen, a reference. This makes it a kern bench run, where the launcher is the runtime, the inputs and the cache are the program's own, and a candidate is a file with no code in it.

What it adds

A candidate is a file with the modules and ops sections of a manifest and nothing else. A kernel in another language, the same cubin at another grid, and the same grid over another cubin are all candidates; tuning geometry needs no syntax of its own, and a directory of files written by the kernel author's script is the sweep.

kern_manifest::candidate (pure host code, property-tested):

  • splice puts a candidate into a verified manifest: params must equal the op's, modules travel with their launches (renamed after their hash on a name clash, pruned when nothing launches them), and the result is verified again.
  • tile assembles one implementation from several, each under when in its own range of a var.

kern bench --candidate <file|dir>:

  1. Runs the manifest, every candidate spliced into it, and the manifest again, over the same workload in one process with the weights resident from one to the next.
  2. Holds each candidate to the manifest's outputs scenario by scenario, on every rank: tokens, counts, and every row of every distribution over tokens the program writes (a logits* buffer or any float buffer as wide as the token domain). identical may be chosen from, and so may within: every row within kern test's 0.01 nat, with tokens or counts moved only where some row changed its argmax. A row with a NaN on one side is infinitely far. Anything else is measured and reported, never chosen.
  3. Per op, picks the tiling of fewest ranges whose cost is within the noise of choosing per value (bench::pick, property-tested against every tiling written out). The noise is what the manifest's own two runs disagree by. A boundary that falls between two measured values more than a doubling apart is named in a gap line: the values between ran in no scenario.
  4. Runs the picked manifest and holds it to the same outputs. If a combination of merely-within candidates does not hold, the choice is made again from the identical alone.
  5. Writes every run, the tables each pick was made from, and picked: the choice as a candidate file.

Bench::load now goes through load_ranks, so bench has no loader of its own. No schema change.

Measured

One GB300, 2026-11-18, software timestamps. Qwen3.8-27B FP8 over the 57 round scenarios of its served trace (73227 calls), five W8A16 GEMM ops.

Baseline Candidates Result
CuTe DSL GEMM, one 64-row tile for every batch its five row tiles, no when all bit-identical; pick tiles tokens in 4–5 ranges, 2220.7 → 2037.6 s (predicted 2039.9). The kernel's author had 2039.7 s by hand
the same kernel, hand-tiled in five ranges the same five 2039.7 → 2038.2 s (predicted 2038.5)
served manifest (Triton GEMM) the DSL kernel, a hand-written tcgen05 kernel, two controls DSL is −25.8% and within the limit in 53 of 57 scenarios; taken at the two ranges where it held everywhere, 2752.8 → 2391.4 s (predicted 2392.3), within the limit in every scenario
  • The manifest's two runs are bit-identical in every scenario and under 0.01% apart in cost.
  • Predicted (ops picked separately, summed) against measured (one manifest): 0.01%–0.11% apart.
  • Against a careful standalone microbench of the same kernels (some 330 lines its author wrote, weights rotated so none is warm): per call the graph's time is 0.96–1.06 of it over 25 op and row-count pairs, and nobody wrote a harness.
  • With one measured row count left out and priced under what then covers it, an op costs 6%–82% more there than its best candidate, which is what the gap line is for.
  • Controls: a candidate that launches only at seqs 3..4 holds in exactly the 12 scenarios there; a build missing a fence between its TMA writes and reads does not reproduce its own tokens, is reported failed, and the run goes on.
  • A pick fed back as the only candidate comes out byte for byte.

What the gate cannot see

A program that drafts its own tokens feeds each row what the last one chose, so one tie that flips changes every row after it. A candidate that is not bit-identical seldom holds in every scenario of such a program (the third row above), and those are kern test e2e-logits's to judge; once accepted, the kernel is the manifest and its variants are identical to it (the first two rows). docs/bench.md says this in the new Candidates section, with the limits: one var per op, values between measured ones follow the larger, and the manifest's distributions stay on the device for the run.

Checked

cargo fmt --check, cargo clippy --workspace --exclude kern-py --all-targets -- -D warnings, cargo test --workspace --exclude kern-py, hawk's restricted-visibility rule, the schema golden and the protocol-names check pass locally.

🤖 Generated with Claude Code

…al graph

An op is an interface and an implementation, so which kernel runs, at
which launch geometry, for which batch size is a question about one op,
and every answer is a manifest that differs in that op's `impl`. A
candidate is such an answer as a file: `modules` and `ops`, nothing else.
A kernel in another language, the same cubin at another grid and the same
grid over another cubin are all candidates; tuning geometry needs no
syntax of its own.

kern-manifest gains `candidate`: `splice` puts a candidate into a verified
manifest (the interface is checked, modules travel with their launches,
the result is verified again), and `tile` assembles one implementation
from several, each under `when` in its own range of a var.

`kern bench --candidate <file|dir>` runs the manifest, every candidate
spliced into it, and the manifest again over the same workload in one
process, weights resident from one to the next. A candidate is held to
the manifest's outputs scenario by scenario (tokens, counts and every row
of every distribution over tokens the program writes) and is chosen from
only where it is identical or within kern test's KL limit. Per op, the
pick is the tiling of fewest ranges whose cost is within the noise of
choosing per value, the noise being what the manifest's two runs disagree
by. The picked manifest is then run and held to the same outputs; if a
combination of merely-within candidates does not hold, the choice is made
again from the identical alone. The report carries every run and the pick
as a candidate file.

`Bench::load` goes through `load_ranks`, so bench no longer has a loader
of its own.

Measured on one GB300 (2026-11-18, software timestamps), Qwen3.8-27B FP8
over the 57 round scenarios of its served trace, five W8A16 GEMM ops:
from one 64-row tile for every batch the pick tiles `tokens` in four to
five ranges, 2220.7 -> 2037.6 s measured against 2039.9 predicted (the
kernel's author had 2039.7 by hand); against the served Triton GEMM a
CuTe DSL kernel is within the limit in 53 of 57 scenarios and is taken
where it held everywhere, 2753.0 -> 2391.1 s against 2392.7 predicted. A
candidate that runs nothing outside one range holds only there, one that
does not reproduce its own tokens is reported failed, and a pick fed back
as the only candidate comes out byte for byte.

Signed-off-by: JinYan Su <751080330@qq.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T10:16:02.569513Z a13876c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a13876cee0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/kern-run/src/bench/candidates.rs Outdated
rows: rows.len(),
rows_differ: rows.iter().filter(|l| l.cmp.n_diff > 0).count(),
kl_sum: rows.iter().map(|l| l.kl).sum(),
kl_max: rows.iter().map(|l| l.kl).fold(0., f64::max),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Reject non-finite KL values before computing the maximum

If a candidate produces NaNs or infinities in a logits row, Runtime::logits can return a NaN KL value. f64::max ignores NaNs, so this fold can report kl_max == 0 even when every compared row has invalid numerics. Judged::verdict then admits the candidate as Within, and confirmation repeats the same check, allowing a numerically broken implementation to be exported. Reject non-finite KL values and one-sided NaNs before aggregation, as kern_test::compare::LogitStats::from_device already does.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 98291b7. A row's KL is read as kern test reads it: infinite when it is not finite or one side has a NaN the other has not, so the maximum, the mean and the flips beyond the limit all see it.

Comment thread crates/kern-run/src/bench/candidates.rs Outdated
Comment on lines +308 to +309
j if j.outputs_differ == 0 && j.rows_differ == 0 => Verdict::Identical,
j if j.rows > 0 && j.kl_max <= LOGIT_KL => Verdict::Within,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Validate output differences independently of logits proximity

For a candidate replacing sampling or speculative-acceptance logic, unchanged logits do not imply correct emitted tokens or counts. This branch accepts any output mismatch whenever some logits rows exist and their KL is small: a candidate can emit an arbitrary token or incorrect accepted-token count while leaving logits untouched, receive Within with KL zero, and pass confirmation. Require counts to agree and establish that any permitted token change actually corresponds to a near-tie in the relevant distribution rather than using unrelated logits proximity to waive all output differences. The GPU-oracle guidance in AGENTS.md specifically distinguishes near-tie flips from confidently wrong tokens.

AGENTS.md reference: AGENTS.md:L158-L161

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 98291b7. A scenario whose tokens or counts moved while no row changed its argmax now differs, whatever the KL: bench runs unsampled, so nothing the distributions show moved them. Outputs that moved are admitted only where some row flipped and every row is within the limit. In a speculative round a count moves with the token before it, so counts are held to the same rule and not to equality.

Comment thread crates/kern-run/src/bench/candidates.rs Outdated
Comment on lines +375 to +378
let rt = &bench.ranks[0].rt;
let judged = match &self.kept {
None => {
now.push(keep(rt, &bench.protocol, s, &priced.vars)?);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Compare candidate outputs on every rank

For multi-rank workloads, Bench::scenario measures every rank and measure feeds each rank its own sequences and prose, but the baseline snapshots and candidate judgments here use only rank 0. A candidate that produces stable but incorrect outputs on another rank can therefore be admitted and exported whenever that error does not affect rank 0's observed buffers; the final confirmation has the same blind spot. Keep and judge each rank's outputs and distributions, then combine their verdicts before admitting a scenario.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 98291b7. Every rank keeps what the manifest handed back and is judged against it; a scenario's verdict is what any rank moved. Not exercised on a multi-rank manifest yet: the gate run is one GB300.

xiaguan and others added 2 commits November 18, 2026 10:43
Three holes in the gate a candidate has to pass, from review.

A row whose KL is not a number, or with a NaN on one side only, is
infinitely far, as `kern test` reads it: `f64::max` skipped the NaN and
left the row within the limit.

Tokens or counts that moved in a scenario where no row changed its
argmax differ, however close the distributions are: bench runs unsampled,
so nothing the distributions show moved them, and the candidate changed
what is done with them.

Every rank keeps what the manifest handed back and is judged against it;
a scenario is what any rank moved. Rank 0 alone stood for the group.

Gate: the round workload against the served manifest on one GB300, 57
scenarios, verdicts unchanged scenario by scenario (the DSL kernel holds
in 53, the hand-written one in 52, the control that runs nothing in 12),
pick 2752.8 -> 2391.4 s measured against 2392.3 predicted. No
multi-rank manifest was run.

Signed-off-by: JinYan Su <751080330@qq.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A pick interpolates nothing: a value between two measured ones runs what
the larger one runs. Leaving one measured row count out of the DSL tile
pick and pricing it under whatever then covers it costs an op 6% to 82%
more there than its best candidate, and the report said nothing. A `gap`
line now follows a pick whose boundary falls between two measured values
more than a doubling apart, and the pick's record carries the same.

docs/bench.md says what a candidate replaces, the microbenchmark a
kernel's author writes, and records what was measured of that: per call
the graph's time is 0.96 to 1.06 of a careful standalone harness's over
25 op and row-count pairs, with no harness written. It also says what
the tile example did not exercise: every boundary there is worth far
more than the noise.

Signed-off-by: JinYan Su <751080330@qq.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@xiaguan
xiaguan merged commit 661ae65 into master Nov 18, 2026
5 checks passed
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