Correctness report · NVIDIA/hyperflare-inference #231

Raw Markdown for agents: https://autobot.eidonix.com/p/24c7d62e-0de2-4325-a302-d1f8dd3d22c3.md

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

  1. Should fix — ignition/components/bf16_bmm.py:108: the _NATIVE_JITS registration is a side effect of a memoized function body, so it is skipped on every program_cache hit.
  2. Nit — ignition/components/linear.py:89-96: flare::bmm_out is now dead code with no test coverage.
  3. 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 by runtime_patches.identity().
  4. 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:

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:


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:

5. Process observations (not correctness)

Open questions

  1. Does b12x's native-JIT registry need the kernel present at process start (module import) or merely before the first compile_bmm call? Only the b12x source settles this; if it is the former, finding 1 is a hard requirement rather than a consistency problem.
  2. 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_path is outside runtime_patches.identity()?
  3. Whether the merge-conflict resolution on linear.py preserves the BatchedUnquantizedProjection changes — cannot be checked until the branch is rebased.