Skip to content

Commit

Permalink
Fallback to CPU more robustly.
Browse files Browse the repository at this point in the history
  • Loading branch information
manyoso committed Sep 14, 2023
1 parent 79843c2 commit aa33419
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gpt4all-backend/llama.cpp-mainline
6 changes: 5 additions & 1 deletion gpt4all-backend/llamamodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ bool LLamaModel::loadModel(const std::string &modelPath)

d_ptr->ctx = llama_init_from_file(modelPath.c_str(), d_ptr->params);
if (!d_ptr->ctx) {
#ifdef GGML_USE_KOMPUTE
// Explicitly free the device so next load it doesn't use it
ggml_vk_free_device();
#endif
std::cerr << "LLAMA ERROR: failed to load model from " << modelPath << std::endl;
return false;
}
Expand All @@ -194,7 +198,7 @@ int32_t LLamaModel::threadCount() const {

LLamaModel::~LLamaModel()
{
if(d_ptr->ctx) {
if (d_ptr->ctx) {
llama_free(d_ptr->ctx);
}
}
Expand Down
11 changes: 9 additions & 2 deletions gpt4all-chat/chatllm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,15 @@ bool ChatLLM::loadModel(const ModelInfo &modelInfo)
emit reportDevice(actualDevice);

bool success = m_llModelInfo.model->loadModel(filePath.toStdString());
if (!success && actualDevice != "CPU") {
emit reportDevice("CPU");
success = m_llModelInfo.model->loadModel(filePath.toStdString());
}

MySettings::globalInstance()->setAttemptModelLoad(QString());
if (!success) {
delete std::exchange(m_llModelInfo.model, nullptr);
delete m_llModelInfo.model;
m_llModelInfo.model = nullptr;
if (!m_isServer)
LLModelStore::globalInstance()->releaseModel(m_llModelInfo); // release back into the store
m_llModelInfo = LLModelInfo();
Expand All @@ -317,7 +323,8 @@ bool ChatLLM::loadModel(const ModelInfo &modelInfo)
case 'S': m_llModelType = LLModelType::STARCODER_; break;
default:
{
delete std::exchange(m_llModelInfo.model, nullptr);
delete m_llModelInfo.model;
m_llModelInfo.model = nullptr;
if (!m_isServer)
LLModelStore::globalInstance()->releaseModel(m_llModelInfo); // release back into the store
m_llModelInfo = LLModelInfo();
Expand Down

0 comments on commit aa33419

Please sign in to comment.