Correctness review — PR 231, "Race a Triton tile kernel against cuBLAS for GLM's BF16 head projections"
Head 0ba78eb, base main, 6 files, +373/−16. Reviewed through the correctness lens against the project charter (runtime small and model-agnostic; "GPU kernel code: report concrete defects only"). I read the whole diff, ignition/components/bf16_bmm.py in full, the changed regions of ignition/components/linear.py, the changed regions of ignition/preparation/tuning.py, tests/preparation/test_toolchain.py, tests/components/test_bf16_bmm.py, tests/components/test_bf16_bmm_cuda.py, the fixtures the new component reuses (fixtures/projection.py, fixtures/resources.py), and every production caller in ignition/recipes/glm53/mla.py.
Key findings
- Should fix —
ignition/components/bf16_bmm.py:108: the_NATIVE_JITSregistration is a side effect of a memoized function body, so it is skipped on everyprogram_cachehit. - Nit —
ignition/components/linear.py:89-96:flare::bmm_outis now dead code with no test coverage. - Nit / open question —
ignition/preparation/tuning.py:66: moving_prepare_compiler_toolchain()to import time changes where a version-mismatch error surfaces, and the libdevice switch is not covered byruntime_patches.identity(). - Everything else I checked through this lens — caller strides and alignment, the mutating-out contract, the plan/handle lifetime, the cuBLAS fallback path — holds. Details below.
1. Should fix — _NATIVE_JITS registration only happens on a program_cache miss
File: ignition/components/bf16_bmm.py:99-110
@program_cache(scope="preparation")
def compile_bmm(payload, tactic, ordinal):
...
# Compile planning leaves a deferred program in the kernel's Triton memo. b12x evicts those
# entries only from kernels in its registry, so a compile worker that planned this program
# would otherwise hand the deferred entry back instead of lowering it and never publish it.
compile_plan._NATIVE_JITS.add(_bmm_kernel) # line 108
with torch.cuda.device(ordinal), compile_only_launches():
return launch_triton(_bmm_kernel, grid, *args, **options, **launch)
What is wrong. The comment states a requirement: the kernel must be in b12x's native-JIT registry before planning runs, so compile workers can evict its deferred planning entries. The add is placed inside the body of a function decorated with @program_cache(scope="preparation") (line 99), so it executes only when the cache misses for the key (payload, tactic, ordinal). On a hit — the same operands and tactic compiled earlier in the same process, or an entry restored from the preparation program cache — the body never runs, and _NATIVE_JITS is left without _bmm_kernel while deferred planning entries for that program may still exist. The registry is process-global, so a kernel compiled once and then served from cache in a later preparation pass (e.g. a second device ordinal in the same process, or the compile-worker path where compile_jobs at line 172-173 hands the same compile_bmm to a worker) is exposed to exactly the failure the comment describes.
Why it is inconsistent with the rest of the repository. All other components that register native JITs do so at module scope, unconditionally, immediately after the @triton.jit definition:
ignition/components/moe_sm103.py:357(not opened) —_NATIVE_JITS.add(_quantize_routes), and:387—_NATIVE_JITS.add(_reduce_routes), imported directly atmoe_sm103.py:31(not opened) (from b12x._lib.compile_plan import _NATIVE_JITS, load_programs).ignition/components/sm103_projection.py:258(not opened) —_NATIVE_JITS.update((_expand_nvfp4, _expand_mxfp8)), imported atsm103_projection.py:33(not opened).
So PR 231 is the only component in the tree that registers conditionally.
Fix. Move _NATIVE_JITS.add(_bmm_kernel) to module scope next to the @triton.jit definition at bf16_bmm.py:63-84, and import the name directly (from b12x._lib.compile_plan import _NATIVE_JITS, ...) instead of reaching through the compile_plan module (line 26). That removes the timing dependency entirely and makes the component match moe_sm103.py (not opened) and sm103_projection.py (not opened). The from b12x._lib import compile_plan import at line 26 then becomes unused.
What would settle it: the exact eviction-ordering contract lives in b12x, a pinned external package not in this repository, so I cannot show from here that the deferred entry actually survives a cache hit. The defect I can show from this repository is the conditional-vs-unconditional registration asymmetry against the two components above; the fix is safe either way because it makes the registration strictly earlier and unconditional.
2. Nit — flare::bmm_out is dead after this change
File: ignition/components/linear.py:89-96
@torch.library.custom_op("flare::bmm_out", mutates_args=("out",))
def _bmm_out(source: torch.Tensor, weight: torch.Tensor, out: torch.Tensor) -> None:
torch.bmm(source, weight, out=out)
@_bmm_out.register_fake
def _fake_bmm_out(source, weight, out):
pass
The old inline cuBLAS path used this op; now BatchedUnquantizedProjection.__call__ (linear.py:84-86) goes through bf16_bmm.execute, and the cuBLAS tactic calls torch.bmm directly (bf16_bmm.py:125). A search for _bmm_out|bmm_out finds only the definition and its fake, plus no test. Either delete the op, or route the cuBLAS tactic through it so the operator stays the single traced entry point (which is what the deleted code did). Not a correctness break — nothing calls it — but it is a custom op registered into torch.library that no longer has a caller.
3. Nit / open question — preparing the compiler toolchain at import
File: ignition/preparation/tuning.py:47-66
_prepare_compiler_toolchain() is now called at module import (tuning.py:66), and the two previous call sites (RecordedPreparationSession.__init__ and the non-recorded branch of declare()) are gone. The function does runtime_patches.apply() (tuning.py:51) and _set_triton_libdevice_path() under config.patch(emulate_precision_casts=_OPTIONS["emulate_precision_casts"]) (tuning.py:62-63, _OPTIONS at compilation.py:26).
This is deliberate and, for the stated purpose, correct: Triton memoizes compiled kernels by specialization without the libdevice path (tuning.py:57-61), so a component whose tests compile before a session exists must see the same library identity. Two observations, neither a defect I can prove:
- Diagnosability.
runtime_patches.apply()raisesPreparationErroron a torch/triton version mismatch (ignition/runtime_patches/__init__.py:81-82for triton,:103-105for torch). Becausebf16_bmm.py:34importsdeclarefromignition.preparation.tuning, andlinear.py:22imports the component, the mismatch now surfaces as an exception raised while importing a components module — including in CPU-only tooling that never prepares anything. Same exception type, different failure point. Worth a line in the PR body or the docstring (tuning.py:48: "Resolve forward math libraries before any Triton program identity is recorded") rather than a code change. - CUDA initialization is not introduced by the import-time call: the new parameterisation asserts
not torch.cuda.is_initialized()after the import path (tests/preparation/test_toolchain.py:26-29installs atorch.cuda._lazy_initthat raises,:74asserts it was not hit).apply()is also idempotent via the_APPLIEDguard (runtime_patches/__init__.py:90-129), so repeated imports are harmless. - Unresolved:
_set_triton_libdevice_pathlives intuning.pyand is not part ofruntime_patches.identity(), which is what feedstorch.compiler.config.cache_key_tag(runtime_patches/__init__.py:99-101, 118). I could not verify from this repository whether a compiled-forward cache entry produced by a process that never ran this switch can be reused by one that did without a key change. b12x's program identity presumably includes theextern_libshash (CUDAOptions.hash(), asserted equal attests/preparation/test_toolchain.py:73), but that is b12x, not this repo. A test that compiles a kernel before and after the switch in one process and compares identities would settle it.
4. Caller and precondition coverage — verified, no finding
The component now rejects operand layouts in query_from_call (bf16_bmm.py:142-153): BF16, 3-D, same device, matching [G,M,K] / [G,K,N] / [G,M,N], contiguous rows (source.stride(2) == 1, output.stride(2) == 1), a K- or N-contiguous weight (1 in weight.stride()[1:]), and 16-byte aligned base pointers for all three.
I followed every production caller. The only user of BatchedUnquantizedProjection is ignition/recipes/glm53/mla.py, at :252-257 (decode/TP1/2/4, context=None), :321-329 (prefill slices) and :340-344 (context-parallel slices), invoked from mla.py:410, 423, 433, 437. In every case source and output are .transpose(0, 1) views of scratch rows of width 256 or 512 BF16 elements (mla.py:92-99), so rows are 512 or 1024 bytes, data_ptr() % 16 == 0, and stride(2) == 1; the weight views (values[:, :256], values[:, 256:], mla.py:242-243) keep a unit stride in weight.stride()[1:] after the k-major transpose at linear.py:79. No caller is broken by the new rejections, and no caller passes an operand the Triton tactic would then mis-address.
Other contract points I checked and cleared:
- No scratch.
scratch_bytes=lambda t: 0(bf16_bmm.py:170) matches both tactics:State.runwrites through the caller'soutput(bf16_bmm.py:119-128) and the cuBLAS fallback istorch.bmm(source, weight, out=output)(:125). - Declared-stride check at run time.
State.runre-validates shape and stride against the declared operands and raisesValueErroron mismatch (bf16_bmm.py:121-123), so a caller cannot silently feed a differently strided view than the one the race measured. - Fixture lifetime.
declare_bmmreuses the sharedinput_factory(bf16_bmm.py:174→fixtures/projection.py:37-42→fixtures/resources.py:15-38), which retains the fixture for the live candidate calls and releases the session's benchmark cache; thePreparedCallat:178-179carriesowners=(operands, weight), matching howlinear.py:129-132and:142-144use the same helper. Thesession=keyword is omitted here whilelinear.py:143passes it, butsession=Noneincandidate_inputsonly skips theRecordedPreparationSessioncache release; the owners tuple still holds the storage, so this is not a use-after-free. - Tactic filtering.
_tacticsonly emits tiles withcolumns % block_n == 0anddepth % block_k == 0(bf16_bmm.py:54-56), matching the kernel's unmasked loads along N and K (:76, :80), and the row mask (:77, :84) covers partial row tiles. Grid usescdiv(rows, block_m)(:95), consistent with the mask. - Fake/registration paths.
execute.register_fake(bf16_bmm.py:137-139) is a no-op, correct for a mutating out-parameter op;mutates_args=("output",)(:131) matches the kernel's write target.
5. Process observations (not correctness)
- GitHub reports merge conflicts on
main, confirmed by the author for head0ba78ebeagainst97cfeb0001f1, conflicting pathignition/components/linear.py(PR comment, 2026-11-13). CI on head0ba78ebis failing. Neither is a code defect I can attribute, but thelinear.pyresolution is exactly the file whoseBatchedUnquantizedProjectionbody changed here, so the rebase should be re-reviewed rather than assumed mechanical. - Repository-split triage (PR comment, 2026-11-24) asks for the BF16 batched-matmul implementation, the projection wiring and the component tests to move to
hyperflare-recipes, leaving only thetuning.pychange in this repository. That is a placement question for the design review, not a correctness one, but it means findings 1 and 2 would move with the files while finding 3 stays here. - Validation claimed in the description (RTX PRO 6000 SM120 CPU/GPU tests; GB10 and GLM MLA checkpoint runs on a combined integration branch) leaves GB10, the GLM MLA checkpoint test and SM103 unverified on this branch. The SM103 gap matters for this component because
sm103_projection.is_sm103routes the dense projection elsewhere (linear.py:117-120), butBatchedUnquantizedProjectionhas no SM103-specific branch, so the TL) tile path is simply exercised on whatever device prepares it.
Open questions
- Does b12x's native-JIT registry need the kernel present at process start (module import) or merely before the first
compile_bmmcall? Only the b12x source settles this; if it is the former, finding 1 is a hard requirement rather than a consistency problem. - Can a Triton disk-cache entry created against the bundled libdevice by an older Flare build be reused with the toolkit identity, given
_set_triton_libdevice_pathis outsideruntime_patches.identity()? - Whether the merge-conflict resolution on
linear.pypreserves theBatchedUnquantizedProjectionchanges — cannot be checked until the branch is rebased.