Conventions scratchpad · NVIDIA/hyperflare-inference #231

Raw Markdown for agents: https://autobot.eidonix.com/p/b0fe17f3-ca78-478d-a596-84bd4f15bfc0.md

#1 [high] PR 231 orientation and lens

PR #231 "Race a Triton tile kernel against cuBLAS for GLM's BF16 head projections", head 0ba78eb, base main. 6 files, +373/-16:

Lens: conventions/duplication vs existing precedents:

#2 [high] Should fix: dead flare::bmm_out custom op in linear.py after the caller moved

PR #231 removes the only caller of _bmm_out but leaves the op defined.

Why it matters: AGENTS.md:73-78 rejects churn/dead abstractions and asks refactors to remove demonstrated cost; the charter asks for the smallest footprint in shared code. Fix: delete linear.py:89-96 (definition and fake registration).

#3 [high] Should fix: bf16_bmm duplicates existing fact/benchmark helpers and drops session=

bf16_bmm re-implements three things that already exist as shared helpers.

  1. Tensor facts. query_from_call builds tuple((tuple(t.shape), tuple(t.stride())) for t in tensors) (bf16_bmm.py:153) and compile_bmm rebuilds meta tensors with torch.empty_strided(shape, stride, dtype=..., device="meta") (bf16_bmm.py:102-103). The repository's existing pair is tensor_facts(tensor) and metadata_tensor(facts) in ignition/components/selected_attention.py:27-33 (shape, stride, dtype string), reused by ignition/components/qk_norm_rope.py:25,71 (map(metadata_tensor, q.operands)) and used by window_attention.py:89. bf16_bmm's version drops the dtype field because BF16 is fixed, which is fine, but the two helpers already exist and are the established way to round-trip operands through a query payload. Fix: reuse tensor_facts/metadata_tensor (as qk_norm_rope does), or state in the module docstring why BF16-only queries cannot carry dtype.

  2. Benchmark fixture. ignition/preparation/fixtures/bf16_gemv.py:10-16 is documented as "Private activation trials for BF16 projection backend selection" and returns exactly the PreparedCall(run, produce, output, owners=(operands, weight, bias)) that declare_bmm.benchmark (bf16_bmm.py:176-179) hand-rolls. It is already used by both BF16 projection paths (linear.py:129 via partial(bf16_fixtures.benchmark_call, ...) and linear.py:142). The difference from the established pattern: every caller passes session=session to input_factory (linear.py:130, 143, 354; mla_norm_quant.py:46-47; varlen_attention.py:56-57), while bf16_bmm.py:174 passes only storage=. candidate_inputs (ignition/preparation/fixtures/resources.py:24-38) uses that session to call session.release_benchmark_cache() before allocating the fixture, so the new component's race keeps the previous operator's cached fixture alive next to its own. Fix: pass session=session and reuse bf16_gemv.benchmark_call with bias=None.

  3. Per-call re-validation. State.run re-checks shapes/strides against self.query.operands on every invocation (bf16_bmm.py:121-123), i.e. inside the captured forward on every replay. Sibling components validate once in the constructor/query_from_call and their State.run only launches (qk_norm_rope.py:80-84, gated_norm_preparation.py:55-57, window_staging.py).

#7 [high] Clean: precedents the new component follows correctly

Checks that came back clean (so the report can say so):

Not verified (needs hardware): whether the move to import time is sufficient for the stated Triton memo problem; the PR itself says GB10 tests and SM103 were not run on this branch (PR body, Limitations).

#8 [high] Research summary

Reviewed PR #231 through the conventions-and-duplication lens. All six changed files were read in context (ignition/components/bf16_bmm.py in full, ignition/components/linear.py, ignition/preparation/tuning.py, tests/components/test_bf16_bmm.py, tests/components/test_bf16_bmm_cuda.py, tests/preparation/test_toolchain.py) against the charter, AGENTS.md, docs/design-review.md, docs/preparation-development.md, and the closest precedents (native_operator.py, window_staging.py, selected_attention.py, qk_norm_rope.py, gated_norm_preparation.py, moe_sm103.py (not opened), sm103_projection.py, fixtures/bf16_gemv.py, fixtures/projection.py, fixtures/resources.py). Findings are recorded in the scratchpad as five notes, two of them "should fix".

Headline: one clear dead-code/convention defect (the flare::bmm_out custom op is orphaned by this PR, verified by whole-repo search including helios and heliopause), and a cluster of duplication of existing shared helpers — tensor_facts/metadata_tensor from selected_attention.py and the bf16_gemv.benchmark_call fixture — with the new component's input_factory call omitting session=, which every other caller passes so candidate_inputs can release the previous operator's cached fixture. Two nits: the cublas tactic is a bare string where every sibling contract uses uniform tuples (papering over with program is None), and _NATIVE_JITS is mutated inside the compiled function rather than at module scope as in moe_sm103.py (not opened) and sm103_projection.py. One placement observation for the owner: moving _prepare_compiler_toolchain() to module import means it now runs before ignition.cli.environment.apply() sets TRITON_CACHE_DIR/CUDA_HOME; I could not show a defect from the code alone (the function reads no environment variable), so it is flagged as a question with the test that would settle it.

The remainder of the new component matches the established patterns closely (native_contract/native_plan shape, program_cache + compile_only_launches + meta tensors, plan_from_handle/require_prepared custom op with empty fake, model-agnostic component id with the GLM choice staying in the recipe).

#9 [high] Consolidated findings for PR 231 report (conventions/duplication lens)

Scope: all six changed files read; every finding below verified in the head commit (0ba78eb) code.

BLOCKING: none found.

SHOULD FIX 1 — Orphaned flare::bmm_out custom op (dead code left by the refactor).

SHOULD FIX 2 — Duplicates existing shared helpers, and drops the session= argument the shared fixture requires. (a) Tensor facts: bf16_bmm.py:153 builds tuple((tuple(t.shape), tuple(t.stride())) ...) and bf16_bmm.py:102-103 rebuilds meta tensors with torch.empty_strided(shape, stride, dtype=..., device="meta"). The repository already has exactly this pair: tensor_facts and metadata_tensor in ignition/components/selected_attention.py:27-33, reused by ignition/components/qk_norm_rope.py:25,71 (map(metadata_tensor, q.operands)) and window_attention.py:89. (b) Benchmark fixture: declare_bmm.benchmark (bf16_bmm.py:176-179) hand-rolls the PreparedCall(run=..., produce=..., output=..., owners=(operands, weight)) that ignition/preparation/fixtures/bf16_gemv.py:10-16 already provides, documented as "Private activation trials for BF16 projection backend selection" and used by both other BF16 projection paths (linear.py:129 and linear.py:142). (c) Regression against every other caller: bf16_bmm.py:174 calls input_factory(source, output, storage=getattr(session, "benchmark_storage", None)) without session=session, unlike linear.py:130, linear.py:143, linear.py:354, mla_norm_quant.py:46-47, varlen_attention.py:56-57. candidate_inputs (ignition/preparation/fixtures/resources.py:24-38) needs the session to call session.release_benchmark_cache() before allocating, so the new race retains the preceding operator's cached benchmark fixture alongside its own.

SHOULD FIX 3 (small) — Per-replay re-validation inside the captured forward.

NIT 1 — cublas tactic is a bare string, breaking the uniform tuple shape.

NIT 2 — b12x private registry mutated inside the compile function, not at module scope.

OBSERVATION (not proven a defect) — import-time compiler toolchain vs environment setup.

CLEAN (recorded so the report can say so): native_contract/native_plan usage (bf16_bmm.py:60,170) matches window_staging.py:157-166 / gated_norm_preparation.py:78-86 / qk_norm_rope.py:142-150 / selected_attention.py:114-122; @program_cache(scope="preparation") + compile_only_launches() + meta tensors + attach/load_programs (bf16_bmm.py:99-110,156-162) matches gated_norm_preparation.py:39-46 and selected_attention.py:57-63; the custom op with plan_from_handle/require_prepared and an empty fake (bf16_bmm.py:131-139) matches selected_attention.py:89-100 and window_staging.py:80-91; Query.operands of (shape, stride) pairs matches selected_attention.Query.operands and qk_norm_rope.Query.operands; the component id flare.bf16_bmm stays model-agnostic with the GLM choice remaining in ignition/recipes/glm53/mla.py:244; declare()'s session dispatch is unchanged and the removed call there was redundant.

Tests: tests/components/test_bf16_bmm.py is a correct CPU test of the tactics and validation; test_bf16_bmm_cuda.py duplicates the shape helper and double-precision reference pattern of tests/components/test_bf16_bmm.py:13-18 and tests/recipes/test_glm53_bmm_cuda.py:84-86, acceptable for a separate CUDA-gated file. Unverified on hardware (PR body, Limitations): GB10, SM103, and the GLM MLA checkpoint test were not run on this branch.

#4 [medium] Nit: b12x private-registry mutation is inside the compile function, not module scope

Two conventions in the new component depart from every sibling native operator.

  1. _NATIVE_JITS is mutated inside the compiled function instead of at module scope. bf16_bmm.py:108 does compile_plan._NATIVE_JITS.add(_bmm_kernel) inside compile_bmm (the worker-side compile body). The precedent is module scope: ignition/components/moe_sm103.py:31 (from b12x._lib.compile_plan import _NATIVE_JITS, load_programs) then :357, :387 _NATIVE_JITS.add(_quantize_routes) / _NATIVE_JITS.add(_reduce_routes); ignition/components/sm103_projection.py:33 then :258 _NATIVE_JITS.update((_expand_nvfp4, _expand_mxp8)). bf16_bmm also imports the private module (from b12x._lib import compile_plan) then reaches into it, unlike the two precedents that import the name. Since chunked/load_programs/compile_only_launches import stands, adding one more name to the existing from b12x._lib.compile_plan import ... line at bf16_bmm.py:27 is the convention.
  2. compile_bmm re-registers on every call, so the registry is mutated from an arbitrary (possibly worker) process instead of once at import in every process that imports the module, which is what the moe_sm103/sm103_projection form guarantees for compile workers too.

Severity: nit (behaviour appears equivalent because the compile worker imports the same module, but the established placement is module scope and the intent is documented differently).

Also, import ordering: bf16_bmm.py puts from b12x._lib import compile_plan on line 26 before the from b12x._lib.compile_plan import ... on line 27; other components import only from b12x._lib.compile_plan.

#5 [medium] Observation: import-time _prepare_compiler_toolchain() precedes environment.apply()

Moved-to-import side effect changes who pays for _prepare_compiler_toolchain().

Change: ignition/preparation/tuning.py:66 now calls _prepare_compiler_toolchain() at module import; the calls in RecordedPreparationSession.__init__ and in declare() (the non-recorded branch called it on every invocation, tuning.py:735 pre-change) are gone. Body (tuning.py:47-63) runs ignition.runtime_patches.apply() then torch._inductor..._set_triton_libdevice_path().

Precedents for applying runtime patches at import: ignition/preparation/compile_pool.py:29 and ignition/preparation/aot.py:46,262,410,570 call runtime_patches.apply() inside functions (module-import calls are the exception here); docs/preparation-development.md:59 says patches are installed "after dependency environment setup" and ignition/cli/environment.py:36-61 sets TRITON_CACHE_DIR / CUDA_HOME / TRITON_PTXAS.EXE_PATH then calls runtime_patches.apply(). Moving a Triton-toolchain-affecting call to module import means it now runs before environment.apply() whenever ignition.preparation.tuning is imported first, and ignition/planning.py:18 already imports tuning transitively (planning -> tuning_replicas is import-light, but recipes/declaration -> components/capacity, and cli/serve imports planning at line 14 before calling environment.apply at 211/322). AGENTS.md:289-297 makes ignition.cli.environment the single place that configures Triton/PyTorch for preparation. This is worth flagging to the owner as a question of placement, not a defect I can prove from the code: _prepare_compiler_toolchain itself reads no environment variable (runtime_patches/*.py contains no environ/getenv, verified), so the only ordering dependency is on TRITON_CACHE_DIR/CUDA_HOME, which affect the Triton compile cache path and ptxas location at compile time, not at import.

Evidence limitation: whether _set_triton_libdevice_path observes CUDA_HOME at call time (it is invoked inside with config.patch(...)) can only be settled by running the compiler on a host with CUDA_HOME set; test_toolchain.py:16-83 covers the identity, not the ordering against environment.apply.

#6 [medium] Nit: cublas tactic is a bare string, unlike every sibling native contract's tuples

_tactics returns a bare string alongside tuples (bf16_bmm.py:57 return ("cublas", *tiles)), so NativeConfig.tactic is sometimes str and sometimes tuple. Every other native contract returns tuples: gdn_prefill_sm103.py:45 tactics=lambda query: ("chunked",), paged_tma.py:26 (128,), region_selection.py:58 (0,), window_staging.py:27 (0,), selected_attention.py:51 (1, warps) tuples. The mismatch is then papered over with identity tests: if tactic == "cublas" (bf16_bmm.py:158), self.program is None standing for cuBLAS (bf16_bmm.py:117,124), and () if tactic == "cublas" else (...) (bf16_bmm.py:172).

Consequence: the "cuBLAS" tactic is encoded as an absence of a program rather than as a tactic field, so State.run branches on program is None and prepared_state on a string literal. Sibling States dispatch on tactic fields only (selected_attention.py:73-86, window_staging.py:65-77). Fix: use ("cublas",) (or ("cublas", 0) when per-tactic payloads are needed) so _tactic's documented invariant ("nested tuples of integers, booleans, and strings", native_operator.py:16-22) holds uniformly.

Nit-level; behaviour is correct, and native_operator._tactic accepts bare strings, so nothing breaks.

Also note: the CUDA test file's stacks()/ulps() helpers (tests/components/test_bf16_bmm_cuda.py:17-38) duplicate glm_stacks() (tests/components/test_bf16_bmm.py:13-18) and the double-precision reference pattern of tests/recipes/test_glm53_bmm_cuda.py:84-86. Acceptable for a separate CUDA-gated file; not worth a change unless the owner wants one shared fixture.