| 18 | // |
| 19 | |
| 20 | llama_context::llama_context( |
| 21 | const llama_model & model, |
| 22 | llama_context_params params) : |
| 23 | model(model) { |
| 24 | LLAMA_LOG_INFO("%s: constructing llama_context\n", __func__); |
| 25 | |
| 26 | t_start_us = model.t_start_us; |
| 27 | t_load_us = model.t_load_us; |
| 28 | |
| 29 | const auto & hparams = model.hparams; |
| 30 | |
| 31 | cparams.n_seq_max = std::max(1u, params.n_seq_max); |
| 32 | if (cparams.n_seq_max > LLAMA_MAX_PARALLEL_SEQUENCES) { |
| 33 | throw std::runtime_error("n_seq_max must be <= " + std::to_string(LLAMA_MAX_PARALLEL_SEQUENCES)); |
| 34 | } |
| 35 | |
| 36 | cparams.n_threads = params.n_threads; |
| 37 | cparams.n_threads_batch = params.n_threads_batch; |
| 38 | cparams.yarn_ext_factor = params.yarn_ext_factor; |
| 39 | cparams.yarn_attn_factor = params.yarn_attn_factor; |
| 40 | cparams.yarn_beta_fast = params.yarn_beta_fast; |
| 41 | cparams.yarn_beta_slow = params.yarn_beta_slow; |
| 42 | cparams.defrag_thold = params.defrag_thold; |
| 43 | cparams.embeddings = params.embeddings; |
| 44 | cparams.offload_kqv = params.offload_kqv; |
| 45 | cparams.flash_attn = params.flash_attn; |
| 46 | cparams.no_perf = params.no_perf; |
| 47 | cparams.pooling_type = params.pooling_type; |
| 48 | cparams.warmup = false; |
| 49 | |
| 50 | cparams.n_ctx = params.n_ctx == 0 ? hparams.n_ctx_train : params.n_ctx; |
| 51 | cparams.rope_freq_base = params.rope_freq_base == 0.0f ? hparams.rope_freq_base_train : params.rope_freq_base; |
| 52 | cparams.rope_freq_scale = params.rope_freq_scale == 0.0f ? hparams.rope_freq_scale_train : params.rope_freq_scale; |
| 53 | |
| 54 | cparams.n_ctx_orig_yarn = params.yarn_orig_ctx != 0 ? params.yarn_orig_ctx : |
| 55 | hparams.n_ctx_orig_yarn != 0 ? hparams.n_ctx_orig_yarn : |
| 56 | hparams.n_ctx_train; |
| 57 | |
| 58 | cparams.cb_eval = params.cb_eval; |
| 59 | cparams.cb_eval_user_data = params.cb_eval_user_data; |
| 60 | |
| 61 | auto rope_scaling_type = params.rope_scaling_type; |
| 62 | if (rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED) { |
| 63 | rope_scaling_type = hparams.rope_scaling_type_train; |
| 64 | } |
| 65 | |
| 66 | if (rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_NONE) { |
| 67 | cparams.rope_freq_scale = 1.0f; // never scale if scaling type is none |
| 68 | } |
| 69 | |
| 70 | if (cparams.yarn_ext_factor < 0.0f) { // negative indicates 'not set' |
| 71 | cparams.yarn_ext_factor = rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_YARN ? 1.0f : 0.0f; |
| 72 | } |
| 73 | |
| 74 | cparams.yarn_attn_factor *= hparams.rope_attn_factor; |
| 75 | |
| 76 | if (cparams.pooling_type == LLAMA_POOLING_TYPE_UNSPECIFIED) { |
| 77 | if (hparams.pooling_type == LLAMA_POOLING_TYPE_UNSPECIFIED) { |
nothing calls this directly
no test coverage detected