@@ -63,7 +63,7 @@ static void llama_log_callback_default(llama_log_level level, const char * text,
63
63
#define LLAMA_LOG_ERROR (...) llama_log_internal(LLAMA_LOG_LEVEL_ERROR, __VA_ARGS__)
64
64
65
65
66
- #if !defined(GGML_USE_CUBLAS) && !defined(GGML_USE_METAL)
66
+ #if !defined(GGML_USE_CUBLAS)
67
67
#include " ggml-alloc.h"
68
68
#define LLAMA_USE_ALLOCATOR
69
69
#else
@@ -1846,10 +1846,6 @@ static bool llama_eval_internal(
1846
1846
1847
1847
#ifdef GGML_USE_METAL
1848
1848
if (lctx.ctx_metal && N == 1 ) {
1849
- // TODO: disabled until #2413 is resolved
1850
- // if (!ggml_metal_if_optimized(lctx.ctx_metal)) {
1851
- // ggml_metal_graph_find_concurrency(lctx.ctx_metal, gf);
1852
- // }
1853
1849
ggml_metal_set_n_cb (lctx.ctx_metal , n_threads);
1854
1850
ggml_metal_graph_compute (lctx.ctx_metal , gf);
1855
1851
ggml_metal_get_tensor (lctx.ctx_metal , res);
@@ -3303,7 +3299,18 @@ struct llama_context * llama_new_context_with_model(
3303
3299
int n_past = hparams.n_ctx - n_tokens;
3304
3300
llama_token token = llama_token_bos (); // not actually used by llama_build_graph, but required to choose between token and embedding inputs graph
3305
3301
ggml_cgraph * gf = llama_build_graph (*ctx, &token, NULL , n_tokens, n_past);
3306
-
3302
+ #ifdef GGML_USE_METAL
3303
+ if (params.n_gpu_layers > 0 ) {
3304
+ ctx->ctx_metal = ggml_metal_init (1 );
3305
+ if (!ctx->ctx_metal ) {
3306
+ LLAMA_LOG_ERROR (" %s: ggml_metal_init() failed\n " , __func__);
3307
+ llama_free (ctx);
3308
+ return NULL ;
3309
+ }
3310
+ ggml_metal_graph_find_concurrency (ctx->ctx_metal , gf, false );
3311
+ ggml_allocr_set_concur_list (ctx->alloc , ggml_metal_get_concur_list (ctx->ctx_metal ),ggml_metal_if_optimized (ctx->ctx_metal ));
3312
+ }
3313
+ #endif
3307
3314
// measure memory requirements for the graph
3308
3315
size_t alloc_size = ggml_allocr_alloc_graph (ctx->alloc , gf) + tensor_alignment;
3309
3316
@@ -3321,6 +3328,11 @@ struct llama_context * llama_new_context_with_model(
3321
3328
3322
3329
ctx->buf_alloc .resize (alloc_size);
3323
3330
ctx->alloc = ggml_allocr_new (ctx->buf_alloc .addr , ctx->buf_alloc .size , tensor_alignment);
3331
+ #ifdef GGML_USE_METAL
3332
+ if (ctx->ctx_metal ) {
3333
+ ggml_allocr_set_concur_list (ctx->alloc , ggml_metal_get_concur_list (ctx->ctx_metal ),ggml_metal_if_optimized (ctx->ctx_metal ));
3334
+ }
3335
+ #endif
3324
3336
}
3325
3337
#else
3326
3338
ctx->buf_compute .resize (MEM_REQ_EVAL ().at (ctx->model .type ) + ggml_graph_overhead ());
@@ -3335,13 +3347,6 @@ struct llama_context * llama_new_context_with_model(
3335
3347
#ifdef GGML_USE_METAL
3336
3348
if (params.n_gpu_layers > 0 ) {
3337
3349
// this allocates all Metal resources and memory buffers
3338
- ctx->ctx_metal = ggml_metal_init (1 );
3339
-
3340
- if (!ctx->ctx_metal ) {
3341
- LLAMA_LOG_ERROR (" %s: ggml_metal_init() failed\n " , __func__);
3342
- llama_free (ctx);
3343
- return NULL ;
3344
- }
3345
3350
3346
3351
void * data_ptr = NULL ;
3347
3352
size_t data_size = 0 ;
@@ -3370,8 +3375,7 @@ struct llama_context * llama_new_context_with_model(
3370
3375
LLAMA_METAL_CHECK_BUF (ggml_metal_add_buffer (ctx->ctx_metal , " eval" , ctx->buf_compute .addr , ctx->buf_compute .size , 0 ));
3371
3376
LLAMA_METAL_CHECK_BUF (ggml_metal_add_buffer (ctx->ctx_metal , " kv" , ctx->kv_self .buf .addr , ctx->kv_self .buf .size , 0 ));
3372
3377
3373
- LLAMA_METAL_CHECK_BUF (ggml_metal_add_buffer (ctx->ctx_metal , " scr0" , ctx->buf_scratch [0 ].addr , ctx->buf_scratch [0 ].size , 0 ));
3374
- LLAMA_METAL_CHECK_BUF (ggml_metal_add_buffer (ctx->ctx_metal , " scr1" , ctx->buf_scratch [1 ].addr , ctx->buf_scratch [1 ].size , 0 ));
3378
+ LLAMA_METAL_CHECK_BUF (ggml_metal_add_buffer (ctx->ctx_metal , " alloc" , ctx->buf_alloc .addr , ctx->buf_alloc .size , 0 ));
3375
3379
#undef LLAMA_METAL_CHECK_BUF
3376
3380
}
3377
3381
#endif
0 commit comments