From d22dcda1cf439ca42f01a76ddb7c96794dd072ef Mon Sep 17 00:00:00 2001 From: Windyskr <54269955+Windyskr@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:13:35 +0800 Subject: [PATCH] perf: cache the device type in sd_backend_is_cpu (#2307) --- src/core/ggml_extend_backend.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/core/ggml_extend_backend.cpp b/src/core/ggml_extend_backend.cpp index 47fe80ff..70f2ae2e 100644 --- a/src/core/ggml_extend_backend.cpp +++ b/src/core/ggml_extend_backend.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -447,7 +448,19 @@ bool sd_backend_is_cpu(ggml_backend_t backend) { return false; } auto dev = ggml_backend_get_device(backend); - return dev != nullptr && ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU; + if (dev == nullptr) { + return false; + } + // ggml_backend_dev_type() can query the driver on every call (CUDA reads all device + // properties), and this check runs inside per-tensor residency loops. + static std::mutex mutex; + static std::unordered_map is_cpu_by_device; + std::lock_guard lock(mutex); + auto it = is_cpu_by_device.find(dev); + if (it == is_cpu_by_device.end()) { + it = is_cpu_by_device.emplace(dev, ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU).first; + } + return it->second; } static int cuda_compiled_arch(ggml_backend_t backend) {