Repository navigation
Description
What Bend should do
These are questions more than a request; the measurements are under Why.
- Could a balanced tree's leaves be dealt to threads in a fixed order, or could a bang expose the thread's index? (CUDA: the leaves of a balanced fork tree land on threads in an order that changes from run to run, so a layout cannot be coalesced #1780)
- Could the tree be grown on more than one group, or could a bang over a known balanced tree skip the grow?
- Is there a way to keep a bang's blocks on the device between bangs (no host touches until the result is read)?
- Could a spin's array masks be computed once per spin when its arrays are passed unchanged (the common case in a flat loop over one block)?
- Would a separate, smaller kernel for leaf-only spins be possible, so a flat loop does not inherit the runtime's 80 registers?
Programs and Nsight reports available on request; I can rerun anything on this GPU.
Why
I wrote a tiled matmul with the one-block pattern of tests/run/stencil3d.bend (Array.fork/Array.join, one block per array, one 4x4 block of C a leaf over 2^14 leaves, A stored transposed so lanes read neighbouring cells), and the same kernel in CUDA (same thread mapping, layouts and data, compiled with NVRTC, --fmad=false as Bend's device build). RTX 4050 Laptop, driver 615.78.08, main 881cb2e (with #1602), idle machine, 5 alternated rounds; the results are the same bits in both:
| ms a product | Bend | handwritten | ratio |
|---|---|---|---|
| 512x512x4096, 4x4 a leaf | 4.62-4.72 | 1.08-1.23 | 4.3x |
| 512x512x4096, 8x4 a leaf | 3.44-3.54 | 0.78-0.82 | 4.4x |
| 256x1536x576, 4x4 a leaf | 2.10-2.17 | 0.224-0.226 | 9.4x |
From K = 1024 to 4096 (512², 4x4): ~0.97 µs a K step in Bend against ~0.27 µs, plus ~0.7 ms a bang, which is why the smaller product does worse. This is where the time goes, with Nsight Systems and Nsight Compute.
A bang's fixed cost (~1.3 ms empty)
Each bang is five bend_dev launches back to back, then the host's turn. Nsight Systems, medians of 19 bangs:
| ms | grow, 1 group | grow, 128 groups | main pass | final | host, between bangs |
|---|---|---|---|---|---|
| an empty bang over a 2^14-leaf tree | 0.55 | 0.10 | 0.11 | 0.007 | 0.58 |
| the 512² matmul (4x4) | 0.32 | 0.15 | 5.06 | 0.009 | 0.30 |
(the fifth launch, between the grows and the main pass, takes ~1 µs). The first launch grows the fork tree on one group of 128 lanes, so a balanced 2^14 tree costs up to 0.55 ms before any leaf runs, and the host's turn adds about as much. On 2.0.37 I counted hundreds of CPU page faults a bang in that turn (Bend's heap is managed memory), while the host only reads a result.
The main pass
Nsight Compute on main, application replay (kernel replay restores device memory, and with Bend's managed heap that makes every replay fault its pages back in):
| Bend | handwritten | |
|---|---|---|
| warp instructions | 175.7 M | 95.5 M |
| SASS instructions a K step | 83 | ~45 |
| registers; blocks of 128 per SM | 80; 6 (registers) | 39-48; 10-12 |
| warps active a cycle | 21.6 | 23.6 |
| sectors a load request | 16.45 | 8.5 |
The step carries, besides the 8 loads and 32 float operations:
- the error poll (
YIELD,BSSY/BSYNC, a counter and a branch every step; CUDA: the error poll in every flat loop (err_spun) makes ptxas yield on each iteration; tight loops run up to 20x slower #1776); - ~30 address instructions: every
Array.getmasks its index (i & (2^cls - 1)) and the mask is rebuilt from the block's class bits each step (blk_atreadsblk_cls(a)of an array that is a loop variable), so the compiler cannot turnbase + iinto a pointer plus a fixed offset; - a 64-bit counter for the
Natfuel.
And the whole runtime is one kernel, so the matmul's loop runs with the register count of the largest path in it (80), which caps the resident lanes at 6 blocks per SM.
The largest single part is coalescing: Bend's loads cost 16.45 sectors a request against the twin's 8.5, because the warps do not hold the leaves the layout assumes. Each block of 128 threads runs one aligned range of 128 leaves, but inside the block the leaves are dealt in an order that changes from run to run (#1780). Renumbering the leaves to fit the order I observed (a permutation of the low 7 bits) brings the loads to 9.59 sectors a request, with the same instructions:
| 512², 4x4, ms a product | |
|---|---|
| as built | 4.62-4.72 |
| leaves renumbered | 2.82-2.96 |
| renumbered, poll split (#1776's patch) | 3.14-3.30 |
| renumbered, poll removed | 2.16-2.22 |
| handwritten | 1.08-1.23 |
The last Bend row minus the ~0.7 ms a bang costs (~1.5 ms) is where the handwritten kernel lands once it copies Bend's index mask, poll and occupancy: 1.36-1.47 ms at 6 blocks per SM (2026-10-18, busy CPU; the mask alone +20%). Copying Bend's addressing further (both masks rebuilt every step, a 64-bit counter) costs the twin nothing measurable. Ruled out: the leaf count (4,096 to 16,384 leaves give the same ratio), unrolling (the twin without unrolling is not slower), managed memory (the twin in one managed heap runs as fast) and the data layout (the same in both).
One thing I did not expect: with the leaves renumbered, #1776's split poll is slower than the plain poll (3.14-3.30 against 2.82-2.96 ms), while on the matmul as built it is faster (222-227 against 230-237 ms for 50 products). I have not looked at why.
Activity
adeerkhan commentedon Oct 22, 2026
A PR for the poll, which is one of the per-step costs of the main pass:
#1820
The device's native spin now runs its body in an inner loop of 4096 steps and
reads the error word once, after them, so the poll's branch (the YIELD and the
BSSY/BSYNC around it) leaves the hot path. The poll's period does not change,
so an error is caught after the same number of steps as before.
Measured on an idle RTX 5060 (driver 615.71.09, CUDA 13.4), your flat loop
with five rounds, the three builds alternated, the best of each: main 648 ms,
the PR 332 ms, and 324 ms with the poll removed entirely, so the split is 2.5%
off the floor. A memory-bound loop is not affected
(
tests/run/stencil3d.bendtakes 175 ms against 172 ms).This addresses the poll only. The coalescing, which the tables say is the
larger part of the main pass (16.45 sectors a request against the twin's 8.5),
is not addressed here: the leaves still land on the lanes in an order that
changes from run to run (#1780), so the layout cannot be chosen to fit.
Two of your other questions look reachable next, in the order they would pay
off:
blk_atrebuilds the index maskfrom the block's class bits on every
Array.get, and the class bits comefrom a handle that the loop threads, so the compiler cannot turn
base + iinto a pointer and a fixed offset. A spin's array parameters donot change while it runs, so the mask can be computed once per spin; the
emitted C would take the mask from the spin's own parameter instead of the
handle.
loop inherits the register count of the largest path in it (80 registers,
six blocks of 128 per SM, against the twin's 39-48 and ten to twelve). A
separate kernel for a bang whose leaves only run flat loops would take the
loop's own count.
If you still have the programs and the Nsight reports for the tiled matmul, I
would like to run the mask change against them: the numbers here are from a
tighter loop than the matmul, and the matmul is where the mask shows.
A native spin polls the error word every 4096 steps, not every step (b…
nuxyel commentedon Oct 25, 2026
Some CPU numbers on the mask, since it shows up there too, from a set of ML workloads I'm putting together (each checked bit for bit against a handwritten C version).
On the C backend the mask is already loop-invariant: in the emitted loop clang keeps it in a register and doesn't reload the class bits. What stays is the
andon every read, and with it the loads can't use a base plus a fixed offset. A rotating dot product (tensor-array's), unrolled by 2 by clang, keeps anandper read, with aleaor amovaround it, plusmovapscopies for the four rotating sums:and the same loop in C (same output,
1214833289), both built with-std=c11 -O3:Bend 2.0.39, clang 23.1.3, one thread pinned to a P-core (Core Ultra 7 155H), user-mode counts, 3 runs each:
On a bigger loop, SmolLM2-1.7B's feed-forward matrix-vector products at one token (tensor-array's
Bands, 24 tokens), the C version takes 3,258 M instructions and ~1.0 G cycles against Bend's 10,635 M and ~2.4 G. Giving the C version the same masks, read from avolatileso clang can't drop them, brings it to 9,902 M and 2.0-2.3 G, so on that loop the mask is most of the distance. Where the reads come in cells of 8 (tensor-array's tiled product), the mask is paid once per cell and the gap is 1.33x.So on the CPU, computing the mask once per spin wouldn't change much by itself; what would is dropping the
andwhen the index is known to stay below the capacity. Here the loop'skcounts down from the array's length, which the type checker could carry as a proof. Is that something the compiler could use, or would it need an explicit unchecked read? Happy to run any change against these.adeerkhan commentedon Oct 26, 2026
Thanks, this is useful — I reproduced the loop on my machine (i7-14700HX, clang 22.1.8, Bend 2.0.39, one thread pinned, three runs):
andremoved fromblk_at(keptblk_cls)So the mask is the main part of the CPU gap here too. The
and-removal was an unsafe edit of the emitted C (the last read wraps), so it is only an upper bound, not a proposed patch.The short answer to your question: I do not think the compiler can use the loop's
ktoday.Array<T>carries no length or capacity in its type, andblk_atgets the class from the handle at run time. There is no fact connecting theNatfuel to the array's class, so a proof written in the source would not reach code generation unless we add a primitive that carries it.The paths I see, in increasing size:
Array.get_unchecked, either behind a proof or marked@unsafelikeArray.fork. This is the small one and would remove theandexactly where the caller knows the range. It is a language/API decision, andbend2/bend.tsis human-owned, so I would not add it without the maintainers agreeing on the spelling and the safety story.i + left <= 2^classonce per loop and run an unmasked copy. Bigger, and it needs the type checker or the compiler to know the array's class; it would help code that does not have an explicit proof.andand document the cost; then downstream users can choose an explicit unchecked read when they have the bound.I agree this should not go into #1820: that PR is the device poll only, and mixing a CPU array-access change into it would make both harder to review. If you want to take (1) or (2), I can open a separate PR and run your SmolLM2 programs against it — the tiled product with cells of 8 and the
Bandsfeed-forward are the two shapes where the numbers would say whether it is worth it.Which would you prefer? I would start with (1) if the maintainers are open to a checked unchecked-read spelling; otherwise (2) is the only path that helps code that does not have a proof.
A native spin polls the error word every 4096 steps, not every step (b…
nuxyel commentedon Oct 27, 2026
Thanks for reproducing it. I'd take (1) first, as
@unsafelikeArray.fork, and only for reads, with one caveat about who can use it. The loops where the mask shows all know their bound. Their originals are in tensor-array, which carries laws and so can't use@unsafe; their parallel copies in tensor-fork and tensor-gpu are outside the proofs already (they share arrays throughArray.fork), and those could take the unchecked read today. For tensor-array itself only (2) would help, or a read that takes a proof of the bound once anArraycarries its capacity. So (1) is the small step and (2) is the one my proved code is waiting for. I don't think doing (1) first wastes anything: the copies would move from the unchecked read to the checked one when (2) lands.The spelling and the safety story are the maintainers' call. What my loops need is a read whose index the caller promises is below the block's capacity, so it skips the wrap; the writes can stay as they are.
When you open the PR I'll run it here and post the tables:
Bands), and tensor-array's tiled product in cells of 8, each against its C version;adeerkhan commentedon Oct 27, 2026
Opened #1880 for the first step:
Array.get_unchecked,@unsafelikeArray.fork, read-only. It skipsblk_at's mask on the C lane and reads$0[$1]on JS; the interpreter keepsArray.get's body.bend2/bend.tsis untouched.The name and the safety contract are proposals for the maintainers to settle; if you want a different spelling, I will change it. The PR also has
tests/run/array_get_unchecked.bendand a local dot-loop check (0.19-0.21 s masked against 0.12-0.16 s unchecked here, same output).Whenever you have the machine, the tables you offered would be the useful next step. It should be a drop-in for the tensor-fork and tensor-gpu copies; tensor-array itself stays on the checked read until the array carries its capacity.
nuxyel commentedon Oct 29, 2026
Thanks for #1880. Here are the tables I offered, on main 4f99f4c built from source, against 2.0.39 and each loop's C version: one thread pinned to a P-core of a Core Ultra 7 155H, user-mode
perf statcounts, clang 23.1.3. Every output has the same bits as its C version, and the instruction counts repeat to within a thousand over ten runs.One thing about the contract first. The dot loops from my earlier comment read the next number at every step, so the last step reads one past the end of each array. When the array is a power of two long, that index is the capacity:
Array.getwraps it, andArray.get_uncheckedmust not be given it. So the unchecked version runs k - 1 reading steps and adds the last product in the0ncase (the same sums in the same order):called with
65535n. tensor-array's scalar dot gets the same change; the tiled product's loop keeps its shape, because its extra read stays inside the packed array's capacity.Bands)Array.getArray.getArray.get_uncheckedBandsmatvec. I wrote that the mask was most of the distance there, because C with opaque masks went from 3,258 M to 9,902 M. Taking the mask out of Bend saves less than that: 0.8 G against the loop as published, 2.0 G against the same loop shape that stops before the end (11,835 M checked). What stays is the loop itself: clang unrolls it by two with register copies for the four rotating sums and zero-extends each U32 index before its load, about 11 instructions a multiply-add against C's 4. So on that loop the mask is a smaller part than I said. In time it goes from 2.2x C to 1.9x (536 to 447 ms).On the RTX 4050, the matmul from this issue, ms a product (5 alternated rounds, idle machine, 50 products a Bend run, 100 a CUDA run; every Bend run gives the same sum):
Array.getArray.get_uncheckedOn the GPU the unchecked read changes nothing (4x4 is 2.6% slower, 8x4 1.2% faster, both within a round's spread), so whatever the kernel waits on there, it isn't the mask.
I'll move my parallel copies to it once it is in a release, and post what changes in their largest users.
adeerkhan commentedon Oct 29, 2026
The contract correction is the important part: the dot loops read one past the end at the last step, and when the array is a power of two that index is the capacity, where
Array.getwraps andget_uncheckedmust not be given it. So k - 1 unchecked reads plus the final product in the0ncase is the right shape — same sums, same order, no read past the end.That shape is exactly what our hot loops have too (
dot0/sdotin tensor-array step the index by one to the last element and read ati + 1), so ifget_uncheckedlands the same k - 1 rewrite applies there, and we would rather take it than keep the mask on the two functions that dominate a step.The position that follows from your numbers:
Array.getmasked by default,get_uncheckedopt-in at exactly the loops where the index is proved in range — the instruction counts you posted are the argument for making the opt-in possible, not for weakening the default.nuxyel commentedon Nov 8, 2026
As I said on 10-29, here is what
Array.get_uncheckedchanged in my two parallel copies, now that it's in 2.0.40.tensor-fork (the CPU products). It now has its own copies of tensor-array's cell loops, with
get_uncheckedin the inner reads. tensor-array itself keeps the checked reads, since it carries laws and no@unsafe. The additions are the same and in the same order, and the bits are the same on 1 and 16 threads and on the JS lane. These are the three products of MetalBend's gemm-fork workload (nt 256x576x1536, tn 1536x256x576, nn 256x1536x576, each accumulated twice), counted as user-mode instructions with one thread pinned to a P-core, on Bend 2.0.40:Depth 0 gains more partly because it now also skips tensor-array's own product path. The machine was busy when I ran these, so I'm not claiming a time. It's published as bend-ml-tensor-fork 0.1.7.0.
About the k - 1 rewrite suggested above for these loops, two shapes ran more instructions here:
(u32)i << 3, and clang can't show that adding 0..7 to it never wraps, so the loads stay scalar; the mask on the first read is what gives it that bound. Those first reads stay checked, once per cell.tensor-gpu (the products under
!). I tried reading the interior tiles unchecked. Edge tiles and the last turn past k stayed checked, since they read past the caller's arrays by design. The bits were the same, but it didn't pay. On the RTX 4050, a 512x576x1536 product'sbend_devmedian was 9.55 ms checked against 9.34 ms unchecked, with ranges of 9.20-10.24 and 8.69-11.54 over 5 alternated rounds, so within noise. On the CPU it ran 1.4% more instructions, because the extra functions cost more than the masks saved. I dropped it. That matches the matmul table above: on the GPU the mask isn't what the kernel is waiting on.So on the CPU the opt-in pays where the loop has the right shape, and on the GPU it doesn't change the gap this issue is about. The full entries are in bend-ml's NOTES.md (2026-11-06 and 2026-11-07).
adeerkhan commentedon Nov 8, 2026
Thank you — this is the part I could not do from here (no Metal, and no way to count instructions), and it settles
both halves of the question the issue opened with:
instructions, with the bits unchanged, is the result I would have quoted; that depth 0 gains more because it also
skips tensor-array's own product path is worth knowing.
those ranges, is within noise, and the 1.4% more instructions on the CPU is the useful negative: the masks are not
what the kernel waits on. That matches what the tiling bench saw, and it means the 4x is not a bounds-check story.
The two rewrites you tried are the interesting part of the report: the loop that stops one read early going to 16,633 M
because clang keeps the loads scalar, and the all-unchecked version at 9,196 M because
(u32)i << 3plus 0..7 is notprovably non-wrapping. "The mask on the first read is what gives it that bound" is the sentence I will remember — it
turns an opt-in into a shape rule rather than a switch.
I am leaving the issue open, because the 4x itself is still open and this narrows it: the next measurement is the
accumulate's shape and the thread count, not the mask.