Subtraction, Simplicity & Conventions review — PR #673
Key findings
- The diff is mostly subtraction, and that part is right. Replacing
WIDTH: tl.constexprwith a runtimewidthunder@triton.jit(do_not_specialize=["width"])deletes six kernel specializations per kernel rather than adding a knob; the chunked merge reuses the existingtl.sort/_sort_key/_sort_pairmachinery instead of introducing a new top-k abstraction. No flags, config switches, fallback layers, or dead code were added._position,_merge, the scratch layout and the constructor signature are untouched. Verdict: close to lean, with four concrete trims and one convention breach (stale doc). - [high] The doc this change contradicts is left stale.
docs/logprobs-design.md:92-100still describes the pre-PR single 8192-key merge and says nothing about width-independent kernels.AGENTS.md:232routes any logprob change through that doc. - [high] The new CPU test restates the implementation formula and pokes private internals (
tests/components/test_logprobs.py:50-68), againstAGENTS.md:194-199. Collapsible from 8 parametrize rows to 2 boundary cases. - [medium] Two leftover constant pairs to collapse:
_REDUCTION_WARPS = 4/_MERGE_WARPS = 4are now identical (logprobs.py:25-26), and the module-levelassertatlogprobs.py:24is a tautology over two literals that can never fire. - [low, GPU-verify]
if total > BLOCK:atlogprobs.py:141is a redundant guard —total,BLOCK,stepare alltl.constexpr, so therange(BLOCK, total, step)loop is already statically empty whentotal <= BLOCK.
F1 [high] — Stale logprob contract left behind (removable/rewritable text)
docs/logprobs-design.md:92-100 is unchanged by the PR and now describes behaviour the code no longer has:
Four prepared Triton launches reduce requested positions. Vocabulary tiles first
compute FP32 maxima and exponential sums, then a small reduction writes each
position's normalization. Tiles retain their best 32 integer sort keys; a final
merge stores K pairs and the chosen token. Keys rank the reported FP32 logprobs
"A final merge stores K pairs" is precisely the single up-to-8192-key sort that ignition/components/logprobs.py:136-147 replaces with a bounded 512-key block plus 480-key merge steps, and the four-kernel width-independence (the PR's own headline mechanism) is nowhere in the doc. AGENTS.md:232 ("Change speculative decoding, log probabilities, or prompt scoring → … the logprob contract") makes reading/updating this document part of touching logprobs.
Concrete simplification: replace the single sentence Tiles retain their best 32 integer sort keys; a final merge stores K pairs and the chosen token. (docs/logprobs-design.md:94-95) with: tiles retain their best 32 packed keys; the final launch sorts at most 512 keys and merges any remaining candidates in 480-key steps, keeping the running top 32 after each sort; the four kernels take the speculative width as a runtime argument, so every width shares one compiled kernel. Two sentences replace accuracy the PR is otherwise careful about — no new document, no new section.
Also worth one clause (same paragraph, :92): PreparedVocabularyParallelLogprobs (ignition/components/vocabulary_logprobs.py (not opened)) is described at :102-110 with the same "final launch merges the gathered candidates" phrasing; if that sibling module still sorts unbounded, the doc should state the contrast explicitly rather than leaving two "final merge" descriptions that now behave differently.
F2 [high] — The CPU merge-block test duplicates the implementation formula and reads private state
tests/components/test_logprobs.py:50-68:
@pytest.mark.parametrize("vocab,expected_block", [
(1, 32),
(64, 32),
(1024, 32),
(1025, 64),
(16384, 512),
(16385, _MERGE_BLOCK),
(31744, _MERGE_BLOCK),
(262144, _MERGE_BLOCK),
])
def test_finish_merge_block_is_bounded_for_large_vocabularies(vocab, expected_block):
...
op = PreparedLogprobs(logits, completion, slots, epochs, live, frame)
finish_args = dict(zip(_finish.arg_names, op._preparation[3][1], strict=True))
assert finish_args["BLOCK"] == expected_block
Three problems, all subtraction-shaped:
- Eight rows re-encode
min(triton.next_power_of_2(tiles * _TILE_CANDIDATES), _MERGE_BLOCK)fromignition/components/logprobs.py:164by hand, so the test must be edited whenever the formula or_VOCAB_TILEchanges, without ever detecting a real regression. - It reaches into
op._preparation[3][1]— the exact tuple this PR edits atlogprobs.py:176-186— and zips it against_finish.arg_names. Two private surfaces for one assertion. AGENTS.md:194-199: "Tests must exercise the affected behavior or contract; source-text checks and mocks that bypass the behavior cannot establish runtime correctness."
Concrete replacement (one test, two cases, still covers the cap that motivates the PR):
@pytest.mark.parametrize("vocab,expected_block", [(1, 32), (262144, _MERGE_BLOCK)])
def test_finish_merge_block_is_bounded(vocab, expected_block):
...
op = PreparedLogprobs(logits, completion, slots, epochs, live, frame)
block = dict(zip(_finish.arg_names, op._preparation[3][1], strict=True))["BLOCK"]
assert block == expected_block and block <= _MERGE_BLOCK
Drops six rows and the formula duplication while keeping both the small-vocabulary floor (32) and the bound (_MERGE_BLOCK). The real behaviour is already covered on GPU by test_logprobs_kernels_share_one_specialization_and_execute_across_widths (:71-…) and the tie-ordering test, so nothing is lost.
F3 [medium] — Two collapsed constant pairs and one tautological assert
(a) ignition/components/logprobs.py:25-26 — the PR silently dropped _MERGE_WARPS from 8 to 4 (diff: -_MERGE_WARPS = 8 / +_MERGE_WARPS = 4), which makes the two constants identical:
_REDUCTION_WARPS = 4
_MERGE_WARPS = 4
The distinction is now vacuous and undocumented. Replace both with a single _WARPS = 4 used at logprobs.py:177, :179, :181, :185 — four call sites, one name, and no reader has to wonder why two warps counts exist when they are equal. (If the 8→4 merge-warp change is intentional and meant to be tunable, it belongs in the PR description and as a comment; as written it is a leftover.)
(b) ignition/components/logprobs.py:21-24:
# Merge per-tile candidates in bounded chunks so large vocabularies (e.g. 256k -> 8192 keys)
# do not unroll an 8192-element bitonic network in Triton/PTX while keeping bit-exact ordering.
_MERGE_BLOCK = 512
assert _MERGE_BLOCK > _TILE_CANDIDATES and (_MERGE_BLOCK & (_MERGE_BLOCK - 1)) == 0
The assert compares two adjacent module literals: 512 > 32 and 512 is a power of two. It can never fire, restates the comment two lines above, and is stripped under python -O. It is also the only module-level assert in ignition/ (a grep for ^assert returns this line alone). Delete line 24, keep lines 21-23. The invariant that actually matters — _MERGE_BLOCK > _TILE_CANDIDATES, i.e. the merge step BLOCK - CANDIDATES stays positive — is better expressed by the comment than by an assertion over constants the same file owns. (Nothing here is a correctness bug; the comment already states the requirement.)
F4 [low, verify on GPU first] — Redundant if total > BLOCK: guard in _finish
ignition/components/logprobs.py:136-147:
k = tl.arange(0, BLOCK)
total: tl.constexpr = TILES * CANDIDATES
base = pos * total
keys = tl.load(Keys + base + k, k < total, other=0)
keys = tl.sort(keys, descending=True)
if total > BLOCK:
step: tl.constexpr = BLOCK - CANDIDATES
for offset in range(BLOCK, total, step):
idx = offset + (k - CANDIDATES)
incoming = tl.load(Keys + base + idx, (k >= CANDIDATES) & (idx < total), other=0)
keys = tl.where(k < CANDIDATES, keys, incoming)
keys = tl.sort(keys, descending=True)
total (:137) and step (:142) are declared tl.constexpr and BLOCK arrives as BLOCK: tl.constexpr (:115), so range(BLOCK, total, step) is statically empty when total <= BLOCK and Triton unrolls it to nothing. The if is a second, redundant encoding of the same condition, plus a nesting level. Deleting the if and dedenting the six body lines leaves the loop as the single source of the chunking rule. Drop in also makes the whole branch read as one merge loop, which is what the comment at :21-22 claims.
Must be checked on a GPU before review says to delete it (FLARE_RUN_CUDA_TESTS=1, compare op.finish.compiled.hash and outputs for vocab=1 and 262144): if the guard's presence changes the specialization key or PTX for small vocabularies, keep it. I did not run CUDA, so this is conditional.
Non-findings (checked and explicitly not worth changing)
- Width de-specialization is genuine subtraction, not a new knob.
@triton.jit(do_not_specialize=["width"])(logprobs.py:112etc.) is the existing repo idiom — seeignition/components/engram.py:23(not opened) andhelios/tests/fixed_core_fixture.py:27 (not opened),33— so no new dependency or convention is introduced. - Four repeated
pos = row.to(tl.int64) * width + collines (logprobs.py:66,83,101,121): a@triton.jithelper would inline to the same code; the repetition is a comment on Triton's lack of shared scoping, not duplication worth a review objection. Leave it. - No dead code, no speculative generality, no fallback branch. The requested-IDs branch (
:127-134) and the sorted branch are gated exactly as before; onlyposwidened. The PR does not weaken a guarantee or add an optional path (AGENTS.md:171-193satisfied on this axis). - The new test file is net-new coverage, not duplicated coverage: before this PR no file tested
_statistics/_normalization/_candidates/_finish;tests/components/test_completion_outputs.pycovers buffers and_merge. The GPU tests are large but they exercise behaviour, not source text, so they are withinAGENTS.md:194-199. - Constructor/scratch/ABI unchanged (
logprobs.py:167-173,_mergeat:248-261), so no compatibility layer or versioning shim was introduced — good.
Open questions / not found
- Is the
_MERGE_WARPS8→4 drop measured at small vocabularies? It is undescribed in the PR body and unmeasured in the report; if intentional it needs one sentence, otherwise it is a leftover to fold into_WARPS. - Does
preparedVocabularyParallelLogprobs(ignition/components/vocabulary_logprobs.py(not opened), described atdocs/logprobs-design.md:102-110) share the unbounded final sort? If yes, the same bounded-merge treatment (and the same doc wording) may be missing there — out of this PR's scope, but it is the natural follow-up and should be adocs/TODO.txtentry perAGENTS.md:57-59rather than silent. - I did not verify F4's CUDA behaviour; and I did not re-check whether an exported core's kernel count matches the "4 cubins instead of 24" claim (no export was run).