You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CUDA: #1447's entry check in bend_dev makes a small training loop ~3% slower (bisected) #1854
Since 2.0.38 a GPU training loop of mine is about 3% slower on CUDA. I bisected it to a97a7c4 (#1447, "GPU bangs chain their rounds on the device"). #1447 notes that its CUDA path wasn't compiled or tested, so here is what it does on a CUDA machine.
The program is a two-layer Llama trained for 200 AdamW steps, with its linear layers on the GPU (bend-ml-tensor-gpu): many small bangs per step, each one round long. RTX 4050 Laptop, driver 615.78.08, CUDA 12.9, Linux.
2.0.37: 16.29 s
2.0.38 and 2.0.39: 16.71 s
bisection (each commit's bend2/main.ts run with bun): good commits 16.28-16.32 s, bad ones 16.82-16.93 s, first bad a97a7c4.
To find the part that costs, I edited 2.0.38's emitted C and built it with the clang line bend -o uses (3 rounds each):
build
train 200
2.0.37
16.24-16.46 s
2.0.38 as emitted
16.74-16.94 s
cuCtxSynchronize after every round, instead of after 1, 2, 4 .. 64
16.77-16.92 s
... and the host launching only the dispatches turn_groups(H[H_FRONT], at) allows, after that sync
16.76-16.82 s
... and without the entry check if (!turn_groups(H[H_FRONT], at)) return;
16.22-16.26 s
So the rounds queued past the end of the work and the empty dispatches don't cost anything measurable. The entry check does, even in dispatches that have work. The kernel uses 255 registers with and without it, so it isn't occupancy. I don't know the mechanism. Under nsys the kernels take the same total time with and without the check, and the host's CPU time is the same too, so the gap shows only in an untraced run. Unified-memory page faults are the same in both.
I have a fix that keeps #1447's chaining (the first round after each sync picked on the host, without the check) and brings the time back; I'll open it as a PR and link it here.
The fix is in #1855: the first round after each sync launches only the dispatches the frontier the host read needs, without the check, and the rounds chained after it keep it. On this loop main takes 16.72 s and the PR 16.27 s; the 17 runtime benches on the GPU are unchanged (geometric mean 1.00x).
Thanks for the bisection. I closed #1855 because it removes the check only where the host can stand in for it, and we'd like to find out why the check costs time in the first place (more detail in my comment there).
To do that we need to run your case. Could you share a minimal file that reproduces the slowdown? If that's hard to cut down, the full Bend program would also help: the training loop and the version of bend-ml-tensor-gpu it uses. Please include:
the exact command you time (bend <file> -o ... and the run flags, e.g. +RTS --gpu ...)
what it prints, so we can check that output matches across versions
your bend --version, plus the GPU, driver and CUDA versions you listed above if they've changed
A smaller program that shows the same 2.0.37 → 2.0.38 gap would be best, but anything we can run is a good start.
Here is a reproduction that uses only public packages: 10,000 chained 32 x 32 products, each one bang of one round, with bend-ml-tensor-gpu from BendHub.
importBaseimportbend-ml-tensor-array@0.1.16.0/main.bendasTAimportbend-ml-tensor-gpu@0.1.4.0/main.bendasTGdefsnd(r: Array<F32>&F32) ->F32:
matchr:
caseTuple{a, +x}: x# x <- x · wᵀ, k times: one small product, so one bang of one round, per stepdefgo(k: Nat, m: TA.MMulNT<32n, 32n, 32n>) ->F32:
matchkm:
case0nTA.MMulNT{a, b, TA.Mat{d}}: snd(Array.get(F32, d, 31))
case1n+pTA.MMulNT{a, b, c}: go(p, TG.matmul_nt(32n, 32n, 32n, 0n, c, b))
defmain() ->IO(Unit):
doIO<Unit>:
IO.print(F32.show(go(10000n, TG.matmul_nt(32n, 32n, 32n, 0n, TA.Mat.fill(32n, 32n, 1.5), TA.Mat.fill(32n, 32n, 0.03125)))))
CUDA_HOME=<toolkit 12.9> bend chain.bend -o chain, then ./chain with no runtime flags (the bangs run on the GPU). It prints 1.5 on every version. RTX 4050 Laptop, driver 615.78.08 (it reports CUDA 13.4; the toolkit I build with is 12.9), clang 23.1.3, Linux. Five alternated runs:
release
s
2.0.37
13.07-13.22
2.0.38
13.53-13.56
2.0.39
13.53-13.57
That is the same ~3.5% as the training loop, about 48 µs a bang.
I tried your three experiments on 2.0.39's emitted C, built with bend -o's clang line, along with a few probes:
variant
s
as emitted
13.53-13.58
without the entry check
12.98-13.02
1. the host reads the frontier with cuMemcpyDtoH after each sync
13.53-13.54
2. only lane 0 loads the frontier and passes it on through __shared__
13.53-13.55
3. the check reads H_GROUPS[at * 3]
13.53-13.55
a check that loads nothing (if (at > 4) return;)
12.99-13.01
a check on a word of H far from the header (H[ALC_OFF])
13.53-13.57
the header page prefetched to the device after the host's turn_set
13.57-13.58
a check on a plain __device__ variable
12.99-13.01
None of the three recovers the time. The branch itself costs nothing, and neither does a load from plain device memory. A load from H costs the same anywhere in the allocation, and prefetching the header doesn't help, so it isn't the header page migrating. What costs is gating the dispatch on a load from the managed allocation at kernel entry. I don't know why the driver makes that slow, and nsys didn't show it either.
So the fix I tried keeps a copy of the frontier in device memory: a __device__ u64 bend_front, written by turn_set on the device (pass 2) and by the host with one 8-byte cuMemcpyHtoD at the start of each bang. The entry check reads it, so every round keeps the check, the chained ones included, and the kernel keeps its three arguments. The reproduction takes 13.07 s with it, against 13.55 s on main. The training loop (200 AdamW steps, five alternated runs, the same output bits) takes 16.20 s, against 16.72 s as emitted and 16.24 s on 2.0.37. I'm opening it as a PR.
Thanks for the reproduction and the variants, they narrow it down a lot. We have no CUDA machine, so what follows is a hypothesis that fits every number you posted, not something we've measured.
Hypothesis: the entry check makes one page fault wait on its own.H is managed memory with its preferred location on the GPU. Between bangs the host writes to it: turn_set writes the header, H_CURSOR is exchanged, and tasks and allocation state go into the rings and at ALC_OFF. Those pages move to the CPU, so the first round after each sync takes GPU page faults with or without the check. That fits "the same page faults in both".
What the check changes is when they're handled. turn_groups(H[H_FRONT], at) is the first thing every block does, and nothing else can start until that load returns. So the header page's fault gets handled alone, and only then do the kernel's other faults go out together. Without the check, the header fault would go in the same batch as the rest. That's one extra fault wait per bang, which would fit your ~48 µs.
How it fits your table:
variant
cost
why
no check, if (at > 4) return
free
no load to wait on
check on a __device__ variable
free
device memory never faults
check on H[ALC_OFF]
slow
the host writes that page too
H_GROUPS[at * 3], lane 0 + __shared__
slow
same page, still waited on alone
host reads with cuMemcpyDtoH
slow
the host's turn_set still writes the header page
header prefetched after turn_set
slow
the prefetch is the same wait, moved earlier in the stream
nsys
no gap
its memory tracing changes how faults are handled
If it's right, the rounds chained on the device don't pay for the check, because their pages are already on the GPU. My objection to #1855 on that point was probably wrong: it worked because it dropped the check exactly on the round that faults.
If you have time, three experiments on the RTX 4050 would confirm or rule it out:
A managed page the host never touches. Gate the entry on a word in a separate small cuMemAllocManaged buffer that only the device writes. This should be free. If it's slow, the hypothesis is wrong.
A prefetch with no check. On a build without the entry check, cuMemPrefetchAsync the header page to the device right before the launches. This should get ~48 µs per bang slower.
nsys with --cuda-um-gpu-page-faults=true. Compare the number of GPU fault groups per bang, not the fault count. There should be one more with the check.
Either way, #1877 removes the cost for every round (I've left a review there). If this turns out to be the mechanism, a later step could keep the control words (cursor, frontier, groups) out of managed memory entirely, so the header page stops moving to the CPU and back on every bang.
I built your three experiments on current main (4f99f4c), plus #1877 rebased on it and a build without the entry check as a control. All five pass the bang tests on CUDA (43 of the 45 tests with a bang and a main print their #| lines; the two printer tests refuse to build on all, as on main), print 1.5 on the reproduction, and give the same output bits on the training loop.
A managed word only the device writes. The entry check reads a word in a separate 2 MiB cuMemAllocManaged buffer, preferred location the GPU. The device's turn_set writes it, and an 8-byte cuMemcpyHtoD puts the host's frontier there at the start of each bang, so the copy engine writes it and the CPU never touches the page. That buffer took one GPU fault group in the whole run.
No check, header prefetched. No entry check, and a cuMemPrefetchAsync of H's first 4 KiB to the device right before each round's launches. It has to come after the host's last read of H[H_FRONT], which the loop condition does: prefetched before that read, the header page still faulted once a bang. Placed after it, the header page never faults.
Fault groups (nsys 2026.3.2, --cuda-um-gpu-page-faults=true, one row of CUDA_UM_GPU_PAGE_FAULT_EVENTS counted as a group):
The totals move by about ±5 between runs of the same build, so they can't show a single extra group. What does change is when the header page faults. With the check, its group is the first of every bang: it starts 0.8 µs after the first launch does, lasts about 25 µs, and the rest of the bang's faults come after it. In every build without a load from H at entry, the header page still faults once a bang, but about 440 µs in, among the others, and each bang's first group lasts about 13 µs. On the training loop, main also takes 3.1-3.4 header-page groups a bang against about 1.0 elsewhere, all in the bang's first launch. Under nsys a bang's groups are handled one after another, so the batching itself doesn't show there.
Timings, idle machine on AC power, 5 alternated rounds (even rounds in reverse order); every run printed 1.5 and the training loop gave the same bits:
So your hypothesis holds up. Prefetching the header page into the GPU just before the launches, with no check at all, costs 56 µs a bang over the build without the check, about the ~48 µs you predicted for one fault wait standing alone, and it lands right back at main's time. The managed word only the device writes costs what #1877 costs, both about 9 µs a bang above no check, so neither is free, but both take back most of the 3.5%. Between the two I'd keep #1877's shape, since it needs no second allocation; your next step (control words out of managed memory) is where the last 9 µs would come from.
Activity
nuxyel commentedon Oct 26, 2026
The fix is in #1855: the first round after each sync launches only the dispatches the frontier the host read needs, without the check, and the rounds chained after it keep it. On this loop main takes 16.72 s and the PR 16.27 s; the 17 runtime benches on the GPU are unchanged (geometric mean 1.00x).
nicolas-abril commentedon Oct 26, 2026
Thanks for the bisection. I closed #1855 because it removes the check only where the host can stand in for it, and we'd like to find out why the check costs time in the first place (more detail in my comment there).
To do that we need to run your case. Could you share a minimal file that reproduces the slowdown? If that's hard to cut down, the full Bend program would also help: the training loop and the version of
bend-ml-tensor-gpuit uses. Please include:bend <file> -o ...and the run flags, e.g.+RTS --gpu ...)bend --version, plus the GPU, driver and CUDA versions you listed above if they've changedA smaller program that shows the same 2.0.37 → 2.0.38 gap would be best, but anything we can run is a good start.
nuxyel commentedon Oct 27, 2026
Here is a reproduction that uses only public packages: 10,000 chained 32 x 32 products, each one bang of one round, with
bend-ml-tensor-gpufrom BendHub.CUDA_HOME=<toolkit 12.9> bend chain.bend -o chain, then./chainwith no runtime flags (the bangs run on the GPU). It prints1.5on every version. RTX 4050 Laptop, driver 615.78.08 (it reports CUDA 13.4; the toolkit I build with is 12.9), clang 23.1.3, Linux. Five alternated runs:That is the same ~3.5% as the training loop, about 48 µs a bang.
I tried your three experiments on 2.0.39's emitted C, built with
bend -o's clang line, along with a few probes:cuMemcpyDtoHafter each sync__shared__H_GROUPS[at * 3]if (at > 4) return;)Hfar from the header (H[ALC_OFF])turn_set__device__variableNone of the three recovers the time. The branch itself costs nothing, and neither does a load from plain device memory. A load from
Hcosts the same anywhere in the allocation, and prefetching the header doesn't help, so it isn't the header page migrating. What costs is gating the dispatch on a load from the managed allocation at kernel entry. I don't know why the driver makes that slow, and nsys didn't show it either.So the fix I tried keeps a copy of the frontier in device memory: a
__device__ u64 bend_front, written byturn_seton the device (pass 2) and by the host with one 8-bytecuMemcpyHtoDat the start of each bang. The entry check reads it, so every round keeps the check, the chained ones included, and the kernel keeps its three arguments. The reproduction takes 13.07 s with it, against 13.55 s on main. The training loop (200 AdamW steps, five alternated runs, the same output bits) takes 16.20 s, against 16.72 s as emitted and 16.24 s on 2.0.37. I'm opening it as a PR.nicolas-abril commentedon Oct 28, 2026
Thanks for the reproduction and the variants, they narrow it down a lot. We have no CUDA machine, so what follows is a hypothesis that fits every number you posted, not something we've measured.
Hypothesis: the entry check makes one page fault wait on its own.
His managed memory with its preferred location on the GPU. Between bangs the host writes to it:turn_setwrites the header,H_CURSORis exchanged, and tasks and allocation state go into the rings and atALC_OFF. Those pages move to the CPU, so the first round after each sync takes GPU page faults with or without the check. That fits "the same page faults in both".What the check changes is when they're handled.
turn_groups(H[H_FRONT], at)is the first thing every block does, and nothing else can start until that load returns. So the header page's fault gets handled alone, and only then do the kernel's other faults go out together. Without the check, the header fault would go in the same batch as the rest. That's one extra fault wait per bang, which would fit your ~48 µs.How it fits your table:
if (at > 4) return__device__variableH[ALC_OFF]H_GROUPS[at * 3], lane 0 +__shared__cuMemcpyDtoHturn_setstill writes the header pageturn_setIf it's right, the rounds chained on the device don't pay for the check, because their pages are already on the GPU. My objection to #1855 on that point was probably wrong: it worked because it dropped the check exactly on the round that faults.
If you have time, three experiments on the RTX 4050 would confirm or rule it out:
cuMemAllocManagedbuffer that only the device writes. This should be free. If it's slow, the hypothesis is wrong.cuMemPrefetchAsyncthe header page to the device right before the launches. This should get ~48 µs per bang slower.--cuda-um-gpu-page-faults=true. Compare the number of GPU fault groups per bang, not the fault count. There should be one more with the check.Either way, #1877 removes the cost for every round (I've left a review there). If this turns out to be the mechanism, a later step could keep the control words (cursor, frontier, groups) out of managed memory entirely, so the header page stops moving to the CPU and back on every bang.
nuxyel commentedon Oct 29, 2026
I built your three experiments on current main (4f99f4c), plus #1877 rebased on it and a build without the entry check as a control. All five pass the bang tests on CUDA (43 of the 45 tests with a bang and a
mainprint their#|lines; the two printer tests refuse to build on all, as on main), print1.5on the reproduction, and give the same output bits on the training loop.cuMemAllocManagedbuffer, preferred location the GPU. The device'sturn_setwrites it, and an 8-bytecuMemcpyHtoDputs the host's frontier there at the start of each bang, so the copy engine writes it and the CPU never touches the page. That buffer took one GPU fault group in the whole run.cuMemPrefetchAsyncof H's first 4 KiB to the device right before each round's launches. It has to come after the host's last read ofH[H_FRONT], which the loop condition does: prefetched before that read, the header page still faulted once a bang. Placed after it, the header page never faults.--cuda-um-gpu-page-faults=true, one row ofCUDA_UM_GPU_PAGE_FAULT_EVENTScounted as a group):The totals move by about ±5 between runs of the same build, so they can't show a single extra group. What does change is when the header page faults. With the check, its group is the first of every bang: it starts 0.8 µs after the first launch does, lasts about 25 µs, and the rest of the bang's faults come after it. In every build without a load from
Hat entry, the header page still faults once a bang, but about 440 µs in, among the others, and each bang's first group lasts about 13 µs. On the training loop, main also takes 3.1-3.4 header-page groups a bang against about 1.0 elsewhere, all in the bang's first launch. Under nsys a bang's groups are handled one after another, so the batching itself doesn't show there.Timings, idle machine on AC power, 5 alternated rounds (even rounds in reverse order); every run printed
1.5and the training loop gave the same bits:So your hypothesis holds up. Prefetching the header page into the GPU just before the launches, with no check at all, costs 56 µs a bang over the build without the check, about the ~48 µs you predicted for one fault wait standing alone, and it lands right back at main's time. The managed word only the device writes costs what #1877 costs, both about 9 µs a bang above no check, so neither is free, but both take back most of the 3.5%. Between the two I'd keep #1877's shape, since it needs no second allocation; your next step (control words out of managed memory) is where the last 9 µs would come from.
comp.ts: fit the device frontier under the ttok cap
CUDA: the dispatches' entry check reads the frontier from device memo…