From 1c92d3ebea813810d5d21da2c362d359f9f60607 Mon Sep 17 00:00:00 2001 From: Shogo Iwano Date: Mon, 30 Nov 2026 18:06:32 +0900 Subject: [PATCH] feat: add sd_device_memory to query device memory --- include/stable-diffusion.h | 4 ++++ src/core/util.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/stable-diffusion.h b/include/stable-diffusion.h index 50c32678a..8c7ad8c76 100644 --- a/include/stable-diffusion.h +++ b/include/stable-diffusion.h @@ -545,6 +545,10 @@ SD_API void disable_imatrix_collection(void); // assignment specs. Returns the number of bytes required, excluding the null // terminator. Passing nullptr or buffer_size 0 only queries the required size. SD_API size_t sd_list_devices(char* buffer, size_t buffer_size); +// Report the free and total memory in bytes of the device at `index` in the +// sd_list_devices order. Returns false if the index is out of range or an +// output pointer is null. +SD_API bool sd_device_memory(size_t index, size_t* free_bytes, size_t* total_bytes); // Register comma-separated RPC endpoints (host:port) before listing devices. // Repeated endpoints are safe; nullptr or an empty string is a no-op. diff --git a/src/core/util.cpp b/src/core/util.cpp index e56dac1bb..22901b76a 100644 --- a/src/core/util.cpp +++ b/src/core/util.cpp @@ -1224,6 +1224,20 @@ std::vector> split_quotation_attention( return result; } +bool sd_device_memory(size_t index, size_t* free_bytes, size_t* total_bytes) { + if (free_bytes == nullptr || total_bytes == nullptr) { + return false; + } + if (ggml_backend_dev_count() == 0) { + ggml_backend_load_all(); + } + if (index >= ggml_backend_dev_count()) { + return false; + } + ggml_backend_dev_memory(ggml_backend_dev_get(index), free_bytes, total_bytes); + return true; +} + size_t sd_list_devices(char* buffer, size_t buffer_size) { if (ggml_backend_dev_count() == 0) { // dynamic-backend builds discover their backend modules at runtime