Skip to content

Commit 94bb63e

Browse files
authored
opencl : fix buffer alignment (#12197)
Fix the following error: ``` ggml-alloc.c:99: not enough space in the buffer ggml_tallocr_alloc: not enough space in the buffer to allocate blk.17.ffn_down.weight (needed 27525120, available 27521024) ``` which occurs when `ggml_backend_opencl_context::alignment` is larger than `cl_ptr_base` (hard-coded to `0x1000`). Also, fix `ggml_backend_opencl_context::alignment` was set to `CL_DEVICE_MEM_BASE_ADDR_ALIGN` which was treated as bytes but the value is reported in bits.
1 parent f792439 commit 94bb63e

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

ggml/src/ggml-opencl/ggml-opencl.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,10 @@ static ggml_backend_opencl_context * ggml_cl2_init(ggml_backend_dev_t dev) {
524524
return backend_ctx;
525525
}
526526

527-
CL_CHECK(clGetDeviceInfo(device, CL_DEVICE_MEM_BASE_ADDR_ALIGN, sizeof(cl_uint), &backend_ctx->alignment, NULL));
527+
cl_uint base_align_in_bits;
528+
CL_CHECK(clGetDeviceInfo(device, CL_DEVICE_MEM_BASE_ADDR_ALIGN, sizeof(cl_uint), &base_align_in_bits, NULL));
529+
GGML_ASSERT(base_align_in_bits % 8u == 0);
530+
backend_ctx->alignment = base_align_in_bits / 8u;
528531
GGML_LOG_INFO("ggml_opencl: mem base addr align: %u\n", backend_ctx->alignment);
529532

530533
clGetDeviceInfo(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(size_t), &backend_ctx->max_alloc_size, NULL);
@@ -1198,17 +1201,14 @@ struct ggml_backend_opencl_buffer_context {
11981201
std::string name;
11991202
};
12001203

1201-
static void * const cl_ptr_base = (void *)(uintptr_t) 0x1000;
1202-
12031204
static void ggml_backend_opencl_buffer_free_buffer(ggml_backend_buffer_t buffer) {
12041205
ggml_backend_opencl_buffer_context * ctx = (ggml_backend_opencl_buffer_context *) buffer->context;
12051206
delete ctx;
12061207
}
12071208

12081209
static void * ggml_backend_opencl_buffer_get_base(ggml_backend_buffer_t buffer) {
1209-
return cl_ptr_base;
1210-
1211-
GGML_UNUSED(buffer);
1210+
ggml_backend_opencl_context * backend_ctx = ggml_cl2_init(buffer->buft->device);
1211+
return (void *) (uintptr_t) backend_ctx->alignment;
12121212
}
12131213

12141214
static enum ggml_status ggml_backend_opencl_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
@@ -1241,7 +1241,7 @@ static enum ggml_status ggml_backend_opencl_buffer_init_tensor(ggml_backend_buff
12411241
tensor->extra = view_extra;
12421242
} else {
12431243
{
1244-
size_t offset = (char *)tensor->data - (char *)cl_ptr_base;
1244+
size_t offset = (char *) tensor->data - (char *) ggml_backend_opencl_buffer_get_base(buffer);
12451245

12461246
ggml_tensor_extra_cl * extra = ctx->ggml_opencl_alloc_temp_tensor_extra();
12471247
extra->offset = offset;

0 commit comments

Comments
 (0)