Skip to content

Race a Triton tile kernel against cuBLAS for GLM's BF16 head projections - #231

Open
original-el8 wants to merge 2 commits into
mainfrom
perf/glm53-bf16-head-projections
Open

original-el8 wants to merge 2 commits into
mainfrom
perf/glm53-bf16-head-projections

Conversation

@original-el8

Copy link
Copy Markdown
Collaborator

GLM-5.3-Flash's MLA query absorption and value expansion multiply token-major head stacks by per-head BF16 kv_b_proj slices through BatchedUnquantizedProjection, which called torch.bmm. On GB10, cuBLAS runs one 32x32 WMMA kernel for every layout of these strided batches and moves an 8,192-row TP2 absorption (403 MB) at about half of the device's measured copy bandwidth.

Behavior

  • BatchedUnquantizedProjection declares a prepared flare.bf16_bmm plan. Its tuning race times cuBLAS against a Triton tiled kernel over the declared strides and keeps the faster one on the preparing device. Both accumulate in FP32 and round once to BF16, use no scratch, and write the caller's strided output view.
  • ignition.preparation.tuning prepares the compiler toolchain (runtime patches, then the CUDA toolkit libdevice) at import instead of at session construction. Triton memoizes compiled kernels by specialization and launch options only, so a kernel compiled against Triton's bundled libdevice before a session existed was later reused under an identity that native discovery had not declared ("preparation used undeclared programs"). The new component's CUDA tests compile per-tactic kernels before their race test in one process and need this ordering.

Measurements

One GB10 (DGX Spark), 8,192 rows, torch.bmm against the selected 64x128x64 tile:

Projection torch.bmm Triton tile Plain copy of the same bytes
TP2 absorption 3.53 ms 2.02 ms 1.84 ms
TP2 expansion 2.62 ms 2.02 ms 1.84 ms
TP4 absorption 1.77 ms 1.00 ms 0.93 ms
TP4 expansion 1.27 ms 1.01 ms 0.93 ms

Every tile is bitwise equal to torch.bmm at the probed GLM shapes on GB10 and RTX PRO 6000; the test contract allows one BF16 ulp.

Validation

  • On this branch (91cfb36 plus these commits): tests/components/test_bf16_bmm.py, tests/components/test_linear.py and tests/preparation/test_toolchain.py passed 23 tests on CPU; tests/components/test_bf16_bmm_cuda.py passed 5 tests on one RTX PRO 6000 (SM120) with an empty Triton cache.
  • The same component source on an integration branch passed test_bf16_bmm_cuda.py on GB10 and tests/recipes/test_glm53_mla_cuda.py at TP2 and TP4 on RTX PRO 6000 with the GLM-5.3-Flash-FP4-Local checkpoint.
  • An exported package combining this component with other prefill changes served TP2 8,192-token prefill at 2945 tokens/s on two DGX Sparks, against 2620 to 2631 tokens/s for packages without those changes. That difference is not attributed to this PR alone.

Limitations

  • Not run on this branch: GB10 tests, the GLM MLA checkpoint test, and SM103. The MXFP8 kv_b_proj path (BatchedQuantizedProjection) is unchanged.
  • The Triton kernel registers itself in b12x's _NATIVE_JITS so compile workers evict its deferred planning entries; b12x evicts only kernels defined in its own modules otherwise.
  • Packages export the tuned choice; no package metadata or Helios code changes.

Flare compiles its Triton kernels against the CUDA toolkit's libdevice so
that native discovery, compiled forwards and Inductor's eager-precision
path share one program identity. That selection ran when a preparation
session was constructed or a standalone declaration prepared. A kernel
compiled earlier in the process against Triton's bundled libdevice stayed
in its JIT memo, which is keyed by specialization and launch options
only, and a later race materialization reused it under the bundled
identity while discovery had declared the toolkit one. The BF16 batched
GEMM test module hit this on its race test after its per-tactic tests
compiled the same specialization: "preparation used undeclared programs
for k-major".

ignition.preparation.tuning now prepares the compiler toolchain at
import, before any component kernel in the process can compile: it
applies Ignition's runtime patches and then selects the toolkit
libdevice. The session constructor and standalone declarations no
longer repeat it. Compiler workers inherit the selection through
TRITON_LIBDEVICE_PATH as before.

Validation on 91cfb36 with this change: tests/preparation/test_toolchain.py
passed 3 tests on CPU, including a case that checks the library identity
right after import. The BF16 batched GEMM component that follows this
commit runs its CUDA race test after its per-tactic tests in one
process; see that commit for its results. SM103 kernels in
moe_sm103.py and sm103_projection.py were not exercised.

Design-Signed-Off-By: flare-02; design-review=4d3a
GLM 5.3 Flash's MLA query absorption and value expansion multiply
token-major head stacks by per-head BF16 kv_b_proj slices through
BatchedUnquantizedProjection, which launched torch.bmm directly. cuBLAS
selects its strided-batched kernel by device: on GB10 it runs
cutlass_80_wmma_tensorop_bf16_s161616gemm_bf16_32x32 for every layout,
which moves the 403 MB of a TP2 8192-row absorption at about half of the
device's measured 220 GB/s copy bandwidth.

BatchedUnquantizedProjection now declares a prepared plan of the
flare.bf16_bmm component. Its tuning race times cuBLAS against a Triton
tiled kernel over the declared strides, with legal tiles chosen by the
stack's row count and the output and reduction widths, and keeps the
faster implementation on the preparing device. Both implementations
accumulate in FP32 and round once to BF16; no scratch is used, and the
output stays the caller's strided view. The compile factory registers the
kernel with b12x's planning-artifact eviction, since b12x only evicts
deferred programs of kernels defined in its own modules and a compile
worker that planned a program would otherwise return the deferred entry
instead of lowering it.

One GB10 (DGX Spark), 8192 rows, torch.bmm versus the selected 64x128x64 tile:
TP2 absorption 3.53 ms to 2.02 ms, TP2 expansion 2.62 ms to 2.02 ms,
TP4 absorption 1.77 ms to 1.00 ms, TP4 expansion 1.27 ms to 1.01 ms. A
plain copy of the same bytes takes 1.84 ms and 0.93 ms. Every tile
produces results bitwise equal to torch.bmm at the probed GLM shapes on
GB10 and RTX PRO 6000; the test contract allows one BF16 ulp. On RTX PRO
6000 the race also selects the 64x128x64 tile for 1024-row stacks.

Validation on 91cfb36 with this change and the libdevice commit before
it: tests/components/test_bf16_bmm.py, tests/components/test_linear.py
and tests/preparation/test_toolchain.py passed 23 tests on CPU, and
tests/components/test_bf16_bmm_cuda.py passed 5 tests on one RTX PRO
6000 (SM120) with an empty Triton cache, covering every legal tactic at
65-, 2- and 4096-row stacks with row-tail masking, and a raced session
captured and replayed without allocation. The same component source,
built on an integration branch, passed test_bf16_bmm_cuda.py on GB10
and tests/recipes/test_glm53_mla_cuda.py at TP2 and TP4 on RTX PRO 6000
with the GLM-5.3-Flash-FP4-Local checkpoint, whose kv_b_proj is BF16;
the GB10 timings above come from that build.

An exported GLM-5.3-Flash package that combines this component with
other prefill changes served TP2 8,192-token prefill at 2945 tokens/s
on two DGX Sparks, against 2620 to 2631 tokens/s for packages without
those changes (three-request probe means); that difference is not
attributed to this commit alone. The MXFP8 kv_b_proj path
(BatchedQuantizedProjection) and SM103 are unchanged and were not run.

Design-Signed-Off-By: flare-02; design-review=4d3a
Signed-off-by: Jason Cook <jasonc@maxlyn.com>
@original-el8 original-el8 added area:kernels GPU kernels and numerical execution. area:preparation Ignition checkpoint loading, tuning, compilation, capture, and export. enhancement New feature or request model:glm-5.3 Affected recipe or documented reproducer: glm-5.3. Impact may be broader. performance Performance, latency, throughput, or memory-efficiency investigation or improvement. triage:design-review Needs a maintainer scope, architecture, or contract decision before implementation or integration. labels Nov 8, 2026
@original-el8

Copy link
Copy Markdown
Collaborator Author

Categorized as kernel performance work requiring triage:design-review, with model:glm-5.3 identifying the affected head projections. The change introduces a prepared BF16 batched-matmul operator, a Triton/cuBLAS tuning choice, b12x private compile-registry integration, and earlier toolchain initialization.

Review the operator's placement and existing b12x alternatives, compile-worker cache eviction, and numerical/stride coverage before accepting the integration. The PR reports SM120 checks on this branch; GB10 and full-model throughput evidence comes from a combined branch, and SM103 is untested. Those combined results do not isolate this PR's serving gain.

kyonjova added a commit to kyonjova/flare-local-inference that referenced this pull request Nov 8, 2026
…integration

Brings original-el8's perf/glm53-bf16-head-projections (0ba78eb) onto the
integration branch: a prepared flare.bf16_bmm plan racing cuBLAS against a
Triton tile for GLM's MLA absorption and expansion, and toolchain preparation
at tuning-module import. Clean merge; no file overlaps with the integration
branches.

Validation on this box (no GPU): tests/components/test_bf16_bmm.py,
tests/components/test_linear.py, tests/preparation/test_toolchain.py 20 passed
3 skipped; GLM recipe CPU tests 39 passed 215 skipped. GB10 export and
test_bf16_bmm_cuda.py not run.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Design-Signed-Off-By: Claude Fable 5.1 (Claude Code); design-review=4d3a
Signed-off-by: Kyonjova <kyonjova@pm.me>
@original-el8

Copy link
Copy Markdown
Collaborator Author

@original-el8 GitHub reports merge conflicts with main, confirmed locally with git merge-tree for PR head 0ba78ebe9000 against main 97cfeb0001f1. Conflicting paths:

  • ignition/components/linear.py

Please reconcile the branch with main and rerun the checks affected by the resolution.

@original-el8

Copy link
Copy Markdown
Collaborator Author

Repository-split triage: this PR spans both repositories and remains open for its runtime changes.

  • hyperflare-recipes: the BF16 batched-matmul implementation, projection wiring, and component tests.
  • This runtime repository: the compiler-toolchain initialization change in ignition/preparation/tuning.py.

The recipe portion needs a linked PR against the hyperflare_recipes/ package, with its tests and validation evidence carried over. The runtime portion can remain here after removing recipe-owned files.

@flare-autobot

Copy link
Copy Markdown
Collaborator

A case file has been opened for 231.
Case File Intake Report

Future reviews will auto-run 90 minutes after code updates are pushed.

  • Re-review sooner: Call @flare-autobot after push.
  • Opt-out: Include #autobot:ignore in the PR or any comment.

@flare-autobot

Copy link
Copy Markdown
Collaborator

Autobot Review

Full Review for Agents · Value Proposition · Case File

Recommendation: Changes Requested

Thanks — the kernel and the prepared-operator shape are clean: the race is expressed as one declared contract, there is no duplicate kernel, no per-model branch, no fallback flag, and I found no concrete defect in _bmm_kernel. Not ready to merge because of placement, not code quality. Full evidence is in the case file.

Blockers:

  • Four of the six changed files belong in hyperflare-recipes. ignition/components/bf16_bmm.py, the BatchedUnquantizedProjection rewrite in linear.py:69-86, and both component test files are reusable BF16 projection mechanisms; AGENTS.md:385-392 and docs/design-review.md:31-37 keep ignition/components/ to the 13 Helios-ABI modules, enforced exactly by tests/preparation/test_core_boundary.py:11-26,62-64, which the new module fails on main (linear.py is not on main at all). Matches your own 2026-11-24 split triage. → Split: this repo keeps ignition/preparation/tuning.py + tests/preparation/test_toolchain.py; open a linked PR in local-inference-lab/hyperflare-recipes with the component, the wiring, both test files and their validation evidence.
  • Rebase before splitting. The head predates the split; the reported conflict is ignition/components/linear.py — the file whose body changed — so the resolution needs re-review, and CI on the head is red.

Should fix:

  • tuning.py:66 now prepares the toolchain at import of a ~60-module dependency, ahead of ignition.cli.environment.apply(), which sets CUDA_HOME (environment.py:52-57) before runtime_patches.apply() (:61) as environment.py:5-11 and docs/preparation-development.md:59-63 require; environment.apply never re-runs _set_triton_libdevice_path, so the libdevice identity is chosen before the configured toolkit exists. Current CLI paths only avoid it by import order. → Keep the import-time call (it is shorter than the two sites it replaced), state the ordering in the description, and pin it with one assertion in the existing subprocess harness: drive CUDA_HOME/environment.apply before the import and assert the libdevice comes from that toolkit. The new "import" case only compares identities.
  • bf16_bmm.py:7-9 overstates the numerics — "FP32 accumulation and one BF16 rounding" is the Triton tile only (:78,84); the default tactic is torch.bmm (:57,124-126), which follows allow_bf16_reduced_precision_reduction. → Say "the Triton tile accumulates in FP32 and rounds once; the cuBLAS tactic follows PyTorch's BF16 matmul settings; the CUDA test bounds every tactic to one BF16 ulp of torch.bmm", and use the same sentence in the description instead of "Both accumulate in FP32 and round once to BF16". No new code.
  • bf16_bmm.py:108 registers _NATIVE_JITS inside the memoised compile_bmm, so it is skipped on every program_cache hit even though the comment at :105-107 requires it before planning. → Import _NATIVE_JITS on :27, add _NATIVE_JITS.add(_bmm_kernel) next to the @triton.jit definition, drop :108 and the compile_plan import (sm103_projection.py:257-258, moe_sm103.py:357 (not opened),387).
  • bf16_bmm.py:174 omits session= in the only input_factory call in the repo that does (linear.py:130-131,143, mla_norm_quant.py:46-47 all pass it), so release_benchmark_cache() never runs before the race allocates (fixtures/resources.py:30-33). → session=session.
  • docs/TODO.txt:185-187 is the libdevice/program-key item this work closes and is still open (AGENTS.md:513-515). → Delete it, or restate what remains and its removal criterion.

Subtract / Simplify:

Could remove ~30 of +373 added lines (and ~349 leave the repo with the split):

  • Split (≈349 lines): bf16_bmm.py + linear.py + both test files leave; the remaining tuning.py change already deletes two lazy call sites in favour of one.
  • Dead code (8 lines): linear.py:89-96, flare::bmm_out and its fake — the diff replaced their only caller with bf16_bmm.execute (:85); torch.bmm into a caller-owned view is now State.run:124-126. Delete, wherever linear.py ends up.
  • Duplicate helpers (≈11 lines): drop State.run's revalidation (:119-123, already enforced by query_from_call:142-153 and on the launch hot path); replace the private benchmark closure (:176-179) with fixtures/bf16_gemv.benchmark_call; use selected_attention.tensor_facts/metadata_tensor; inline prepared_state into materialize=.
  • Tests that cannot fail (≈6 lines): test_bf16_bmm.py:31-33 is arithmetically guaranteed (256-column output vs columns % tile[1] == 0) and :21-27 transcribes the _TILES table; test_bf16_bmm_cuda.py:63 adds a statistic after :62/:64 already bound the error. Keep test_bf16_bmm.py:36-44 and the rest of the CUDA test.

Nits:

4 nits

To opt out, include #autobot:ignore in your post or comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:kernels GPU kernels and numerical execution. area:preparation Ignition checkpoint loading, tuning, compilation, capture, and export. enhancement New feature or request model:glm-5.3 Affected recipe or documented reproducer: glm-5.3. Impact may be broader. performance Performance, latency, throughput, or memory-efficiency investigation or improvement. triage:design-review Needs a maintainer scope, architecture, or contract decision before implementation or integration.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants