Skip to content

CUDA: the dispatches' entry check reads the frontier from device memory, not from the managed H - #1877

Merged
nicolas-abril merged 3 commits into
bendlang:mainfrom
nuxyel:cuda-front-device
Oct 30, 2026
Merged

nicolas-abril merged 3 commits into
bendlang:mainfrom
nuxyel:cuda-front-device

Conversation

@nuxyel

@nuxyel nuxyel commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1854.

#1447's entry check, if (!turn_groups(H[H_FRONT], at)) return;, makes every block of every CUDA dispatch wait on a load from the managed allocation, and that costs ~3.5% on programs made of many small bangs (#1854). A load from plain device memory costs nothing there, so this keeps a copy of the frontier in a __device__ u64 bend_front:

  • turn_set writes it on the device too (pass 2, behind BEND_RTC, so Metal and the host are unchanged);
  • the host writes it with one 8-byte cuMemcpyHtoD at the start of gpu_pass, after its own turn_set, and fails loudly if that copy fails;
  • gpu_load takes its address with cuModuleGetGlobal (checked against CUDA_SUCCESS);
  • the entry check reads it instead of H[H_FRONT].

Every round keeps the check, the chained rounds included, the kernel keeps its three arguments, and Metal's code is unchanged.

RTX 4050 Laptop, driver 615.78.08, toolkit 12.9, alternated runs on an idle machine, the same output on both (the first two rows timed again on the rebased commit, 5 rounds; the benches before the rebase):

main this PR
#1854's reproduction (10,000 one-round bangs, median of 5) 13.58 s (13.52-13.72) 13.05 s (13.04-13.06)
a two-layer Llama, 200 AdamW steps on the GPU (median of 5) 16.71 s (16.66-16.79) 16.21 s (16.19-16.27)
the 17 runtime benches on the GPU, +RTS --gpu (median of 3 each) geomean 1.00x (0.99-1.03x)

After the rebase on 4f99f4c, the tests with a bang and a main (45 files), built with both compilers and run on the GPU: 43 print their #| lines on both; the other two, tests/printer/offload_bang_g and _h, refuse to build on both, as they should.

bend2/comp.ts is at 66,059 ttok against the 66,000 cap (main is at 65,956), so the PR adds 103. I kept the change as small as I could (13 lines added, 2 changed) rather than trim unrelated code to fit. I have no Metal machine; the Metal path doesn't change, since every addition is behind BEND_RTC or in the CUDA host section.

@nicolas-abril nicolas-abril left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is the shape we were hoping for. Every round keeps the check, the kernel keeps its three arguments, and Metal is unchanged. I've posted on #1854 what I think the mechanism is (the check makes the header page's fault wait on its own, ahead of the kernel's other faults), with three experiments that would confirm it. If it holds, reading the frontier from device memory deals with the cause, for every round.

We have no CUDA machine, so we can't check the timings ourselves. On Metal I built this branch and ran tests/comptime/offload.bend and offload_def.bend with +RTS --gpu on; both print 4.

A few things before merging:

  1. The cuMemcpyHtoD result isn't checked. Every other CUDA call on this path fails loudly. If this copy fails, the check reads a stale frontier and a dispatch can be skipped without any error.
  2. cuModuleGetGlobal(...) is used as a bare truth value. The line above it compares with != CUDA_SUCCESS; this one should too.
  3. #define FRONT bend_front = hides an assignment. H[H_FRONT] = FRONT f; reads as one store but does two. A plain #ifdef BEND_RTC / bend_front = f; / #endif inside turn_set, with the __device__ u64 bend_front; declaration next to the other CUDA globals, would say what it does.

About the size cap: we'll handle comp.ts's cap separately, so don't trim unrelated code for it here. A quick simplification pass over these lines would still help, and point 3 may well come out shorter than the macro.

@nuxyel
nuxyel force-pushed the cuda-front-device branch from 6eb1d9e to 169d9c8 Compare October 29, 2026 03:37
@nuxyel

nuxyel commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I rebased the branch on current main (4f99f4c), made the three changes and squashed them into the one commit:

  1. gpu_pass checks what cuMemcpyHtoD returns and fails with "device write failed", like the launch and the sync next to it.
  2. gpu_load compares cuModuleGetGlobal(...) with != CUDA_SUCCESS.
  3. The FRONT macro is gone: turn_set writes bend_front = f; inside a plain #ifdef BEND_RTC. The __device__ u64 bend_front; declaration is now in the Globals section, as an #elif BEND_RTC right after the host-only #if !DEVICE block that holds gpu_dev, gpu_lib and gpu_pso (it can't go inside that block, which the device compile skips).

comp.ts is at 66,059 ttok (ttok 0.3, -m gpt-3.5-turbo, as the repo gate counts it); main is at 65,956, so the PR adds 103. The #ifdef came out the same size as the macro; the two error checks add 22. I didn't touch anything else.

On the RTX 4050 (driver 615.78.08, toolkit 12.9): offload.bend and offload_def.bend print 4. Of the 45 tests with a bang and a main, built for CUDA and run on the GPU, 43 print their #| lines, the same as on main; tests/printer/offload_bang_g and _h refuse to build on both, with the same error. #1854's reproduction prints 1.5 and the training loop gives the same output bits as main. I haven't timed the rebased branch yet; the numbers in the description are from before the rebase.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks, this addresses everything from my review, and the timings on #1854 support the theory.

One more experiment before we merge, if you have time. Your table shows #1877 is still about 9 µs a bang slower than no check at all. One possible cause is that gpu_pass copies straight from &H[H_FRONT], which is in managed memory. With unified addressing the driver can see that the source is managed, so it may treat this 8-byte copy as a copy between managed memory and the device rather than a plain host-to-device write. The host reads H[H_FRONT] in the loop condition anyway, so copying from a local costs nothing extra. It's a two-line change:

// A pass launched past an empty frontier returns at once.
static void gpu_pass(u64* H) {
  u64 front = H[H_FRONT];
  if (cuMemcpyHtoD(gpu_front, &front, 8) != CUDA_SUCCESS) {
    err_fail("device write failed");
  }
  for (u32 n = 1; H[H_FRONT] != 0; n = n < 32 ? 2 * n : 64) {

The rest of the function stays the same.

A second variant also makes the copy asynchronous on the default stream, so the host doesn't wait for it before queueing the first launch:

  if (cuMemcpyHtoDAsync(gpu_front, &front, 8, NULL) != CUDA_SUCCESS) {

This is safe with a stack variable. For pageable host memory, cuMemcpyHtoDAsync returns only after the bytes have been staged. The launches in gpu_run also use the NULL stream, so they still run after the copy.

Could you time both variants the same way as the table on #1854 (per bang vs main, plus the training loop)?

build per bang vs main training loop
#1877 −53 µs 0.970x
#1877 + copy from a local ? ?
#1877 + local + cuMemcpyHtoDAsync ? ?
no check at all −61 µs 0.963x

If neither one is measurably faster, I'm happy to merge #1877 as it is. I couldn't run this myself because I don't have a CUDA machine, so it's untested. Also, comp.ts is at 66,059 tokens against the 66,000 cap, so the repo gate fails at the moment.

@nuxyel

nuxyel commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

I timed both variants on the PR's commit (169d9c8, on main 4f99f4c), the same way as the table on #1854: RTX 4050 Laptop, idle machine on AC power, 5 alternated rounds (even rounds in reverse order). The variants change one line each in gpu_pass:

  • (a) u64 front = H[H_FRONT]; and cuMemcpyHtoD(gpu_front, &front, 8);
  • (b) the same with cuMemcpyHtoDAsync(gpu_front, &front, 8, NULL).

Both pass the bang tests on CUDA like the PR (43 of the 45 tests with a bang and a main print their #| lines; the two printer tests refuse to build, as on main). Every run printed 1.5 on the reproduction and the training loop gave the same bits.

build reproduction, 10,000 bangs (s) per bang vs main training loop, 200 steps (s)
main 13.52-13.54 (median 13.54) 16.64-16.80 (16.66)
#1877 13.04-13.08 (13.04) -50 µs 16.13-16.22 (16.20), 0.972x
(a) copy from a local 13.04-13.06 (13.05) -49 µs 16.12-16.17 (16.14), 0.969x
(b) local + cuMemcpyHtoDAsync 13.04-13.06 (13.04) -50 µs 16.16-16.29 (16.19), 0.972x
no check 12.96-12.99 (12.97) -57 µs 16.02-16.20 (16.05), 0.963x

Neither is faster than the spread between rounds of the same build (about 4 µs a bang on the reproduction), so the copy's source isn't where the rest goes, and I left the PR as it is. The gap to no check came out at about 7 µs a bang this time, against 9 in the #1854 run.

Thanks for taking the size cap separately.

nicolas-abril and others added 2 commits October 30, 2026 15:45
Renames bend_front to dev_front and declares it in the existing BEND_RTC
block, so the PR no longer needs an #elif of its own. dev_front keeps the
frontier off the managed H, so the entry check doesn't wait on a page
fault (bendlang#1854).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril
nicolas-abril merged commit 0a2f889 into bendlang:main Oct 30, 2026
1 check passed
@nuxyel
nuxyel deleted the cuda-front-device branch October 31, 2026 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CUDA: #1447's entry check in bend_dev makes a small training loop ~3% slower (bisected)

2 participants