@@ -389,6 +389,7 @@ struct ggml_backend_opencl_context {
389389 ADRENO_GPU_GEN adreno_gen;
390390
391391 cl_int alignment;
392+ size_t global_mem_size;
392393 size_t max_alloc_size;
393394 size_t max_workgroup_size;
394395 bool fp16_support;
@@ -3386,6 +3387,9 @@ static ggml_backend_opencl_context * ggml_cl2_init(ggml_backend_dev_t dev) {
33863387 backend_ctx->alignment = base_align_in_bits / 8u;
33873388 GGML_LOG_INFO("ggml_opencl: mem base addr align: %u\n", backend_ctx->alignment);
33883389
3390+ clGetDeviceInfo(device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(size_t), &backend_ctx->global_mem_size, NULL);
3391+ GGML_LOG_INFO("ggml_opencl: global mem size: %zu MB\n", backend_ctx->global_mem_size/1024/1024);
3392+
33893393 clGetDeviceInfo(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(size_t), &backend_ctx->max_alloc_size, NULL);
33903394 GGML_LOG_INFO("ggml_opencl: max mem alloc size: %zu MB\n", backend_ctx->max_alloc_size/1024/1024);
33913395
@@ -6356,11 +6360,16 @@ static const char * ggml_backend_opencl_device_get_description(ggml_backend_dev_
63566360}
63576361
63586362static void ggml_backend_opencl_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
6359- // no memory to report
6360- *free = 0;
6361- *total = 0;
6363+ ggml_backend_opencl_device_context * dev_ctx = (ggml_backend_opencl_device_context *) dev->context;
6364+ ggml_backend_opencl_context * backend_ctx = (ggml_backend_opencl_context *) dev_ctx->backend_ctx;
63626365
6363- GGML_UNUSED(dev);
6366+ static const size_t opencl_extra_margin = 1024ull*1024ull*1024ull;
6367+
6368+ // OpenCL does not provide reliable currently-free device memory.
6369+ // Use total/global memory as a best-effort upper bound.
6370+ // Improved safety: Reduce by a 1GiB extra margin for common --fit
6371+ *total = backend_ctx->global_mem_size;
6372+ *free = *total > opencl_extra_margin ? *total - opencl_extra_margin : 0;
63646373}
63656374
63666375static enum ggml_backend_dev_type ggml_backend_opencl_device_get_type(ggml_backend_dev_t dev) {
0 commit comments