From 1b755b6cba17bcfaaff72eb6c40111ab5b19b38b Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Fri, 2 Jun 2023 10:47:12 -0400 Subject: [PATCH] Try and fix build on mac. --- gpt4all-backend/CMakeLists.txt | 6 +++--- gpt4all-backend/llmodel.cpp | 22 ---------------------- gpt4all-backend/llmodel_shared.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 gpt4all-backend/llmodel_shared.cpp diff --git a/gpt4all-backend/CMakeLists.txt b/gpt4all-backend/CMakeLists.txt index 69917f46..e790c744 100644 --- a/gpt4all-backend/CMakeLists.txt +++ b/gpt4all-backend/CMakeLists.txt @@ -76,7 +76,7 @@ foreach(BUILD_VARIANT IN LISTS BUILD_VARIANTS) # Add each individual implementations add_library(llamamodel-mainline-${BUILD_VARIANT} SHARED - llamamodel.cpp) + llamamodel.cpp llmodel_shared.cpp) target_compile_definitions(llamamodel-mainline-${BUILD_VARIANT} PRIVATE LLAMA_VERSIONS=>=3 LLAMA_DATE=999999) prepare_target(llamamodel-mainline llama-mainline) @@ -94,11 +94,11 @@ foreach(BUILD_VARIANT IN LISTS BUILD_VARIANTS) prepare_target(llamamodel-230511 llama-230511) add_library(gptj-${BUILD_VARIANT} SHARED - gptj.cpp utils.h utils.cpp) + gptj.cpp utils.h utils.cpp llmodel_shared.cpp) prepare_target(gptj ggml-230511) add_library(mpt-${BUILD_VARIANT} SHARED - mpt.cpp utils.h utils.cpp) + mpt.cpp utils.h utils.cpp llmodel_shared.cpp) prepare_target(mpt ggml-230511) endforeach() diff --git a/gpt4all-backend/llmodel.cpp b/gpt4all-backend/llmodel.cpp index 0de32300..e67fdac6 100644 --- a/gpt4all-backend/llmodel.cpp +++ b/gpt4all-backend/llmodel.cpp @@ -96,28 +96,6 @@ const LLModel::Implementation* LLModel::implementation(std::ifstream& f, const s return nullptr; } -void LLModel::recalculateContext(PromptContext &promptCtx, std::function recalculate) { - size_t i = 0; - promptCtx.n_past = 0; - while (i < promptCtx.tokens.size()) { - size_t batch_end = std::min(i + promptCtx.n_batch, promptCtx.tokens.size()); - std::vector batch(promptCtx.tokens.begin() + i, promptCtx.tokens.begin() + batch_end); - assert(promptCtx.n_past + int32_t(batch.size()) <= promptCtx.n_ctx); - if (!evalTokens(promptCtx, batch)) { - std::cerr << "LLModel ERROR: Failed to process prompt\n"; - goto stop_generating; - } - promptCtx.n_past += batch.size(); - if (!recalculate(true)) - goto stop_generating; - i = batch_end; - } - assert(promptCtx.n_past == int32_t(promptCtx.tokens.size())); - -stop_generating: - recalculate(false); -} - LLModel *LLModel::construct(const std::string &modelPath, std::string buildVariant) { //TODO: Auto-detect CUDA/OpenCL if (buildVariant == "auto") { diff --git a/gpt4all-backend/llmodel_shared.cpp b/gpt4all-backend/llmodel_shared.cpp new file mode 100644 index 00000000..18690c89 --- /dev/null +++ b/gpt4all-backend/llmodel_shared.cpp @@ -0,0 +1,26 @@ +#include "llmodel.h" + +#include +#include + +void LLModel::recalculateContext(PromptContext &promptCtx, std::function recalculate) { + size_t i = 0; + promptCtx.n_past = 0; + while (i < promptCtx.tokens.size()) { + size_t batch_end = std::min(i + promptCtx.n_batch, promptCtx.tokens.size()); + std::vector batch(promptCtx.tokens.begin() + i, promptCtx.tokens.begin() + batch_end); + assert(promptCtx.n_past + int32_t(batch.size()) <= promptCtx.n_ctx); + if (!evalTokens(promptCtx, batch)) { + std::cerr << "LLModel ERROR: Failed to process prompt\n"; + goto stop_generating; + } + promptCtx.n_past += batch.size(); + if (!recalculate(true)) + goto stop_generating; + i = batch_end; + } + assert(promptCtx.n_past == int32_t(promptCtx.tokens.size())); + +stop_generating: + recalculate(false); +}