From 8c0b9cd04ac76d75bcf7b3da398fb00de03489ba Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 2 Sep 2026 20:09:46 +0200 Subject: [PATCH] metal : fix memory query under low-memory conditions (#27701) * metal: Fix memory query under low-memory conditions * Simply variable name Co-authored-by: Georgi Gerganov * Write it even shorter Co-authored-by: Niklas Wenzel --------- Co-authored-by: Georgi Gerganov Co-authored-by: Niklas Wenzel --- ggml/src/ggml-metal/ggml-metal-device.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ggml/src/ggml-metal/ggml-metal-device.m b/ggml/src/ggml-metal/ggml-metal-device.m index ef81084241..e20a4e8916 100644 --- a/ggml/src/ggml-metal/ggml-metal-device.m +++ b/ggml/src/ggml-metal/ggml-metal-device.m @@ -1471,8 +1471,10 @@ void ggml_metal_device_event_synchronize(ggml_metal_device_t dev, ggml_metal_eve void ggml_metal_device_get_memory(ggml_metal_device_t dev, size_t * free, size_t * total) { if (@available(macOS 10.12, iOS 16.0, *)) { - *total = dev->mtl_device.recommendedMaxWorkingSetSize; - *free = *total - dev->mtl_device.currentAllocatedSize; + *total = dev->mtl_device.recommendedMaxWorkingSetSize; + size_t cur = dev->mtl_device.currentAllocatedSize; + // it's possible to allocate more than `recommendedMaxWorkingSetSize` + *free = *total > cur ? *total - cur : 0; } else { *free = 0; *total = 0;