| 3390 | } |
| 3391 | |
| 3392 | void llama_reserve_model_kv_cache(llama_model *model, const llama_context_params *cparams) { |
| 3393 | #if defined(GGML_USE_CUBLAS) |
| 3394 | if (!ggml_cublas_loaded()) { |
| 3395 | throw std::runtime_error(format("cannot offload to GPU: " GGML_CUDA_NAME " not loaded")); |
| 3396 | } |
| 3397 | |
| 3398 | const llama_hparams &hparams = model->hparams; |
| 3399 | if (model->n_gpu_layers < hparams.n_layer + 1) { |
| 3400 | // should only reserve kv cache for models with all layers offloaded |
| 3401 | return; |
| 3402 | } |
| 3403 | |
| 3404 | const uint32_t n_embd = hparams.n_embd_gqa(); |
| 3405 | const uint32_t n_layer = hparams.n_layer; |
| 3406 | |
| 3407 | const int64_t n_mem = n_layer*cparams->n_ctx; |
| 3408 | const int64_t n_elements = n_embd*n_mem; |
| 3409 | |
| 3410 | const ggml_type wtype = cparams->f16_kv ? GGML_TYPE_F16 : GGML_TYPE_F32; |
| 3411 | const size_t cache_size = n_elements*ggml_type_size(wtype); |
| 3412 | |
| 3413 | // reserve for k cache and v cache |
| 3414 | for (int i = 0; i < 2; i++) { |
| 3415 | if (!llama_reduce_vram_budget(cache_size)) { |
| 3416 | return; |
| 3417 | } |
| 3418 | model->n_gpu_layers++; |
| 3419 | } |
| 3420 | #endif |
| 3421 | } |
| 3422 | |
| 3423 | static void llm_load_tensors( |
| 3424 | llama_model_loader & ml, |
no test coverage detected