Skip to content

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

Closed

Description

@nuxyel

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.

Activity

  1. nuxyel commented on Oct 26, 2026

    @nuxyel
    ContributorAuthor

    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).

  2. nicolas-abril commented on Oct 26, 2026

    @nicolas-abril
    Collaborator

    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.

  3. nuxyel commented on Oct 27, 2026

    @nuxyel
    ContributorAuthor

    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.

    import Base
    import bend-ml-tensor-array@0.1.16.0/main.bend as TA
    import bend-ml-tensor-gpu@0.1.4.0/main.bend as TG
    
    def snd(r: Array<F32> & F32) -> F32:
      match r:
        case Tuple{a, +x}: x
    
    # x <- x · wᵀ, k times: one small product, so one bang of one round, per step
    def go(k: Nat, m: TA.MMulNT<32n, 32n, 32n>) -> F32:
      match k m:
        case 0n TA.MMulNT{a, b, TA.Mat{d}}: snd(Array.get(F32, d, 31))
        case 1n+p TA.MMulNT{a, b, c}: go(p, TG.matmul_nt(32n, 32n, 32n, 0n, c, b))
    
    def main() -> IO(Unit):
      do IO<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.

  4. nicolas-abril commented on Oct 28, 2026

    @nicolas-abril
    Collaborator

    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:

    1. 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.
    2. 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.
    3. 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.

  5. nuxyel commented on Oct 29, 2026

    @nuxyel
    ContributorAuthor

    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.

    1. 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.
    2. 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.
    3. 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):
    build reproduction: groups/bang (3 runs) header-page groups/bang training loop: groups/bang (2 runs) header-page groups/bang
    main 53.0-58.1 1.00-1.04 55.3-58.2 3.13-3.41
    #1877 49.9-53.5 1.00-1.02 55.3-55.5 1.03-1.06
    1. managed word 50.4-58.4 1.00 54.3-55.0 1.01-1.02
    no check 52.8-60.0 1.00-1.06 54.7-56.0 1.02-1.03
    2. no check + prefetch 50.9-56.9 0 53.7-53.9 0

    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:

    build reproduction, 10,000 bangs (s) per bang vs main training loop, 200 steps (s)
    main 13.52-13.72 (median 13.58) 16.66-16.79 (median 16.71)
    #1877 13.04-13.06 (13.05) -53 µs 16.19-16.27 (16.21), 0.970x
    1. managed word 13.05-13.08 (13.05) -52 µs 16.14-16.21 (16.18), 0.968x
    no check 12.94-12.99 (12.96) -61 µs 16.03-16.13 (16.10), 0.963x
    2. no check + prefetch 13.51-13.54 (13.52) -5 µs 16.66-16.80 (16.72), 1.001x

    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.

  6. added a commit that references this issue on Oct 30, 2026
    0a2f889
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions