Skip to content

Commit 7c8d3ab

Browse files
metal : log recommendedMaxWorkingSetSize on iOS 16+ (#4936)
* metal: Log `recommendedMaxWorkingSetSize` on iOS 16+ * Only log on iOS and macOS, ignoring tvOS and other platforms * Check for Xcode version before using recommendedMaxWorkingSetSize --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent 122ed48 commit 7c8d3ab

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

ggml-metal.m

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,12 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
369369
GGML_METAL_LOG_INFO("%s: simdgroup reduction support = %s\n", __func__, ctx->support_simdgroup_reduction ? "true" : "false");
370370
GGML_METAL_LOG_INFO("%s: simdgroup matrix mul. support = %s\n", __func__, ctx->support_simdgroup_mm ? "true" : "false");
371371
GGML_METAL_LOG_INFO("%s: hasUnifiedMemory = %s\n", __func__, ctx->device.hasUnifiedMemory ? "true" : "false");
372-
#if TARGET_OS_OSX
373-
GGML_METAL_LOG_INFO("%s: recommendedMaxWorkingSetSize = %8.2f MB\n", __func__, ctx->device.recommendedMaxWorkingSetSize / 1e6);
372+
373+
#if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
374+
if (@available(macOS 10.12, iOS 16.0, *)) {
375+
GGML_METAL_LOG_INFO("%s: recommendedMaxWorkingSetSize = %8.2f MB\n", __func__, ctx->device.recommendedMaxWorkingSetSize / 1e6);
376+
}
377+
#elif TARGET_OS_OSX
374378
if (ctx->device.maxTransferRate != 0) {
375379
GGML_METAL_LOG_INFO("%s: maxTransferRate = %8.2f MB/s\n", __func__, ctx->device.maxTransferRate / 1e6);
376380
} else {
@@ -2369,6 +2373,25 @@ GGML_CALL static void ggml_backend_metal_buffer_clear(ggml_backend_buffer_t buff
23692373
UNUSED(buft);
23702374
}
23712375

2376+
static void ggml_backend_metal_log_allocated_size(id<MTLDevice> device) {
2377+
#if TARGET_OS_OSX || (TARGET_OS_IOS && __clang_major__ >= 15)
2378+
if (@available(macOS 10.12, iOS 16.0, *)) {
2379+
GGML_METAL_LOG_INFO(", (%8.2f / %8.2f)",
2380+
device.currentAllocatedSize / 1024.0 / 1024.0,
2381+
device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
2382+
2383+
if (device.currentAllocatedSize > device.recommendedMaxWorkingSetSize) {
2384+
GGML_METAL_LOG_WARN("%s: warning: current allocated size is greater than the recommended max working set size\n", __func__);
2385+
} else {
2386+
GGML_METAL_LOG_INFO("\n");
2387+
}
2388+
} else {
2389+
GGML_METAL_LOG_INFO(", (%8.2f)\n", device.currentAllocatedSize / 1024.0 / 1024.0);
2390+
}
2391+
#endif
2392+
UNUSED(device);
2393+
}
2394+
23722395
GGML_CALL static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
23732396
struct ggml_backend_metal_buffer_context * ctx = malloc(sizeof(struct ggml_backend_metal_buffer_context));
23742397

@@ -2401,22 +2424,7 @@ GGML_CALL static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buff
24012424
}
24022425

24032426
GGML_METAL_LOG_INFO("%s: allocated buffer, size = %8.2f MiB", __func__, size_aligned / 1024.0 / 1024.0);
2404-
2405-
2406-
#if TARGET_OS_OSX
2407-
GGML_METAL_LOG_INFO(", (%8.2f / %8.2f)",
2408-
device.currentAllocatedSize / 1024.0 / 1024.0,
2409-
device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
2410-
2411-
if (device.currentAllocatedSize > device.recommendedMaxWorkingSetSize) {
2412-
GGML_METAL_LOG_WARN("%s: warning: current allocated size is greater than the recommended max working set size\n", __func__);
2413-
} else {
2414-
GGML_METAL_LOG_INFO("\n");
2415-
}
2416-
#else
2417-
GGML_METAL_LOG_INFO(", (%8.2f)\n", device.currentAllocatedSize / 1024.0 / 1024.0);
2418-
#endif
2419-
2427+
ggml_backend_metal_log_allocated_size(device);
24202428

24212429
return ggml_backend_buffer_init(buft, ggml_backend_metal_buffer_i, ctx, size);
24222430
}
@@ -2524,19 +2532,7 @@ GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data,
25242532
}
25252533
}
25262534

2527-
#if TARGET_OS_OSX
2528-
GGML_METAL_LOG_INFO(", (%8.2f / %8.2f)",
2529-
device.currentAllocatedSize / 1024.0 / 1024.0,
2530-
device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
2531-
2532-
if (device.currentAllocatedSize > device.recommendedMaxWorkingSetSize) {
2533-
GGML_METAL_LOG_WARN("%s: warning: current allocated size is greater than the recommended max working set size\n", __func__);
2534-
} else {
2535-
GGML_METAL_LOG_INFO("\n");
2536-
}
2537-
#else
2538-
GGML_METAL_LOG_INFO(", (%8.2f)\n", device.currentAllocatedSize / 1024.0 / 1024.0);
2539-
#endif
2535+
ggml_backend_metal_log_allocated_size(device);
25402536

25412537
return ggml_backend_buffer_init(ggml_backend_metal_buffer_type(), ggml_backend_metal_buffer_i, ctx, size);
25422538
}

0 commit comments

Comments
 (0)