| 2218 | } |
| 2219 | |
| 2220 | llama_context * llama_init_from_model( |
| 2221 | llama_model * model, |
| 2222 | llama_context_params params) { |
| 2223 | if (!model) { |
| 2224 | LLAMA_LOG_ERROR("%s: model cannot be NULL\n", __func__); |
| 2225 | return nullptr; |
| 2226 | } |
| 2227 | |
| 2228 | if (params.n_batch == 0 && params.n_ubatch == 0) { |
| 2229 | LLAMA_LOG_ERROR("%s: n_batch and n_ubatch cannot both be zero\n", __func__); |
| 2230 | return nullptr; |
| 2231 | } |
| 2232 | |
| 2233 | if (params.n_ctx == 0 && model->hparams.n_ctx_train == 0) { |
| 2234 | LLAMA_LOG_ERROR("%s: n_ctx and model->hparams.n_ctx_train cannot both be zero\n", __func__); |
| 2235 | return nullptr; |
| 2236 | } |
| 2237 | |
| 2238 | if (params.flash_attn && model->arch == LLM_ARCH_GROK) { |
| 2239 | LLAMA_LOG_WARN("%s: flash_attn is not compatible with Grok - forcing off\n", __func__); |
| 2240 | params.flash_attn = false; |
| 2241 | } |
| 2242 | |
| 2243 | if (ggml_is_quantized(params.type_v) && !params.flash_attn) { |
| 2244 | LLAMA_LOG_ERROR("%s: V cache quantization requires flash_attn\n", __func__); |
| 2245 | return nullptr; |
| 2246 | } |
| 2247 | |
| 2248 | try { |
| 2249 | auto * ctx = new llama_context(*model, params); |
| 2250 | return ctx; |
| 2251 | } catch (const std::exception & err) { |
| 2252 | LLAMA_LOG_ERROR("%s: failed to initialize the context: %s\n", __func__, err.what()); |
| 2253 | } |
| 2254 | |
| 2255 | return nullptr; |
| 2256 | } |
| 2257 | |
| 2258 | // deprecated |
| 2259 | llama_context * llama_new_context_with_model( |