#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:
- NEW ignition/components/bf16_bmm.py (184 lines): Query(operands: tuple of (shape,stride)), _tactics, CONTRACT = native_contract(component_id="flare.bf16_bmm"), _bmm_kernel (@triton.jit), _launch, compile_bmm (@program_cache(scope="preparation")), State(query,tactic,program=None) with run(), execute = torch.library.custom_op("flare::bf16_bmm", mutates_args=("output",)), query_from_call, prepared_state, declare_bmm.
- ignition/components/linear.py: BatchedUnquantizedProjection now delegates to bf16_bmm.declare_bmm/execute;
_bmm_outcustom op (flare::bmm_out, linear.py:89-96) is left defined but its only call site was removed. - ignition/preparation/tuning.py:
_prepare_compiler_toolchain()now runs at module import (line 66) instead of in RecordedPreparationSession.init and declare(). - tests: tests/components/test_bf16_bmm.py (new CPU), tests/components/test_bf16_bmm_cuda.py (new CUDA), tests/preparation/test_toolchain.py (adds "import" subprocess param).
Lens: conventions/duplication vs existing precedents:
- native_contract/native_plan: ignition/components/native_operator.py:44,87; used by qk_norm_rope.py:147, gated_norm_preparation.py:83, window_staging.py:162, selected_attention.py:119, window_attention.py:202 (materialize closures inline,
_NATIVE_JITSadded at module scope). - _NATIVE_JITS precedent: moe_sm103.py:31,357,387 and sm103_projection.py:33,258 (module scope).
- benchmark fixture precedent: ignition/preparation/fixtures/bf16_gemv.py:10-16 benchmark_call(state, *, inputs, weight, bias) -> PreparedCall(run, produce, output, owners=(operands, weight, bias)); used linear.py:129,142.
- input_factory precedent with session: linear.py:130,143,354 pass session=session; bf16_bmm.py:174 does not.
- tensor_facts/metadata_tensor helpers: selected_attention.py:27-33, reused by qk_norm_rope.py:25,71.
- Rules: AGENTS.md:73-78 (reject duplicate kernels/unnecessary abstraction), AGENTS.md:235 (prefer proven b12x operators over duplicate kernels), AGENTS.md:192-217 (captured-graph op family approval), docs/design-review.md principles 4, 11, 15.
#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.
- ignition/components/linear.py:89-96 still defines
@torch.library.custom_op("flare::bmm_out", mutates_args=("out",))+_fake_bmm_out, whose sole caller wasBatchedUnquantizedProjection.__call__(old_bmm_out(source, self.weight, self.output)), replaced at linear.py:85 bybf16_bmm.execute(...). - Whole-repo search for
bmm_outat the head commit returns only: the definition/fake (linear.py:89-96) and the unrelatedsm103_bmm_outused at linear.py:452. No test, recipe, helios or heliopause reference (searches in helios/src, heliopause, tests all empty). - The behaviour it performed (
torch.bmm(source, weight, out=out)as a mutating opaque op) is now a superset insidebf16_bmm.State.run(bf16_bmm.py:124-126), so nothing is lost by deleting it.
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.
-
Tensor facts.
query_from_callbuildstuple((tuple(t.shape), tuple(t.stride())) for t in tensors)(bf16_bmm.py:153) andcompile_bmmrebuilds meta tensors withtorch.empty_strided(shape, stride, dtype=..., device="meta")(bf16_bmm.py:102-103). The repository's existing pair istensor_facts(tensor)andmetadata_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: reusetensor_facts/metadata_tensor(as qk_norm_rope does), or state in the module docstring why BF16-only queries cannot carry dtype. -
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 viapartial(bf16_fixtures.benchmark_call, ...)and linear.py:142). The difference from the established pattern: every caller passessession=sessiontoinput_factory(linear.py:130, 143, 354; mla_norm_quant.py:46-47; varlen_attention.py:56-57), while bf16_bmm.py:174 passes onlystorage=.candidate_inputs(ignition/preparation/fixtures/resources.py:24-38) uses that session to callsession.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: passsession=sessionand reusebf16_gemv.benchmark_callwithbias=None. -
Per-call re-validation.
State.runre-checks shapes/strides againstself.query.operandson 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_calland theirState.runonly 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):
native_contract+native_planusage (bf16_bmm.py:60,170) follows the established pattern exactly: Query frozen dataclass of JSON-compatible fields,_tacticscached-free pure function,scratch_bytes=lambda t: 0,materialize(tactic)closure,compile_jobs(tactic, detected)returningCompileJob.create("module:function", payload, tactic, detected.ordinal). Compare 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()+torch.empty_strided(..., device="meta")+attach_programs/load_programs(bf16_bmm.py:99-110, 156-162) is the same shape as gated_norm_preparation.py:39-46, qk_norm_rope.py:67-71, selected_attention.py:57-63.plan_from_handle/require_prepared(plan, COMPONENT_ID, device)inside amutates_argscustom_op with an emptyregister_fake(bf16_bmm.py:131-139) matches selected_attention.py:89-100 and window_staging.py:80-91.- Query field
operands: tupleof (shape, stride) pairs matches selected_attention.Query.operands / qk_norm_rope.Query.operands. - No per-model branch: component id
flare.bf16_bmmis model-agnostic; the GLM special case lives in ignition/recipes/glm53/mla.py:244, which already chose BatchedUnquantizedProjection (no new branch added). declare(session, (plan.request(...prepare_call..., benchmark_call...),))anddeclare()'s session dispatch unchanged (tuning.py:730-736); the removed_prepare_compiler_toolchain()insidedeclarewas redundant with the module-import call and withRecordedPreparationSession.__init__.- test_toolchain.py's new "import" parameter keeps the same subprocess harness (no new mechanism).
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).
- ignition/components/linear.py:89-96 defines
_bmm_out(@torch.library.custom_op("flare::bmm_out", mutates_args=("out",)), bodytorch.bmm(source, weight, out=out)) plus_fake_bmm_out. - Its only caller was
BatchedUnquantizedProjection.__call__; this PR replaced that call withbf16_bmm.execute(source, self.weight, self.output, self.plan.handle)at linear.py:85. - Verified by search: the only remaining
bmm_outhits repo-wide are this definition/fake and the unrelatedsm103_bmm_out(linear.py:463-469, used at linear.py:452). Nothing in tests/, ignition/recipes/, helios/src or heliopause referencesflare::bmm_out. - Why it matters: AGENTS.md:73-78 rejects unresolved abstractions and churn and requires refactors to remove demonstrated cost; the behaviour is now a strict subset of
bf16_bmm.State.run(bf16_bmm.py:124-126 calls the sametorch.bmm). - Fix: delete linear.py:89-96.
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.
- Fix: pass
session=session; reusebf16_gemv.benchmark_call(..., bias=None); reusetensor_facts/metadata_tensoras qk_norm_rope.py does.
SHOULD FIX 3 (small) — Per-replay re-validation inside the captured forward.
State.runre-derives and compares shapes/strides on every invocation (bf16_bmm.py:119-123), i.e. on every CUDA-graph replay. Siblings validate once at declaration:query_from_call(bf16_bmm.py:142-153) already checks geometry, and qk_norm_rope.py:80-84, gated_norm_preparation.py:55-57 and window_staging.py do not re-validate inState.run.- Fix: drop the redundant check from
run, or state the reason it must stay (it does guard against a caller passing stacks other than the declared ones).
NIT 1 — cublas tactic is a bare string, breaking the uniform tuple shape.
- bf16_bmm.py:57 returns
("cublas", *tiles)(a str plus 5-tuples), then bf16_bmm.py:158 teststactic == "cublas"and bf16_bmm.py:172() if tactic == "cublas", and bf16_bmm.py:117/124 encode "cuBLAS" asself.program is None. Every sibling native contract returns tuples: gdn_prefill_sm103.py:45("chunked",), paged_tma.py:26(128,), region_selection.py:58(0,), window_staging.py:27(0,), selected_attention.py:51(1, warps).native_operator._tactic(native_operator.py:16-22) documents "nested tuples of integers, booleans, and strings". - Fix: make it
("cublas",)so dispatch reads the tactic rather than the absence of a program.
NIT 2 — b12x private registry mutated inside the compile function, not at module scope.
- bf16_bmm.py:108
compile_plan._NATIVE_JITS.add(_bmm_kernel)sits insidecompile_bmm, and the module imports the private module (from b12x._lib import compile_plan, bf16_bmm.py:26) instead of the name. Precedents are module scope and named imports: ignition/components/moe_sm103.py:31 then :357, :387; ignition/components/sm103_projection.py:33 then :258. - Consequence: registration happens in whichever process compiles rather than in every importer at import. Appears behaviourally equivalent (the compile worker imports the module), but the placement departs from the established pattern.
- Fix:
from b12x._lib.compile_plan import _NATIVE_JITSin the existing import block at bf16_bmm.py:27 and_NATIVE_JITS.add(_bmm_kernel)next to the kernel definition.
OBSERVATION (not proven a defect) — import-time compiler toolchain vs environment setup.
- ignition/preparation/tuning.py:66 now runs
_prepare_compiler_toolchain()at import; the calls inRecordedPreparationSession.__init__and indeclare()'s non-recorded branch were removed. The body (tuning.py:47-63) callsruntime_patches.apply()then_set_triton_libdevice_path(). - docs/preparation-development.md:59 and ignition/cli/environment.py:36-61 define the order as "after dependency environment setup":
environment.apply()sets TRITON_CACHE_DIR, CUDA_HOME and TRITON_PTXAS.EXE_PATH and then applies patches. ignition/cli/serve.py importsignition.planningat line 14 and callsenvironment.applyat 211/322/476. - I verified
ignition/runtime_patches/*.pycontains noenviron/getenv, so_prepare_compiler_toolchainitself reads no environment variable; the separable question is whether_set_triton_libdevice_pathresolves CUDA_HOME at call time. Settled only by running the toolchain test on a host with CUDA_HOME set; tests/preparation/test_toolchain.py:16-83 checks identity, not ordering againstenvironment.apply. - Recommendation: raise as a question to the owner about placement, not as a defect.
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.
_NATIVE_JITSis mutated inside the compiled function instead of at module scope. bf16_bmm.py:108 doescompile_plan._NATIVE_JITS.add(_bmm_kernel)insidecompile_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. Sincechunked/load_programs/compile_only_launchesimport stands, adding one more name to the existingfrom b12x._lib.compile_plan import ...line at bf16_bmm.py:27 is the convention.compile_bmmre-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.