@@ -63,7 +63,7 @@ static void llama_log_callback_default(llama_log_level level, const char * text,
6363#define LLAMA_LOG_ERROR (...) llama_log_internal(LLAMA_LOG_LEVEL_ERROR, __VA_ARGS__)
6464
6565
66- #if !defined(GGML_USE_CUBLAS) && !defined(GGML_USE_METAL)
66+ #if !defined(GGML_USE_CUBLAS)
6767#include " ggml-alloc.h"
6868#define LLAMA_USE_ALLOCATOR
6969#else
@@ -1846,10 +1846,6 @@ static bool llama_eval_internal(
18461846
18471847#ifdef GGML_USE_METAL
18481848 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- // }
18531849 ggml_metal_set_n_cb (lctx.ctx_metal , n_threads);
18541850 ggml_metal_graph_compute (lctx.ctx_metal , gf);
18551851 ggml_metal_get_tensor (lctx.ctx_metal , res);
@@ -3303,7 +3299,18 @@ struct llama_context * llama_new_context_with_model(
33033299 int n_past = hparams.n_ctx - n_tokens;
33043300 llama_token token = llama_token_bos (); // not actually used by llama_build_graph, but required to choose between token and embedding inputs graph
33053301 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
33073314 // measure memory requirements for the graph
33083315 size_t alloc_size = ggml_allocr_alloc_graph (ctx->alloc , gf) + tensor_alignment;
33093316
@@ -3321,6 +3328,11 @@ struct llama_context * llama_new_context_with_model(
33213328
33223329 ctx->buf_alloc .resize (alloc_size);
33233330 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
33243336 }
33253337#else
33263338 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(
33353347#ifdef GGML_USE_METAL
33363348 if (params.n_gpu_layers > 0 ) {
33373349 // 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- }
33453350
33463351 void * data_ptr = NULL ;
33473352 size_t data_size = 0 ;
@@ -3370,8 +3375,7 @@ struct llama_context * llama_new_context_with_model(
33703375 LLAMA_METAL_CHECK_BUF (ggml_metal_add_buffer (ctx->ctx_metal , " eval" , ctx->buf_compute .addr , ctx->buf_compute .size , 0 ));
33713376 LLAMA_METAL_CHECK_BUF (ggml_metal_add_buffer (ctx->ctx_metal , " kv" , ctx->kv_self .buf .addr , ctx->kv_self .buf .size , 0 ));
33723377
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 ));
33753379#undef LLAMA_METAL_CHECK_BUF
33763380 }
33773381#endif
0 commit comments