Skip to content

cuda: fix crash when querying memory on device with no free memory. - #25157

Merged
JohannesGaessler merged 1 commit into
ggml-org:masterfrom
cphlipot:nonfatal_cuda_mem_check
Jul 11, 2026
Merged

JohannesGaessler merged 1 commit into
ggml-org:masterfrom
cphlipot:nonfatal_cuda_mem_check

Conversation

@cphlipot

@cphlipot cphlipot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Make the check that queries available CUDA memory not fatally crash
llama-server if the CUDA device is low on available RAM. instead just
report no ram so llama-server doesn't try to load a model to it.

Additional information

If a Cuda device has no or limited available memory, the actual call
to cudaMemGetInfo() itself can cause a fatal crash due to a cuda out
of memory error (there is not enough memory to actually query memory)

This causes an issue because we query memory for all devices at
startup even if the user isn't trying to use the device for inference.

Fix this by making the error non-fatal and assigning zero total/free
memory to the device. This will have the downstream effect of the fit
algorithm not trying to put any layers on it, which is desired outcome
vs hard crashing.

this also prevents crashes in cuda enabled builds when user explicitly
passes '-dev none'

Caveat: because mmporj does not allow user to select a device and may
still internally decide to use a cuda device, the user may still need
--no-mmproj-offload or --no-mmproj to fully prevent all potentially
fatal cuda allocations. A fix for this is deliberately excluded from
this PR as it is a separate issue.

Partially fixes #25145 (note caveat above for part of that issues that is not fixed)

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes - evalate impact of this and other proposed fixs on the rest of the application logic.

If a Cuda device has no or limited available memory, the actual call
to cudaMemGetInfo() itself can cause a fatal crash due to a cuda out
of memory error (there is not enough memory to actually query memory)

This causes an issue because we query memory for all devices at
startup even if the user isn't trying to use the device for inference.

Fix this by making the error non-fatal and assigning zero total/free
memory to the device. This will have the downstream effect of the fit
algorithm not trying to put any layers on it, which is desired outcome
vs hard crashing.

this also prevents crashes in cuda enabled builds when user explicitly
passes '-dev none'
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Jun 30, 2026
@cphlipot
cphlipot marked this pull request as ready for review June 30, 2026 03:46
@cphlipot
cphlipot requested a review from a team as a code owner June 30, 2026 03:46
@cphlipot cphlipot changed the title cuda: Don't crash when querying memory on device with no free memory. cuda: fix crash when querying memory on device with no free memory. Jun 30, 2026

@JohannesGaessler JohannesGaessler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this would correctly handle the edge case where there is insufficient free memory for the cudaMemGetInfo call. I'm not sure whether this PR is potentially masking another issue but with a warning I think it should be fine.

@ORippler

Copy link
Copy Markdown
Collaborator

This causes an issue because we query memory for all devices at
startup even if the user isn't trying to use the device for inference.

Not sure if llama-server exposes a way to select the devices that should be used for inference, but setting CUDA_VISIBLE_DEVICES is the way to mask this for CUDA.

E.g., launch CUDA_VISIBLE_DEVICES=0,2 llama-server to exclude device 1 from inference

@ORippler

Copy link
Copy Markdown
Collaborator

CC @0cc4m, I thought we used a subprocess to query available memory (in that case, we should not crash host llama-server when querying memory in the CUDA subproc)

@JohannesGaessler

Copy link
Copy Markdown
Contributor

Regardless of whether or not a subprocess can be used to retrieve the memory safely, a program trying to fetch memory info via the ggml C API should not experience a crash if the memory is already full.

@0cc4m

0cc4m commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

CC @0cc4m, I thought we used a subprocess to query available memory (in that case, we should not crash host llama-server when querying memory in the CUDA subproc)

My server memory awareness PR (#21231) is currently waiting for @ngxson to finish #24822. I hope we can get it moving again afterwards, it has been stuck for a while.

@ORippler ORippler 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.

Regardless of whether or not a subprocess can be used to retrieve the memory safely, a program trying to fetch memory info via the ggml C API should not experience a crash if the memory is already full.

That I agree

@ORippler

Copy link
Copy Markdown
Collaborator

My server memory awareness PR (#21231) is currently waiting for @ngxson to finish #24822.

Hmm, but it still shouldn't query CUDA memory if -dev none is given, or should it? 🤔

@0cc4m

0cc4m commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

I assume cphlipot is talking about the autofit feature, both CUDA_VISIBLE_DEVICES and -dev should be able to limit which devices get queried.

@cphlipot

Copy link
Copy Markdown
Contributor Author

Hmm, but it still shouldn't query CUDA memory if -dev none is given, or should it?

@ORippler I think the way the code is written right now -dev isn't even parsed/evaluated until after we query memory. Memory query in llama-server is actually done at the same time we enumerate devices.

I agree this would be nice behavior to have but i still think the existing PR makes sense to implement for a few reasons:

  • it makes the cuda memory query failure mode operate similarly to Vulkan (non-fatal and report 0 free/total)
  • prevents a ./build/bin/llama-server --list-devices fatal crash and i'm not clear that -dev is actually meant to filter that list. Note that --list-devices also prints free/total memory,
  • Current PR is very simple, any rework needed to use -dev to filter which devices we query would likely require us to move the memory querying outside the device enumeration, which still wouldn't fix all crashes (eg. autofit, --list-devices)

I assume cphlipot is talking about the autofit feature, both CUDA_VISIBLE_DEVICES and -dev should be able to limit which devices get queried.

@0cc4m This is supposed to address crashes encountered in multiple cases: autofit, -dev none, and --list-devices. CUDA_VISIBLE_DEVICES does work to limit which devices get queried, and i have been using this as a workaround, but -dev only impacts which devices we use for inference, not which devices we enumerate and query memory from, so you'd still get a crash even with -dev filtering.

@ORippler

ORippler commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

i still think the existing PR makes sense to implement for a few reasons

+1, hence my approval of this PR.

-dev only impacts which devices we use for inference, not which devices we enumerate and query memory from, so you'd still get a crash even with -dev filtering.

I only wanted to point out that this is a potential area of improvement for llama-server (or at least I was surprised to learn we query devices excluded via -dev filtering)

@JohannesGaessler

Copy link
Copy Markdown
Contributor

I will merge unless someone objects.

@JohannesGaessler
JohannesGaessler merged commit 3cec3bc into ggml-org:master Jul 11, 2026
21 of 22 checks passed
@cphlipot
cphlipot deleted the nonfatal_cuda_mem_check branch July 14, 2026 05:08
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
…ggml-org#25157)

If a Cuda device has no or limited available memory, the actual call
to cudaMemGetInfo() itself can cause a fatal crash due to a cuda out
of memory error (there is not enough memory to actually query memory)

This causes an issue because we query memory for all devices at
startup even if the user isn't trying to use the device for inference.

Fix this by making the error non-fatal and assigning zero total/free
memory to the device. This will have the downstream effect of the fit
algorithm not trying to put any layers on it, which is desired outcome
vs hard crashing.

this also prevents crashes in cuda enabled builds when user explicitly
passes '-dev none'
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
…ggml-org#25157)

If a Cuda device has no or limited available memory, the actual call
to cudaMemGetInfo() itself can cause a fatal crash due to a cuda out
of memory error (there is not enough memory to actually query memory)

This causes an issue because we query memory for all devices at
startup even if the user isn't trying to use the device for inference.

Fix this by making the error non-fatal and assigning zero total/free
memory to the device. This will have the downstream effect of the fit
algorithm not trying to put any layers on it, which is desired outcome
vs hard crashing.

this also prevents crashes in cuda enabled builds when user explicitly
passes '-dev none'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: CUDA OOM error when trying to load model entirely on CPU with "-dev none"

4 participants