Skip to content

Commit 9b74dc7

Browse files
committed
metal : use virtual GPU address for private buffers
ggml-ci
1 parent 918b26f commit 9b74dc7

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

ggml/src/ggml-metal/ggml-metal.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#import <Metal/Metal.h>
1111

12+
#include <stdatomic.h>
13+
1214
#undef MIN
1315
#undef MAX
1416
#define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -6326,8 +6328,10 @@ static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_ba
63266328
ctx->all_data = ggml_metal_host_malloc(size_aligned);
63276329
ctx->is_shared = true;
63286330
} else {
6329-
// dummy, non-NULL value - we'll populate this after creating the Metal buffer below
6330-
ctx->all_data = (void *) 0x000000400ULL;
6331+
// virtual address for GPU memory allocations
6332+
static atomic_uintptr_t addr_device = 0x000000400ULL;
6333+
6334+
ctx->all_data = (void *) atomic_fetch_add_explicit(&addr_device, size_aligned, memory_order_relaxed);
63316335
ctx->is_shared = false;
63326336
}
63336337
ctx->all_size = size_aligned;
@@ -6350,7 +6354,10 @@ static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_ba
63506354
} else {
63516355
ctx->buffers[0].metal = [device newBufferWithLength:size_aligned options:MTLResourceStorageModePrivate];
63526356

6353-
ctx->all_data = (void *) (ctx->buffers[0].metal.gpuAddress);
6357+
// MTLBuffer.gpuAddress is not available on early OSes, so we use a virtual address instead
6358+
//if (@available(macOS 10.13, iOS 16.0, *)) {
6359+
// ctx->all_data = (void *) (ctx->buffers[0].metal.gpuAddress);
6360+
//}
63546361
}
63556362
}
63566363

0 commit comments

Comments
 (0)