| 886 | // |
| 887 | |
| 888 | struct common_init_result common_init_from_params(common_params & params) { |
| 889 | common_init_result iparams; |
| 890 | auto mparams = common_model_params_to_llama(params); |
| 891 | |
| 892 | llama_model * model = llama_model_load_from_file(params.model.path.c_str(), mparams); |
| 893 | if (model == NULL) { |
| 894 | LOG_ERR("%s: failed to load model '%s'\n", __func__, params.model.path.c_str()); |
| 895 | return iparams; |
| 896 | } |
| 897 | |
| 898 | const llama_vocab * vocab = llama_model_get_vocab(model); |
| 899 | |
| 900 | if (params.reranking) { |
| 901 | bool ok = true; |
| 902 | |
| 903 | if (llama_vocab_bos(vocab) == LLAMA_TOKEN_NULL) { |
| 904 | LOG_WRN("%s: warning: vocab does not have a BOS token, reranking will not work\n", __func__); |
| 905 | ok = false; |
| 906 | } |
| 907 | |
| 908 | bool has_eos = llama_vocab_eos(vocab) != LLAMA_TOKEN_NULL; |
| 909 | bool has_sep = llama_vocab_sep(vocab) != LLAMA_TOKEN_NULL; |
| 910 | |
| 911 | if (!has_eos && !has_sep) { |
| 912 | LOG_WRN("%s: warning: vocab does not have an EOS token or SEP token, reranking will not work\n", __func__); |
| 913 | ok = false; |
| 914 | } else if (!has_eos) { |
| 915 | LOG_WRN("%s: warning: vocab does not have an EOS token, using SEP token as fallback\n", __func__); |
| 916 | } else if (!has_sep) { |
| 917 | LOG_WRN("%s: warning: vocab does not have a SEP token, reranking will not work\n", __func__); |
| 918 | ok = false; |
| 919 | } |
| 920 | |
| 921 | if (!ok) { |
| 922 | llama_model_free(model); |
| 923 | |
| 924 | return iparams; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | auto cparams = common_context_params_to_llama(params); |
| 929 | |
| 930 | llama_context * lctx = llama_init_from_model(model, cparams); |
| 931 | if (lctx == NULL) { |
| 932 | LOG_ERR("%s: failed to create context with model '%s'\n", __func__, params.model.path.c_str()); |
| 933 | llama_model_free(model); |
| 934 | return iparams; |
| 935 | } |
| 936 | |
| 937 | if (params.ctx_shift && !llama_kv_self_can_shift(lctx)) { |
| 938 | LOG_WRN("%s: KV cache shifting is not supported for this context, disabling KV cache shifting\n", __func__); |
| 939 | params.ctx_shift = false; |
| 940 | } |
| 941 | |
| 942 | if (!params.control_vectors.empty()) { |
| 943 | if (params.control_vector_layer_start <= 0) params.control_vector_layer_start = 1; |
| 944 | if (params.control_vector_layer_end <= 0) params.control_vector_layer_end = llama_model_n_layer(model); |
| 945 | |