@@ -3225,15 +3225,19 @@ GGML_CALL static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buff
32253225 ctx->n_buffers = 1 ;
32263226
32273227 if (ctx->all_data != NULL ) {
3228- ctx->buffers [0 ].data = ctx->all_data ;
3229- ctx->buffers [0 ].size = size;
3230- ctx->buffers [0 ].metal = [device newBufferWithBytesNoCopy: ctx->all_data
3231- length: size_aligned
3232- options: MTLResourceStorageModeShared
3233- deallocator: nil ];
3228+ ctx->buffers [0 ].data = ctx->all_data ;
3229+ ctx->buffers [0 ].size = size;
3230+ ctx->buffers [0 ].metal = nil ;
3231+
3232+ if (size_aligned > 0 ) {
3233+ ctx->buffers [0 ].metal = [device newBufferWithBytesNoCopy: ctx->all_data
3234+ length: size_aligned
3235+ options: MTLResourceStorageModeShared
3236+ deallocator: nil ];
3237+ }
32343238 }
32353239
3236- if (ctx->all_data == NULL || ctx->buffers [0 ].metal == nil ) {
3240+ if (size_aligned > 0 && ( ctx->all_data == NULL || ctx->buffers [0 ].metal == nil ) ) {
32373241 GGML_METAL_LOG_ERROR (" %s : error: failed to allocate buffer, size = %8.2f MiB\n " , __func__, size_aligned / 1024.0 / 1024.0 );
32383242 free (ctx);
32393243 ggml_backend_metal_free_device ();
@@ -3310,14 +3314,17 @@ GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data,
33103314
33113315 // the buffer fits into the max buffer size allowed by the device
33123316 if (size_aligned <= device.maxBufferLength ) {
3313- ctx->buffers [ctx->n_buffers].data = data;
3314- ctx->buffers [ctx->n_buffers].size = size;
3317+ ctx->buffers [ctx->n_buffers].data = data;
3318+ ctx->buffers [ctx->n_buffers].size = size;
3319+ ctx->buffers [ctx->n_buffers].metal = nil ;
33153320
3316- ctx->buffers [ctx->n_buffers].metal = [device newBufferWithBytesNoCopy: data length: size_aligned options: MTLResourceStorageModeShared deallocator: nil ];
3321+ if (size_aligned > 0 ) {
3322+ ctx->buffers [ctx->n_buffers].metal = [device newBufferWithBytesNoCopy: data length: size_aligned options: MTLResourceStorageModeShared deallocator: nil ];
33173323
3318- if (ctx->buffers [ctx->n_buffers].metal == nil ) {
3319- GGML_METAL_LOG_ERROR (" %s : error: failed to allocate buffer, size = %8.2f MiB\n " , __func__, size_aligned / 1024.0 / 1024.0 );
3320- return false ;
3324+ if (ctx->buffers [ctx->n_buffers].metal == nil ) {
3325+ GGML_METAL_LOG_ERROR (" %s : error: failed to allocate buffer, size = %8.2f MiB\n " , __func__, size_aligned / 1024.0 / 1024.0 );
3326+ return false ;
3327+ }
33213328 }
33223329
33233330 ggml_backend_metal_log_allocated_size (device, size_aligned);
@@ -3333,14 +3340,17 @@ GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data,
33333340 for (size_t i = 0 ; i < size; i += size_step) {
33343341 const size_t size_step_aligned = (i + size_view <= size) ? size_view : (size_aligned - i);
33353342
3336- ctx->buffers [ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
3337- ctx->buffers [ctx->n_buffers].size = size_step_aligned;
3343+ ctx->buffers [ctx->n_buffers].data = (void *) ((uint8_t *) data + i);
3344+ ctx->buffers [ctx->n_buffers].size = size_step_aligned;
3345+ ctx->buffers [ctx->n_buffers].metal = nil ;
33383346
3339- ctx->buffers [ctx->n_buffers].metal = [device newBufferWithBytesNoCopy: (void *) ((uint8_t *) data + i) length: size_step_aligned options: MTLResourceStorageModeShared deallocator: nil ];
3347+ if (size_step_aligned > 0 ) {
3348+ ctx->buffers [ctx->n_buffers].metal = [device newBufferWithBytesNoCopy: (void *) ((uint8_t *) data + i) length: size_step_aligned options: MTLResourceStorageModeShared deallocator: nil ];
33403349
3341- if (ctx->buffers [ctx->n_buffers].metal == nil ) {
3342- GGML_METAL_LOG_ERROR (" %s : error: failed to allocate buffer, size = %8.2f MiB\n " , __func__, size_step_aligned / 1024.0 / 1024.0 );
3343- return false ;
3350+ if (ctx->buffers [ctx->n_buffers].metal == nil ) {
3351+ GGML_METAL_LOG_ERROR (" %s : error: failed to allocate buffer, size = %8.2f MiB\n " , __func__, size_step_aligned / 1024.0 / 1024.0 );
3352+ return false ;
3353+ }
33443354 }
33453355
33463356 ggml_backend_metal_log_allocated_size (device, size_step_aligned);
0 commit comments