Skip to content

Bound PreparedLogprobs merge block and share kernels across speculative widths - #673

Open
kbenkhaled wants to merge 2 commits into
NVIDIA:mainfrom
kbenkhaled:logprobs-chunked-finish
Open

kbenkhaled wants to merge 2 commits into
NVIDIA:mainfrom
kbenkhaled:logprobs-chunked-finish

Conversation

@kbenkhaled

@kbenkhaled kbenkhaled commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Why

When preparing a model with speculative decoding, PreparedLogprobs (ignition/components/logprobs.py) currently hits two avoidable Triton compile bottlenecks:

  1. Recompiling once per speculative width: All four logprob kernels mark WIDTH as a compile-time constant (tl.constexpr), even though it is only used as a stride (row * WIDTH + col). A model with widths 1..6 therefore compiles every kernel 6 separate times.
  2. Sorting all candidates at once in _finish: To find the top 20 tokens across the vocabulary, _finish loads every tile's top 32 candidates into one block and calls a single tl.sort(). For a 262k-token vocabulary (256 tiles), that unrolls an 8,192-element uint64 bitonic sort network.

On an 8 GiB Jetson Orin Nano (CUDA 13.2), compiling those 6 specializations of _finish took ~19.5 minutes during graph catalog construction. On a 128 GiB Jetson Thor (CUDA 13.2), cold preparation across widths 1..6 took 65.40 s, and _finish ran in 84.2 us at width 6.

What changed

  • One compiled kernel for all widths (ignition/components/logprobs.py): Changed WIDTH: tl.constexpr to a runtime width argument with @triton.jit(do_not_specialize=["width"]) in _statistics, _normalization, _candidates, and _finish, and promoted the row offset (row.to(tl.int64) * width + col) to 64-bit indexing. All speculative widths now share a single compiled cubin per kernel (with _finish remaining specialized per vocabulary via TILES: tl.constexpr).
  • Chunked top-32 merge in _finish (ignition/components/logprobs.py): Capped _finish's sort block at 512 elements instead of growing up to 8,192, and reduced _MERGE_WARPS from 8 to 4 across all vocabularies. When a vocabulary has more than 512 tile candidates, _finish loops over the remaining candidates in 480-element chunks, keeping the top 32 packed uint64 keys after each sort. Because MAX_TOP_LOGPROBS is 20 (≤ 32), the final top 20—including tie breaking, signed-zero canonicalization, -inf, and < 20 vocabulary padding—is bit-for-bit identical.
  • Tests (tests/components/test_logprobs.py): Added CPU merge-block checks and CUDA tests covering shared cubin reuse across widths (1, 2, 6, 16), exact top-20 tie ordering across chunk boundaries for vocabularies from 1 to 262,144 (BF16 and FP32), +0.0 / -inf / < 20 edge cases, requested token IDs, inactive-slot/epoch gating, and CUDA graph capture/replay.

Result

Metric (V = 262,144, widths 1..6) Before (1fbcc3c9) After (3ac58fea) Speedup
Jetson Thor (SM110) cold PreparedLogprobs.prepare() 65.40 s 4.46 s 14.7× faster
Jetson Thor (SM110) _finish GPU latency (width 6) 84.2 us 38.2 us 2.2× faster
Jetson Orin Nano 8 GiB (SM87) cold PreparedLogprobs.prepare() ~19.5 min 11.83 s ~100× faster

CUDA-graph replay overhead (on - base median across 3 alternating trials, Jetson Thor SM110, BF16):

Vocab Width Rows Before (1fbcc3c9, us) After (3ac58fea, us) Speedup Gated-off before → after (us)
16,384 1 1 / 8 / 16 15.2 / 37.3 / 59.9 15.0 / 37.4 / 60.1 1.00–1.01× 4.1–5.7 → 4.1–5.7
16,384 6 1 / 8 / 16 30.3 / 159.1 / 303.4 30.2 / 159.1 / 303.8 1.00× 4.6–11.5 → 4.5–10.9
32,768 1 1 / 8 / 16 22.4 / 62.2 / 110.2 24.3 / 64.7 / 112.9 0.92–0.98× 4.7–6.3 → 4.7–6.4
32,768 6 1 / 8 / 16 50.7 / 307.4 / 600.7 53.1 / 311.1 / 606.7 0.96–0.99× 4.9–18.8 → 5.0–17.3
131,072 1 1 / 8 / 16 71.8 / 239.0 / 427.5 57.3 / 224.7 / 414.3 1.03–1.25× 4.6–12.8 → 4.6–12.8
131,072 6 1 / 8 / 16 190.9 / 1239.9 / 2429.0 176.3 / 1201.8 / 2369.9 1.02–1.08× 7.7–61.8 → 7.4–55.4
248,320 1 1 / 8 / 16 148.9 / 466.2 / 824.9 100.0 / 415.1 / 774.3 1.07–1.49× 5.1–20.7 → 5.0–20.8
248,320 6 1 / 8 / 16 374.7 / 2473.9 / 5055.6 322.4 / 2264.1 / 4519.2 1.09–1.16× 11.0–115.2 → 10.5–103.4
262,144 1 1 / 8 / 16 149.1 / 484.3 / 861.4 101.2 / 435.8 / 814.9 1.06–1.47× 5.2–21.2 → 5.1–21.3
262,144 6 1 / 8 / 16 388.4 / 2580.5 / 5277.6 339.1 / 2385.2 / 4765.6 1.08–1.15× 11.3–119.5 → 10.7–106.3
  • Exported MTP cores keep 4 logprob cubins instead of 24, with no runtime core format or ABI changes.
  • Raw int32 output buffers matched main (1fbcc3c9) bit-for-bit on Jetson Thor across vocab in (19, 1025, 16385, 31744, 262144) at width 6, and matched eager execution bit-for-bit through compile_forward() at widths 1 and 4 (V = 16,385).

Validation and limitations

  • CUDA + CPU tests (FLARE_RUN_CUDA_TESTS=1 pytest -q tests/components/test_logprobs.py tests/components/test_completion_outputs.py tests/components/test_completion_capacity.py): 87 passed, 0 failed on Jetson Thor (SM110) on 3ac58fea (and on Jetson Orin Nano 8 GiB SM87 on 13372138).
  • Bit-exact & compiled checks: Verified raw int32 equality against 1fbcc3c9 (with >32 ties straddling the top-20 cutoff and chunk boundaries, plus +0.0/-inf rows) and bit-exact compile_forward() (wrap_triton -> flare::storage_call) execution on Jetson Thor.
  • Limitations: SM103/SM120/SM121 GPUs, end-to-end ignition export / flare run serving benchmarks, and Orin Nano's old BLOCK=8192 runtime latency were not measured in this pass.

Discord: no username provided

…ve widths

Why:
`PreparedLogprobs` specialized all four Triton kernels (`_statistics`,
`_normalization`, `_candidates`, and `_finish`) on `WIDTH: tl.constexpr`, and
`_finish` sorted all `TILES * 32` per-tile `uint64` candidates in a single
`tl.sort()` block. For a 262,144-token vocabulary (`TILES = 256`), `_finish`
unrolled an 8,192-element `uint64` bitonic sort network once per speculative
width (`1..6`). On an 8 GiB Jetson Orin Nano (CUDA 13.2 `ptxas`), compiling
those six specializations took ~19.5 minutes during graph catalog construction;
on a 128 GiB Jetson Thor (CUDA 13.2), cold preparation across widths `1..6`
took 65.40 s and `_finish` ran in 84.2 us at width 6.

What:
- Before: `WIDTH` was a `tl.constexpr` parameter in `_statistics`,
  `_normalization`, `_candidates`, and `_finish`, causing separate Triton
  compilations per speculative width, and `_finish` set
  `BLOCK = next_power_of_2(tiles * 32)` (up to 8,192 at 8 warps).
- After (`ignition/components/logprobs.py`):
  - Replaced `WIDTH: tl.constexpr` with runtime `width` decorated with
    `@triton.jit(do_not_specialize=["width"])` across all four kernels and
    promoted `pos = row.to(tl.int64) * width + col` for 64-bit addressing.
  - Bounded `_finish`'s `BLOCK` at `_MERGE_BLOCK = 512` (`_MERGE_WARPS = 4`),
    iteratively merging `BLOCK - CANDIDATES` (480) incoming keys into slots
    `CANDIDATES..BLOCK-1` when `TILES * CANDIDATES > BLOCK`. Because each step
    retains the top 32 unsigned `uint64` packed keys and `MAX_TOP_LOGPROBS` is
    20, the global top 20 (including exact tie ordering, signed-zero
    canonicalization, `-inf`, and `vocab < 20` sentinel padding) is bit-exact.
- Tests (`tests/components/test_logprobs.py`): added CPU merge-block bound
  checks and CUDA tests covering single-specialization sharing and execution
  across widths `(1, 2, 6, 16)` with multiple active rows, exact top-20 tie
  ordering across chunk boundaries and vocabularies `[1, 5, 19, 64, 1025,
  16384, 16385, 31744, 248320, 262144]` (BF16 and FP32), canonical `+0.0` bits
  with `-inf` tails, requested-flag-only gating, requested token IDs
  (including negative and out-of-range IDs), stale-epoch / `row >= live`
  gating, and stream-ordered functional CUDA graph capture and replay.

Result:
- Observed on Jetson Thor (`SM110`, `V = 262,144`, widths `1..6`): cold
  `PreparedLogprobs.prepare()` dropped from `65.40 s` to `4.46 s` (`14.7x`
  faster), `_finish` GPU latency at width 6 dropped from `84.2 us` to `38.2 us`
  (`2.2x` faster), and all tested widths share one compiled cubin per kernel.
- Observed on Jetson Orin Nano 8 GiB (`SM87`, `V = 262,144`, widths `1..6`):
  cold `PreparedLogprobs.prepare()` across all six widths completed in `11.83 s`
  (`_finish` compiles once in `4.25 s` vs ~3m 15s x 6 = ~19.5 min previously,
  `241.3 us` GPU latency at width 6).
- Output buffers matched `main` (`1fbcc3c9`) bit-for-bit (`torch.equal` on raw
  `int32` words) across `vocab in (19, 1025, 16385, 31744, 262144)` at width 6
  on Jetson Thor and matched eager execution bit-for-bit through
  `compile_forward()` at widths 1 and 4 (`V = 16,385`), with no core format or
  ABI change.

Evidence and limitations:
- Live CUDA + CPU pytest (`FLARE_RUN_CUDA_TESTS=1 pytest -q
  tests/components/test_logprobs.py tests/components/test_completion_outputs.py
  tests/components/test_completion_capacity.py`): `87 passed, 0 failed` on both
  Jetson Thor (`SM110`, 3.73 s warm / 66.99 s cold) and Jetson Orin Nano 8 GiB
  (`SM87`, 200.8 s cold).
- Raw `int32` output word equality against `1fbcc3c9` verified on Jetson Thor
  for `vocab in (19, 1025, 16385, 31744, 262144)` with >32 cutoff-straddling
  ties and dominant `+0.0`/`-inf` rows.
- Verified Flare's `compile_forward()` (`wrap_triton` -> `flare::storage_call`)
  lowering produces bit-exact outputs at `width=1` and `width=4` (`V = 16,385`)
  on Jetson Thor.
- Limitations: SM103/SM120/SM121 hardware, full end-to-end `ignition export` /
  `flare run` serving benchmarks, and SM87 baseline `BLOCK=8192` runtime
  latency were not re-measured in this pass. Equivalent `tl.sort` / `WIDTH`
  kernels in external recipe repositories (`vocabulary_logprobs.py`,
  `prompt_logprobs.py`) are outside this repository and left for follow-up.

Design-Signed-Off-By: Gemini 4 Neon; design-review=2e5b
Signed-off-by: kbenkhaled <kbenkhaled@nvidia.com>
@flare-autobot

Copy link
Copy Markdown
Collaborator

A case file has been opened for 673.
Case File Intake Report

Future reviews will auto-run 90 minutes after code updates are pushed.

  • Re-review sooner: Call @flare-autobot after push.
  • Opt-out: Include #autobot:ignore in the PR or any comment.

@flare-autobot flare-autobot added area:kernels GPU kernels and numerical execution. performance Performance, latency, throughput, or memory-efficiency investigation or improvement. triage:review Proposed change needs code review; this does not imply merge readiness. triage:validation Proposed implementation needs acceptance evidence; no claim that a reported defect is fixed. and removed triage:review Proposed change needs code review; this does not imply merge readiness. labels Dec 2, 2026
@flare-autobot

Copy link
Copy Markdown
Collaborator

Autobot Review

Full Review · Value Proposition · Case File

Recommendation: Changes Requested

The logprobs.py kernel rework is mathematically sound, bit-exact, and adds the repository's first direct unit and CUDA tests for these kernels; no correctness, ABI, or CUDA-graph capture defects were found. One should-fix remains before merge: the launch changes that apply to all vocabularies are only measured at a single point (V = 262,144, width = 6), the 8 → 4 warp transition is unstated, and CPU CI has not run on head 1337213.

  • Blockers: 0 · Should fix: 1 · Subtract/Simplify: 1 · Nits: 3
Walkthrough
  • ignition/components/logprobs.py:59-121: Converted WIDTH: tl.constexpr to a runtime width parameter with @triton.jit(do_not_specialize=["width"]) across _statistics, _normalization, _candidates, and _finish, keeping parameter positions aligned with prepare()/functional()/run() and promoting row offsets to 64-bit (pos = row.to(tl.int64) * width + col).
  • ignition/components/logprobs.py:21-26, 136-150, 164, 182-185: Bounded _finish's sort block at _MERGE_BLOCK = 512, reduced _MERGE_WARPS from 8 to 4, and added a 480-key chunked merge loop (step = BLOCK - CANDIDATES) when total = TILES * CANDIDATES > BLOCK, preserving the running top 32 packed uint64 keys after each sort.
  • tests/components/test_logprobs.py:1-279: Added a CPU meta-device test checking BLOCK bounds across vocabularies 1..262,144 and three FLARE_RUN_CUDA_TESTS=1 tests verifying single-cubin reuse across widths (1, 2, 6, 16), exact top-20 tie ordering across chunk boundaries (BF16/FP32, +0.0, -inf, < 20 padding, inactive slots), requested token IDs, epoch/live-row gating, and functional(dynamic_inputs=True) CUDA graph capture/replay.
Merge Risk: Medium · up to 1337213
  • Shared hot-path launch change across all vocabularies: PreparedLogprobs is the sole generation-logprob reduction component in ignition/components/logprobs.py (used by PreparedSampling and external speculative recipes). Capping BLOCK at 512 alters _finish for every vocabulary >= 16,385 (up to 9 serial sorts at V = 131,072 and 17 at V = 262,144), and reducing _MERGE_WARPS from 8 to 4 alters _finish for every vocabulary including <= 16,384.
  • ABI / compatibility / concurrency safety is preserved: Scratch shapes/dtypes (stats, lse, keys), CompletionOutputs layouts, _merge, and CUDA-graph capture safety are unchanged; previously exported cores embed their own cubins and continue to load unaffected.
  • Unverified CI & target hardware matrix: Head 1337213 currently has 0 CI checks run, and SM103/SM120/SM121 plus end-to-end ignition export / flare run remain unmeasured.
Pre-merge checks: ✅ 2 passed · ⚠️ 2 warnings
  • ✅ Security / malicious screen: Diff is confined to Triton logprob reduction kernels and pytest tests with no network, shell, credential, or untrusted-input surfaces.
  • ⚠️ Charter & value fit: Cleanly placed in one shared ignition/components/ module with no model branch, flag, or ABI break, addressing a measured compile-time problem; however, the Charter's served-workload benchmark / SM103-SM120-SM121 matrix check and docs/logprobs-design.md:129-130's complete-replay measurement requirement are not yet met.
  • ✅ Related / duplicate / conflict check: No competing or conflicting PRs touch ignition/components/logprobs.py; complementary to merged heliopause: prune unreferenced kernels from exported CUDA ELF64 cubins #652 (cubin pruning on export).
  • ⚠️ Tests & verification evidence: Strong new CUDA test suite reported passing (87 passed) on Thor (SM110) and Orin Nano (SM87), but 0 GitHub CI checks have run on 1337213 and runtime latency is only measured at one point (V = 262,144, width = 6 on Thor).
Should fix (1) · Subtract/Simplify (1) · Nits (3)

Should fix

  1. Measure the unmeasured runtime regimes and document the full before/after launch change (ignition/components/logprobs.py:23-26, 137-147, 164) — two independent launch changes apply well beyond the single measured point:
    • _MERGE_WARPS changed from 8 to 4 (ignition/components/logprobs.py:26 vs main:22), affecting every vocabulary including V <= 16,384, while the PR description only mentions 4 warps inside the 512 cap bullet without stating the 8 → 4 transition.
    • merge_block = min(triton.next_power_of_2(tiles * _TILE_CANDIDATES), _MERGE_BLOCK) (ignition/components/logprobs.py:164) replaces a single 1024..4096-key sort with 2..9 serial 512-key sorts for every V >= 16,385 (e.g. 9 at V = 131,072); none of V <= 16,384, 32k–128k, or width = 1 are measured before/after, and docs/logprobs-design.md:129-130 requires complete replay and service measurements for performance claims.
    • Fix: add before/after CUDA-graph latency measurements (e.g. scripts/benchmark_logprobs.py at width in (1, 6)) for small/mid/262,144 vocabularies plus a complete replay or exported-core check (docs/logprobs-design.md:129-130, docs/performance-work.md:45-51), or bound the speedup claim to V = 262,144 and track SM103/SM120/SM121 + exported-core checks in docs/TODO.txt (AGENTS.md:57-58); update "What changed" (AGENTS.md:88-94) to state the 8 → 4 warp change and that _finish stays per-vocabulary via TILES: tl.constexpr; ensure CPU CI (.github/workflows/python-cpu.yml) runs and passes on the PR head.

Subtract / Simplify

  1. Drop tautological module-level assert (ignition/components/logprobs.py:24) — assert _MERGE_BLOCK > _TILE_CANDIDATES and (_MERGE_BLOCK & (_MERGE_BLOCK - 1)) == 0 compares two adjacent module literals (512, 32), is the only top-level assert in ignition/, cannot fail at runtime, and is stripped under python -O. Deleting line 24 leaves the comment at lines 21–22 intact.

Nits

  • Empty Discord: line in PR description: populate per AGENTS.md:144-153 and docs/design-review.md:163-169.
  • Optional doc update (docs/logprobs-design.md:92-100): the existing contract-level sentence still holds, but one sentence on bounded chunked merging and width-agnostic launches would keep the preparation section current.
  • Avoid hardcoded tuple index (tests/components/test_logprobs.py:67): select _finish by kernel identity (next(args for kernel, args, *_ in op._preparation if kernel is _finish)) instead of op._preparation[3][1].

Have your agent review the Full Review for complete verified findings, file:line citations, and suggested changes.

To opt out, include #autobot:ignore in your post or comment.

ignition/components/logprobs.py checked two adjacent module-level integer
literals (_MERGE_BLOCK = 512 and _TILE_CANDIDATES = 32) with a top-level
assert, and tests/components/test_logprobs.py indexed _finish out of
op._preparation by hardcoded position [3].

Changes:
- ignition/components/logprobs.py: remove the module-level assert on
  _MERGE_BLOCK and _TILE_CANDIDATES.
- tests/components/test_logprobs.py: select _finish arguments by matching
  `kernel is _finish` instead of hardcoded tuple index `[3]`.

Result: removes dead module-import assertion and decouples the unit test
from preparation tuple ordering, with zero runtime behavior change.

Validation (Jetson Thor SM110, hf-thor-v2b container):
- FLARE_RUN_CUDA_TESTS=1 pytest -q tests/components/test_logprobs.py
  tests/components/test_completion_outputs.py
  tests/components/test_completion_capacity.py: 87 passed, 0 failed.
- Before (1fbcc3c) vs after CUDA-graph overhead (median on-minus-base
  across 3 alternating trials, BF16, width in {1, 6}, rows in {1, 8, 16}):
  - V=16,384 (8 -> 4 warps, same BLOCK=512): 1.00x-1.01x across all 6
    shapes (e.g. w=1,r=1: 15.2 -> 15.0 us; w=6,r=16: 303.4 -> 303.8 us).
  - V=32,768: 22.4 -> 24.3 us (w=1,r=1); 50.7 -> 53.1 us (w=6,r=1);
    600.7 -> 606.7 us (w=6,r=16, 0.99x).
  - V=131,072: 1.02x-1.25x faster across all 6 shapes (w=1,r=1:
    71.8 -> 57.3 us; w=6,r=1: 190.9 -> 176.3 us; w=6,r=16: 2429 -> 2370 us).
  - V=248,320: 1.07x-1.49x faster across all 6 shapes (w=1,r=1:
    148.9 -> 100.0 us; w=6,r=1: 374.7 -> 322.4 us; w=6,r=16: 5056 -> 4519 us).
  - V=262,144: 1.06x-1.47x faster across all 6 shapes (w=1,r=1:
    149.1 -> 101.2 us; w=6,r=1: 388.4 -> 339.1 us; w=6,r=16: 5278 -> 4766 us).

Limitations: SM103/SM120/SM121 GPUs and end-to-end served throughput were
not measured on this commit.

Design-Signed-Off-By: Gemini 4 Neon; design-review=2e5b
Signed-off-by: kbenkhaled <kbenkhaled@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:kernels GPU kernels and numerical execution. performance Performance, latency, throughput, or memory-efficiency investigation or improvement. triage:validation Proposed implementation needs acceptance evidence; no claim that a reported defect is fixed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants