| 960 | } |
| 961 | |
| 962 | std::tuple<struct llama_model *, struct llama_context *> llama_init_from_gpt_params(gpt_params & params) { |
| 963 | auto mparams = llama_model_params_from_gpt_params(params); |
| 964 | auto cparams = llama_context_params_from_gpt_params(params); |
| 965 | |
| 966 | llama_model * model = llama_load_model_from_file_with_context(params.model.c_str(), mparams, &cparams); |
| 967 | if (model == NULL) { |
| 968 | fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, params.model.c_str()); |
| 969 | return std::make_tuple(nullptr, nullptr); |
| 970 | } |
| 971 | |
| 972 | llama_context * lctx = llama_new_context_with_model(model, cparams); |
| 973 | if (lctx == NULL) { |
| 974 | fprintf(stderr, "%s: error: failed to create context with model '%s'\n", __func__, params.model.c_str()); |
| 975 | llama_free_model(model); |
| 976 | return std::make_tuple(nullptr, nullptr); |
| 977 | } |
| 978 | |
| 979 | for (unsigned int i = 0; i < params.lora_adapter.size(); ++i) { |
| 980 | const std::string& lora_adapter = std::get<0>(params.lora_adapter[i]); |
| 981 | float lora_scale = std::get<1>(params.lora_adapter[i]); |
| 982 | int err = llama_model_apply_lora_from_file(model, |
| 983 | lora_adapter.c_str(), |
| 984 | lora_scale, |
| 985 | ((i > 0) || params.lora_base.empty()) |
| 986 | ? NULL |
| 987 | : params.lora_base.c_str(), |
| 988 | params.n_threads); |
| 989 | if (err != 0) { |
| 990 | fprintf(stderr, "%s: error: failed to apply lora adapter\n", __func__); |
| 991 | llama_free(lctx); |
| 992 | llama_free_model(model); |
| 993 | return std::make_tuple(nullptr, nullptr); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | if (params.ignore_eos) { |
| 998 | params.sparams.logit_bias[llama_token_eos(model)] = -INFINITY; |
| 999 | } |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | { |
| 1004 | LOG("warming up the model with an empty run\n"); |
| 1005 | |
| 1006 | std::vector<llama_token> tmp = { llama_token_bos(model), llama_token_eos(model), }; |
| 1007 | llama_decode(lctx, llama_batch_get_one(tmp.data(), std::min(tmp.size(), (size_t) params.n_batch), 0, 0)); |
| 1008 | llama_kv_cache_clear(lctx); |
| 1009 | llama_reset_timings(lctx); |
| 1010 | } |
| 1011 | |
| 1012 | return std::make_tuple(model, lctx); |
| 1013 | } |
| 1014 | |
| 1015 | // |
| 1016 | // Vocab utils |