Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/unload model api #97

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions controllers/llamaCPP.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,16 @@ void llamaCPP::loadModel(
warmupModel();
callback(resp);
}

void inferences::llamaCPP::unloadModel(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
Json::Value jsonResp;
if (model_loaded) {
llama.unloadModel();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I tested, the server stops working after this line.
The below lines do not execute to return result

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry my mistake. The API endpoint map to the wrong handler:
it should be METHOD_ADD(llamaCPP::unloadModel, "unloadmodel", Delete);
instead of METHOD_ADD(llamaCPP::loadModel, "unloadmodel", Delete);

model_loaded = false;
jsonResp["message"] = "Model unloaded successfully";
}
jsonResp["message"] = "No model loaded";
auto resp = nitro_utils::nitroHttpJsonResponse(jsonResp);
callback(resp);
}
11 changes: 11 additions & 0 deletions controllers/llamaCPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ struct llama_server_context {
return true;
}

void unloadModel() {
llama_free(ctx);
llama_free_model(model);
ctx = nullptr;
model = nullptr;
}

std::vector<llama_token> tokenize(const json &json_prompt,
bool add_bos) const {
// If `add_bos` is true, we only add BOS, when json_prompt is a string,
Expand Down Expand Up @@ -1272,6 +1279,7 @@ class llamaCPP : public drogon::HttpController<llamaCPP> {
METHOD_ADD(llamaCPP::chatCompletion, "chat_completion", Post);
METHOD_ADD(llamaCPP::embedding, "embedding", Post);
METHOD_ADD(llamaCPP::loadModel, "loadmodel", Post);
METHOD_ADD(llamaCPP::loadModel, "unloadmodel", Delete);
// PATH_ADD("/llama/chat_completion", Post);
METHOD_LIST_END
void chatCompletion(const HttpRequestPtr &req,
Expand All @@ -1282,6 +1290,9 @@ class llamaCPP : public drogon::HttpController<llamaCPP> {
std::function<void(const HttpResponsePtr &)> &&callback);
void warmupModel();

void unloadModel(const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback);

private:
llama_server_context llama;
bool model_loaded = false;
Expand Down