Skip to content

Commit f657865

Browse files
committed
metal : add default log function that prints to stderr, cleanup code
1 parent 323881e commit f657865

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

ggml-metal.m

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,15 @@ @interface GGMLMetalClass : NSObject
180180
@implementation GGMLMetalClass
181181
@end
182182

183-
ggml_log_callback ggml_metal_log_callback = NULL;
183+
184+
static void ggml_metal_default_log_callback(enum ggml_log_level level, const char * msg, void * user_data) {
185+
fprintf(stderr, "%s", msg);
186+
187+
UNUSED(level);
188+
UNUSED(user_data);
189+
}
190+
191+
ggml_log_callback ggml_metal_log_callback = ggml_metal_default_log_callback;
184192
void * ggml_metal_log_user_data = NULL;
185193

186194
void ggml_metal_log_set_callback(ggml_log_callback log_callback, void * user_data) {
@@ -622,7 +630,7 @@ int ggml_metal_if_optimized(struct ggml_metal_context * ctx) {
622630

623631
// multiple buffers are used only to avoid the maximum buffer size limitation when using mmap
624632
int n_buffers;
625-
struct ggml_metal_buffer buffers[GGML_METAL_MAX_BUFFERS];
633+
struct ggml_backend_metal_buffer buffers[GGML_METAL_MAX_BUFFERS];
626634
};
627635

628636
// finds the Metal buffer that contains the tensor data on the GPU device
@@ -2499,13 +2507,29 @@ static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_ba
24992507
deallocator:nil];
25002508

25012509
if (ctx->buffers[0].metal == nil) {
2502-
GGML_METAL_LOG_ERROR("%s: error: failed to allocate '%-16s' buffer, size = %8.2f MiB\n", __func__, "default", size_aligned / 1024.0 / 1024.0);
2510+
GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
25032511
free(ctx);
25042512
ggml_backend_metal_free_device();
25052513
return NULL;
25062514
}
25072515

2508-
GGML_METAL_LOG_INFO("%s: allocated '%-16s' buffer, size = %8.2f MiB\n", __func__, "default", size_aligned / 1024.0 / 1024.0);
2516+
GGML_METAL_LOG_INFO("%s: allocated buffer, size = %8.2f MiB", __func__, size_aligned / 1024.0 / 1024.0);
2517+
2518+
2519+
#if TARGET_OS_OSX
2520+
GGML_METAL_LOG_INFO(", (%8.2f / %8.2f)",
2521+
device.currentAllocatedSize / 1024.0 / 1024.0,
2522+
device.recommendedMaxWorkingSetSize / 1024.0 / 1024.0);
2523+
2524+
if (device.currentAllocatedSize > device.recommendedMaxWorkingSetSize) {
2525+
GGML_METAL_LOG_WARN("%s: warning: current allocated size is greater than the recommended max working set size\n", __func__);
2526+
} else {
2527+
GGML_METAL_LOG_INFO("\n");
2528+
}
2529+
#else
2530+
GGML_METAL_LOG_INFO(", (%8.2f)\n", device.currentAllocatedSize / 1024.0 / 1024.0);
2531+
#endif
2532+
25092533

25102534
return ggml_backend_buffer_init(buft, ggml_backend_metal_buffer_i, ctx, size);
25112535
}
@@ -2560,22 +2584,19 @@ ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t siz
25602584

25612585
id<MTLDevice> device = ggml_backend_metal_get_device();
25622586

2563-
const char * name = "from_ptr";
2564-
25652587
// the buffer fits into the max buffer size allowed by the device
25662588
if (size_aligned <= device.maxBufferLength) {
2567-
ctx->buffers[ctx->n_buffers].name = name;
25682589
ctx->buffers[ctx->n_buffers].data = data;
25692590
ctx->buffers[ctx->n_buffers].size = size;
25702591

25712592
ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:data length:size_aligned options:MTLResourceStorageModeShared deallocator:nil];
25722593

25732594
if (ctx->buffers[ctx->n_buffers].metal == nil) {
2574-
GGML_METAL_LOG_ERROR("%s: error: failed to allocate '%-16s' buffer, size = %8.2f MiB\n", __func__, name, size_aligned / 1024.0 / 1024.0);
2595+
GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_aligned / 1024.0 / 1024.0);
25752596
return false;
25762597
}
25772598

2578-
GGML_METAL_LOG_INFO("%s: allocated '%-16s' buffer, size = %8.2f MiB\n", __func__, name, size_aligned / 1024.0 / 1024.0);
2599+
GGML_METAL_LOG_INFO("%s: allocated buffer, size = %8.2f MiB", __func__, size_aligned / 1024.0 / 1024.0);
25792600

25802601
++ctx->n_buffers;
25812602
} else {
@@ -2588,18 +2609,17 @@ ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t siz
25882609
for (size_t i = 0; i < size; i += size_step) {
25892610
const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
25902611

2591-
ctx->buffers[ctx->n_buffers].name = name;
25922612
ctx->buffers[ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
25932613
ctx->buffers[ctx->n_buffers].size = size_step_aligned;
25942614

25952615
ctx->buffers[ctx->n_buffers].metal = [device newBufferWithBytesNoCopy:(void *) ((uint8_t *) data + i) length:size_step_aligned options:MTLResourceStorageModeShared deallocator:nil];
25962616

25972617
if (ctx->buffers[ctx->n_buffers].metal == nil) {
2598-
GGML_METAL_LOG_ERROR("%s: error: failed to allocate '%-16s' buffer, size = %8.2f MiB\n", __func__, name, size_step_aligned / 1024.0 / 1024.0);
2618+
GGML_METAL_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f MiB\n", __func__, size_step_aligned / 1024.0 / 1024.0);
25992619
return false;
26002620
}
26012621

2602-
GGML_METAL_LOG_INFO("%s: allocated '%-16s' buffer, size = %8.2f MiB, offs = %12ld", __func__, name, size_step_aligned / 1024.0 / 1024.0, i);
2622+
GGML_METAL_LOG_INFO("%s: allocated buffer, size = %8.2f MiB, offs = %12ld", __func__, size_step_aligned / 1024.0 / 1024.0, i);
26032623
if (i + size_step < size) {
26042624
GGML_METAL_LOG_INFO("\n");
26052625
}
@@ -2673,17 +2693,7 @@ static bool ggml_backend_metal_supports_op(ggml_backend_t backend, const struct
26732693
/* .supports_op = */ ggml_backend_metal_supports_op,
26742694
};
26752695

2676-
// TODO: make a common log callback for all backends in ggml-backend
2677-
static void ggml_backend_log_callback(enum ggml_log_level level, const char * msg, void * user_data) {
2678-
fprintf(stderr, "%s", msg);
2679-
2680-
UNUSED(level);
2681-
UNUSED(user_data);
2682-
}
2683-
26842696
ggml_backend_t ggml_backend_metal_init(void) {
2685-
ggml_metal_log_set_callback(ggml_backend_log_callback, NULL);
2686-
26872697
struct ggml_metal_context * ctx = ggml_metal_init(GGML_DEFAULT_N_THREADS);
26882698

26892699
if (ctx == NULL) {

0 commit comments

Comments
 (0)