| 22 | // |
| 23 | |
| 24 | llama_context::llama_context( |
| 25 | const llama_model & model, |
| 26 | llama_context_params params) : |
| 27 | model(model), |
| 28 | cvec(std::make_unique<llama_adapter_cvec>()), |
| 29 | loras(std::make_unique<llama_adapter_loras>()), |
| 30 | balloc(std::make_unique<llama_batch_allocr>(model.hparams.n_pos_per_embd())) { |
| 31 | // TODO warning when creating llama_context with awkward ctx size that is not a power of 2, |
| 32 | // may need to be backend-dependent |
| 33 | LLAMA_LOG_INFO("%s: constructing llama_context\n", __func__); |
| 34 | |
| 35 | t_start_us = model.t_start_us; |
| 36 | t_load_us = model.t_load_us; |
| 37 | |
| 38 | const auto & hparams = model.hparams; |
| 39 | |
| 40 | cparams.n_seq_max = std::max(1u, params.n_seq_max); |
| 41 | if (cparams.n_seq_max > LLAMA_MAX_SEQ) { |
| 42 | throw std::runtime_error("n_seq_max must be <= " + std::to_string(LLAMA_MAX_SEQ)); |
| 43 | } |
| 44 | |
| 45 | cparams.n_threads = params.n_threads; |
| 46 | cparams.n_threads_batch = params.n_threads_batch; |
| 47 | cparams.yarn_ext_factor = params.yarn_ext_factor >= 0.0f ? params.yarn_ext_factor : hparams.yarn_ext_factor; |
| 48 | cparams.yarn_attn_factor = params.yarn_attn_factor >= 0.0f ? params.yarn_attn_factor : hparams.yarn_attn_factor; |
| 49 | cparams.yarn_beta_fast = params.yarn_beta_fast >= 0.0f ? params.yarn_beta_fast : hparams.yarn_beta_fast; |
| 50 | cparams.yarn_beta_slow = params.yarn_beta_slow >= 0.0f ? params.yarn_beta_slow : hparams.yarn_beta_slow; |
| 51 | cparams.embeddings = params.embeddings; |
| 52 | cparams.offload_kqv = params.offload_kqv; |
| 53 | cparams.no_perf = params.no_perf; |
| 54 | cparams.pooling_type = params.pooling_type; |
| 55 | cparams.warmup = false; |
| 56 | |
| 57 | cparams.n_ctx = params.n_ctx == 0 ? hparams.n_ctx_train : params.n_ctx; |
| 58 | cparams.rope_freq_base = params.rope_freq_base == 0.0f ? hparams.rope_freq_base_train : params.rope_freq_base; |
| 59 | cparams.rope_freq_scale = params.rope_freq_scale == 0.0f ? hparams.rope_freq_scale_train : params.rope_freq_scale; |
| 60 | |
| 61 | cparams.n_ctx_orig_yarn = params.yarn_orig_ctx != 0 ? params.yarn_orig_ctx : |
| 62 | hparams.n_ctx_orig_yarn != 0 ? hparams.n_ctx_orig_yarn : |
| 63 | hparams.n_ctx_train; |
| 64 | |
| 65 | cparams.cb_eval = params.cb_eval; |
| 66 | cparams.cb_eval_user_data = params.cb_eval_user_data; |
| 67 | |
| 68 | // Initialize backend samplers here so they are part of the sampling graph |
| 69 | // before the reserve passes run later in this function. This avoids a later |
| 70 | // re-reserve when graph nodes change. |
| 71 | if (params.samplers != nullptr && params.n_samplers > 0) { |
| 72 | for (size_t i = 0; i < params.n_samplers; ++i) { |
| 73 | const auto & config = params.samplers[i]; |
| 74 | |
| 75 | if (llama_sampler_chain_get(config.sampler, -1) == nullptr) { |
| 76 | throw std::runtime_error("the backend samplers must be of type llama_sampler_chain"); |
| 77 | } |
| 78 | |
| 79 | if (set_sampler(config.seq_id, config.sampler)) { |
| 80 | const int n_samplers = llama_sampler_chain_n(config.sampler); |
| 81 |
nothing calls this directly
no test coverage detected