Skip to content

Metal's refusal of an asked --gpu span names the device's limit - #1047

Merged
nicolas-abril merged 3 commits into
bendlang:mainfrom
MattCozendey:bugfix/gpu-cap
Sep 25, 2026
Merged

nicolas-abril merged 3 commits into
bendlang:mainfrom
MattCozendey:bugfix/gpu-cap

Conversation

@MattCozendey

@MattCozendey MattCozendey commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

On Metal, an explicit --gpu span over maxBufferLength already stops at newBufferWithBytesNoCopy. With this change the error names the device's limit, so the user knows what to ask for:

bend: --gpu 20480MB is over the device's 9093MB

Any other buffer failure keeps the old message, the GPU span is more than the device has.

  • gpu_span, the defaults and corpus_setup are as on main: no span is clamped.
  • The CUDA lane is untouched: an explicit span larger than the card still works, as cuMemAllocManaged pages fault in on demand.
  • GUIDE.md is unchanged: --gpu 4GB means exactly what it says.

This does not fix #942. Its crashing runs are unaffected; the real fixes there are still the launch deadline and the warning for a ! with no parallelism.

What I verified

  • bun gates/repo.ts: PASS 46 / 46.
  • The Metal host (Objective-C) side has not been compiled, and nothing here has run on a GPU. The change uses the same snprintf + err_fail pattern as the rest of comp.ts.

🤖 Generated with Claude Code

@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 for splitting this out. You extracted exactly what I asked for on #1012, so this is a change of mind on my side, not something you missed. After looking at it more closely, I don't think an explicit --gpu size should be clamped with a note. It should fail loudly, as it did before.

Why

  • It doesn't change #942's run. On Apple Silicon both Metal limits are well above 8 GB. On an M4 with 16 GB, recommendedMaxWorkingSetSize is 12124 MB and maxBufferLength is 9093 MB, and a 32 GB Mac is higher still. So --gpu 8GB is still wired whole with this PR. The #942 crash was the command buffer that never completed. The limit on each process doesn't touch that, and it can't stop several processes adding up either.
  • On Metal, it turns a clear error into a silent-ish clamp. Above maxBufferLength, main already stops with bend: the GPU span is more than the device has. I checked --gpu 10GB, 20GB and 100GB on that M4. On Apple Silicon maxBufferLength is the lower of the two limits, so the cap here is effectively that same threshold. The only change is that the error becomes a smaller span plus a stderr line.
  • It clashes with the out-of-memory hint. The GPU out-of-memory error says "run again with a bigger span, as in --gpu 8GB". A user who follows that advice past the cap gets a note and then the same out-of-memory error, which is confusing.
  • It caps CUDA, which was deliberately left uncapped. The CUDA span is one cuMemAllocManaged range whose pages fault in on demand. An explicit span larger than the card works today (slowly), and this PR cuts it to cuDeviceTotalMem. When the single span was introduced (fde5e24), the defaults were chosen per backend by what memory costs there: 2 GB on Metal, the whole card on CUDA. Capping the lanes where memory is cheap "would only turn working programs into failures and buy nothing". That commit also set an explicit span the device can't hold to be refused, not clamped.

What I'd like instead

  • Keep gpu_span, the defaults and corpus_setup as they are on main.
  • On Metal, make the existing refusal name the limit, so the user knows what to ask for. For example: bend: --gpu 20GB is over the device's 9093MB (from maxBufferLength). That is a few lines around newBufferWithBytesNoCopy in gpu_load.
  • Leave the CUDA lane alone.
  • Drop the GUIDE change, since --gpu 4GB would mean exactly what it says again.
  • Please update the title and description so they don't claim to fix part of #942. The runs that crash in #942 are unaffected, and the real fixes there are still the launch deadline and the warning for a ! with no parallelism.

This review was written with AI assistance (Claude Code) and checked by me.

…lang#942)

bendlang#942 found an explicit --gpu 8GB wired whole: only the default went through
gpu_span. gpu_span now answers the device's limit (Metal's recommended
working set under its largest buffer, CUDA's total memory). Both the asked
span and the default stop there, and an asked span over it says so on
stderr. Metal's default stays 2GB. The cores' span is unchanged.

This bounds one process. Several GPU programs running at once can still
sum past the machine's memory.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The span is no longer clamped: gpu_span, the defaults, corpus_setup and
the CUDA lane are as on main, and GUIDE's --gpu 4GB means what it says.
An asked span over maxBufferLength still stops, now with
"bend: --gpu 20480MB is over the device's 9093MB", so the user knows
what to ask for.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@MattCozendey

Copy link
Copy Markdown
Contributor Author

Updated as requested.

@MattCozendey MattCozendey changed the title An asked --gpu span stops at what the device takes, with a note (#942) Metal's refusal of an asked --gpu span names the device's limit Sep 25, 2026
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@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 exactly it. I built it on an M4 (16 GB, maxBufferLength 9093 MB):

  • --gpu on, 2GB, 4GB, 8GB and 8.8GB run and print the right answer.
  • 9GB, 10GB, 20GB and 100GB stop with bend: --gpu <N>MB is over the device's 9093MB and exit 1.
  • bun gates/repo.ts passes 46 / 46.

I pushed one small commit that removes the comment on the refusal, and I'm squash-merging.


This review was written with AI assistance (Claude Code) and checked by me.

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.

A ! call with no available parallelism holds the GPU indefinitely and crashes macOS (WindowServer watchdog)

2 participants